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 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | #define CT_AUTO 0 |
| 39 | #define CT_UNIX2DOS 1 |
| 40 | #define CT_DOS2UNIX 2 |
| 41 | |
Eric Andersen | 5944396 | 2001-08-22 05:06:29 +0000 | [diff] [blame] | 42 | /* We are making a lame pseudo-random string generator here. in |
| 43 | * convert(), each pass through the while loop will add more and more |
| 44 | * stuff into value, which is _supposed_ to wrap. We don't care about |
| 45 | * it being accurate. We care about it being messy, since we then mod |
| 46 | * it by the sizeof(letters) and then use that as an index into letters |
| 47 | * to pick a random letter to add to out temporary file. */ |
| 48 | typedef unsigned long int bb_uint64_t; |
Eric Andersen | be0c360 | 2001-08-02 10:55:32 +0000 | [diff] [blame] | 49 | |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 50 | static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 51 | |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 52 | // if fn is NULL then input is stdin and output is stdout |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 53 | static int convert(char *fn, int ConvType) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 54 | { |
| 55 | int c, fd; |
| 56 | struct timeval tv; |
| 57 | char tempFn[BUFSIZ]; |
Eric Andersen | 5944396 | 2001-08-22 05:06:29 +0000 | [diff] [blame] | 58 | static bb_uint64_t value=0; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 59 | FILE *in = stdin, *out = stdout; |
| 60 | |
| 61 | if (fn != NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | in = bb_xfopen(fn, "rw"); |
Eric Andersen | 009ae1f | 2002-07-03 04:24:08 +0000 | [diff] [blame] | 63 | safe_strncpy(tempFn, fn, sizeof(tempFn)); |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 64 | c = strlen(tempFn); |
| 65 | tempFn[c] = '.'; |
| 66 | while(1) { |
Eric Andersen | 6c0396b | 2004-05-05 19:39:21 +0000 | [diff] [blame] | 67 | /* tempFn is BUFSIZ so the last addressable spot it BUFSIZ-1. |
| 68 | * The loop increments by 2. So this must check for BUFSIZ-3. */ |
| 69 | if (c >=BUFSIZ-3) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | bb_error_msg_and_die("unique name not found"); |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 71 | /* Get some semi random stuff to try and make a |
| 72 | * random filename based (and in the same dir as) |
| 73 | * the input file... */ |
| 74 | gettimeofday (&tv, NULL); |
Eric Andersen | 5944396 | 2001-08-22 05:06:29 +0000 | [diff] [blame] | 75 | value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid (); |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 76 | tempFn[++c] = letters[value % 62]; |
| 77 | tempFn[c+1] = '\0'; |
| 78 | value /= 62; |
| 79 | |
| 80 | if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) { |
| 81 | continue; |
| 82 | } |
| 83 | out = fdopen(fd, "w+"); |
| 84 | if (!out) { |
| 85 | close(fd); |
| 86 | remove(tempFn); |
| 87 | continue; |
| 88 | } |
| 89 | break; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | while ((c = fgetc(in)) != EOF) { |
| 94 | if (c == '\r') { |
| 95 | if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { |
| 96 | // 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] | 97 | remove(tempFn); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 98 | if (fclose(in) < 0 || fclose(out) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | bb_perror_nomsg(); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 100 | return -2; |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | if (!ConvType) |
| 105 | ConvType = CT_DOS2UNIX; |
| 106 | break; |
| 107 | } |
| 108 | if (c == '\n') { |
| 109 | if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { |
| 110 | // 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] | 111 | remove(tempFn); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 112 | if ((fclose(in) < 0) || (fclose(out) < 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 113 | bb_perror_nomsg(); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 114 | return -2; |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | if (!ConvType) { |
| 119 | ConvType = CT_UNIX2DOS; |
| 120 | } |
| 121 | if (ConvType == CT_UNIX2DOS) { |
| 122 | fputc('\r', out); |
| 123 | } |
| 124 | fputc('\n', out); |
| 125 | break; |
| 126 | } |
| 127 | fputc(c, out); |
| 128 | } |
| 129 | if (c != EOF) |
| 130 | while ((c = fgetc(in)) != EOF) { |
| 131 | if (c == '\r') |
| 132 | continue; |
| 133 | if (c == '\n') { |
| 134 | if (ConvType == CT_UNIX2DOS) |
| 135 | fputc('\r', out); |
| 136 | fputc('\n', out); |
| 137 | continue; |
| 138 | } |
| 139 | fputc(c, out); |
| 140 | } |
| 141 | |
| 142 | if (fn != NULL) { |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 143 | if (fclose(in) < 0 || fclose(out) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 144 | bb_perror_nomsg(); |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 145 | remove(tempFn); |
| 146 | return -2; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Eric Andersen | 5944396 | 2001-08-22 05:06:29 +0000 | [diff] [blame] | 149 | /* Assume they are both on the same filesystem (which |
| 150 | * should be true since we put them into the same directory |
| 151 | * so we _should_ be ok, but you never know... */ |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 152 | if (rename(tempFn, fn) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 153 | bb_perror_msg("unable to rename '%s' as '%s'", tempFn, fn); |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 154 | return -1; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 161 | int dos2unix_main(int argc, char *argv[]) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 162 | { |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 163 | int ConvType = CT_AUTO; |
| 164 | int o; |
| 165 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 166 | //See if we are supposed to be doing dos2unix or unix2dos |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 167 | if (argv[0][0]=='d') { |
| 168 | ConvType = CT_DOS2UNIX; |
| 169 | } |
| 170 | if (argv[0][0]=='u') { |
| 171 | ConvType = CT_UNIX2DOS; |
| 172 | } |
| 173 | |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 174 | // process parameters |
| 175 | while ((o = getopt(argc, argv, "du")) != EOF) { |
| 176 | switch (o) { |
| 177 | case 'd': |
| 178 | ConvType = CT_UNIX2DOS; |
| 179 | break; |
| 180 | case 'u': |
| 181 | ConvType = CT_DOS2UNIX; |
| 182 | break; |
| 183 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 184 | bb_show_usage(); |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 185 | } |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 186 | } |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 187 | |
| 188 | if (optind < argc) { |
| 189 | while(optind < argc) |
| 190 | if ((o = convert(argv[optind++], ConvType)) < 0) |
| 191 | break; |
| 192 | } |
| 193 | else |
| 194 | o = convert(NULL, ConvType); |
| 195 | |
| 196 | return o; |
| 197 | } |
| 198 | |