diff options
| author | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-04-12 02:26:04 +0000 |
|---|---|---|
| committer | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-04-12 02:26:04 +0000 |
| commit | dbb1e51a591c2f3ddd5f12d75b3a03cf867586e2 (patch) | |
| tree | 1edffdd583fc4386c86b34b9539245235376aca3 /coreutils | |
| parent | 3e8cfb7ab62f0bab16c6397ed51dd75b69d9f3b3 (diff) | |
| download | busybox-w32-dbb1e51a591c2f3ddd5f12d75b3a03cf867586e2.tar.gz busybox-w32-dbb1e51a591c2f3ddd5f12d75b3a03cf867586e2.tar.bz2 busybox-w32-dbb1e51a591c2f3ddd5f12d75b3a03cf867586e2.zip | |
use tmpfile() and revert my previous changes... convert() belongs here
git-svn-id: svn://busybox.net/trunk/busybox@2327 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/dos2unix.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c index b2dcfd9c2..8308c4179 100644 --- a/coreutils/dos2unix.c +++ b/coreutils/dos2unix.c | |||
| @@ -27,9 +27,93 @@ | |||
| 27 | * See the COPYING file for license information. | 27 | * See the COPYING file for license information. |
| 28 | */ | 28 | */ |
| 29 | 29 | ||
| 30 | #include <string.h> | ||
| 30 | #include <getopt.h> | 31 | #include <getopt.h> |
| 31 | #include "busybox.h" | 32 | #include "busybox.h" |
| 32 | 33 | ||
| 34 | // if fn is NULL then input is stdin and output is stdout | ||
| 35 | extern int convert(char *fn, int ConvType) { | ||
| 36 | char c; | ||
| 37 | char *tempFn = NULL; | ||
| 38 | FILE *in = stdin, *out = stdout; | ||
| 39 | |||
| 40 | if (fn != NULL) { | ||
| 41 | if ((in = wfopen(fn, "r")) == NULL) { | ||
| 42 | return -1; | ||
| 43 | } | ||
| 44 | if ((out = tmpfile()) == NULL) { | ||
| 45 | perror_msg(NULL); | ||
| 46 | return -2; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | while ((c = fgetc(in)) != EOF) { | ||
| 51 | if (c == '\r') { | ||
| 52 | if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { | ||
| 53 | // file is alredy in DOS format so it is not necessery to touch it | ||
| 54 | if (fclose(in) < 0 || fclose(out) < 0) { | ||
| 55 | perror_msg(NULL); | ||
| 56 | return -2; | ||
| 57 | } | ||
| 58 | return 0; | ||
| 59 | } | ||
| 60 | if (!ConvType) | ||
| 61 | ConvType = CT_DOS2UNIX; | ||
| 62 | break; | ||
| 63 | } | ||
| 64 | if (c == '\n') { | ||
| 65 | if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { | ||
| 66 | // file is alredy in UNIX format so it is not necessery to touch it | ||
| 67 | if ((fclose(in) < 0) || (fclose(out) < 0)) { | ||
| 68 | perror_msg(NULL); | ||
| 69 | return -2; | ||
| 70 | } | ||
| 71 | return 0; | ||
| 72 | } | ||
| 73 | if (!ConvType) { | ||
| 74 | ConvType = CT_UNIX2DOS; | ||
| 75 | } | ||
| 76 | if (ConvType == CT_UNIX2DOS) { | ||
| 77 | fputc('\r', out); | ||
| 78 | } | ||
| 79 | fputc('\n', out); | ||
| 80 | break; | ||
| 81 | } | ||
| 82 | fputc(c, out); | ||
| 83 | } | ||
| 84 | if (c != EOF) | ||
| 85 | while ((c = fgetc(in)) != EOF) { | ||
| 86 | if (c == '\r') | ||
| 87 | continue; | ||
| 88 | if (c == '\n') { | ||
| 89 | if (ConvType == CT_UNIX2DOS) | ||
| 90 | fputc('\r', out); | ||
| 91 | fputc('\n', out); | ||
| 92 | continue; | ||
| 93 | } | ||
| 94 | fputc(c, out); | ||
| 95 | } | ||
| 96 | |||
| 97 | if (fn != NULL) { | ||
| 98 | if (fclose(in) < 0 || fclose(out) < 0 || | ||
| 99 | (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) { | ||
| 100 | perror_msg(NULL); | ||
| 101 | return -2; | ||
| 102 | } | ||
| 103 | |||
| 104 | while ((c = fgetc(in)) != EOF) { | ||
| 105 | fputc(c, out); | ||
| 106 | } | ||
| 107 | |||
| 108 | if ((fclose(in) < 0) || (fclose(out) < 0)) { | ||
| 109 | perror_msg(NULL); | ||
| 110 | return -2; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | return 0; | ||
| 115 | } | ||
| 116 | |||
| 33 | int dos2unix_main(int argc, char *argv[]) { | 117 | int dos2unix_main(int argc, char *argv[]) { |
| 34 | int ConvType = CT_AUTO; | 118 | int ConvType = CT_AUTO; |
| 35 | int o; | 119 | int o; |
