From 9d67b8b8fe0b54315f831cc43949c0ab530b86ca Mon Sep 17 00:00:00 2001 From: kremlin Date: Thu, 2 Oct 2014 18:30:57 -0400 Subject: [PATCH] write homework 5 --- assgn5/Makefile | 9 +++++ assgn5/assgn5.tar.gz | Bin 0 -> 864 bytes assgn5/copy.c | 78 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 assgn5/Makefile create mode 100644 assgn5/assgn5.tar.gz create mode 100644 assgn5/copy.c 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 0000000000000000000000000000000000000000..644b24a6e7d8a3e3c878ef5c8c6fd7b23b7d84f1 GIT binary patch literal 864 zcmV-m1E2gKiwFq7(JfQ}17UM>XKpnvbYXG;?UrG0+cpq}^VRwlm#j$SPHH6ENq~46 zP`E8NtcwG;K^CNeK~t1NL?#82N@}e6?>kDet-1-)4^6fKd|+78>G8%E@oT}0z!O?D^)$W9g-6^G>ht@-{^}-(v+7O=M6Mi$Lq_Wq~MiB^e zmJne$1uFCTD+-}3@6bWWun>i2NthEwX3Q|vw0d5`CdG86KZS4@{rE8dc7gxnRP}t? z7?14#&R#F7_y1nh+w0l=AMJPC{(laB;(}&H!r&Ke5-wj|{pzhuDHSIB+EfURj@H&N z>bc&O=$r13QPPF?{6+H+wiyNwd^}ur~uN8Ovfb8nMmq~R4U5) zl}nn8Qzd7Cy)iO?nM~}fQg2eE7x85u#TpuL1W~`XltNHhyw>d%{>u4221U!18JQcu z%FgB$z!pij+HKf8f-tr@wp#j@8+v6&R5`a=lxPM!WarR6tc8vHrE#Jdx$ZZ-)24To z(tXYeOG%NL$~RXy`PARy;)Y~Au^ARKCd?ef_cQ@oFcaQKC|_GFX-2c=Tp?{Qle~TkaHqdmQT$mfBx%%-P6}3E;!{ z_tDv3r|o_{wMC@GPH3*NEHdmKTxKf{S{AmSZ9O09p)#qXua~Xb3~Q_SU_gq?h210A zEv&EQcgL>8@0O>5?Ersy#^8Yx0Q#V7+(x;w1^oKtsb5-Ynn}$T!))NC!N}5~&I6B~ qJNNg);cz${4u`|xa5x+ehr{7;I2;a#!{Kl|8~*^t`dyO%C;$K$i?c!i literal 0 HcmV?d00001 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; +} -- 2.41.0