1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <dirent.h>
#define MAX_SIZE 1024
int main(int argc, char **argv) { int i;
if(argc < 3) { printf("Usage : %s <filename1> <filename2> <filename3>\n", argv[0]); printf("or\nUsage : %s <filename1> <filename2> ... <directory name>\n", argv[0]); }
const char *dir = argv[argc-1]; DIR *isdir = opendir(dir);
if(isdir) { for(i=1; i < argc - 1; i++) { if(access(argv[i], F_OK) != -1) { int len = strlen(dir) + strlen("/") + strlen(argv[i]);
char *file = (char*)calloc(sizeof(char), len); strncpy(file, dir, strlen(dir)); strncat(file, "/", strlen("/")); strncat(file, argv[i], strlen(argv[i])); FILE *src = fopen(argv[i], "r"); FILE *dst = fopen(file, "w"); char buf[MAX_SIZE] ={0, }; while(fread(buf, 1, sizeof(buf), src)) { buf[strlen(buf)] = '\0'; fwrite(buf, 1, sizeof(buf), dst); }
if(file) { memset(file, 0, len); free(file); }
fclose(dst); fclose(src); }
else { printf("Cannot found '%s' file\n", argv[i]); } } }
else { printf("Cannot found '%s' directory\n", dir); } closedir(isdir); return 0; }
|