aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-04-09 22:48:12 +0000
committerEric Andersen <andersen@codepoet.org>2001-04-09 22:48:12 +0000
commite5dfced23a904d08afa5dcee190c3c3d845d9f50 (patch)
treeef367ee8a9096884fb40debdc9e10af8583f9d5f
parenta75e2867435faa68ea03735fe09ad298fa3e4e72 (diff)
downloadbusybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.tar.gz
busybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.tar.bz2
busybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.zip
Apply Vladimir's latest cleanup patch.
-Erik
-rw-r--r--AUTHORS3
-rw-r--r--Changelog15
-rw-r--r--Config.h3
-rw-r--r--Makefile3
-rw-r--r--applets/busybox.c23
-rw-r--r--archival/dpkg.c20
-rw-r--r--busybox.c23
-rw-r--r--cmdedit.c249
-rw-r--r--coreutils/du.c17
-rw-r--r--coreutils/echo.c2
-rw-r--r--coreutils/pwd.c16
-rw-r--r--coreutils/tr.c2
-rw-r--r--cp_mv.c10
-rw-r--r--docs/busybox.pod9
-rw-r--r--docs/busybox_footer.pod9
-rw-r--r--dpkg.c20
-rw-r--r--du.c17
-rw-r--r--echo.c2
-rw-r--r--include/libbb.h7
-rw-r--r--lash.c37
-rw-r--r--libbb/concat_path_file.c24
-rw-r--r--libbb/libbb.h7
-rw-r--r--libbb/print_file.c29
-rw-r--r--libbb/process_escape_sequence.c73
-rw-r--r--libbb/xgetcwd.c52
-rw-r--r--pwd.c16
-rw-r--r--sh.c37
-rw-r--r--shell/cmdedit.c249
-rw-r--r--shell/lash.c37
-rw-r--r--tr.c2
30 files changed, 500 insertions, 513 deletions
diff --git a/AUTHORS b/AUTHORS
index 5913e2795..593979cad 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -73,3 +73,6 @@ Charles P. Wright <cpwright@villagenet.com>
73Enrique Zanardi <ezanardi@ull.es> 73Enrique Zanardi <ezanardi@ull.es>
74 tarcat (since removed), loadkmap, various fixes, Debian maintenance 74 tarcat (since removed), loadkmap, various fixes, Debian maintenance
75 75
76Vladimir Oleynik <dzo@simtreas.ru>
77 cmdedit, stty-port, locale, various fixes
78 and irreconcilable critic of everything not perfect.
diff --git a/Changelog b/Changelog
index b3531ae54..4adc7746c 100644
--- a/Changelog
+++ b/Changelog
@@ -42,12 +42,15 @@
42 for init.c 42 for init.c
43 * Glenn McGrath -- Fixed problems with dpkg and dpkg-deb applets 43 * Glenn McGrath -- Fixed problems with dpkg and dpkg-deb applets
44 * Glenn McGrath -- Don't try to automount devfs 44 * Glenn McGrath -- Don't try to automount devfs
45 * Vladimir N. Oleynik -- optimizations for more.c 45 * Vladimir Oleynik -- optimizations for more.c
46 * Vladimir N. Oleynik -- Added locale support to the shell. 46 * Vladimir Oleynik -- Added locale support to the shell, and fixed
47 * Vladimir N. Oleynik -- moved struct applet from busybox.c to applets.c 47 locale support in several other places
48 * Vladimir N. Oleynik -- A size optimization for rdate 48 * Vladimir Oleynik -- moved struct applet from busybox.c to applets.c
49 * Vladimir N. Oleynik -- Fixed printf applets's locale handling 49 * Vladimir Oleynik -- A size optimization for rdate
50 * Vladimir N. Oleynik -- More cmdedit updates 50 * Vladimir Oleynik -- Fixed printf applets's locale handling
51 * Vladimir Oleynik -- More cmdedit updates
52 * Vladimir Oleynik -- Fixed `du' applet so it continues running
53 after permission errors.
51 * Pierre Peiffer <pierre.peiffer@sxb.bsf.alcatel.fr> -- made 54 * Pierre Peiffer <pierre.peiffer@sxb.bsf.alcatel.fr> -- made
52 find_pid_by_name() cope with swapped out processes. 55 find_pid_by_name() cope with swapped out processes.
53 * Jari Ruusu <jari.ruusu@pp.inet.fi> -- updates so that setting 56 * Jari Ruusu <jari.ruusu@pp.inet.fi> -- updates so that setting
diff --git a/Config.h b/Config.h
index 7d8e83d38..3600d0648 100644
--- a/Config.h
+++ b/Config.h
@@ -349,6 +349,9 @@
349#define BB_FEATURE_VI_SET // :set 349#define BB_FEATURE_VI_SET // :set
350#define BB_FEATURE_VI_WIN_RESIZE // handle window resize 350#define BB_FEATURE_VI_WIN_RESIZE // handle window resize
351// 351//
352// Enable a if you system have setuped locale
353//#define BB_LOCALE_SUPPORT
354//
352// End of Features List 355// End of Features List
353// 356//
354// 357//
diff --git a/Makefile b/Makefile
index e16c1de95..9c3b117d8 100644
--- a/Makefile
+++ b/Makefile
@@ -245,7 +245,8 @@ my_getgrgid.c my_getpwnamegid.c my_getpwuid.c my_getgrnam.c my_getpwnam.c \
245recursive_action.c safe_read.c safe_strncpy.c syscalls.c module_syscalls.c \ 245recursive_action.c safe_read.c safe_strncpy.c syscalls.c module_syscalls.c \
246syslog_msg_with_name.c time_string.c trim.c vdprintf.c wfopen.c xfuncs.c \ 246syslog_msg_with_name.c time_string.c trim.c vdprintf.c wfopen.c xfuncs.c \
247xregcomp.c error_msg_and_die.c perror_msg.c perror_msg_and_die.c \ 247xregcomp.c error_msg_and_die.c perror_msg.c perror_msg_and_die.c \
248verror_msg.c vperror_msg.c mtab.c mtab_file.c 248verror_msg.c vperror_msg.c mtab.c mtab_file.c xgetcwd.c concat_path_file.c
249
249LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) 250LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
250LIBBB_CFLAGS = -I$(LIBBB) 251LIBBB_CFLAGS = -I$(LIBBB)
251ifneq ($(strip $(BB_SRC_DIR)),) 252ifneq ($(strip $(BB_SRC_DIR)),)
diff --git a/applets/busybox.c b/applets/busybox.c
index 5085556d6..9db26df27 100644
--- a/applets/busybox.c
+++ b/applets/busybox.c
@@ -10,6 +10,10 @@
10#define BB_DECLARE_EXTERN 10#define BB_DECLARE_EXTERN
11#include "messages.c" 11#include "messages.c"
12 12
13#ifdef BB_LOCALE_SUPPORT
14#include <locale.h>
15#endif
16
13int been_there_done_that = 0; /* Also used in applets.c */ 17int been_there_done_that = 0; /* Also used in applets.c */
14const char *applet_name; 18const char *applet_name;
15 19
@@ -60,7 +64,7 @@ static void install_links(const char *busybox, int use_symbolic_links)
60{ 64{
61 __link_f Link = link; 65 __link_f Link = link;
62 66
63 char command[256]; 67 char *fpc;
64 int i; 68 int i;
65 int rc; 69 int rc;
66 70
@@ -68,13 +72,13 @@ static void install_links(const char *busybox, int use_symbolic_links)
68 Link = symlink; 72 Link = symlink;
69 73
70 for (i = 0; applets[i].name != NULL; i++) { 74 for (i = 0; applets[i].name != NULL; i++) {
71 sprintf ( command, "%s/%s", 75 fpc = concat_path_file(
72 install_dir[applets[i].location], applets[i].name); 76 install_dir[applets[i].location], applets[i].name);
73 rc = Link(busybox, command); 77 rc = Link(busybox, fpc);
74 78 if (rc!=0 && errno!=EEXIST) {
75 if (rc) { 79 perror_msg("%s", fpc);
76 perror_msg("%s", command);
77 } 80 }
81 free(fpc);
78 } 82 }
79} 83}
80 84
@@ -97,6 +101,11 @@ int main(int argc, char **argv)
97 } 101 }
98#endif 102#endif
99 103
104#ifdef BB_LOCALE_SUPPORT
105 if(getpid()!=1) /* Do not set locale for `init' */
106 setlocale(LC_ALL, "");
107#endif
108
100 run_applet_by_name(applet_name, argc, argv); 109 run_applet_by_name(applet_name, argc, argv);
101 error_msg_and_die("applet not found"); 110 error_msg_and_die("applet not found");
102} 111}
diff --git a/archival/dpkg.c b/archival/dpkg.c
index 57b31697b..5687e00be 100644
--- a/archival/dpkg.c
+++ b/archival/dpkg.c
@@ -20,7 +20,7 @@
20#define DODEPENDS 1 20#define DODEPENDS 1
21 21
22/* Should we do debugging? */ 22/* Should we do debugging? */
23#define DODEBUG 1 23//#define DODEBUG 1
24 24
25#ifdef DODEBUG 25#ifdef DODEBUG
26#define SYSTEM(x) do_system(x) 26#define SYSTEM(x) do_system(x)
@@ -274,7 +274,7 @@ static package_t *depends_resolve(package_t *pkgs, void *status)
274 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && 274 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides &&
275 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) { 275 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
276 if (chk->requiredcount >= DEPENDSMAX) { 276 if (chk->requiredcount >= DEPENDSMAX) {
277 fprintf(stderr, "Too many dependencies for %s\n", chk->package); 277 error_msg("Too many dependencies for %s", chk->package);
278 return 0; 278 return 0;
279 } 279 }
280 if (chk != pkg) { 280 if (chk != pkg) {
@@ -284,7 +284,7 @@ static package_t *depends_resolve(package_t *pkgs, void *status)
284 } 284 }
285 } 285 }
286 if (chk == 0) { 286 if (chk == 0) {
287 fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]); 287 error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]);
288 return 0; 288 return 0;
289 } 289 }
290 } 290 }
@@ -382,7 +382,7 @@ static void *status_read(void)
382 printf("(Reading database...)\n"); 382 printf("(Reading database...)\n");
383 } 383 }
384 384
385 if ((f = fopen(statusfile, "r")) == NULL) { 385 if ((f = wfopen(statusfile, "r")) == NULL) {
386 return(NULL); 386 return(NULL);
387 } 387 }
388 388
@@ -483,8 +483,7 @@ static int status_merge(void *status, package_t *pkgs)
483 status_words_flag[statpkg->status_flag - 1], 483 status_words_flag[statpkg->status_flag - 1],
484 status_words_status[statpkg->status_status - 1]); 484 status_words_status[statpkg->status_status - 1]);
485 } 485 }
486 fputs(line, fout); 486 fprintf(fout, "%s\n", line);
487 fputc('\n', fout);
488 } 487 }
489 fclose(fin); 488 fclose(fin);
490 } 489 }
@@ -551,7 +550,7 @@ static int dpkg_doconfigure(package_t *pkg)
551 if (is_file(postinst)) { 550 if (is_file(postinst)) {
552 snprintf(buf, sizeof(buf), "%s configure", postinst); 551 snprintf(buf, sizeof(buf), "%s configure", postinst);
553 if ((r = do_system(buf)) != 0) { 552 if ((r = do_system(buf)) != 0) {
554 fprintf(stderr, "postinst exited with status %d\n", r); 553 error_msg("postinst exited with status %d\n", r);
555 pkg->status_status = status_status_halfconfigured; 554 pkg->status_status = status_status_halfconfigured;
556 return 1; 555 return 1;
557 } 556 }
@@ -565,7 +564,7 @@ static int dpkg_dounpack(package_t *pkg)
565{ 564{
566 int r = 0, i; 565 int r = 0, i;
567 int status = TRUE; 566 int status = TRUE;
568 char *cwd; 567 char *cwd = xgetcwd(0);
569 char *src_file = NULL; 568 char *src_file = NULL;
570 char *dst_file = NULL; 569 char *dst_file = NULL;
571// char *lst_file = NULL; 570// char *lst_file = NULL;
@@ -574,7 +573,8 @@ static int dpkg_dounpack(package_t *pkg)
574 573
575 DPRINTF("Unpacking %s\n", pkg->package); 574 DPRINTF("Unpacking %s\n", pkg->package);
576 575
577 cwd = getcwd(0, 0); 576 if(cwd==NULL)
577 exit(EXIT_FAILURE);
578 chdir("/"); 578 chdir("/");
579 deb_extract(dpkg_deb_extract, "/", pkg->file); 579 deb_extract(dpkg_deb_extract, "/", pkg->file);
580 580
@@ -714,7 +714,7 @@ static int dpkg_configure(package_t *pkgs, void *status)
714 found = tfind(pkg, &status, package_compare); 714 found = tfind(pkg, &status, package_compare);
715 715
716 if (found == 0) { 716 if (found == 0) {
717 fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package); 717 error_msg("Trying to configure %s, but it is not installed", pkg->package);
718 r = 1; 718 r = 1;
719 } 719 }
720 /* configure the package listed in the status file; 720 /* configure the package listed in the status file;
diff --git a/busybox.c b/busybox.c
index 5085556d6..9db26df27 100644
--- a/busybox.c
+++ b/busybox.c
@@ -10,6 +10,10 @@
10#define BB_DECLARE_EXTERN 10#define BB_DECLARE_EXTERN
11#include "messages.c" 11#include "messages.c"
12 12
13#ifdef BB_LOCALE_SUPPORT
14#include <locale.h>
15#endif
16
13int been_there_done_that = 0; /* Also used in applets.c */ 17int been_there_done_that = 0; /* Also used in applets.c */
14const char *applet_name; 18const char *applet_name;
15 19
@@ -60,7 +64,7 @@ static void install_links(const char *busybox, int use_symbolic_links)
60{ 64{
61 __link_f Link = link; 65 __link_f Link = link;
62 66
63 char command[256]; 67 char *fpc;
64 int i; 68 int i;
65 int rc; 69 int rc;
66 70
@@ -68,13 +72,13 @@ static void install_links(const char *busybox, int use_symbolic_links)
68 Link = symlink; 72 Link = symlink;
69 73
70 for (i = 0; applets[i].name != NULL; i++) { 74 for (i = 0; applets[i].name != NULL; i++) {
71 sprintf ( command, "%s/%s", 75 fpc = concat_path_file(
72 install_dir[applets[i].location], applets[i].name); 76 install_dir[applets[i].location], applets[i].name);
73 rc = Link(busybox, command); 77 rc = Link(busybox, fpc);
74 78 if (rc!=0 && errno!=EEXIST) {
75 if (rc) { 79 perror_msg("%s", fpc);
76 perror_msg("%s", command);
77 } 80 }
81 free(fpc);
78 } 82 }
79} 83}
80 84
@@ -97,6 +101,11 @@ int main(int argc, char **argv)
97 } 101 }
98#endif 102#endif
99 103
104#ifdef BB_LOCALE_SUPPORT
105 if(getpid()!=1) /* Do not set locale for `init' */
106 setlocale(LC_ALL, "");
107#endif
108
100 run_applet_by_name(applet_name, argc, argv); 109 run_applet_by_name(applet_name, argc, argv);
101 error_msg_and_die("applet not found"); 110 error_msg_and_die("applet not found");
102} 111}
diff --git a/cmdedit.c b/cmdedit.c
index a3710812f..eef1a88c8 100644
--- a/cmdedit.c
+++ b/cmdedit.c
@@ -31,8 +31,6 @@
31 */ 31 */
32 32
33 33
34//#define TEST
35
36#include <stdio.h> 34#include <stdio.h>
37#include <errno.h> 35#include <errno.h>
38#include <unistd.h> 36#include <unistd.h>
@@ -43,10 +41,16 @@
43#include <signal.h> 41#include <signal.h>
44#include <limits.h> 42#include <limits.h>
45 43
46#ifndef TEST
47
48#include "busybox.h" 44#include "busybox.h"
49 45
46#ifdef BB_LOCALE_SUPPORT
47#define Isprint(c) isprint((c))
48#else
49#define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') )
50#endif
51
52#ifndef TEST
53
50#define D(x) 54#define D(x)
51 55
52#else 56#else
@@ -59,13 +63,6 @@
59 63
60#define D(x) x 64#define D(x) x
61 65
62#ifndef TRUE
63#define TRUE 1
64#endif
65#ifndef FALSE
66#define FALSE 0
67#endif
68
69#endif /* TEST */ 66#endif /* TEST */
70 67
71#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION 68#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
@@ -92,29 +89,6 @@
92#endif /* advanced FEATURES */ 89#endif /* advanced FEATURES */
93 90
94 91
95#ifdef TEST
96void *xrealloc(void *old, size_t size)
97{
98 return realloc(old, size);
99}
100
101void *xmalloc(size_t size)
102{
103 return malloc(size);
104}
105char *xstrdup(const char *s)
106{
107 return strdup(s);
108}
109
110void *xcalloc(size_t size, size_t se)
111{
112 return calloc(size, se);
113}
114
115#define error_msg(s, d) fprintf(stderr, s, d)
116#endif /* TEST */
117
118 92
119struct history { 93struct history {
120 char *s; 94 char *s;
@@ -238,7 +212,7 @@ static void win_changed(int nsig)
238static void cmdedit_reset_term(void) 212static void cmdedit_reset_term(void)
239{ 213{
240 if ((handlers_sets & SET_RESET_TERM) != 0) { 214 if ((handlers_sets & SET_RESET_TERM) != 0) {
241 /* sparc and other have broken termios support: use old termio handling. */ 215/* sparc and other have broken termios support: use old termio handling. */
242 setTermSettings(fileno(stdin), (void *) &initial_settings); 216 setTermSettings(fileno(stdin), (void *) &initial_settings);
243 handlers_sets &= ~SET_RESET_TERM; 217 handlers_sets &= ~SET_RESET_TERM;
244 } 218 }
@@ -270,9 +244,9 @@ static void cmdedit_set_out_char(int next_char)
270 int c = (int)((unsigned char) command_ps[cursor]); 244 int c = (int)((unsigned char) command_ps[cursor]);
271 245
272 if (c == 0) 246 if (c == 0)
273 c = ' '; /* destroy end char? */ 247 c = ' '; /* destroy end char? */
274#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT 248#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
275 if (!isprint(c)) { /* Inverse put non-printable characters */ 249 if (!Isprint(c)) { /* Inverse put non-printable characters */
276 if (c >= 128) 250 if (c >= 128)
277 c -= 128; 251 c -= 128;
278 if (c < ' ') 252 if (c < ' ')
@@ -328,7 +302,7 @@ static void input_backward(int num)
328{ 302{
329 if (num > cursor) 303 if (num > cursor)
330 num = cursor; 304 num = cursor;
331 cursor -= num; /* new cursor (in command, not terminal) */ 305 cursor -= num; /* new cursor (in command, not terminal) */
332 306
333 if (cmdedit_x >= num) { /* no to up line */ 307 if (cmdedit_x >= num) { /* no to up line */
334 cmdedit_x -= num; 308 cmdedit_x -= num;
@@ -369,147 +343,116 @@ static void parse_prompt(const char *prmt_ptr)
369 put_prompt(); 343 put_prompt();
370} 344}
371#else 345#else
372static void add_to_prompt(char **prmt_mem_ptr, int *alm,
373 int *prmt_len, const char *addb)
374{
375 *prmt_len += strlen(addb);
376 if (*alm < (*prmt_len) + 1) {
377 *alm = (*prmt_len) + 1;
378 *prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm);
379 }
380 strcat(*prmt_mem_ptr, addb);
381}
382
383static void parse_prompt(const char *prmt_ptr) 346static void parse_prompt(const char *prmt_ptr)
384{ 347{
385 int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */
386 int prmt_len = 0; 348 int prmt_len = 0;
387 int sub_len = 0; 349 int sub_len = 0;
388 int flg_not_length = '['; 350 char flg_not_length = '[';
389 char *prmt_mem_ptr = xstrdup(prmt_ptr); 351 char *prmt_mem_ptr = xcalloc(1, 1);
390 char pwd_buf[PATH_MAX + 1]; 352 char *pwd_buf = xgetcwd(0);
391 char buf[16]; 353 char buf2[PATH_MAX + 1];
392 int c; 354 char buf[2];
393 355 char c;
394 pwd_buf[0] = 0; 356 char *pbuf;
395 *prmt_mem_ptr = 0;
396 357
397 while (*prmt_ptr) { 358 while (*prmt_ptr) {
359 pbuf = buf;
360 pbuf[1] = 0;
398 c = *prmt_ptr++; 361 c = *prmt_ptr++;
399 if (c == '\\') { 362 if (c == '\\') {
400 c = *prmt_ptr; 363 const char *cp = prmt_ptr;
401 if (c == 0) 364 int l;
365
366 c = process_escape_sequence(&prmt_ptr);
367 if(prmt_ptr==cp) {
368 if (*cp == 0)
402 break; 369 break;
403 prmt_ptr++; 370 c = *prmt_ptr++;
404 switch (c) { 371 switch (c) {
405#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR 372#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
406 case 'u': 373 case 'u':
407 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf); 374 pbuf = user_buf;
408 continue; 375 break;
409#endif 376#endif
410 case 'h': 377 case 'h':
411 if (hostname_buf[0] == 0) { 378 pbuf = hostname_buf;
412 hostname_buf = xcalloc(256, 1); 379 if (*pbuf == 0) {
413 if (gethostname(hostname_buf, 255) < 0) { 380 pbuf = xcalloc(256, 1);
414 strcpy(hostname_buf, "?"); 381 if (gethostname(pbuf, 255) < 0) {
382 strcpy(pbuf, "?");
415 } else { 383 } else {
416 char *s = strchr(hostname_buf, '.'); 384 char *s = strchr(pbuf, '.');
417 385
418 if (s) 386 if (s)
419 *s = 0; 387 *s = 0;
420 } 388 }
389 hostname_buf = pbuf;
421 } 390 }
422 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, 391 break;
423 hostname_buf); 392 case '$':
424 continue;
425 case '$':
426 c = my_euid == 0 ? '#' : '$'; 393 c = my_euid == 0 ? '#' : '$';
427 break; 394 break;
428#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR 395#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
429 case 'w': 396 case 'w':
430 if (pwd_buf[0] == 0) { 397 pbuf = pwd_buf;
431 int l; 398 l = strlen(home_pwd_buf);
432 399 if (home_pwd_buf[0] != 0 &&
433 getcwd(pwd_buf, PATH_MAX); 400 strncmp(home_pwd_buf, pbuf, l) == 0 &&
434 l = strlen(home_pwd_buf); 401 (pbuf[l]=='/' || pbuf[l]=='\0') &&
435 if (home_pwd_buf[0] != 0 && 402 strlen(pwd_buf+l)<PATH_MAX) {
436 strncmp(home_pwd_buf, pwd_buf, l) == 0) { 403 pbuf = buf2;
437 strcpy(pwd_buf + 1, pwd_buf + l); 404 *pbuf = '~';
438 pwd_buf[0] = '~'; 405 strcpy(pbuf+1, pwd_buf+l);
439 } 406 }
440 } 407 break;
441 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf);
442 continue;
443#endif 408#endif
444 case 'W': 409 case 'W':
445 if (pwd_buf[0] == 0) { 410 pbuf = pwd_buf;
446 char *z; 411 cp = strrchr(pbuf,'/');
447 412 if ( (cp != NULL) && (cp != pbuf) )
448 getcwd(pwd_buf, PATH_MAX); 413 pbuf += (cp-pbuf)+1;
449 z = strrchr(pwd_buf,'/'); 414 break;
450 if ( (z != NULL) && (z != pwd_buf) ) { 415 case '!':
451 z++; 416 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
452 strcpy(pwd_buf,z); 417 break;
453 } 418 case 'e': case 'E': /* \e \E = \033 */
454 }
455 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf);
456 continue;
457 case '!':
458 snprintf(buf, sizeof(buf), "%d", num_ok_lines);
459 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
460 continue;
461 case 'e':
462 case 'E': /* \e \E = \033 */
463 c = '\033'; 419 c = '\033';
464 break; 420 break;
465 case 'x': 421 case 'x': case 'X':
466 case 'X':
467 case '0':
468 case '1':
469 case '2':
470 case '3':
471 case '4':
472 case '5':
473 case '6':
474 case '7':{
475 int l;
476 int ho = 0;
477 char *eho;
478
479 if (c == 'X')
480 c = 'x';
481
482 for (l = 0; l < 3;) { 422 for (l = 0; l < 3;) {
483 423 int h;
484 buf[l++] = *prmt_ptr; 424 buf2[l++] = *prmt_ptr;
485 buf[l] = 0; 425 buf2[l] = 0;
486 ho = strtol(buf, &eho, c == 'x' ? 16 : 8); 426 h = strtol(buf2, &pbuf, 16);
487 if (ho > UCHAR_MAX || (eho - buf) < l) { 427 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
488 l--; 428 l--;
489 break; 429 break;
490 } 430 }
491 prmt_ptr++; 431 prmt_ptr++;
492 } 432 }
493 buf[l] = 0; 433 buf2[l] = 0;
494 ho = strtol(buf, 0, c == 'x' ? 16 : 8); 434 c = (char)strtol(buf2, 0, 16);
495 c = ho == 0 ? '?' : (char) ho; 435 if(c==0)
436 c = '?';
437 pbuf = buf;
496 break; 438 break;
497 } 439 case '[': case ']':
498 case '[':
499 case ']':
500 if (c == flg_not_length) { 440 if (c == flg_not_length) {
501 flg_not_length = flg_not_length == '[' ? ']' : '['; 441 flg_not_length = flg_not_length == '[' ? ']' : '[';
502 continue; 442 continue;
503 } 443 }
504 break; 444 break;
505 } 445 }
446 }
506 } 447 }
507 buf[0] = c; 448 if(pbuf == buf)
508 buf[1] = 0; 449 *pbuf = c;
509 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf); 450 prmt_len += strlen(pbuf);
451 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
510 if (flg_not_length == ']') 452 if (flg_not_length == ']')
511 sub_len++; 453 sub_len++;
512 } 454 }
455 free(pwd_buf);
513 cmdedit_prompt = prmt_mem_ptr; 456 cmdedit_prompt = prmt_mem_ptr;
514 cmdedit_prmt_len = prmt_len - sub_len; 457 cmdedit_prmt_len = prmt_len - sub_len;
515 put_prompt(); 458 put_prompt();
@@ -789,7 +732,7 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
789 char **paths = path1; 732 char **paths = path1;
790 int npaths; 733 int npaths;
791 int i; 734 int i;
792 char found[BUFSIZ + 4 + PATH_MAX]; 735 char *found;
793 char *pfind = strrchr(command, '/'); 736 char *pfind = strrchr(command, '/');
794 737
795 path1[0] = "."; 738 path1[0] = ".";
@@ -822,7 +765,6 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
822 continue; 765 continue;
823 766
824 while ((next = readdir(dir)) != NULL) { 767 while ((next = readdir(dir)) != NULL) {
825 const char *str_merge = "%s/%s";
826 char *str_found = next->d_name; 768 char *str_found = next->d_name;
827 769
828 /* matched ? */ 770 /* matched ? */
@@ -835,25 +777,23 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
835 else 777 else
836 continue; 778 continue;
837 } 779 }
838 if (paths[i][strlen(paths[i]) - 1] == '/') 780 found = concat_path_file(paths[i], str_found);
839 str_merge = "%s%s";
840 sprintf(found, str_merge, paths[i], str_found);
841 /* hmm, remover in progress? */ 781 /* hmm, remover in progress? */
842 if (stat(found, &st) < 0) 782 if (stat(found, &st) < 0)
843 continue; 783 goto cont;
844 /* find with dirs ? */ 784 /* find with dirs ? */
845 if (paths[i] != dirbuf) 785 if (paths[i] != dirbuf)
846 strcpy(found, next->d_name); /* only name */ 786 strcpy(found, next->d_name); /* only name */
847 if (S_ISDIR(st.st_mode)) { 787 if (S_ISDIR(st.st_mode)) {
848 /* name is directory */ 788 /* name is directory */
849 /* algorithmic only "/" ? */ 789 str_found = found;
850 if (*str_found) 790 found = concat_path_file(found, "");
851 strcat(found, "/"); 791 free(str_found);
852 str_found = add_quote_for_spec_chars(found); 792 str_found = add_quote_for_spec_chars(found);
853 } else { 793 } else {
854 /* not put found file if search only dirs for cd */ 794 /* not put found file if search only dirs for cd */
855 if (type == FIND_DIR_ONLY) 795 if (type == FIND_DIR_ONLY)
856 continue; 796 goto cont;
857 str_found = add_quote_for_spec_chars(found); 797 str_found = add_quote_for_spec_chars(found);
858 if (type == FIND_FILE_ONLY || 798 if (type == FIND_FILE_ONLY ||
859 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE)) 799 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE))
@@ -863,6 +803,8 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
863 matches = xrealloc(matches, (nm + 1) * sizeof(char *)); 803 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
864 804
865 matches[nm++] = str_found; 805 matches[nm++] = str_found;
806cont:
807 free(found);
866 } 808 }
867 closedir(dir); 809 closedir(dir);
868 } 810 }
@@ -1440,7 +1382,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
1440 } 1382 }
1441 } else 1383 } else
1442#endif 1384#endif
1443 if (!isprint(c)) /* Skip non-printable characters */ 1385 if (!Isprint(c)) /* Skip non-printable characters */
1444 break; 1386 break;
1445 1387
1446 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */ 1388 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
@@ -1554,6 +1496,9 @@ extern void cmdedit_terminate(void)
1554 1496
1555#ifdef TEST 1497#ifdef TEST
1556 1498
1499const char *applet_name = "debug stuff usage";
1500const char *memory_exhausted = "Memory exhausted";
1501
1557#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT 1502#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1558#include <locale.h> 1503#include <locale.h>
1559#endif 1504#endif
diff --git a/coreutils/du.c b/coreutils/du.c
index 7cb888de8..119895e49 100644
--- a/coreutils/du.c
+++ b/coreutils/du.c
@@ -83,7 +83,8 @@ static long du(char *filename)
83 int len; 83 int len;
84 84
85 if ((lstat(filename, &statbuf)) != 0) { 85 if ((lstat(filename, &statbuf)) != 0) {
86 perror_msg_and_die("%s", filename); 86 perror_msg("%s", filename);
87 return 0;
87 } 88 }
88 89
89 du_depth++; 90 du_depth++;
@@ -110,22 +111,16 @@ static long du(char *filename)
110 filename[--len] = '\0'; 111 filename[--len] = '\0';
111 112
112 while ((entry = readdir(dir))) { 113 while ((entry = readdir(dir))) {
113 char newfile[BUFSIZ + 1]; 114 char *newfile;
114 char *name = entry->d_name; 115 char *name = entry->d_name;
115 116
116 if ((strcmp(name, "..") == 0) 117 if ((strcmp(name, "..") == 0)
117 || (strcmp(name, ".") == 0)) { 118 || (strcmp(name, ".") == 0)) {
118 continue; 119 continue;
119 } 120 }
120 121 newfile = concat_path_file(filename, name);
121 if (len + strlen(name) + 1 > BUFSIZ) {
122 error_msg(name_too_long);
123 du_depth--;
124 return 0;
125 }
126 sprintf(newfile, "%s/%s", filename, name);
127
128 sum += du(newfile); 122 sum += du(newfile);
123 free(newfile);
129 } 124 }
130 closedir(dir); 125 closedir(dir);
131 print(sum, filename); 126 print(sum, filename);
@@ -197,7 +192,7 @@ int du_main(int argc, char **argv)
197 return status; 192 return status;
198} 193}
199 194
200/* $Id: du.c,v 1.43 2001/03/09 14:36:42 andersen Exp $ */ 195/* $Id: du.c,v 1.44 2001/04/09 22:48:11 andersen Exp $ */
201/* 196/*
202Local Variables: 197Local Variables:
203c-file-style: "linux" 198c-file-style: "linux"
diff --git a/coreutils/echo.c b/coreutils/echo.c
index 1ca373467..31c031528 100644
--- a/coreutils/echo.c
+++ b/coreutils/echo.c
@@ -81,7 +81,7 @@ echo_main(int argc, char** argv)
81 81
82just_echo: 82just_echo:
83 while (argc > 0) { 83 while (argc > 0) {
84 char *arg = argv[0]; 84 const char *arg = argv[0];
85 register int c; 85 register int c;
86 86
87 while ((c = *arg++)) { 87 while ((c = *arg++)) {
diff --git a/coreutils/pwd.c b/coreutils/pwd.c
index 2f36b1f05..f6a00bf1e 100644
--- a/coreutils/pwd.c
+++ b/coreutils/pwd.c
@@ -32,11 +32,13 @@
32 32
33extern int pwd_main(int argc, char **argv) 33extern int pwd_main(int argc, char **argv)
34{ 34{
35 char buf[BUFSIZ + 1]; 35 static char *buf;
36 36
37 if (getcwd(buf, sizeof(buf)) == NULL) 37 buf = xgetcwd(buf);
38 perror_msg_and_die("getcwd"); 38
39 39 if (buf != NULL) {
40 puts(buf); 40 puts(buf);
41 return EXIT_SUCCESS; 41 return EXIT_SUCCESS;
42 }
43 return EXIT_FAILURE;
42} 44}
diff --git a/coreutils/tr.c b/coreutils/tr.c
index 32a4f2917..ce15cfdf8 100644
--- a/coreutils/tr.c
+++ b/coreutils/tr.c
@@ -93,7 +93,7 @@ static void map(register unsigned char *string1, unsigned int string1_len,
93 } 93 }
94} 94}
95 95
96static unsigned int expand(char *arg, register unsigned char *buffer) 96static unsigned int expand(const char *arg, register unsigned char *buffer)
97{ 97{
98 unsigned char *buffer_start = buffer; 98 unsigned char *buffer_start = buffer;
99 int i, ac; 99 int i, ac;
diff --git a/cp_mv.c b/cp_mv.c
index fb48d3c5c..f4b5cd0d6 100644
--- a/cp_mv.c
+++ b/cp_mv.c
@@ -256,18 +256,16 @@ extern int cp_mv_main(int argc, char **argv)
256 int state = 0; 256 int state = 0;
257 char *pushd, *d, *p; 257 char *pushd, *d, *p;
258 258
259 if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) { 259 if ((pushd = xgetcwd(0)) == NULL)
260 perror_msg("getcwd()");
261 continue; 260 continue;
262 } 261
263 if (chdir(baseDestName) < 0) { 262 if (chdir(baseDestName) < 0) {
264 perror_msg("chdir(%s)", baseSrcName); 263 perror_msg("chdir(%s)", baseSrcName);
265 continue; 264 continue;
266 } 265 }
267 if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) { 266 if ((d = xgetcwd(0)) == NULL)
268 perror_msg("getcwd()");
269 continue; 267 continue;
270 } 268
271 while (!state && *d != '\0') { 269 while (!state && *d != '\0') {
272 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */ 270 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
273 perror_msg("stat(%s)", d); 271 perror_msg("stat(%s)", d);
diff --git a/docs/busybox.pod b/docs/busybox.pod
index 8283e4717..56a22b891 100644
--- a/docs/busybox.pod
+++ b/docs/busybox.pod
@@ -2505,6 +2505,13 @@ Glenn McGrath <bug1@netconnect.com.au>
2505 2505
2506=for html <br> 2506=for html <br>
2507 2507
2508Vladimir Oleynik <dzo@simtreas.ru>
2509
2510 cmdedit, stty-port, locale, various fixes
2511 and irreconcilable critic of everything not perfect.
2512
2513=for html <br>
2514
2508Bruce Perens <bruce@pixar.com> 2515Bruce Perens <bruce@pixar.com>
2509 2516
2510 Original author of BusyBox. His code is still in many apps. 2517 Original author of BusyBox. His code is still in many apps.
@@ -2553,4 +2560,4 @@ Enrique Zanardi <ezanardi@ull.es>
2553 2560
2554=cut 2561=cut
2555 2562
2556# $Id: busybox.pod,v 1.96 2001/04/05 21:45:54 andersen Exp $ 2563# $Id: busybox.pod,v 1.97 2001/04/09 22:48:11 andersen Exp $
diff --git a/docs/busybox_footer.pod b/docs/busybox_footer.pod
index 961f027cd..759f127e5 100644
--- a/docs/busybox_footer.pod
+++ b/docs/busybox_footer.pod
@@ -96,6 +96,13 @@ Glenn McGrath <bug1@netconnect.com.au>
96 96
97=for html <br> 97=for html <br>
98 98
99Vladimir Oleynik <dzo@simtreas.ru>
100
101 cmdedit, stty-port, locale, various fixes
102 and irreconcilable critic of everything not perfect.
103
104=for html <br>
105
99Bruce Perens <bruce@pixar.com> 106Bruce Perens <bruce@pixar.com>
100 107
101 Original author of BusyBox. His code is still in many apps. 108 Original author of BusyBox. His code is still in many apps.
@@ -144,4 +151,4 @@ Enrique Zanardi <ezanardi@ull.es>
144 151
145=cut 152=cut
146 153
147# $Id: busybox_footer.pod,v 1.1 2001/04/05 19:41:23 beppu Exp $ 154# $Id: busybox_footer.pod,v 1.2 2001/04/09 22:48:11 andersen Exp $
diff --git a/dpkg.c b/dpkg.c
index 57b31697b..5687e00be 100644
--- a/dpkg.c
+++ b/dpkg.c
@@ -20,7 +20,7 @@
20#define DODEPENDS 1 20#define DODEPENDS 1
21 21
22/* Should we do debugging? */ 22/* Should we do debugging? */
23#define DODEBUG 1 23//#define DODEBUG 1
24 24
25#ifdef DODEBUG 25#ifdef DODEBUG
26#define SYSTEM(x) do_system(x) 26#define SYSTEM(x) do_system(x)
@@ -274,7 +274,7 @@ static package_t *depends_resolve(package_t *pkgs, void *status)
274 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && 274 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides &&
275 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) { 275 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
276 if (chk->requiredcount >= DEPENDSMAX) { 276 if (chk->requiredcount >= DEPENDSMAX) {
277 fprintf(stderr, "Too many dependencies for %s\n", chk->package); 277 error_msg("Too many dependencies for %s", chk->package);
278 return 0; 278 return 0;
279 } 279 }
280 if (chk != pkg) { 280 if (chk != pkg) {
@@ -284,7 +284,7 @@ static package_t *depends_resolve(package_t *pkgs, void *status)
284 } 284 }
285 } 285 }
286 if (chk == 0) { 286 if (chk == 0) {
287 fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]); 287 error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]);
288 return 0; 288 return 0;
289 } 289 }
290 } 290 }
@@ -382,7 +382,7 @@ static void *status_read(void)
382 printf("(Reading database...)\n"); 382 printf("(Reading database...)\n");
383 } 383 }
384 384
385 if ((f = fopen(statusfile, "r")) == NULL) { 385 if ((f = wfopen(statusfile, "r")) == NULL) {
386 return(NULL); 386 return(NULL);
387 } 387 }
388 388
@@ -483,8 +483,7 @@ static int status_merge(void *status, package_t *pkgs)
483 status_words_flag[statpkg->status_flag - 1], 483 status_words_flag[statpkg->status_flag - 1],
484 status_words_status[statpkg->status_status - 1]); 484 status_words_status[statpkg->status_status - 1]);
485 } 485 }
486 fputs(line, fout); 486 fprintf(fout, "%s\n", line);
487 fputc('\n', fout);
488 } 487 }
489 fclose(fin); 488 fclose(fin);
490 } 489 }
@@ -551,7 +550,7 @@ static int dpkg_doconfigure(package_t *pkg)
551 if (is_file(postinst)) { 550 if (is_file(postinst)) {
552 snprintf(buf, sizeof(buf), "%s configure", postinst); 551 snprintf(buf, sizeof(buf), "%s configure", postinst);
553 if ((r = do_system(buf)) != 0) { 552 if ((r = do_system(buf)) != 0) {
554 fprintf(stderr, "postinst exited with status %d\n", r); 553 error_msg("postinst exited with status %d\n", r);
555 pkg->status_status = status_status_halfconfigured; 554 pkg->status_status = status_status_halfconfigured;
556 return 1; 555 return 1;
557 } 556 }
@@ -565,7 +564,7 @@ static int dpkg_dounpack(package_t *pkg)
565{ 564{
566 int r = 0, i; 565 int r = 0, i;
567 int status = TRUE; 566 int status = TRUE;
568 char *cwd; 567 char *cwd = xgetcwd(0);
569 char *src_file = NULL; 568 char *src_file = NULL;
570 char *dst_file = NULL; 569 char *dst_file = NULL;
571// char *lst_file = NULL; 570// char *lst_file = NULL;
@@ -574,7 +573,8 @@ static int dpkg_dounpack(package_t *pkg)
574 573
575 DPRINTF("Unpacking %s\n", pkg->package); 574 DPRINTF("Unpacking %s\n", pkg->package);
576 575
577 cwd = getcwd(0, 0); 576 if(cwd==NULL)
577 exit(EXIT_FAILURE);
578 chdir("/"); 578 chdir("/");
579 deb_extract(dpkg_deb_extract, "/", pkg->file); 579 deb_extract(dpkg_deb_extract, "/", pkg->file);
580 580
@@ -714,7 +714,7 @@ static int dpkg_configure(package_t *pkgs, void *status)
714 found = tfind(pkg, &status, package_compare); 714 found = tfind(pkg, &status, package_compare);
715 715
716 if (found == 0) { 716 if (found == 0) {
717 fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package); 717 error_msg("Trying to configure %s, but it is not installed", pkg->package);
718 r = 1; 718 r = 1;
719 } 719 }
720 /* configure the package listed in the status file; 720 /* configure the package listed in the status file;
diff --git a/du.c b/du.c
index 7cb888de8..119895e49 100644
--- a/du.c
+++ b/du.c
@@ -83,7 +83,8 @@ static long du(char *filename)
83 int len; 83 int len;
84 84
85 if ((lstat(filename, &statbuf)) != 0) { 85 if ((lstat(filename, &statbuf)) != 0) {
86 perror_msg_and_die("%s", filename); 86 perror_msg("%s", filename);
87 return 0;
87 } 88 }
88 89
89 du_depth++; 90 du_depth++;
@@ -110,22 +111,16 @@ static long du(char *filename)
110 filename[--len] = '\0'; 111 filename[--len] = '\0';
111 112
112 while ((entry = readdir(dir))) { 113 while ((entry = readdir(dir))) {
113 char newfile[BUFSIZ + 1]; 114 char *newfile;
114 char *name = entry->d_name; 115 char *name = entry->d_name;
115 116
116 if ((strcmp(name, "..") == 0) 117 if ((strcmp(name, "..") == 0)
117 || (strcmp(name, ".") == 0)) { 118 || (strcmp(name, ".") == 0)) {
118 continue; 119 continue;
119 } 120 }
120 121 newfile = concat_path_file(filename, name);
121 if (len + strlen(name) + 1 > BUFSIZ) {
122 error_msg(name_too_long);
123 du_depth--;
124 return 0;
125 }
126 sprintf(newfile, "%s/%s", filename, name);
127
128 sum += du(newfile); 122 sum += du(newfile);
123 free(newfile);
129 } 124 }
130 closedir(dir); 125 closedir(dir);
131 print(sum, filename); 126 print(sum, filename);
@@ -197,7 +192,7 @@ int du_main(int argc, char **argv)
197 return status; 192 return status;
198} 193}
199 194
200/* $Id: du.c,v 1.43 2001/03/09 14:36:42 andersen Exp $ */ 195/* $Id: du.c,v 1.44 2001/04/09 22:48:11 andersen Exp $ */
201/* 196/*
202Local Variables: 197Local Variables:
203c-file-style: "linux" 198c-file-style: "linux"
diff --git a/echo.c b/echo.c
index 1ca373467..31c031528 100644
--- a/echo.c
+++ b/echo.c
@@ -81,7 +81,7 @@ echo_main(int argc, char** argv)
81 81
82just_echo: 82just_echo:
83 while (argc > 0) { 83 while (argc > 0) {
84 char *arg = argv[0]; 84 const char *arg = argv[0];
85 register int c; 85 register int c;
86 86
87 while ((c = *arg++)) { 87 while ((c = *arg++)) {
diff --git a/include/libbb.h b/include/libbb.h
index 05f61f25b..0001cac6f 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -131,7 +131,7 @@ extern int find_real_root_device_name(char* name);
131extern char *get_line_from_file(FILE *file); 131extern char *get_line_from_file(FILE *file);
132extern void print_file(FILE *file); 132extern void print_file(FILE *file);
133extern int print_file_by_name(char *filename); 133extern int print_file_by_name(char *filename);
134extern char process_escape_sequence(char **ptr); 134extern char process_escape_sequence(const char **ptr);
135extern char *get_last_path_component(char *path); 135extern char *get_last_path_component(char *path);
136extern FILE *wfopen(const char *path, const char *mode); 136extern FILE *wfopen(const char *path, const char *mode);
137extern FILE *xfopen(const char *path, const char *mode); 137extern FILE *xfopen(const char *path, const char *mode);
@@ -150,7 +150,7 @@ extern char *xstrndup (const char *s, int n);
150extern char * safe_strncpy(char *dst, const char *src, size_t size); 150extern char * safe_strncpy(char *dst, const char *src, size_t size);
151 151
152struct suffix_mult { 152struct suffix_mult {
153 char *suffix; 153 const char *suffix;
154 int mult; 154 int mult;
155}; 155};
156 156
@@ -213,4 +213,7 @@ enum {
213int ask_confirmation(void); 213int ask_confirmation(void);
214int klogctl(int type, char * b, int len); 214int klogctl(int type, char * b, int len);
215 215
216char *xgetcwd(char *cwd);
217char *concat_path_file(const char *path, const char *filename);
218
216#endif /* __LIBBB_H__ */ 219#endif /* __LIBBB_H__ */
diff --git a/lash.c b/lash.c
index 89325b63d..ee45b1a0d 100644
--- a/lash.c
+++ b/lash.c
@@ -64,7 +64,10 @@
64#include <sys/wait.h> 64#include <sys/wait.h>
65#include <unistd.h> 65#include <unistd.h>
66#include <getopt.h> 66#include <getopt.h>
67
68#ifdef BB_LOCALE_SUPPORT
67#include <locale.h> 69#include <locale.h>
70#endif
68 71
69//#define BB_FEATURE_SH_WORDEXP 72//#define BB_FEATURE_SH_WORDEXP
70 73
@@ -80,7 +83,6 @@
80#include "cmdedit.h" 83#include "cmdedit.h"
81 84
82 85
83static const int MAX_LINE = 256; /* size of input buffer for cwd data */
84static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ 86static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
85#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" 87#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
86 88
@@ -230,7 +232,7 @@ static char syntax_err[]="syntax error near unexpected token";
230#endif 232#endif
231 233
232static char *PS1; 234static char *PS1;
233static char *PS2; 235static char *PS2 = "> ";
234 236
235 237
236#ifdef DEBUG_SHELL 238#ifdef DEBUG_SHELL
@@ -294,7 +296,7 @@ static int builtin_cd(struct child_prog *child)
294 printf("cd: %s: %m\n", newdir); 296 printf("cd: %s: %m\n", newdir);
295 return EXIT_FAILURE; 297 return EXIT_FAILURE;
296 } 298 }
297 getcwd(cwd, sizeof(char)*MAX_LINE); 299 cwd = xgetcwd(cwd);
298 300
299 return EXIT_SUCCESS; 301 return EXIT_SUCCESS;
300} 302}
@@ -410,7 +412,6 @@ static int builtin_jobs(struct child_prog *child)
410/* built-in 'pwd' handler */ 412/* built-in 'pwd' handler */
411static int builtin_pwd(struct child_prog *dummy) 413static int builtin_pwd(struct child_prog *dummy)
412{ 414{
413 getcwd(cwd, MAX_LINE);
414 printf( "%s\n", cwd); 415 printf( "%s\n", cwd);
415 return EXIT_SUCCESS; 416 return EXIT_SUCCESS;
416} 417}
@@ -434,13 +435,14 @@ static int builtin_export(struct child_prog *child)
434#ifndef BB_FEATURE_SH_SIMPLE_PROMPT 435#ifndef BB_FEATURE_SH_SIMPLE_PROMPT
435 if (strncmp(v, "PS1=", 4)==0) 436 if (strncmp(v, "PS1=", 4)==0)
436 PS1 = getenv("PS1"); 437 PS1 = getenv("PS1");
437 else if (strncmp(v, "PS2=", 4)==0)
438 PS2 = getenv("PS2");
439#endif 438#endif
439
440#ifdef BB_LOCALE_SUPPORT
440 if(strncmp(v, "LC_ALL=", 7)==0) 441 if(strncmp(v, "LC_ALL=", 7)==0)
441 setlocale(LC_ALL, getenv("LC_ALL")); 442 setlocale(LC_ALL, getenv("LC_ALL"));
442 if(strncmp(v, "LC_CTYPE=", 9)==0) 443 if(strncmp(v, "LC_CTYPE=", 9)==0)
443 setlocale(LC_CTYPE, getenv("LC_CTYPE")); 444 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
445#endif
444 446
445 return (res); 447 return (res);
446} 448}
@@ -623,10 +625,7 @@ static int builtin_unset(struct child_prog *child)
623 */ 625 */
624static int run_command_predicate(char *cmd) 626static int run_command_predicate(char *cmd)
625{ 627{
626 int n=strlen(cmd); 628 local_pending_command = xstrdup(cmd);
627 local_pending_command = xmalloc(n+1);
628 strncpy(local_pending_command, cmd, n);
629 local_pending_command[n]='\0';
630 return( busy_loop(NULL)); 629 return( busy_loop(NULL));
631} 630}
632#endif 631#endif
@@ -808,15 +807,10 @@ static inline void cmdedit_set_initial_prompt(void)
808{ 807{
809#ifdef BB_FEATURE_SH_SIMPLE_PROMPT 808#ifdef BB_FEATURE_SH_SIMPLE_PROMPT
810 PS1 = NULL; 809 PS1 = NULL;
811 PS2 = "> ";
812#else 810#else
813 PS1 = getenv("PS1"); 811 PS1 = getenv("PS1");
814 if(PS1==0) { 812 if(PS1==0)
815 PS1 = "\\w \\$ "; 813 PS1 = "\\w \\$ ";
816 }
817 PS2 = getenv("PS2");
818 if(PS2==0)
819 PS2 = "> ";
820#endif 814#endif
821} 815}
822 816
@@ -954,7 +948,7 @@ static int expand_arguments(char *command)
954 /* Fix up escape sequences to be the Real Thing(tm) */ 948 /* Fix up escape sequences to be the Real Thing(tm) */
955 while( command && command[ix]) { 949 while( command && command[ix]) {
956 if (command[ix] == '\\') { 950 if (command[ix] == '\\') {
957 char *tmp = command+ix+1; 951 const char *tmp = command+ix+1;
958 command[ix] = process_escape_sequence( &tmp ); 952 command[ix] = process_escape_sequence( &tmp );
959 memmove(command+ix + 1, tmp, strlen(tmp)+1); 953 memmove(command+ix + 1, tmp, strlen(tmp)+1);
960 } 954 }
@@ -1829,8 +1823,10 @@ static int busy_loop(FILE * input)
1829#ifdef BB_FEATURE_CLEAN_UP 1823#ifdef BB_FEATURE_CLEAN_UP
1830void free_memory(void) 1824void free_memory(void)
1831{ 1825{
1832 if (cwd) 1826 if (cwd) {
1833 free(cwd); 1827 free(cwd);
1828 cwd = NULL;
1829 }
1834 if (local_pending_command) 1830 if (local_pending_command)
1835 free(local_pending_command); 1831 free(local_pending_command);
1836 1832
@@ -1850,7 +1846,6 @@ int shell_main(int argc_l, char **argv_l)
1850 1846
1851 /* These variables need re-initializing when recursing */ 1847 /* These variables need re-initializing when recursing */
1852 shell_context = 0; 1848 shell_context = 0;
1853 cwd=NULL;
1854 local_pending_command = NULL; 1849 local_pending_command = NULL;
1855 close_me_head = NULL; 1850 close_me_head = NULL;
1856 job_list.head = NULL; 1851 job_list.head = NULL;
@@ -1921,8 +1916,7 @@ int shell_main(int argc_l, char **argv_l)
1921 } 1916 }
1922 1917
1923 /* initialize the cwd -- this is never freed...*/ 1918 /* initialize the cwd -- this is never freed...*/
1924 cwd=(char*)xmalloc(sizeof(char)*MAX_LINE+1); 1919 cwd = xgetcwd(0);
1925 getcwd(cwd, sizeof(char)*MAX_LINE);
1926 1920
1927#ifdef BB_FEATURE_CLEAN_UP 1921#ifdef BB_FEATURE_CLEAN_UP
1928 atexit(free_memory); 1922 atexit(free_memory);
@@ -1932,7 +1926,6 @@ int shell_main(int argc_l, char **argv_l)
1932 cmdedit_set_initial_prompt(); 1926 cmdedit_set_initial_prompt();
1933#else 1927#else
1934 PS1 = NULL; 1928 PS1 = NULL;
1935 PS2 = "> ";
1936#endif 1929#endif
1937 1930
1938 return (busy_loop(input)); 1931 return (busy_loop(input));
diff --git a/libbb/concat_path_file.c b/libbb/concat_path_file.c
new file mode 100644
index 000000000..d53dc0e2e
--- /dev/null
+++ b/libbb/concat_path_file.c
@@ -0,0 +1,24 @@
1/*
2 * busybox library eXtendet funcion
3 *
4 * concatenate path and file name to new allocation buffer,
5 * not addition '/' if path name already have '/'
6 *
7*/
8
9#include "libbb.h"
10
11extern char *concat_path_file(const char *path, const char *filename)
12{
13 char *outbuf;
14 int l;
15 int flg_slash = 1;
16
17 l = strlen(path);
18 if(l>0 && path[l-1] == '/')
19 flg_slash--;
20 l += strlen(filename);
21 outbuf = xmalloc(l+1+flg_slash);
22 sprintf(outbuf, (flg_slash ? "%s/%s" : "%s%s"), path, filename);
23 return outbuf;
24}
diff --git a/libbb/libbb.h b/libbb/libbb.h
index 05f61f25b..0001cac6f 100644
--- a/libbb/libbb.h
+++ b/libbb/libbb.h
@@ -131,7 +131,7 @@ extern int find_real_root_device_name(char* name);
131extern char *get_line_from_file(FILE *file); 131extern char *get_line_from_file(FILE *file);
132extern void print_file(FILE *file); 132extern void print_file(FILE *file);
133extern int print_file_by_name(char *filename); 133extern int print_file_by_name(char *filename);
134extern char process_escape_sequence(char **ptr); 134extern char process_escape_sequence(const char **ptr);
135extern char *get_last_path_component(char *path); 135extern char *get_last_path_component(char *path);
136extern FILE *wfopen(const char *path, const char *mode); 136extern FILE *wfopen(const char *path, const char *mode);
137extern FILE *xfopen(const char *path, const char *mode); 137extern FILE *xfopen(const char *path, const char *mode);
@@ -150,7 +150,7 @@ extern char *xstrndup (const char *s, int n);
150extern char * safe_strncpy(char *dst, const char *src, size_t size); 150extern char * safe_strncpy(char *dst, const char *src, size_t size);
151 151
152struct suffix_mult { 152struct suffix_mult {
153 char *suffix; 153 const char *suffix;
154 int mult; 154 int mult;
155}; 155};
156 156
@@ -213,4 +213,7 @@ enum {
213int ask_confirmation(void); 213int ask_confirmation(void);
214int klogctl(int type, char * b, int len); 214int klogctl(int type, char * b, int len);
215 215
216char *xgetcwd(char *cwd);
217char *concat_path_file(const char *path, const char *filename);
218
216#endif /* __LIBBB_H__ */ 219#endif /* __LIBBB_H__ */
diff --git a/libbb/print_file.c b/libbb/print_file.c
index 52a39774f..b47723454 100644
--- a/libbb/print_file.c
+++ b/libbb/print_file.c
@@ -2,9 +2,7 @@
2/* 2/*
3 * Utility routines. 3 * Utility routines.
4 * 4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what 5 * Copyright (C) 1999-2001 Erik Andersen <andersee@debian.org>
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 * 6 *
9 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -19,13 +17,10 @@
19 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */ 20 */
27 21
28#include <stdio.h> 22#include <stdio.h>
23#include <sys/stat.h>
29#include "libbb.h" 24#include "libbb.h"
30 25
31 26
@@ -41,11 +36,21 @@ extern void print_file(FILE *file)
41 36
42extern int print_file_by_name(char *filename) 37extern int print_file_by_name(char *filename)
43{ 38{
44 FILE *file; 39 struct stat statBuf;
45 if ((file = wfopen(filename, "r")) == NULL) 40 int status = TRUE;
46 return FALSE; 41
47 print_file(file); 42 if(is_directory(filename, TRUE, &statBuf)==TRUE) {
48 return TRUE; 43 error_msg("%s: Is directory", filename);
44 status = FALSE;
45 } else {
46 FILE *f = wfopen(filename, "r");
47 if(f!=NULL)
48 print_file(f);
49 else
50 status = FALSE;
51 }
52
53 return status;
49} 54}
50 55
51 56
diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c
index ad2be94ee..67b0490ce 100644
--- a/libbb/process_escape_sequence.c
+++ b/libbb/process_escape_sequence.c
@@ -2,9 +2,8 @@
2/* 2/*
3 * Utility routines. 3 * Utility routines.
4 * 4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what 5 * Copyright (C) Manuel Nova III <mnovoa3@bellsouth.net>
6 * isn't something I'm going to worry about... If you wrote something 6 * and Vladimir Oleynik <vodz@usa.net>
7 * here, please feel free to acknowledge your work.
8 * 7 *
9 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
@@ -20,9 +19,7 @@
20 * along with this program; if not, write to the Free Software 19 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * 21 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell 22 *
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */ 23 */
27 24
28#include <stdio.h> 25#include <stdio.h>
@@ -31,45 +28,45 @@
31 28
32 29
33 30
34char process_escape_sequence(char **ptr) 31char process_escape_sequence(const char **ptr)
35{ 32{
36 static const char charmap[] = { 33 static const char charmap[] = {
37 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0, 34 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
38 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' }; 35 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
39
40 const char *p;
41 char *q;
42 int num_digits;
43 unsigned int n;
44 36
45 n = 0; 37 const char *p;
46 q = *ptr; 38 const char *q;
39 int num_digits;
40 unsigned int n;
41
42 n = 0;
43 q = *ptr;
47 44
48 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) { 45 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) {
49 if ((*q < '0') || (*q > '7')) { /* not a digit? */ 46 if ((*q < '0') || (*q > '7')) { /* not a digit? */
50 break; 47 break;
51 } 48 }
52 n = n * 8 + (*q++ - '0'); 49 n = n * 8 + (*q++ - '0');
53 } 50 }
54 51
55 if (num_digits == 0) { /* mnemonic escape sequence? */ 52 if (num_digits == 0) { /* mnemonic escape sequence? */
56 for (p=charmap ; *p ; p++) { 53 for (p=charmap ; *p ; p++) {
57 if (*p == *q) { 54 if (*p == *q) {
58 q++; 55 q++;
59 break; 56 break;
60 } 57 }
61 } 58 }
62 n = *(p+(sizeof(charmap)/2)); 59 n = *(p+(sizeof(charmap)/2));
63 } 60 }
64 61
65 /* doesn't hurt to fall through to here from mnemonic case */ 62 /* doesn't hurt to fall through to here from mnemonic case */
66 if (n > UCHAR_MAX) { /* is octal code too big for a char? */ 63 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
67 n /= 8; /* adjust value and */ 64 n /= 8; /* adjust value and */
68 --q; /* back up one char */ 65 --q; /* back up one char */
69 } 66 }
70 67
71 *ptr = q; 68 *ptr = q;
72 return (char) n; 69 return (char) n;
73} 70}
74 71
75 72
diff --git a/libbb/xgetcwd.c b/libbb/xgetcwd.c
new file mode 100644
index 000000000..274668166
--- /dev/null
+++ b/libbb/xgetcwd.c
@@ -0,0 +1,52 @@
1/*
2 * xgetcwd.c -- return current directory with unlimited length
3 * Copyright (C) 1992, 1996 Free Software Foundation, Inc.
4 * Written by David MacKenzie <djm@gnu.ai.mit.edu>.
5 *
6 * Special function for busybox written by Vladimir Oleynik <vodz@usa.net>
7*/
8
9#include <stdlib.h>
10#include <errno.h>
11#include <unistd.h>
12#include <limits.h>
13#include "libbb.h"
14
15/* Amount to increase buffer size by in each try. */
16#define PATH_INCR 32
17
18/* Return the current directory, newly allocated, arbitrarily long.
19 Return NULL and set errno on error.
20 If argument is not NULL (previous usage allocate memory), call free()
21*/
22
23char *
24xgetcwd (char *cwd)
25{
26 char *ret;
27 unsigned path_max;
28
29 errno = 0;
30 path_max = (unsigned) PATH_MAX;
31 path_max += 2; /* The getcwd docs say to do this. */
32
33 if(cwd==0)
34 cwd = xmalloc (path_max);
35
36 errno = 0;
37 while ((ret = getcwd (cwd, path_max)) == NULL && errno == ERANGE) {
38 path_max += PATH_INCR;
39 cwd = xrealloc (cwd, path_max);
40 errno = 0;
41 }
42
43 if (ret == NULL) {
44 int save_errno = errno;
45 free (cwd);
46 errno = save_errno;
47 perror_msg("getcwd()");
48 return NULL;
49 }
50
51 return cwd;
52}
diff --git a/pwd.c b/pwd.c
index 2f36b1f05..f6a00bf1e 100644
--- a/pwd.c
+++ b/pwd.c
@@ -32,11 +32,13 @@
32 32
33extern int pwd_main(int argc, char **argv) 33extern int pwd_main(int argc, char **argv)
34{ 34{
35 char buf[BUFSIZ + 1]; 35 static char *buf;
36 36
37 if (getcwd(buf, sizeof(buf)) == NULL) 37 buf = xgetcwd(buf);
38 perror_msg_and_die("getcwd"); 38
39 39 if (buf != NULL) {
40 puts(buf); 40 puts(buf);
41 return EXIT_SUCCESS; 41 return EXIT_SUCCESS;
42 }
43 return EXIT_FAILURE;
42} 44}
diff --git a/sh.c b/sh.c
index 89325b63d..ee45b1a0d 100644
--- a/sh.c
+++ b/sh.c
@@ -64,7 +64,10 @@
64#include <sys/wait.h> 64#include <sys/wait.h>
65#include <unistd.h> 65#include <unistd.h>
66#include <getopt.h> 66#include <getopt.h>
67
68#ifdef BB_LOCALE_SUPPORT
67#include <locale.h> 69#include <locale.h>
70#endif
68 71
69//#define BB_FEATURE_SH_WORDEXP 72//#define BB_FEATURE_SH_WORDEXP
70 73
@@ -80,7 +83,6 @@
80#include "cmdedit.h" 83#include "cmdedit.h"
81 84
82 85
83static const int MAX_LINE = 256; /* size of input buffer for cwd data */
84static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ 86static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
85#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" 87#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
86 88
@@ -230,7 +232,7 @@ static char syntax_err[]="syntax error near unexpected token";
230#endif 232#endif
231 233
232static char *PS1; 234static char *PS1;
233static char *PS2; 235static char *PS2 = "> ";
234 236
235 237
236#ifdef DEBUG_SHELL 238#ifdef DEBUG_SHELL
@@ -294,7 +296,7 @@ static int builtin_cd(struct child_prog *child)
294 printf("cd: %s: %m\n", newdir); 296 printf("cd: %s: %m\n", newdir);
295 return EXIT_FAILURE; 297 return EXIT_FAILURE;
296 } 298 }
297 getcwd(cwd, sizeof(char)*MAX_LINE); 299 cwd = xgetcwd(cwd);
298 300
299 return EXIT_SUCCESS; 301 return EXIT_SUCCESS;
300} 302}
@@ -410,7 +412,6 @@ static int builtin_jobs(struct child_prog *child)
410/* built-in 'pwd' handler */ 412/* built-in 'pwd' handler */
411static int builtin_pwd(struct child_prog *dummy) 413static int builtin_pwd(struct child_prog *dummy)
412{ 414{
413 getcwd(cwd, MAX_LINE);
414 printf( "%s\n", cwd); 415 printf( "%s\n", cwd);
415 return EXIT_SUCCESS; 416 return EXIT_SUCCESS;
416} 417}
@@ -434,13 +435,14 @@ static int builtin_export(struct child_prog *child)
434#ifndef BB_FEATURE_SH_SIMPLE_PROMPT 435#ifndef BB_FEATURE_SH_SIMPLE_PROMPT
435 if (strncmp(v, "PS1=", 4)==0) 436 if (strncmp(v, "PS1=", 4)==0)
436 PS1 = getenv("PS1"); 437 PS1 = getenv("PS1");
437 else if (strncmp(v, "PS2=", 4)==0)
438 PS2 = getenv("PS2");
439#endif 438#endif
439
440#ifdef BB_LOCALE_SUPPORT
440 if(strncmp(v, "LC_ALL=", 7)==0) 441 if(strncmp(v, "LC_ALL=", 7)==0)
441 setlocale(LC_ALL, getenv("LC_ALL")); 442 setlocale(LC_ALL, getenv("LC_ALL"));
442 if(strncmp(v, "LC_CTYPE=", 9)==0) 443 if(strncmp(v, "LC_CTYPE=", 9)==0)
443 setlocale(LC_CTYPE, getenv("LC_CTYPE")); 444 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
445#endif
444 446
445 return (res); 447 return (res);
446} 448}
@@ -623,10 +625,7 @@ static int builtin_unset(struct child_prog *child)
623 */ 625 */
624static int run_command_predicate(char *cmd) 626static int run_command_predicate(char *cmd)
625{ 627{
626 int n=strlen(cmd); 628 local_pending_command = xstrdup(cmd);
627 local_pending_command = xmalloc(n+1);
628 strncpy(local_pending_command, cmd, n);
629 local_pending_command[n]='\0';
630 return( busy_loop(NULL)); 629 return( busy_loop(NULL));
631} 630}
632#endif 631#endif
@@ -808,15 +807,10 @@ static inline void cmdedit_set_initial_prompt(void)
808{ 807{
809#ifdef BB_FEATURE_SH_SIMPLE_PROMPT 808#ifdef BB_FEATURE_SH_SIMPLE_PROMPT
810 PS1 = NULL; 809 PS1 = NULL;
811 PS2 = "> ";
812#else 810#else
813 PS1 = getenv("PS1"); 811 PS1 = getenv("PS1");
814 if(PS1==0) { 812 if(PS1==0)
815 PS1 = "\\w \\$ "; 813 PS1 = "\\w \\$ ";
816 }
817 PS2 = getenv("PS2");
818 if(PS2==0)
819 PS2 = "> ";
820#endif 814#endif
821} 815}
822 816
@@ -954,7 +948,7 @@ static int expand_arguments(char *command)
954 /* Fix up escape sequences to be the Real Thing(tm) */ 948 /* Fix up escape sequences to be the Real Thing(tm) */
955 while( command && command[ix]) { 949 while( command && command[ix]) {
956 if (command[ix] == '\\') { 950 if (command[ix] == '\\') {
957 char *tmp = command+ix+1; 951 const char *tmp = command+ix+1;
958 command[ix] = process_escape_sequence( &tmp ); 952 command[ix] = process_escape_sequence( &tmp );
959 memmove(command+ix + 1, tmp, strlen(tmp)+1); 953 memmove(command+ix + 1, tmp, strlen(tmp)+1);
960 } 954 }
@@ -1829,8 +1823,10 @@ static int busy_loop(FILE * input)
1829#ifdef BB_FEATURE_CLEAN_UP 1823#ifdef BB_FEATURE_CLEAN_UP
1830void free_memory(void) 1824void free_memory(void)
1831{ 1825{
1832 if (cwd) 1826 if (cwd) {
1833 free(cwd); 1827 free(cwd);
1828 cwd = NULL;
1829 }
1834 if (local_pending_command) 1830 if (local_pending_command)
1835 free(local_pending_command); 1831 free(local_pending_command);
1836 1832
@@ -1850,7 +1846,6 @@ int shell_main(int argc_l, char **argv_l)
1850 1846
1851 /* These variables need re-initializing when recursing */ 1847 /* These variables need re-initializing when recursing */
1852 shell_context = 0; 1848 shell_context = 0;
1853 cwd=NULL;
1854 local_pending_command = NULL; 1849 local_pending_command = NULL;
1855 close_me_head = NULL; 1850 close_me_head = NULL;
1856 job_list.head = NULL; 1851 job_list.head = NULL;
@@ -1921,8 +1916,7 @@ int shell_main(int argc_l, char **argv_l)
1921 } 1916 }
1922 1917
1923 /* initialize the cwd -- this is never freed...*/ 1918 /* initialize the cwd -- this is never freed...*/
1924 cwd=(char*)xmalloc(sizeof(char)*MAX_LINE+1); 1919 cwd = xgetcwd(0);
1925 getcwd(cwd, sizeof(char)*MAX_LINE);
1926 1920
1927#ifdef BB_FEATURE_CLEAN_UP 1921#ifdef BB_FEATURE_CLEAN_UP
1928 atexit(free_memory); 1922 atexit(free_memory);
@@ -1932,7 +1926,6 @@ int shell_main(int argc_l, char **argv_l)
1932 cmdedit_set_initial_prompt(); 1926 cmdedit_set_initial_prompt();
1933#else 1927#else
1934 PS1 = NULL; 1928 PS1 = NULL;
1935 PS2 = "> ";
1936#endif 1929#endif
1937 1930
1938 return (busy_loop(input)); 1931 return (busy_loop(input));
diff --git a/shell/cmdedit.c b/shell/cmdedit.c
index a3710812f..eef1a88c8 100644
--- a/shell/cmdedit.c
+++ b/shell/cmdedit.c
@@ -31,8 +31,6 @@
31 */ 31 */
32 32
33 33
34//#define TEST
35
36#include <stdio.h> 34#include <stdio.h>
37#include <errno.h> 35#include <errno.h>
38#include <unistd.h> 36#include <unistd.h>
@@ -43,10 +41,16 @@
43#include <signal.h> 41#include <signal.h>
44#include <limits.h> 42#include <limits.h>
45 43
46#ifndef TEST
47
48#include "busybox.h" 44#include "busybox.h"
49 45
46#ifdef BB_LOCALE_SUPPORT
47#define Isprint(c) isprint((c))
48#else
49#define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') )
50#endif
51
52#ifndef TEST
53
50#define D(x) 54#define D(x)
51 55
52#else 56#else
@@ -59,13 +63,6 @@
59 63
60#define D(x) x 64#define D(x) x
61 65
62#ifndef TRUE
63#define TRUE 1
64#endif
65#ifndef FALSE
66#define FALSE 0
67#endif
68
69#endif /* TEST */ 66#endif /* TEST */
70 67
71#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION 68#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
@@ -92,29 +89,6 @@
92#endif /* advanced FEATURES */ 89#endif /* advanced FEATURES */
93 90
94 91
95#ifdef TEST
96void *xrealloc(void *old, size_t size)
97{
98 return realloc(old, size);
99}
100
101void *xmalloc(size_t size)
102{
103 return malloc(size);
104}
105char *xstrdup(const char *s)
106{
107 return strdup(s);
108}
109
110void *xcalloc(size_t size, size_t se)
111{
112 return calloc(size, se);
113}
114
115#define error_msg(s, d) fprintf(stderr, s, d)
116#endif /* TEST */
117
118 92
119struct history { 93struct history {
120 char *s; 94 char *s;
@@ -238,7 +212,7 @@ static void win_changed(int nsig)
238static void cmdedit_reset_term(void) 212static void cmdedit_reset_term(void)
239{ 213{
240 if ((handlers_sets & SET_RESET_TERM) != 0) { 214 if ((handlers_sets & SET_RESET_TERM) != 0) {
241 /* sparc and other have broken termios support: use old termio handling. */ 215/* sparc and other have broken termios support: use old termio handling. */
242 setTermSettings(fileno(stdin), (void *) &initial_settings); 216 setTermSettings(fileno(stdin), (void *) &initial_settings);
243 handlers_sets &= ~SET_RESET_TERM; 217 handlers_sets &= ~SET_RESET_TERM;
244 } 218 }
@@ -270,9 +244,9 @@ static void cmdedit_set_out_char(int next_char)
270 int c = (int)((unsigned char) command_ps[cursor]); 244 int c = (int)((unsigned char) command_ps[cursor]);
271 245
272 if (c == 0) 246 if (c == 0)
273 c = ' '; /* destroy end char? */ 247 c = ' '; /* destroy end char? */
274#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT 248#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
275 if (!isprint(c)) { /* Inverse put non-printable characters */ 249 if (!Isprint(c)) { /* Inverse put non-printable characters */
276 if (c >= 128) 250 if (c >= 128)
277 c -= 128; 251 c -= 128;
278 if (c < ' ') 252 if (c < ' ')
@@ -328,7 +302,7 @@ static void input_backward(int num)
328{ 302{
329 if (num > cursor) 303 if (num > cursor)
330 num = cursor; 304 num = cursor;
331 cursor -= num; /* new cursor (in command, not terminal) */ 305 cursor -= num; /* new cursor (in command, not terminal) */
332 306
333 if (cmdedit_x >= num) { /* no to up line */ 307 if (cmdedit_x >= num) { /* no to up line */
334 cmdedit_x -= num; 308 cmdedit_x -= num;
@@ -369,147 +343,116 @@ static void parse_prompt(const char *prmt_ptr)
369 put_prompt(); 343 put_prompt();
370} 344}
371#else 345#else
372static void add_to_prompt(char **prmt_mem_ptr, int *alm,
373 int *prmt_len, const char *addb)
374{
375 *prmt_len += strlen(addb);
376 if (*alm < (*prmt_len) + 1) {
377 *alm = (*prmt_len) + 1;
378 *prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm);
379 }
380 strcat(*prmt_mem_ptr, addb);
381}
382
383static void parse_prompt(const char *prmt_ptr) 346static void parse_prompt(const char *prmt_ptr)
384{ 347{
385 int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */
386 int prmt_len = 0; 348 int prmt_len = 0;
387 int sub_len = 0; 349 int sub_len = 0;
388 int flg_not_length = '['; 350 char flg_not_length = '[';
389 char *prmt_mem_ptr = xstrdup(prmt_ptr); 351 char *prmt_mem_ptr = xcalloc(1, 1);
390 char pwd_buf[PATH_MAX + 1]; 352 char *pwd_buf = xgetcwd(0);
391 char buf[16]; 353 char buf2[PATH_MAX + 1];
392 int c; 354 char buf[2];
393 355 char c;
394 pwd_buf[0] = 0; 356 char *pbuf;
395 *prmt_mem_ptr = 0;
396 357
397 while (*prmt_ptr) { 358 while (*prmt_ptr) {
359 pbuf = buf;
360 pbuf[1] = 0;
398 c = *prmt_ptr++; 361 c = *prmt_ptr++;
399 if (c == '\\') { 362 if (c == '\\') {
400 c = *prmt_ptr; 363 const char *cp = prmt_ptr;
401 if (c == 0) 364 int l;
365
366 c = process_escape_sequence(&prmt_ptr);
367 if(prmt_ptr==cp) {
368 if (*cp == 0)
402 break; 369 break;
403 prmt_ptr++; 370 c = *prmt_ptr++;
404 switch (c) { 371 switch (c) {
405#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR 372#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
406 case 'u': 373 case 'u':
407 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf); 374 pbuf = user_buf;
408 continue; 375 break;
409#endif 376#endif
410 case 'h': 377 case 'h':
411 if (hostname_buf[0] == 0) { 378 pbuf = hostname_buf;
412 hostname_buf = xcalloc(256, 1); 379 if (*pbuf == 0) {
413 if (gethostname(hostname_buf, 255) < 0) { 380 pbuf = xcalloc(256, 1);
414 strcpy(hostname_buf, "?"); 381 if (gethostname(pbuf, 255) < 0) {
382 strcpy(pbuf, "?");
415 } else { 383 } else {
416 char *s = strchr(hostname_buf, '.'); 384 char *s = strchr(pbuf, '.');
417 385
418 if (s) 386 if (s)
419 *s = 0; 387 *s = 0;
420 } 388 }
389 hostname_buf = pbuf;
421 } 390 }
422 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, 391 break;
423 hostname_buf); 392 case '$':
424 continue;
425 case '$':
426 c = my_euid == 0 ? '#' : '$'; 393 c = my_euid == 0 ? '#' : '$';
427 break; 394 break;
428#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR 395#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
429 case 'w': 396 case 'w':
430 if (pwd_buf[0] == 0) { 397 pbuf = pwd_buf;
431 int l; 398 l = strlen(home_pwd_buf);
432 399 if (home_pwd_buf[0] != 0 &&
433 getcwd(pwd_buf, PATH_MAX); 400 strncmp(home_pwd_buf, pbuf, l) == 0 &&
434 l = strlen(home_pwd_buf); 401 (pbuf[l]=='/' || pbuf[l]=='\0') &&
435 if (home_pwd_buf[0] != 0 && 402 strlen(pwd_buf+l)<PATH_MAX) {
436 strncmp(home_pwd_buf, pwd_buf, l) == 0) { 403 pbuf = buf2;
437 strcpy(pwd_buf + 1, pwd_buf + l); 404 *pbuf = '~';
438 pwd_buf[0] = '~'; 405 strcpy(pbuf+1, pwd_buf+l);
439 } 406 }
440 } 407 break;
441 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf);
442 continue;
443#endif 408#endif
444 case 'W': 409 case 'W':
445 if (pwd_buf[0] == 0) { 410 pbuf = pwd_buf;
446 char *z; 411 cp = strrchr(pbuf,'/');
447 412 if ( (cp != NULL) && (cp != pbuf) )
448 getcwd(pwd_buf, PATH_MAX); 413 pbuf += (cp-pbuf)+1;
449 z = strrchr(pwd_buf,'/'); 414 break;
450 if ( (z != NULL) && (z != pwd_buf) ) { 415 case '!':
451 z++; 416 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
452 strcpy(pwd_buf,z); 417 break;
453 } 418 case 'e': case 'E': /* \e \E = \033 */
454 }
455 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf);
456 continue;
457 case '!':
458 snprintf(buf, sizeof(buf), "%d", num_ok_lines);
459 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
460 continue;
461 case 'e':
462 case 'E': /* \e \E = \033 */
463 c = '\033'; 419 c = '\033';
464 break; 420 break;
465 case 'x': 421 case 'x': case 'X':
466 case 'X':
467 case '0':
468 case '1':
469 case '2':
470 case '3':
471 case '4':
472 case '5':
473 case '6':
474 case '7':{
475 int l;
476 int ho = 0;
477 char *eho;
478
479 if (c == 'X')
480 c = 'x';
481
482 for (l = 0; l < 3;) { 422 for (l = 0; l < 3;) {
483 423 int h;
484 buf[l++] = *prmt_ptr; 424 buf2[l++] = *prmt_ptr;
485 buf[l] = 0; 425 buf2[l] = 0;
486 ho = strtol(buf, &eho, c == 'x' ? 16 : 8); 426 h = strtol(buf2, &pbuf, 16);
487 if (ho > UCHAR_MAX || (eho - buf) < l) { 427 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
488 l--; 428 l--;
489 break; 429 break;
490 } 430 }
491 prmt_ptr++; 431 prmt_ptr++;
492 } 432 }
493 buf[l] = 0; 433 buf2[l] = 0;
494 ho = strtol(buf, 0, c == 'x' ? 16 : 8); 434 c = (char)strtol(buf2, 0, 16);
495 c = ho == 0 ? '?' : (char) ho; 435 if(c==0)
436 c = '?';
437 pbuf = buf;
496 break; 438 break;
497 } 439 case '[': case ']':
498 case '[':
499 case ']':
500 if (c == flg_not_length) { 440 if (c == flg_not_length) {
501 flg_not_length = flg_not_length == '[' ? ']' : '['; 441 flg_not_length = flg_not_length == '[' ? ']' : '[';
502 continue; 442 continue;
503 } 443 }
504 break; 444 break;
505 } 445 }
446 }
506 } 447 }
507 buf[0] = c; 448 if(pbuf == buf)
508 buf[1] = 0; 449 *pbuf = c;
509 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf); 450 prmt_len += strlen(pbuf);
451 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
510 if (flg_not_length == ']') 452 if (flg_not_length == ']')
511 sub_len++; 453 sub_len++;
512 } 454 }
455 free(pwd_buf);
513 cmdedit_prompt = prmt_mem_ptr; 456 cmdedit_prompt = prmt_mem_ptr;
514 cmdedit_prmt_len = prmt_len - sub_len; 457 cmdedit_prmt_len = prmt_len - sub_len;
515 put_prompt(); 458 put_prompt();
@@ -789,7 +732,7 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
789 char **paths = path1; 732 char **paths = path1;
790 int npaths; 733 int npaths;
791 int i; 734 int i;
792 char found[BUFSIZ + 4 + PATH_MAX]; 735 char *found;
793 char *pfind = strrchr(command, '/'); 736 char *pfind = strrchr(command, '/');
794 737
795 path1[0] = "."; 738 path1[0] = ".";
@@ -822,7 +765,6 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
822 continue; 765 continue;
823 766
824 while ((next = readdir(dir)) != NULL) { 767 while ((next = readdir(dir)) != NULL) {
825 const char *str_merge = "%s/%s";
826 char *str_found = next->d_name; 768 char *str_found = next->d_name;
827 769
828 /* matched ? */ 770 /* matched ? */
@@ -835,25 +777,23 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
835 else 777 else
836 continue; 778 continue;
837 } 779 }
838 if (paths[i][strlen(paths[i]) - 1] == '/') 780 found = concat_path_file(paths[i], str_found);
839 str_merge = "%s%s";
840 sprintf(found, str_merge, paths[i], str_found);
841 /* hmm, remover in progress? */ 781 /* hmm, remover in progress? */
842 if (stat(found, &st) < 0) 782 if (stat(found, &st) < 0)
843 continue; 783 goto cont;
844 /* find with dirs ? */ 784 /* find with dirs ? */
845 if (paths[i] != dirbuf) 785 if (paths[i] != dirbuf)
846 strcpy(found, next->d_name); /* only name */ 786 strcpy(found, next->d_name); /* only name */
847 if (S_ISDIR(st.st_mode)) { 787 if (S_ISDIR(st.st_mode)) {
848 /* name is directory */ 788 /* name is directory */
849 /* algorithmic only "/" ? */ 789 str_found = found;
850 if (*str_found) 790 found = concat_path_file(found, "");
851 strcat(found, "/"); 791 free(str_found);
852 str_found = add_quote_for_spec_chars(found); 792 str_found = add_quote_for_spec_chars(found);
853 } else { 793 } else {
854 /* not put found file if search only dirs for cd */ 794 /* not put found file if search only dirs for cd */
855 if (type == FIND_DIR_ONLY) 795 if (type == FIND_DIR_ONLY)
856 continue; 796 goto cont;
857 str_found = add_quote_for_spec_chars(found); 797 str_found = add_quote_for_spec_chars(found);
858 if (type == FIND_FILE_ONLY || 798 if (type == FIND_FILE_ONLY ||
859 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE)) 799 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE))
@@ -863,6 +803,8 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
863 matches = xrealloc(matches, (nm + 1) * sizeof(char *)); 803 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
864 804
865 matches[nm++] = str_found; 805 matches[nm++] = str_found;
806cont:
807 free(found);
866 } 808 }
867 closedir(dir); 809 closedir(dir);
868 } 810 }
@@ -1440,7 +1382,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
1440 } 1382 }
1441 } else 1383 } else
1442#endif 1384#endif
1443 if (!isprint(c)) /* Skip non-printable characters */ 1385 if (!Isprint(c)) /* Skip non-printable characters */
1444 break; 1386 break;
1445 1387
1446 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */ 1388 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
@@ -1554,6 +1496,9 @@ extern void cmdedit_terminate(void)
1554 1496
1555#ifdef TEST 1497#ifdef TEST
1556 1498
1499const char *applet_name = "debug stuff usage";
1500const char *memory_exhausted = "Memory exhausted";
1501
1557#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT 1502#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1558#include <locale.h> 1503#include <locale.h>
1559#endif 1504#endif
diff --git a/shell/lash.c b/shell/lash.c
index 89325b63d..ee45b1a0d 100644
--- a/shell/lash.c
+++ b/shell/lash.c
@@ -64,7 +64,10 @@
64#include <sys/wait.h> 64#include <sys/wait.h>
65#include <unistd.h> 65#include <unistd.h>
66#include <getopt.h> 66#include <getopt.h>
67
68#ifdef BB_LOCALE_SUPPORT
67#include <locale.h> 69#include <locale.h>
70#endif
68 71
69//#define BB_FEATURE_SH_WORDEXP 72//#define BB_FEATURE_SH_WORDEXP
70 73
@@ -80,7 +83,6 @@
80#include "cmdedit.h" 83#include "cmdedit.h"
81 84
82 85
83static const int MAX_LINE = 256; /* size of input buffer for cwd data */
84static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ 86static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
85#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" 87#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
86 88
@@ -230,7 +232,7 @@ static char syntax_err[]="syntax error near unexpected token";
230#endif 232#endif
231 233
232static char *PS1; 234static char *PS1;
233static char *PS2; 235static char *PS2 = "> ";
234 236
235 237
236#ifdef DEBUG_SHELL 238#ifdef DEBUG_SHELL
@@ -294,7 +296,7 @@ static int builtin_cd(struct child_prog *child)
294 printf("cd: %s: %m\n", newdir); 296 printf("cd: %s: %m\n", newdir);
295 return EXIT_FAILURE; 297 return EXIT_FAILURE;
296 } 298 }
297 getcwd(cwd, sizeof(char)*MAX_LINE); 299 cwd = xgetcwd(cwd);
298 300
299 return EXIT_SUCCESS; 301 return EXIT_SUCCESS;
300} 302}
@@ -410,7 +412,6 @@ static int builtin_jobs(struct child_prog *child)
410/* built-in 'pwd' handler */ 412/* built-in 'pwd' handler */
411static int builtin_pwd(struct child_prog *dummy) 413static int builtin_pwd(struct child_prog *dummy)
412{ 414{
413 getcwd(cwd, MAX_LINE);
414 printf( "%s\n", cwd); 415 printf( "%s\n", cwd);
415 return EXIT_SUCCESS; 416 return EXIT_SUCCESS;
416} 417}
@@ -434,13 +435,14 @@ static int builtin_export(struct child_prog *child)
434#ifndef BB_FEATURE_SH_SIMPLE_PROMPT 435#ifndef BB_FEATURE_SH_SIMPLE_PROMPT
435 if (strncmp(v, "PS1=", 4)==0) 436 if (strncmp(v, "PS1=", 4)==0)
436 PS1 = getenv("PS1"); 437 PS1 = getenv("PS1");
437 else if (strncmp(v, "PS2=", 4)==0)
438 PS2 = getenv("PS2");
439#endif 438#endif
439
440#ifdef BB_LOCALE_SUPPORT
440 if(strncmp(v, "LC_ALL=", 7)==0) 441 if(strncmp(v, "LC_ALL=", 7)==0)
441 setlocale(LC_ALL, getenv("LC_ALL")); 442 setlocale(LC_ALL, getenv("LC_ALL"));
442 if(strncmp(v, "LC_CTYPE=", 9)==0) 443 if(strncmp(v, "LC_CTYPE=", 9)==0)
443 setlocale(LC_CTYPE, getenv("LC_CTYPE")); 444 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
445#endif
444 446
445 return (res); 447 return (res);
446} 448}
@@ -623,10 +625,7 @@ static int builtin_unset(struct child_prog *child)
623 */ 625 */
624static int run_command_predicate(char *cmd) 626static int run_command_predicate(char *cmd)
625{ 627{
626 int n=strlen(cmd); 628 local_pending_command = xstrdup(cmd);
627 local_pending_command = xmalloc(n+1);
628 strncpy(local_pending_command, cmd, n);
629 local_pending_command[n]='\0';
630 return( busy_loop(NULL)); 629 return( busy_loop(NULL));
631} 630}
632#endif 631#endif
@@ -808,15 +807,10 @@ static inline void cmdedit_set_initial_prompt(void)
808{ 807{
809#ifdef BB_FEATURE_SH_SIMPLE_PROMPT 808#ifdef BB_FEATURE_SH_SIMPLE_PROMPT
810 PS1 = NULL; 809 PS1 = NULL;
811 PS2 = "> ";
812#else 810#else
813 PS1 = getenv("PS1"); 811 PS1 = getenv("PS1");
814 if(PS1==0) { 812 if(PS1==0)
815 PS1 = "\\w \\$ "; 813 PS1 = "\\w \\$ ";
816 }
817 PS2 = getenv("PS2");
818 if(PS2==0)
819 PS2 = "> ";
820#endif 814#endif
821} 815}
822 816
@@ -954,7 +948,7 @@ static int expand_arguments(char *command)
954 /* Fix up escape sequences to be the Real Thing(tm) */ 948 /* Fix up escape sequences to be the Real Thing(tm) */
955 while( command && command[ix]) { 949 while( command && command[ix]) {
956 if (command[ix] == '\\') { 950 if (command[ix] == '\\') {
957 char *tmp = command+ix+1; 951 const char *tmp = command+ix+1;
958 command[ix] = process_escape_sequence( &tmp ); 952 command[ix] = process_escape_sequence( &tmp );
959 memmove(command+ix + 1, tmp, strlen(tmp)+1); 953 memmove(command+ix + 1, tmp, strlen(tmp)+1);
960 } 954 }
@@ -1829,8 +1823,10 @@ static int busy_loop(FILE * input)
1829#ifdef BB_FEATURE_CLEAN_UP 1823#ifdef BB_FEATURE_CLEAN_UP
1830void free_memory(void) 1824void free_memory(void)
1831{ 1825{
1832 if (cwd) 1826 if (cwd) {
1833 free(cwd); 1827 free(cwd);
1828 cwd = NULL;
1829 }
1834 if (local_pending_command) 1830 if (local_pending_command)
1835 free(local_pending_command); 1831 free(local_pending_command);
1836 1832
@@ -1850,7 +1846,6 @@ int shell_main(int argc_l, char **argv_l)
1850 1846
1851 /* These variables need re-initializing when recursing */ 1847 /* These variables need re-initializing when recursing */
1852 shell_context = 0; 1848 shell_context = 0;
1853 cwd=NULL;
1854 local_pending_command = NULL; 1849 local_pending_command = NULL;
1855 close_me_head = NULL; 1850 close_me_head = NULL;
1856 job_list.head = NULL; 1851 job_list.head = NULL;
@@ -1921,8 +1916,7 @@ int shell_main(int argc_l, char **argv_l)
1921 } 1916 }
1922 1917
1923 /* initialize the cwd -- this is never freed...*/ 1918 /* initialize the cwd -- this is never freed...*/
1924 cwd=(char*)xmalloc(sizeof(char)*MAX_LINE+1); 1919 cwd = xgetcwd(0);
1925 getcwd(cwd, sizeof(char)*MAX_LINE);
1926 1920
1927#ifdef BB_FEATURE_CLEAN_UP 1921#ifdef BB_FEATURE_CLEAN_UP
1928 atexit(free_memory); 1922 atexit(free_memory);
@@ -1932,7 +1926,6 @@ int shell_main(int argc_l, char **argv_l)
1932 cmdedit_set_initial_prompt(); 1926 cmdedit_set_initial_prompt();
1933#else 1927#else
1934 PS1 = NULL; 1928 PS1 = NULL;
1935 PS2 = "> ";
1936#endif 1929#endif
1937 1930
1938 return (busy_loop(input)); 1931 return (busy_loop(input));
diff --git a/tr.c b/tr.c
index 32a4f2917..ce15cfdf8 100644
--- a/tr.c
+++ b/tr.c
@@ -93,7 +93,7 @@ static void map(register unsigned char *string1, unsigned int string1_len,
93 } 93 }
94} 94}
95 95
96static unsigned int expand(char *arg, register unsigned char *buffer) 96static unsigned int expand(const char *arg, register unsigned char *buffer)
97{ 97{
98 unsigned char *buffer_start = buffer; 98 unsigned char *buffer_start = buffer;
99 int i, ac; 99 int i, ac;