0a3c8d5fa5e06e7e0b25c7b6de8b56c682836ff6
7 int main(int argc
, char *argv
[]) {
9 int cur
, flag_append
, flag_force
;
10 int fd_from
, fd_to
, mode
;
16 while((cur
= getopt(argc
, argv
, "af")) != -1) {
28 printf("invalid argument\n");
33 if(argc
- optind
!= 2) {
35 printf("invalid number of args\n");
38 } else if(flag_append
&& flag_force
) {
40 printf("cannot simultaneously specify overwrite and append\n");
44 if(access(argv
[optind
+ 1], F_OK
) != -1 && !flag_append
&& !flag_force
) {
46 printf("destination file already exists, pass -f to overwrite\n");
50 if(flag_force
) mode
= O_TRUNC
;
51 else if(flag_append
) mode
= O_APPEND
;
54 fd_from
= open(argv
[optind
], O_RDONLY
);
55 fd_to
= open(argv
[optind
+ 1], mode
| O_WRONLY
, S_IRWXU
);
59 printf("could not open source file\n");
62 } else if(fd_to
== -1 && !flag_force
&& !flag_append
) {
64 printf("could not create new destination file\n");
67 } else if(fd_to
== -1) {
71 while(read(fd_from
, buf
, 1) == 1)