| 1 | #include <stdio.h> | 
|---|
| 2 | #include <stdlib.h> | 
|---|
| 3 | #include <string.h> | 
|---|
| 4 |  | 
|---|
| 5 | static | 
|---|
| 6 | char* | 
|---|
| 7 | concatPath(const char* dir, const char* name, const char* ext) | 
|---|
| 8 | { | 
|---|
| 9 | size_t nDir  = (dir != NULL) ? strlen(dir) : 0; | 
|---|
| 10 | size_t nPath = nDir + 1 + strlen(name) + strlen(ext?ext:"") + 1; | 
|---|
| 11 | char* path = malloc(nPath); | 
|---|
| 12 |  | 
|---|
| 13 | /* directory */ | 
|---|
| 14 | if (nDir > 0 && strcmp(dir, ".") != 0) { | 
|---|
| 15 | strcpy(path, dir); | 
|---|
| 16 | if (path[nDir - 1] != '\\' && path[nDir - 1] != '/') { | 
|---|
| 17 | strcat(path, "\\"); | 
|---|
| 18 | } | 
|---|
| 19 | } | 
|---|
| 20 | else { | 
|---|
| 21 | strcpy(path, ""); | 
|---|
| 22 | } | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | /* name */ | 
|---|
| 26 | strcat(path, name); | 
|---|
| 27 |  | 
|---|
| 28 | /* extension */ | 
|---|
| 29 | if (ext != NULL && strrchr(name, '.') == NULL) { | 
|---|
| 30 | strcat(path, ext); | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | return path; | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | static | 
|---|
| 37 | int | 
|---|
| 38 | dosify(const char* srcdir, const char* dstdir, const char* name) | 
|---|
| 39 | { | 
|---|
| 40 | FILE* dFile, *sFile; | 
|---|
| 41 | char* dName, *sName; | 
|---|
| 42 |  | 
|---|
| 43 | sName = concatPath(srcdir, name, NULL); | 
|---|
| 44 | dName = concatPath(dstdir, name, ".txt"); | 
|---|
| 45 |  | 
|---|
| 46 | sFile = fopen(sName, "rb"); | 
|---|
| 47 | if (sFile == NULL) { | 
|---|
| 48 | fprintf(stderr, "Can't open \"%s\" for reading\n", sName); | 
|---|
| 49 | return 0; | 
|---|
| 50 | } | 
|---|
| 51 | else { | 
|---|
| 52 | dFile = fopen(dName, "w"); | 
|---|
| 53 | if (dFile == NULL) { | 
|---|
| 54 | fclose(sFile); | 
|---|
| 55 | fprintf(stderr, "Can't open \"%s\" for writing\n", dName); | 
|---|
| 56 | return 0; | 
|---|
| 57 | } | 
|---|
| 58 | else { | 
|---|
| 59 | char buffer[1024]; | 
|---|
| 60 | while (!ferror(dFile) && | 
|---|
| 61 | fgets(buffer, sizeof(buffer), sFile) != NULL) { | 
|---|
| 62 | fprintf(dFile, "%s", buffer); | 
|---|
| 63 | } | 
|---|
| 64 | if (ferror(sFile) || ferror(dFile)) { | 
|---|
| 65 | fprintf(stderr, | 
|---|
| 66 | "Error copying \"%s\" to \"%s\"\n", sName, dName); | 
|---|
| 67 | fclose(dFile); | 
|---|
| 68 | fclose(sFile); | 
|---|
| 69 | _unlink(dName); | 
|---|
| 70 | return 0; | 
|---|
| 71 | } | 
|---|
| 72 | } | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | fclose(dFile); | 
|---|
| 76 | fclose(sFile); | 
|---|
| 77 | free(dName); | 
|---|
| 78 | free(sName); | 
|---|
| 79 | return 1; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | #include <windows.h> | 
|---|
| 83 | int | 
|---|
| 84 | main(int argc, char** argv) | 
|---|
| 85 | { | 
|---|
| 86 | int i; | 
|---|
| 87 |  | 
|---|
| 88 | if (argc < 3) { | 
|---|
| 89 | fprintf(stderr, "usage: %s <srcdir> <dstdir> [files]\n", argv[0]); | 
|---|
| 90 | return 1; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | for (i = 3; i < argc; ++i) { | 
|---|
| 94 | if (!dosify(argv[1], argv[2], argv[i])) | 
|---|
| 95 | return 1; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | return 0; | 
|---|
| 99 | } | 
|---|