From: kremlin Date: Thu, 2 Oct 2014 22:30:57 +0000 (-0400) Subject: write homework 5 X-Git-Url: https://uglyman.kremlin.cc/gitweb/gitweb.cgi?a=commitdiff_plain;h=9d67b8b8fe0b54315f831cc43949c0ab530b86ca;p=assignments.git write homework 5 --- diff --git a/assgn5/Makefile b/assgn5/Makefile new file mode 100644 index 0000000..8034288 --- /dev/null +++ b/assgn5/Makefile @@ -0,0 +1,9 @@ +PHONY: all + +CC=/usr/bin/gcc + +all: + $(CC) -Wall -Werror -Wextra -pedantic copy.c -o copy -Wno-unused-parameter -Wno-unused + +debug: + $(CC) -g -O0 -Wall -Werror -Wextra -pedantic copy.c -o copy -Wno-unused-parameter -Wno-unused diff --git a/assgn5/assgn5.tar.gz b/assgn5/assgn5.tar.gz new file mode 100644 index 0000000..644b24a Binary files /dev/null and b/assgn5/assgn5.tar.gz differ diff --git a/assgn5/copy.c b/assgn5/copy.c new file mode 100644 index 0000000..0a3c8d5 --- /dev/null +++ b/assgn5/copy.c @@ -0,0 +1,78 @@ +#include +#include +#include + +#include + +int main(int argc, char *argv[]) { + + int cur, flag_append, flag_force; + int fd_from, fd_to, mode; + char buf[1]; + + flag_append = 0; + flag_force = 0; + + while((cur = getopt(argc, argv, "af")) != -1) { + switch(cur) { + + case 'a': + flag_append = 1; + break; + + case 'f': + flag_force = 1; + break; + + default: + printf("invalid argument\n"); + return 1; + } + } + + if(argc - optind != 2) { + + printf("invalid number of args\n"); + return 1; + + } else if(flag_append && flag_force) { + + printf("cannot simultaneously specify overwrite and append\n"); + return 1; + } + + if(access(argv[optind + 1], F_OK) != -1 && !flag_append && !flag_force) { + + printf("destination file already exists, pass -f to overwrite\n"); + return 1; + } + + if(flag_force) mode = O_TRUNC; + else if(flag_append) mode = O_APPEND; + else mode = O_CREAT; + + fd_from = open(argv[optind], O_RDONLY); + fd_to = open(argv[optind + 1], mode | O_WRONLY, S_IRWXU); + + if(fd_from == -1) { + + printf("could not open source file\n"); + return 1; + + } else if(fd_to == -1 && !flag_force && !flag_append) { + + printf("could not create new destination file\n"); + return 1; + + } else if(fd_to == -1) { + + } + + while(read(fd_from, buf, 1) == 1) + write(fd_to, buf, 1); + + close(fd_from); + close(fd_to); + + return 0; +}