[patch] size reduction for dos2unix/unix2dos
Bernhard Fischer
rep.nop at aon.at
Sun Aug 28 19:54:25 UTC 2005
Hi,
[in order to not always add to the size of busybox, let me suggest a
size reduction this time]
$ size coreutils/dos2unix.o.oorig coreutils/dos2unix.o
text data bss dec hex filename
957 0 4 961 3c1 coreutils/dos2unix.o.oorig
838 0 4 842 34a coreutils/dos2unix.o
- remove CT_AUTO (dead code since the applets always start with 'd' or
'u'). This allows us to remove the "(!ConvType)" cases too, since they
cannot be reached ever (same as before; just removes dead code).
- remove the alphabet-array in favour of dynamically emitting the
desired char.
- don't initialze *in and *out twice (if a file was given) which also
saves a few bytes.
- correct a few typos in comments while at it.
- fold extra if(c!=EOF) into the while (saved another few bytes for me).
- appletname starts either with 'd' or 'u', so look if it is 'd' and
ditch the check for 'c'.
- peruse bb_getopt_ulflags.
- use RESERVE_CONFIG_BUFFER for tempFn.
- add one simple testcase each for dos2unix and for unix2dos
thank you,
Bernhard
-------------- next part --------------
Index: coreutils/dos2unix.c
===================================================================
--- coreutils/dos2unix.c (revision 11230)
+++ coreutils/dos2unix.c (working copy)
@@ -35,7 +35,6 @@
#include <sys/time.h>
#include "busybox.h"
-#define CT_AUTO 0
#define CT_UNIX2DOS 1
#define CT_DOS2UNIX 2
@@ -47,16 +46,16 @@
* to pick a random letter to add to out temporary file. */
typedef unsigned long int bb_uint64_t;
-static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+/* XXX static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";*/
-// if fn is NULL then input is stdin and output is stdout
+/* if fn is NULL then input is stdin and output is stdout */
static int convert(char *fn, int ConvType)
{
int c, fd;
struct timeval tv;
- char tempFn[BUFSIZ];
+ RESERVE_CONFIG_BUFFER(tempFn, BUFSIZ);
static bb_uint64_t value=0;
- FILE *in = stdin, *out = stdout;
+ FILE *in, *out;
if (fn != NULL) {
in = bb_xfopen(fn, "rw");
@@ -73,7 +72,10 @@ static int convert(char *fn, int ConvTyp
* the input file... */
gettimeofday (&tv, NULL);
value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
- tempFn[++c] = letters[value % 62];
+/* XXX tempFn[++c] = letters[value % 62]; */
+ tempFn[++c] = ((value%62) < 26)?(value%62)+97:
+ ((value%62) < 52)?(value%62)+39:
+ (value%62)-4;
tempFn[c+1] = '\0';
value /= 62;
@@ -88,12 +90,16 @@ static int convert(char *fn, int ConvTyp
}
break;
}
+ } else {
+ in = stdin;
+ out = stdout;
}
while ((c = fgetc(in)) != EOF) {
if (c == '\r') {
if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
- // file is alredy in DOS format so it is not necessery to touch it
+ /* file is already in DOS format so it is
+ * not necessary to touch it. */
remove(tempFn);
if (fclose(in) < 0 || fclose(out) < 0) {
bb_perror_nomsg();
@@ -101,13 +107,12 @@ static int convert(char *fn, int ConvTyp
}
return 0;
}
- if (!ConvType)
- ConvType = CT_DOS2UNIX;
break;
}
if (c == '\n') {
if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
- // file is alredy in UNIX format so it is not necessery to touch it
+ /* file is already in DOS format so it is
+ * not necessary to touch it. */
remove(tempFn);
if ((fclose(in) < 0) || (fclose(out) < 0)) {
bb_perror_nomsg();
@@ -115,27 +120,21 @@ static int convert(char *fn, int ConvTyp
}
return 0;
}
- if (!ConvType) {
- ConvType = CT_UNIX2DOS;
- }
if (ConvType == CT_UNIX2DOS) {
fputc('\r', out);
}
- fputc('\n', out);
- break;
}
fputc(c, out);
}
- if (c != EOF)
- while ((c = fgetc(in)) != EOF) {
- if (c == '\r')
- continue;
- if (c == '\n') {
- if (ConvType == CT_UNIX2DOS)
- fputc('\r', out);
- fputc('\n', out);
- continue;
- }
+ while (c != EOF && (c = fgetc(in)) != EOF) {
+ if (c == '\r')
+ continue;
+ if (c == '\n') {
+ if (ConvType == CT_UNIX2DOS)
+ fputc('\r', out);
+ fputc('\n', out);
+ continue;
+ }
fputc(c, out);
}
@@ -160,30 +159,23 @@ static int convert(char *fn, int ConvTyp
int dos2unix_main(int argc, char *argv[])
{
- int ConvType = CT_AUTO;
+ int ConvType;
int o;
- //See if we are supposed to be doing dos2unix or unix2dos
+ /* See if we are supposed to be doing dos2unix or unix2dos */
if (argv[0][0]=='d') {
ConvType = CT_DOS2UNIX;
- }
- if (argv[0][0]=='u') {
+ } else {
ConvType = CT_UNIX2DOS;
}
- // process parameters
- while ((o = getopt(argc, argv, "du")) != EOF) {
- switch (o) {
- case 'd':
- ConvType = CT_UNIX2DOS;
- break;
- case 'u':
- ConvType = CT_DOS2UNIX;
- break;
- default:
- bb_show_usage();
- }
- }
+ /* process parameters */
+ o = bb_getopt_ulflags(argc, argv, "ud");
+
+ /* Do the conversion requested by an argument else do the default
+ * conversion depending on our name. */
+ if (o)
+ ConvType = o;
if (optind < argc) {
while(optind < argc)
Index: testsuite/dos2unix/unix2dos-works
===================================================================
--- testsuite/dos2unix/unix2dos-works (revision 0)
+++ testsuite/dos2unix/unix2dos-works (revision 0)
@@ -0,0 +1,2 @@
+test "$(echo -en "1\n2\n" | busybox unix2dos)" == "1
+2
"
Index: testsuite/dos2unix/dos2unix-works
===================================================================
--- testsuite/dos2unix/dos2unix-works (revision 0)
+++ testsuite/dos2unix/dos2unix-works (revision 0)
@@ -0,0 +1,2 @@
+test "$(echo -en "1\r\n2\r\n" | busybox dos2unix)" == "1
+2"
More information about the busybox
mailing list