1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <sys/types.h> 5 #include <sys/stat.h> 6 #include <fcntl.h> 7 #include <dirent.h> 8 #include <errno.h> 9 #include <string.h> 10 11 int 12 main(int argc, char *argv[]) 13 { 14 int tfd; 15 DIR *sdir; 16 struct dirent *dirent; 17 18 if (argc != 3) { 19 fprintf(stderr, "Usage: %s SRC DST\n", argv[0]); 20 exit(1); 21 } 22 23 sdir = opendir(argv[1]); 24 if (sdir == NULL) { 25 fprintf(stderr, "Failed to open %s: %s\n", 26 argv[1], strerror(errno)); 27 exit(2); 28 } 29 30 tfd = open(argv[2], O_DIRECTORY); 31 if (tfd < 0) { 32 fprintf(stderr, "Failed to open %s: %s\n", 33 argv[2], strerror(errno)); 34 closedir(sdir); 35 exit(3); 36 } 37 38 while ((dirent = readdir(sdir)) != NULL) { 39 if (dirent->d_name[0] == '.' && 40 (dirent->d_name[1] == '.' || dirent->d_name[1] == '\0')) 41 continue; 42 43 int fd = openat(tfd, dirent->d_name, O_CREAT|O_WRONLY, 0666); 44 if (fd < 0) { 45 fprintf(stderr, "Failed to create %s/%s: %s\n", 46 argv[2], dirent->d_name, strerror(errno)); 47 closedir(sdir); 48 close(tfd); 49 exit(4); 50 } 51 close(fd); 52 } 53 54 closedir(sdir); 55 close(tfd); 56 57 return (0); 58 } 59