Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 2 | * dos2unix for BusyBox |
| 3 | * |
| 4 | * dos2unix '\n' convertor 0.5.0 |
| 5 | * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997) |
| 6 | * Copyright 1997,.. by Peter Hanecak <hanecak@megaloman.sk>. |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * dos2unix filters reading input from stdin and writing output to stdout. |
| 10 | * Without arguments it reverts the format (e.i. if source is in UNIX format, |
| 11 | * output is in DOS format and vice versa). |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License |
| 15 | * as published by the Free Software Foundation; either version 2 |
| 16 | * of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 26 | * |
| 27 | * See the COPYING file for license information. |
| 28 | */ |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 29 | |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 30 | #include <string.h> |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 31 | #include <getopt.h> |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
Eric Andersen | 5944396 | 2001-08-22 05:06:29 +0000 | [diff] [blame] | 33 | #include <stdint.h> |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 34 | #include <fcntl.h> |
| 35 | #include <sys/time.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 36 | #include "busybox.h" |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 37 | |
Eric Andersen | 5944396 | 2001-08-22 05:06:29 +0000 | [diff] [blame] | 38 | /* We are making a lame pseudo-random string generator here. in |
| 39 | * convert(), each pass through the while loop will add more and more |
| 40 | * stuff into value, which is _supposed_ to wrap. We don't care about |
| 41 | * it being accurate. We care about it being messy, since we then mod |
| 42 | * it by the sizeof(letters) and then use that as an index into letters |
| 43 | * to pick a random letter to add to out temporary file. */ |
| 44 | typedef unsigned long int bb_uint64_t; |
Eric Andersen | be0c360 | 2001-08-02 10:55:32 +0000 | [diff] [blame] | 45 | |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 46 | static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 47 | |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 48 | // if fn is NULL then input is stdin and output is stdout |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 49 | static int convert(char *fn, int ConvType) |
| 50 | { |
| 51 | int c, fd; |
| 52 | struct timeval tv; |
| 53 | char tempFn[BUFSIZ]; |
Eric Andersen | 5944396 | 2001-08-22 05:06:29 +0000 | [diff] [blame] | 54 | static bb_uint64_t value=0; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 55 | FILE *in = stdin, *out = stdout; |
| 56 | |
| 57 | if (fn != NULL) { |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 58 | if ((in = wfopen(fn, "rw")) == NULL) { |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 59 | return -1; |
| 60 | } |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 61 | strcpy(tempFn, fn); |
| 62 | c = strlen(tempFn); |
| 63 | tempFn[c] = '.'; |
| 64 | while(1) { |
| 65 | if (c >=BUFSIZ) |
| 66 | error_msg_and_die("unique name not found"); |
| 67 | /* Get some semi random stuff to try and make a |
| 68 | * random filename based (and in the same dir as) |
| 69 | * the input file... */ |
| 70 | gettimeofday (&tv, NULL); |
Eric Andersen | 5944396 | 2001-08-22 05:06:29 +0000 | [diff] [blame] | 71 | value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid (); |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 72 | tempFn[++c] = letters[value % 62]; |
| 73 | tempFn[c+1] = '\0'; |
| 74 | value /= 62; |
| 75 | |
| 76 | if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) { |
| 77 | continue; |
| 78 | } |
| 79 | out = fdopen(fd, "w+"); |
| 80 | if (!out) { |
| 81 | close(fd); |
| 82 | remove(tempFn); |
| 83 | continue; |
| 84 | } |
| 85 | break; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | while ((c = fgetc(in)) != EOF) { |
| 90 | if (c == '\r') { |
| 91 | if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { |
| 92 | // file is alredy in DOS format so it is not necessery to touch it |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 93 | remove(tempFn); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 94 | if (fclose(in) < 0 || fclose(out) < 0) { |
| 95 | perror_msg(NULL); |
| 96 | return -2; |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | if (!ConvType) |
| 101 | ConvType = CT_DOS2UNIX; |
| 102 | break; |
| 103 | } |
| 104 | if (c == '\n') { |
| 105 | if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { |
| 106 | // file is alredy in UNIX format so it is not necessery to touch it |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 107 | remove(tempFn); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 108 | if ((fclose(in) < 0) || (fclose(out) < 0)) { |
| 109 | perror_msg(NULL); |
| 110 | return -2; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | if (!ConvType) { |
| 115 | ConvType = CT_UNIX2DOS; |
| 116 | } |
| 117 | if (ConvType == CT_UNIX2DOS) { |
| 118 | fputc('\r', out); |
| 119 | } |
| 120 | fputc('\n', out); |
| 121 | break; |
| 122 | } |
| 123 | fputc(c, out); |
| 124 | } |
| 125 | if (c != EOF) |
| 126 | while ((c = fgetc(in)) != EOF) { |
| 127 | if (c == '\r') |
| 128 | continue; |
| 129 | if (c == '\n') { |
| 130 | if (ConvType == CT_UNIX2DOS) |
| 131 | fputc('\r', out); |
| 132 | fputc('\n', out); |
| 133 | continue; |
| 134 | } |
| 135 | fputc(c, out); |
| 136 | } |
| 137 | |
| 138 | if (fn != NULL) { |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 139 | if (fclose(in) < 0 || fclose(out) < 0) { |
| 140 | perror_msg(NULL); |
| 141 | remove(tempFn); |
| 142 | return -2; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Eric Andersen | 5944396 | 2001-08-22 05:06:29 +0000 | [diff] [blame] | 145 | /* Assume they are both on the same filesystem (which |
| 146 | * should be true since we put them into the same directory |
| 147 | * so we _should_ be ok, but you never know... */ |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 148 | if (rename(tempFn, fn) < 0) { |
| 149 | perror_msg("unable to rename '%s' as '%s'", tempFn, fn); |
| 150 | return -1; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 157 | int dos2unix_main(int argc, char *argv[]) |
| 158 | { |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 159 | int ConvType = CT_AUTO; |
| 160 | int o; |
| 161 | |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 162 | //See if we are supposed to be doing dos2unix or unix2dos |
| 163 | if (argv[0][0]=='d') { |
| 164 | ConvType = CT_DOS2UNIX; |
| 165 | } |
| 166 | if (argv[0][0]=='u') { |
| 167 | ConvType = CT_UNIX2DOS; |
| 168 | } |
| 169 | |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 170 | // process parameters |
| 171 | while ((o = getopt(argc, argv, "du")) != EOF) { |
| 172 | switch (o) { |
| 173 | case 'd': |
| 174 | ConvType = CT_UNIX2DOS; |
| 175 | break; |
| 176 | case 'u': |
| 177 | ConvType = CT_DOS2UNIX; |
| 178 | break; |
| 179 | default: |
| 180 | show_usage(); |
| 181 | } |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 182 | } |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 183 | |
| 184 | if (optind < argc) { |
| 185 | while(optind < argc) |
| 186 | if ((o = convert(argv[optind++], ConvType)) < 0) |
| 187 | break; |
| 188 | } |
| 189 | else |
| 190 | o = convert(NULL, ConvType); |
| 191 | |
| 192 | return o; |
| 193 | } |
| 194 | |