aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-03-02 12:51:26 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-03-02 12:51:26 +0000
commitfb29038b59fa639b79de99c4edddfa6462918765 (patch)
treea527be76248ef6d10a5861a2e423d6f049b9d51a
parent509697f00df5286674fdbdf7b02a57fea83636d0 (diff)
downloadbusybox-w32-fb29038b59fa639b79de99c4edddfa6462918765.tar.gz
busybox-w32-fb29038b59fa639b79de99c4edddfa6462918765.tar.bz2
busybox-w32-fb29038b59fa639b79de99c4edddfa6462918765.zip
libbb: inntroduce and use bb_pstrcmp() and qsort_string_vector()
msh: glob0/glob1/glob2/glob3 are just a sorting routine! remove them. bb_pstrcmp - 25 +25 qsort_string_vector - 24 +24 expand 2209 2227 +18 getopt32 1359 1370 +11 passwd_main 1072 1074 +2 handle_incoming_and_exit 2737 2735 -2 UNSPEC_print 66 64 -2 forkexec 1343 1339 -4 input_tab 3338 3330 -8 get_dir 185 177 -8 expmeta 481 473 -8 cmdputs 405 397 -8 xstrcmp 12 - -12 find_pair 187 167 -20 match_compare 25 - -25 dir_strcmp 25 - -25 glob2 27 - -27 glob3 34 - -34 glob1 256 - -256 ------------------------------------------------------------------------------ (add/remove: 2/6 grow/shrink: 3/8 up/down: 80/-439) Total: -359 bytes
-rw-r--r--editors/diff.c8
-rw-r--r--include/libbb.h3
-rw-r--r--libbb/Kbuild1
-rw-r--r--libbb/lineedit.c7
-rw-r--r--shell/msh.c131
5 files changed, 15 insertions, 135 deletions
diff --git a/editors/diff.c b/editors/diff.c
index 8844ec1c0..1359af838 100644
--- a/editors/diff.c
+++ b/editors/diff.c
@@ -1095,12 +1095,6 @@ static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
1095 1095
1096 1096
1097#if ENABLE_FEATURE_DIFF_DIR 1097#if ENABLE_FEATURE_DIFF_DIR
1098static int dir_strcmp(const void *p1, const void *p2)
1099{
1100 return strcmp(*(char *const *) p1, *(char *const *) p2);
1101}
1102
1103
1104/* This function adds a filename to dl, the directory listing. */ 1098/* This function adds a filename to dl, the directory listing. */
1105static int add_to_dirlist(const char *filename, 1099static int add_to_dirlist(const char *filename,
1106 struct stat ATTRIBUTE_UNUSED * sb, void *userdata, 1100 struct stat ATTRIBUTE_UNUSED * sb, void *userdata,
@@ -1144,7 +1138,7 @@ static char **get_dir(char *path)
1144 } 1138 }
1145 1139
1146 /* Sort dl alphabetically. */ 1140 /* Sort dl alphabetically. */
1147 qsort(dl, dl_count, sizeof(char *), dir_strcmp); 1141 qsort_string_vector(dl, dl_count);
1148 1142
1149 dl[dl_count] = NULL; 1143 dl[dl_count] = NULL;
1150 return dl; 1144 return dl;
diff --git a/include/libbb.h b/include/libbb.h
index 8305e59fc..9b52dd231 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -545,6 +545,9 @@ extern FILE *fopen_or_warn(const char *filename, const char *mode);
545/* "Opens" stdin if filename is special, else just opens file: */ 545/* "Opens" stdin if filename is special, else just opens file: */
546extern FILE *fopen_or_warn_stdin(const char *filename); 546extern FILE *fopen_or_warn_stdin(const char *filename);
547 547
548int bb_pstrcmp(const void *a, const void *b);
549void qsort_string_vector(char **sv, unsigned count);
550
548/* Wrapper which restarts poll on EINTR or ENOMEM. 551/* Wrapper which restarts poll on EINTR or ENOMEM.
549 * On other errors complains [perror("poll")] and returns. 552 * On other errors complains [perror("poll")] and returns.
550 * Warning! May take (much) longer than timeout_ms to return! 553 * Warning! May take (much) longer than timeout_ms to return!
diff --git a/libbb/Kbuild b/libbb/Kbuild
index fd366559a..654722cd6 100644
--- a/libbb/Kbuild
+++ b/libbb/Kbuild
@@ -12,6 +12,7 @@ lib-y += bb_askpass.o
12lib-y += bb_basename.o 12lib-y += bb_basename.o
13lib-y += bb_do_delay.o 13lib-y += bb_do_delay.o
14lib-y += bb_pwd.o 14lib-y += bb_pwd.o
15lib-y += bb_qsort.o
15lib-y += bb_strtonum.o 16lib-y += bb_strtonum.o
16lib-y += change_identity.o 17lib-y += change_identity.o
17lib-y += chomp.o 18lib-y += chomp.o
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index d716169d4..7e408207c 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -796,11 +796,6 @@ static char *add_quote_for_spec_chars(char *found)
796 return s; 796 return s;
797} 797}
798 798
799static int match_compare(const void *a, const void *b)
800{
801 return strcmp(*(char**)a, *(char**)b);
802}
803
804/* Do TAB completion */ 799/* Do TAB completion */
805static void input_tab(smallint *lastWasTab) 800static void input_tab(smallint *lastWasTab)
806{ 801{
@@ -841,7 +836,7 @@ static void input_tab(smallint *lastWasTab)
841 /* Sort, then remove any duplicates found */ 836 /* Sort, then remove any duplicates found */
842 if (matches) { 837 if (matches) {
843 int i, n = 0; 838 int i, n = 0;
844 qsort(matches, num_matches, sizeof(char*), match_compare); 839 qsort_string_vector(matches, num_matches);
845 for (i = 0; i < num_matches - 1; ++i) { 840 for (i = 0; i < num_matches - 1; ++i) {
846 if (matches[i] && matches[i+1]) { /* paranoia */ 841 if (matches[i] && matches[i+1]) { /* paranoia */
847 if (strcmp(matches[i], matches[i+1]) == 0) { 842 if (strcmp(matches[i], matches[i+1]) == 0) {
diff --git a/shell/msh.c b/shell/msh.c
index cde18d08f..52a11bc48 100644
--- a/shell/msh.c
+++ b/shell/msh.c
@@ -527,9 +527,6 @@ static int grave(int quoted);
527static void globname(char *we, char *pp); 527static void globname(char *we, char *pp);
528static char *generate(char *start1, char *end1, char *middle, char *end); 528static char *generate(char *start1, char *end1, char *middle, char *end);
529static int anyspcl(struct wdblock *wb); 529static int anyspcl(struct wdblock *wb);
530static int xstrcmp(char *p1, char *p2);
531static void glob0(char *a0, unsigned a1
532 /*, int item_sz, int (*f)(char *, char *) */);
533static void readhere(char **name, char *s, int ec); 530static void readhere(char **name, char *s, int ec);
534static int xxchar(struct ioarg *ap); 531static int xxchar(struct ioarg *ap);
535 532
@@ -4269,11 +4266,11 @@ static struct wdblock *glob(char *cp, struct wdblock *wb)
4269 DELETE(cl->w_words[i]); 4266 DELETE(cl->w_words[i]);
4270 DELETE(cl); 4267 DELETE(cl);
4271 } 4268 }
4272 for (i = 0; i < cl->w_nword; i++)
4273 unquote(cl->w_words[i]);
4274 glob0((char *) cl->w_words, cl->w_nword /*, sizeof(char *), xstrcmp*/);
4275 if (cl->w_nword) { 4269 if (cl->w_nword) {
4276 for (i = 0; i < cl->w_nword; i++) 4270 for (i = 0; i < cl->w_nword; i++)
4271 unquote(cl->w_words[i]);
4272 qsort_string_vector(cl->w_words, cl->w_nword);
4273 for (i = 0; i < cl->w_nword; i++)
4277 wb = addword(cl->w_words[i], wb); 4274 wb = addword(cl->w_words[i], wb);
4278 DELETE(cl); 4275 DELETE(cl);
4279 return wb; 4276 return wb;
@@ -4296,11 +4293,13 @@ static void globname(char *we, char *pp)
4296 for (np = we; np != pp; pp--) 4293 for (np = we; np != pp; pp--)
4297 if (pp[-1] == '/') 4294 if (pp[-1] == '/')
4298 break; 4295 break;
4299 for (dp = cp = get_space((int) (pp - np) + 3); np < pp;) 4296 dp = cp = get_space((int) (pp - np) + 3);
4297 while (np < pp)
4300 *cp++ = *np++; 4298 *cp++ = *np++;
4301 *cp++ = '.'; 4299 *cp++ = '.';
4302 *cp = '\0'; 4300 *cp = '\0';
4303 for (gp = cp = get_space(strlen(pp) + 1); *np && *np != '/';) 4301 gp = cp = get_space(strlen(pp) + 1);
4302 while (*np && *np != '/')
4304 *cp++ = *np++; 4303 *cp++ = *np++;
4305 *cp = '\0'; 4304 *cp = '\0';
4306 dirp = opendir(dp); 4305 dirp = opendir(dp);
@@ -4372,11 +4371,6 @@ static int anyspcl(struct wdblock *wb)
4372 return 0; 4371 return 0;
4373} 4372}
4374 4373
4375static int xstrcmp(char *p1, char *p2)
4376{
4377 return strcmp(*(char **) p1, *(char **) p2);
4378}
4379
4380 4374
4381/* -------- word.c -------- */ 4375/* -------- word.c -------- */
4382 4376
@@ -4423,118 +4417,11 @@ static char **getwords(struct wdblock *wb)
4423 } 4417 }
4424 nb = sizeof(*wd) * wb->w_nword; 4418 nb = sizeof(*wd) * wb->w_nword;
4425 wd = get_space(nb); 4419 wd = get_space(nb);
4426 memcpy((char *) wd, (char *) wb->w_words, nb); 4420 memcpy(wd, wb->w_words, nb);
4427 DELETE(wb); /* perhaps should done by caller */ 4421 DELETE(wb); /* perhaps should done by caller */
4428 return wd; 4422 return wd;
4429} 4423}
4430 4424
4431/*static int (*cmp_func) (char *, char *);*/
4432/*static int glob_item_sz;*/
4433#define cmp_func xstrcmp
4434enum { glob_item_sz = sizeof(char *) };
4435
4436static void glob3(char *index1, char *index2, char *index3)
4437{
4438 int m = glob_item_sz;
4439 do {
4440 char c = *index1;
4441 *index1++ = *index3;
4442 *index3++ = *index2;
4443 *index2++ = c;
4444 } while (--m);
4445}
4446
4447static void glob2(char *index1, char *index2)
4448{
4449 int m = glob_item_sz;
4450 do {
4451 char c = *index1;
4452 *index1++ = *index2;
4453 *index2++ = c;
4454 } while (--m);
4455}
4456
4457static void glob1(char *base, char *lim)
4458{
4459 char *i, *j;
4460 int v2;
4461 char *lptr, *hptr;
4462 int c;
4463 unsigned n;
4464
4465 v2 = glob_item_sz;
4466 top:
4467 n = (int) (lim - base);
4468 if (n <= v2)
4469 return;
4470 n = v2 * (n / (2 * v2));
4471 hptr = lptr = base + n;
4472 i = base;
4473 j = lim - v2;
4474 for (;;) {
4475 if (i < lptr) {
4476 c = cmp_func(i, lptr);
4477 if (c == 0) {
4478 lptr -= v2;
4479 glob2(i, lptr);
4480 continue;
4481 }
4482 if (c < 0) {
4483 i += v2;
4484 continue;
4485 }
4486 }
4487 begin:
4488 if (j > hptr) {
4489 c = cmp_func(hptr, j);
4490 if (c == 0) {
4491 hptr += v2;
4492 glob2(hptr, j);
4493 goto begin;
4494 }
4495 if (c > 0) {
4496 if (i == lptr) {
4497 hptr += v2;
4498 glob3(i, hptr, j);
4499 lptr += v2;
4500 i = lptr;
4501 goto begin;
4502 }
4503 glob2(i, j);
4504 j -= v2;
4505 i += v2;
4506 continue;
4507 }
4508 j -= v2;
4509 goto begin;
4510 }
4511
4512 if (i == lptr) {
4513 if (lptr - base >= lim - hptr) {
4514 glob1(hptr + v2, lim);
4515 lim = lptr;
4516 } else {
4517 glob1(base, lptr);
4518 base = hptr + v2;
4519 }
4520 goto top;
4521 }
4522
4523 lptr -= v2;
4524 glob3(j, lptr, i);
4525 hptr -= v2;
4526 j = hptr;
4527 }
4528}
4529
4530static void glob0(char *a0, unsigned a1
4531 /*, int item_sz, int (*f) (char *, char *) */)
4532{
4533 /*cmp_func = f; - always xstrcmp */
4534 /*glob_item_sz = item_sz; - always sizeof(char*) */
4535 glob1(a0, a0 + a1 * /*item_sz:*/ sizeof(char*));
4536}
4537
4538 4425
4539/* -------- io.c -------- */ 4426/* -------- io.c -------- */
4540 4427