From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/OR_USING_C/03.1.c | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 reference/C/CONTRIB/OR_USING_C/03.1.c (limited to 'reference/C/CONTRIB/OR_USING_C/03.1.c') diff --git a/reference/C/CONTRIB/OR_USING_C/03.1.c b/reference/C/CONTRIB/OR_USING_C/03.1.c new file mode 100644 index 0000000..f1b7844 --- /dev/null +++ b/reference/C/CONTRIB/OR_USING_C/03.1.c @@ -0,0 +1,60 @@ +/* Change to if you're on System V. + */ +#include + +main(argc, argv) +int argc; +char **argv; +{ + int n; + int from, to; + char buf[1024]; + + /* + * Check our arguments. Note that to write the error + * message we can't just use "%s" as we did in Example + * 2-3; we have to write each string separately. + */ + if (argc != 3) { + write(2, "Usage: ", 7); + write(2, *argv, strlen(*argv)); + write(2, " from-file to-file\n", 19); + exit(1); + } + + /* + * Open the from-file for reading. + */ + if ((from = open(argv[1], O_RDONLY)) < 0) { + perror(argv[1]); + exit(1); + } + + /* + * Open the to-file for appending. If to-file does + * not exist, open will create it with mode 644 + * (-rw-r--r--). Note that we specify the mode + * in octal, not decimal + */ + if ((to = open(argv[2], O_WRONLY|O_CREAT|O_APPEND, 0644)) < 0) { + perror(argv[2]); + exit(1); + } + + /* + * Now read a buffer-full at a time from the from-file, + * and write it to the to-file. Note that we only + * write the number of characters read read in, + * rather than always writing 1024 characters. + */ + while ((n = read(from, buf, sizeof(buf))) > 0) + write(to, buf, n); + + /* + * Now close the files. + */ + close(from); + close(to); + exit(0); +} + -- cgit v1.2.3-54-g00ecf