開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第8章(UNIXシステム・インタフェース)、8.1(ファイル記述子)、8.2(低水準入出力 - Read と Write)、8.3(Open, Creat, Close Unlink)、演習8-1を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 8-1.
コード
sample.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
void error(char *, ...);
int main(int argc, char *argv[])
{
int fd;
char *prog = argv[0];
void filecopy(int, int);
if (argc == 1) {
filecopy(0, 1);
} else {
while (--argc > 0) {
if ((fd = open(*++argv, O_RDONLY, 0)) == -1) {
error("can't open %s", *argv);
} else {
filecopy(fd, 1);
close(fd);
}
}
}
return 0;
}
void filecopy(int ifd, int ofd)
{
int fd, n;
char buf[BUFSIZ];
while ((n = read(ifd, buf, BUFSIZ)) > 0) {
if (write(ofd, buf, n) != n) {
error("can't write");
}
}
}
#include <stdarg.h>
void error(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "error: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
入出力結果(Terminal)
$ ./a.out sample.txt
Ah Love! could you and I with Fate conspire
To grasp this sorry Scheme of Things entire,
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
ah love! could you and i with fate conspire
to grasp this sorry scheme of things entire,
would not we shatter it to bits -- and then
re-mould it nearer to the heart's desire!
1
5
2
4
3
!@#$%Ah Love! could you and I with Fate conspire
!@#$%To grasp this sorry Scheme of Things entire,
!@#$%Would not we shatter it to bits -- and then
!@#$%Re-mould it nearer to the Heart's Desire!
!@#$%ah love! could you and i with fate conspire
!@#$%to grasp this sorry scheme of things entire,
!@#$%would not we shatter it to bits -- and then
!@#$%re-mould it nearer to the heart's desire!
$ ./a.out sample.txt sample.c
Ah Love! could you and I with Fate conspire
To grasp this sorry Scheme of Things entire,
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
ah love! could you and i with fate conspire
to grasp this sorry scheme of things entire,
would not we shatter it to bits -- and then
re-mould it nearer to the heart's desire!
1
5
2
4
3
!@#$%Ah Love! could you and I with Fate conspire
!@#$%To grasp this sorry Scheme of Things entire,
!@#$%Would not we shatter it to bits -- and then
!@#$%Re-mould it nearer to the Heart's Desire!
!@#$%ah love! could you and i with fate conspire
!@#$%to grasp this sorry scheme of things entire,
!@#$%would not we shatter it to bits -- and then
!@#$%re-mould it nearer to the heart's desire!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
void error(char *, ...);
int main(int argc, char *argv[])
{
int fd;
char *prog = argv[0];
void filecopy(int, int);
if (argc == 1) {
filecopy(0, 1);
} else {
while (--argc > 0) {
if ((fd = open(*++argv, O_RDONLY, 0)) == -1) {
error("can't open %s", *argv);
} else {
filecopy(fd, 1);
close(fd);
}
}
}
return 0;
}
void filecopy(int ifd, int ofd)
{
int fd, n;
char buf[BUFSIZ];
while ((n = read(ifd, buf, BUFSIZ)) > 0) {
if (write(ofd, buf, n) != n) {
error("can't write");
}
}
}
#include <stdarg.h>
void error(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "error: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
$ ./a.out
abcde
abcde
$ ./a.out abcde
error: can't open abcde
$
0 コメント:
コメントを投稿