diff --git a/applets.h b/applets.h index 1fb6dcd92..9fc59cda5 100644 --- a/applets.h +++ b/applets.h @@ -408,7 +408,7 @@ APPLET(uniq, uniq_main, _BB_DIR_USR_BIN) #endif #ifdef BB_UNIX2DOS - APPLET(unix2dos, unix2dos_main, _BB_DIR_USR_BIN) + APPLET(unix2dos, dos2unix_main, _BB_DIR_USR_BIN) #endif #ifdef BB_UPDATE APPLET(update, update_main, _BB_DIR_SBIN) diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c index e97c3ba9a..68aee13c5 100644 --- a/coreutils/dos2unix.c +++ b/coreutils/dos2unix.c @@ -29,21 +29,51 @@ #include #include +#include +#include +#include #include "busybox.h" +static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + // if fn is NULL then input is stdin and output is stdout -static int convert(char *fn, int ConvType) { - int c; - char *tempFn = NULL; +static int convert(char *fn, int ConvType) +{ + int c, fd; + struct timeval tv; + char tempFn[BUFSIZ]; + static uint64_t value=0; FILE *in = stdin, *out = stdout; if (fn != NULL) { - if ((in = wfopen(fn, "r")) == NULL) { + if ((in = wfopen(fn, "rw")) == NULL) { return -1; } - if ((out = tmpfile()) == NULL) { - perror_msg(NULL); - return -2; + strcpy(tempFn, fn); + c = strlen(tempFn); + tempFn[c] = '.'; + while(1) { + if (c >=BUFSIZ) + error_msg_and_die("unique name not found"); + /* Get some semi random stuff to try and make a + * random filename based (and in the same dir as) + * the input file... */ + gettimeofday (&tv, NULL); + value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid (); + tempFn[++c] = letters[value % 62]; + tempFn[c+1] = '\0'; + value /= 62; + + if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) { + continue; + } + out = fdopen(fd, "w+"); + if (!out) { + close(fd); + remove(tempFn); + continue; + } + break; } } @@ -51,6 +81,7 @@ static int convert(char *fn, int ConvType) { if (c == '\r') { if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { // file is alredy in DOS format so it is not necessery to touch it + remove(tempFn); if (fclose(in) < 0 || fclose(out) < 0) { perror_msg(NULL); return -2; @@ -64,6 +95,7 @@ static int convert(char *fn, int ConvType) { if (c == '\n') { if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { // file is alredy in UNIX format so it is not necessery to touch it + remove(tempFn); if ((fclose(in) < 0) || (fclose(out) < 0)) { perror_msg(NULL); return -2; @@ -95,29 +127,35 @@ static int convert(char *fn, int ConvType) { } if (fn != NULL) { - if (fclose(in) < 0 || fclose(out) < 0 || - (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) { - perror_msg(NULL); - return -2; + if (fclose(in) < 0 || fclose(out) < 0) { + perror_msg(NULL); + remove(tempFn); + return -2; } - while ((c = fgetc(in)) != EOF) { - fputc(c, out); - } - - if ((fclose(in) < 0) || (fclose(out) < 0)) { - perror_msg(NULL); - return -2; + /* Assume they are both on the same filesystem */ + if (rename(tempFn, fn) < 0) { + perror_msg("unable to rename '%s' as '%s'", tempFn, fn); + return -1; } } return 0; } -int dos2unix_main(int argc, char *argv[]) { +int dos2unix_main(int argc, char *argv[]) +{ int ConvType = CT_AUTO; int o; + //See if we are supposed to be doing dos2unix or unix2dos + if (argv[0][0]=='d') { + ConvType = CT_DOS2UNIX; + } + if (argv[0][0]=='u') { + ConvType = CT_UNIX2DOS; + } + // process parameters while ((o = getopt(argc, argv, "du")) != EOF) { switch (o) { diff --git a/dos2unix.c b/dos2unix.c index e97c3ba9a..68aee13c5 100644 --- a/dos2unix.c +++ b/dos2unix.c @@ -29,21 +29,51 @@ #include #include +#include +#include +#include #include "busybox.h" +static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + // if fn is NULL then input is stdin and output is stdout -static int convert(char *fn, int ConvType) { - int c; - char *tempFn = NULL; +static int convert(char *fn, int ConvType) +{ + int c, fd; + struct timeval tv; + char tempFn[BUFSIZ]; + static uint64_t value=0; FILE *in = stdin, *out = stdout; if (fn != NULL) { - if ((in = wfopen(fn, "r")) == NULL) { + if ((in = wfopen(fn, "rw")) == NULL) { return -1; } - if ((out = tmpfile()) == NULL) { - perror_msg(NULL); - return -2; + strcpy(tempFn, fn); + c = strlen(tempFn); + tempFn[c] = '.'; + while(1) { + if (c >=BUFSIZ) + error_msg_and_die("unique name not found"); + /* Get some semi random stuff to try and make a + * random filename based (and in the same dir as) + * the input file... */ + gettimeofday (&tv, NULL); + value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid (); + tempFn[++c] = letters[value % 62]; + tempFn[c+1] = '\0'; + value /= 62; + + if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) { + continue; + } + out = fdopen(fd, "w+"); + if (!out) { + close(fd); + remove(tempFn); + continue; + } + break; } } @@ -51,6 +81,7 @@ static int convert(char *fn, int ConvType) { if (c == '\r') { if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { // file is alredy in DOS format so it is not necessery to touch it + remove(tempFn); if (fclose(in) < 0 || fclose(out) < 0) { perror_msg(NULL); return -2; @@ -64,6 +95,7 @@ static int convert(char *fn, int ConvType) { if (c == '\n') { if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { // file is alredy in UNIX format so it is not necessery to touch it + remove(tempFn); if ((fclose(in) < 0) || (fclose(out) < 0)) { perror_msg(NULL); return -2; @@ -95,29 +127,35 @@ static int convert(char *fn, int ConvType) { } if (fn != NULL) { - if (fclose(in) < 0 || fclose(out) < 0 || - (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) { - perror_msg(NULL); - return -2; + if (fclose(in) < 0 || fclose(out) < 0) { + perror_msg(NULL); + remove(tempFn); + return -2; } - while ((c = fgetc(in)) != EOF) { - fputc(c, out); - } - - if ((fclose(in) < 0) || (fclose(out) < 0)) { - perror_msg(NULL); - return -2; + /* Assume they are both on the same filesystem */ + if (rename(tempFn, fn) < 0) { + perror_msg("unable to rename '%s' as '%s'", tempFn, fn); + return -1; } } return 0; } -int dos2unix_main(int argc, char *argv[]) { +int dos2unix_main(int argc, char *argv[]) +{ int ConvType = CT_AUTO; int o; + //See if we are supposed to be doing dos2unix or unix2dos + if (argv[0][0]=='d') { + ConvType = CT_DOS2UNIX; + } + if (argv[0][0]=='u') { + ConvType = CT_UNIX2DOS; + } + // process parameters while ((o = getopt(argc, argv, "du")) != EOF) { switch (o) { diff --git a/include/applets.h b/include/applets.h index 1fb6dcd92..9fc59cda5 100644 --- a/include/applets.h +++ b/include/applets.h @@ -408,7 +408,7 @@ APPLET(uniq, uniq_main, _BB_DIR_USR_BIN) #endif #ifdef BB_UNIX2DOS - APPLET(unix2dos, unix2dos_main, _BB_DIR_USR_BIN) + APPLET(unix2dos, dos2unix_main, _BB_DIR_USR_BIN) #endif #ifdef BB_UPDATE APPLET(update, update_main, _BB_DIR_SBIN) diff --git a/unix2dos.c b/unix2dos.c deleted file mode 100644 index 37da9a9cc..000000000 --- a/unix2dos.c +++ /dev/null @@ -1,49 +0,0 @@ -/* - Mini unix2dos implementation for busybox - - Copyright 1994,1995 Patrick Volkerding, Moorhead, Minnesota USA - All rights reserved. - - Redistribution and use of this source code, with or without modification, is - permitted provided that the following condition is met: - - 1. Redistributions of this source code must retain the above copyright - notice, this condition, and the following disclaimer. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#include "busybox.h" -#include - -int unix2dos_main( int argc, char **argv ) { - int c; - if (argc > 1) { - c = *argv[1]; - if (c == '-') { - show_usage(); - } - } - c = getchar(); - while (c != EOF) { - /* Eat any \r's... they shouldn't be here */ - while (c == '\r') c = getchar(); - if (c == EOF) break; - if (c != '\n') { - putchar(c); - } else { - printf("\r\n"); - } - c = getchar(); - } - return 0; -}