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> |
| 33 | #include <fcntl.h> |
| 34 | #include <sys/time.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 35 | #include "busybox.h" |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 36 | |
Eric Andersen | be0c360 | 2001-08-02 10:55:32 +0000 | [diff] [blame] | 37 | /* Teach libc5 what a uint64_t is */ |
| 38 | #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1) |
| 39 | typedef unsigned long int uint64_t; |
| 40 | #endif |
| 41 | |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 42 | static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 43 | |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 44 | // if fn is NULL then input is stdin and output is stdout |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 45 | static int convert(char *fn, int ConvType) |
| 46 | { |
| 47 | int c, fd; |
| 48 | struct timeval tv; |
| 49 | char tempFn[BUFSIZ]; |
| 50 | static uint64_t value=0; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 51 | FILE *in = stdin, *out = stdout; |
| 52 | |
| 53 | if (fn != NULL) { |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 54 | if ((in = wfopen(fn, "rw")) == NULL) { |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 55 | return -1; |
| 56 | } |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 57 | strcpy(tempFn, fn); |
| 58 | c = strlen(tempFn); |
| 59 | tempFn[c] = '.'; |
| 60 | while(1) { |
| 61 | if (c >=BUFSIZ) |
| 62 | error_msg_and_die("unique name not found"); |
| 63 | /* Get some semi random stuff to try and make a |
| 64 | * random filename based (and in the same dir as) |
| 65 | * the input file... */ |
| 66 | gettimeofday (&tv, NULL); |
| 67 | value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid (); |
| 68 | tempFn[++c] = letters[value % 62]; |
| 69 | tempFn[c+1] = '\0'; |
| 70 | value /= 62; |
| 71 | |
| 72 | if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) { |
| 73 | continue; |
| 74 | } |
| 75 | out = fdopen(fd, "w+"); |
| 76 | if (!out) { |
| 77 | close(fd); |
| 78 | remove(tempFn); |
| 79 | continue; |
| 80 | } |
| 81 | break; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | while ((c = fgetc(in)) != EOF) { |
| 86 | if (c == '\r') { |
| 87 | if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { |
| 88 | // 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] | 89 | remove(tempFn); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 90 | if (fclose(in) < 0 || fclose(out) < 0) { |
| 91 | perror_msg(NULL); |
| 92 | return -2; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | if (!ConvType) |
| 97 | ConvType = CT_DOS2UNIX; |
| 98 | break; |
| 99 | } |
| 100 | if (c == '\n') { |
| 101 | if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { |
| 102 | // 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] | 103 | remove(tempFn); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 104 | if ((fclose(in) < 0) || (fclose(out) < 0)) { |
| 105 | perror_msg(NULL); |
| 106 | return -2; |
| 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | if (!ConvType) { |
| 111 | ConvType = CT_UNIX2DOS; |
| 112 | } |
| 113 | if (ConvType == CT_UNIX2DOS) { |
| 114 | fputc('\r', out); |
| 115 | } |
| 116 | fputc('\n', out); |
| 117 | break; |
| 118 | } |
| 119 | fputc(c, out); |
| 120 | } |
| 121 | if (c != EOF) |
| 122 | while ((c = fgetc(in)) != EOF) { |
| 123 | if (c == '\r') |
| 124 | continue; |
| 125 | if (c == '\n') { |
| 126 | if (ConvType == CT_UNIX2DOS) |
| 127 | fputc('\r', out); |
| 128 | fputc('\n', out); |
| 129 | continue; |
| 130 | } |
| 131 | fputc(c, out); |
| 132 | } |
| 133 | |
| 134 | if (fn != NULL) { |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 135 | if (fclose(in) < 0 || fclose(out) < 0) { |
| 136 | perror_msg(NULL); |
| 137 | remove(tempFn); |
| 138 | return -2; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 141 | /* Assume they are both on the same filesystem */ |
| 142 | if (rename(tempFn, fn) < 0) { |
| 143 | perror_msg("unable to rename '%s' as '%s'", tempFn, fn); |
| 144 | return -1; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 151 | int dos2unix_main(int argc, char *argv[]) |
| 152 | { |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 153 | int ConvType = CT_AUTO; |
| 154 | int o; |
| 155 | |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 156 | //See if we are supposed to be doing dos2unix or unix2dos |
| 157 | if (argv[0][0]=='d') { |
| 158 | ConvType = CT_DOS2UNIX; |
| 159 | } |
| 160 | if (argv[0][0]=='u') { |
| 161 | ConvType = CT_UNIX2DOS; |
| 162 | } |
| 163 | |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 164 | // process parameters |
| 165 | while ((o = getopt(argc, argv, "du")) != EOF) { |
| 166 | switch (o) { |
| 167 | case 'd': |
| 168 | ConvType = CT_UNIX2DOS; |
| 169 | break; |
| 170 | case 'u': |
| 171 | ConvType = CT_DOS2UNIX; |
| 172 | break; |
| 173 | default: |
| 174 | show_usage(); |
| 175 | } |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 176 | } |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 177 | |
| 178 | if (optind < argc) { |
| 179 | while(optind < argc) |
| 180 | if ((o = convert(argv[optind++], ConvType)) < 0) |
| 181 | break; |
| 182 | } |
| 183 | else |
| 184 | o = convert(NULL, ConvType); |
| 185 | |
| 186 | return o; |
| 187 | } |
| 188 | |