From ef6747e290d0456d140129f28283e0761713da2e Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 27 Mar 2013 15:15:33 +0100 Subject: hexdump: don't unconditionally limit the usable address range Signed-off-by: Denys Vlasenko --- libbb/dump.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/dump.c b/libbb/dump.c index 7e435643b..566881a78 100644 --- a/libbb/dump.c +++ b/libbb/dump.c @@ -333,7 +333,7 @@ static void do_skip(priv_dumper_t *dumper, const char *fname, int statok) return; } } - if (fseek(stdin, dumper->pub.dump_skip, SEEK_SET)) { + if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) { bb_simple_perror_msg_and_die(fname); } dumper->address += dumper->pub.dump_skip; -- cgit v1.2.3-55-g6feb From 6c852bfcadd78f53ec7a5f35882636a2a4a66eb0 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 28 Mar 2013 13:20:12 +0100 Subject: lineedit: add handling of \H in prompt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Based on the patch by Arnaud RĂ©billout Signed-off-by: Denys Vlasenko --- libbb/lineedit.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) (limited to 'libbb') diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 52b49e8a9..591bb6d5e 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -1789,7 +1789,44 @@ static void parse_and_put_prompt(const char *prmt_ptr) if (c == '\\') { const char *cp = prmt_ptr; int l; - +/* + * Supported via bb_process_escape_sequence: + * \a ASCII bell character (07) + * \e ASCII escape character (033) + * \n newline + * \r carriage return + * \\ backslash + * \nnn char with octal code nnn + * Supported: + * \$ if the effective UID is 0, a #, otherwise a $ + * \! history number of this command + * (buggy?) + * \w current working directory, with $HOME abbreviated with a tilde + * Note: we do not support $PROMPT_DIRTRIM=n feature + * \h hostname up to the first '.' + * \H hostname + * \u username + * \[ begin a sequence of non-printing characters + * \] end a sequence of non-printing characters + * Not supported: + * \# command number of this command + * \j number of jobs currently managed by the shell + * \l basename of the shell's terminal device name + * \s name of the shell, the basename of $0 (the portion following the final slash) + * \V release of bash, version + patch level (e.g., 2.00.0) + * \W basename of the current working directory, with $HOME abbreviated with a tilde + * \d date in "Weekday Month Date" format (e.g., "Tue May 26") + * \D{format} + * format is passed to strftime(3). + * An empty format results in a locale-specific time representation. + * The braces are required. + * \T current time in 12-hour HH:MM:SS format + * \@ current time in 12-hour am/pm format + * \A current time in 24-hour HH:MM format + * Mishandled by bb_process_escape_sequence: + * \t current time in 24-hour HH:MM:SS format + * \v version of bash (e.g., 2.00) + */ c = bb_process_escape_sequence(&prmt_ptr); if (prmt_ptr == cp) { if (*cp == '\0') @@ -1802,9 +1839,11 @@ static void parse_and_put_prompt(const char *prmt_ptr) pbuf = user_buf ? user_buf : (char*)""; break; # endif + case 'H': case 'h': pbuf = free_me = safe_gethostname(); - *strchrnul(pbuf, '.') = '\0'; + if (c == 'h') + strchrnul(pbuf, '.')[0] = '\0'; break; case '$': c = (geteuid() == 0 ? '#' : '$'); @@ -1832,9 +1871,10 @@ static void parse_and_put_prompt(const char *prmt_ptr) case '!': pbuf = free_me = xasprintf("%d", num_ok_lines); break; - case 'e': case 'E': /* \e \E = \033 */ - c = '\033'; - break; +// bb_process_escape_sequence does this now: +// case 'e': case 'E': /* \e \E = \033 */ +// c = '\033'; +// break; case 'x': case 'X': { char buf2[4]; for (l = 0; l < 3;) { -- cgit v1.2.3-55-g6feb From 8f2cb7ab26b9c720c24cdeffb624bfe0c2352353 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 29 Mar 2013 12:30:33 +0100 Subject: libbb: introduce and use strftime_[YYYYMMDD]HHMMSS() function old new delta strftime_fmt - 53 +53 strftime_YYYYMMDDHHMMSS - 12 +12 strftime_HHMMSS - 12 +12 human_time 44 43 -1 fmtstr_t 9 - -9 step_time 361 345 -16 watch_main 261 232 -29 ------------------------------------------------------------------------------ (add/remove: 3/1 grow/shrink: 0/3 up/down: 77/-55) Total: 22 bytes text data bss dec hex filename 919203 932 17692 937827 e4f63 busybox_old 919209 932 17692 937833 e4f69 busybox_unstripped Signed-off-by: Denys Vlasenko --- coreutils/stat.c | 2 +- include/libbb.h | 3 ++- libbb/login.c | 3 +-- libbb/time.c | 21 +++++++++++++++++++++ networking/ntpd.c | 4 ++-- networking/ntpd_simple.c | 2 +- procps/watch.c | 12 +++++++----- 7 files changed, 35 insertions(+), 12 deletions(-) (limited to 'libbb') diff --git a/coreutils/stat.c b/coreutils/stat.c index c8677ebaa..dc9d81c35 100644 --- a/coreutils/stat.c +++ b/coreutils/stat.c @@ -127,7 +127,7 @@ static const char *human_time(time_t t) /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/ #define buf bb_common_bufsiz1 - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t)); + strcpy(strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &t), ".000000000"); return buf; #undef buf } diff --git a/include/libbb.h b/include/libbb.h index 79a37a759..6dd4d7cae 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -523,7 +523,8 @@ struct BUG_too_small { void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC; time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC; - +char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; +char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; int xsocket(int domain, int type, int protocol) FAST_FUNC; void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC; diff --git a/libbb/login.c b/libbb/login.c index 8a82c6add..8f080b775 100644 --- a/libbb/login.c +++ b/libbb/login.c @@ -16,7 +16,6 @@ #define LOGIN " login: " static const char fmtstr_d[] ALIGN1 = "%A, %d %B %Y"; -static const char fmtstr_t[] ALIGN1 = "%H:%M:%S"; void FAST_FUNC print_login_issue(const char *issue_file, const char *tty) { @@ -73,7 +72,7 @@ void FAST_FUNC print_login_issue(const char *issue_file, const char *tty) strftime(buf, sizeof(buf), fmtstr_d, localtime(&t)); break; case 't': - strftime(buf, sizeof(buf), fmtstr_t, localtime(&t)); + strftime_HHMMSS(buf, sizeof(buf), &t); break; case 'l': outbuf = tty; diff --git a/libbb/time.c b/libbb/time.c index e2b938471..57e14b66c 100644 --- a/libbb/time.c +++ b/libbb/time.c @@ -187,6 +187,27 @@ time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm) return t; } +static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt) +{ + time_t t; + if (!tp) { + tp = &t; + time(tp); + } + /* Returns pointer to NUL */ + return buf + strftime(buf, len, fmt, localtime(tp)); +} + +char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp) +{ + return strftime_fmt(buf, len, tp, "%H:%M:%S"); +} + +char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) +{ + return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S"); +} + #if ENABLE_MONOTONIC_SYSCALL #include diff --git a/networking/ntpd.c b/networking/ntpd.c index 79b7c3793..0f4319ef2 100644 --- a/networking/ntpd.c +++ b/networking/ntpd.c @@ -887,11 +887,11 @@ step_time(double offset) VERB2 { tval = tvc.tv_sec; - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval)); + strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); bb_error_msg("current time is %s.%06u", buf, (unsigned)tvc.tv_usec); } tval = tvn.tv_sec; - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval)); + strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset); /* Correct various fields which contain time-relative values: */ diff --git a/networking/ntpd_simple.c b/networking/ntpd_simple.c index 55bded8ff..3e7fc4719 100644 --- a/networking/ntpd_simple.c +++ b/networking/ntpd_simple.c @@ -378,7 +378,7 @@ step_time_once(double offset) bb_perror_msg_and_die("settimeofday"); tval = tv.tv_sec; - strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval)); + strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); bb_error_msg("setting clock to %s (offset %fs)", buf, offset); diff --git a/procps/watch.c b/procps/watch.c index 36af1cca7..0397f21bf 100644 --- a/procps/watch.c +++ b/procps/watch.c @@ -69,7 +69,6 @@ int watch_main(int argc UNUSED_PARAM, char **argv) printf("\033[H""\033[J"); if (!(opt & 0x2)) { // no -t const unsigned time_len = sizeof("1234-67-90 23:56:89"); - time_t t; // STDERR_FILENO is procps3 compat: // "watch ls 2>/dev/null" does not detect tty size @@ -79,10 +78,13 @@ int watch_main(int argc UNUSED_PARAM, char **argv) free(header); header = xasprintf("Every %us: %-*s", period, (int)width, cmd); } - time(&t); - if (time_len < width) - strftime(header + width - time_len, time_len, - "%Y-%m-%d %H:%M:%S", localtime(&t)); + if (time_len < width) { + strftime_YYYYMMDDHHMMSS( + header + width - time_len, + time_len, + /*time_t*:*/ NULL + ); + } // compat: empty line between header and cmd output printf("%s\n\n", header); -- cgit v1.2.3-55-g6feb From 1d14569a66c741361afe0ebc6e2ef8ba9a6aeb71 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 29 Mar 2013 13:09:05 +0100 Subject: lineedit: implement \T \t \A \@ prompts escapes, fix \W escape, drop \! function old new delta parse_and_put_prompt 742 793 +51 read_line_input 3836 3826 -10 Signed-off-by: Denys Vlasenko --- libbb/lineedit.c | 84 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 37 deletions(-) (limited to 'libbb') diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 591bb6d5e..45448dfab 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -133,9 +133,6 @@ struct lineedit_statics { CHAR_T *command_ps; const char *cmdedit_prompt; -#if ENABLE_FEATURE_EDITING_FANCY_PROMPT - int num_ok_lines; /* = 1; */ -#endif #if ENABLE_USERNAME_OR_HOMEDIR char *user_buf; @@ -172,7 +169,6 @@ extern struct lineedit_statics *const lineedit_ptr_to_statics; #define command_len (S.command_len ) #define command_ps (S.command_ps ) #define cmdedit_prompt (S.cmdedit_prompt ) -#define num_ok_lines (S.num_ok_lines ) #define user_buf (S.user_buf ) #define home_pwd_buf (S.home_pwd_buf ) #define matches (S.matches ) @@ -185,7 +181,6 @@ extern struct lineedit_statics *const lineedit_ptr_to_statics; (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \ barrier(); \ cmdedit_termw = 80; \ - IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \ IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \ IF_FEATURE_EDITING_VI(delptr = delbuf;) \ } while (0) @@ -1532,7 +1527,6 @@ static void remember_in_history(char *str) # if ENABLE_FEATURE_EDITING_SAVEHISTORY && !ENABLE_FEATURE_EDITING_SAVE_ON_EXIT save_history(str); # endif - IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;) } #else /* MAX_HISTORY == 0 */ @@ -1768,17 +1762,16 @@ static void parse_and_put_prompt(const char *prmt_ptr) size_t cur_prmt_len = 0; char flg_not_length = '['; char *prmt_mem_ptr = xzalloc(1); - char *cwd_buf = xrealloc_getcwd_or_warn(NULL); +# if ENABLE_USERNAME_OR_HOMEDIR + char *cwd_buf = NULL; +# endif + char timebuf[sizeof("HH:MM:SS")]; char cbuf[2]; char c; char *pbuf; cmdedit_prmt_len = 0; - if (!cwd_buf) { - cwd_buf = (char *)bb_msg_unknown; - } - cbuf[1] = '\0'; /* never changes */ while (*prmt_ptr) { @@ -1787,7 +1780,7 @@ static void parse_and_put_prompt(const char *prmt_ptr) pbuf = cbuf; c = *prmt_ptr++; if (c == '\\') { - const char *cp = prmt_ptr; + const char *cp; int l; /* * Supported via bb_process_escape_sequence: @@ -1799,35 +1792,38 @@ static void parse_and_put_prompt(const char *prmt_ptr) * \nnn char with octal code nnn * Supported: * \$ if the effective UID is 0, a #, otherwise a $ - * \! history number of this command - * (buggy?) * \w current working directory, with $HOME abbreviated with a tilde * Note: we do not support $PROMPT_DIRTRIM=n feature + * \W basename of the current working directory, with $HOME abbreviated with a tilde * \h hostname up to the first '.' * \H hostname * \u username * \[ begin a sequence of non-printing characters * \] end a sequence of non-printing characters + * \T current time in 12-hour HH:MM:SS format + * \@ current time in 12-hour am/pm format + * \A current time in 24-hour HH:MM format + * \t current time in 24-hour HH:MM:SS format + * (all of the above work as \A) * Not supported: + * \! history number of this command * \# command number of this command * \j number of jobs currently managed by the shell * \l basename of the shell's terminal device name * \s name of the shell, the basename of $0 (the portion following the final slash) * \V release of bash, version + patch level (e.g., 2.00.0) - * \W basename of the current working directory, with $HOME abbreviated with a tilde * \d date in "Weekday Month Date" format (e.g., "Tue May 26") * \D{format} * format is passed to strftime(3). * An empty format results in a locale-specific time representation. * The braces are required. - * \T current time in 12-hour HH:MM:SS format - * \@ current time in 12-hour am/pm format - * \A current time in 24-hour HH:MM format * Mishandled by bb_process_escape_sequence: - * \t current time in 24-hour HH:MM:SS format * \v version of bash (e.g., 2.00) */ - c = bb_process_escape_sequence(&prmt_ptr); + cp = prmt_ptr; + c = *cp; + if (c != 't') /* don't treat \t as tab */ + c = bb_process_escape_sequence(&prmt_ptr); if (prmt_ptr == cp) { if (*cp == '\0') break; @@ -1848,29 +1844,41 @@ static void parse_and_put_prompt(const char *prmt_ptr) case '$': c = (geteuid() == 0 ? '#' : '$'); break; + case 'T': /* 12-hour HH:MM:SS format */ + case '@': /* 12-hour am/pm format */ + case 'A': /* 24-hour HH:MM format */ + case 't': /* 24-hour HH:MM:SS format */ + /* We show all of them as 24-hour HH:MM */ + strftime_HHMMSS(timebuf, sizeof(timebuf), NULL)[-3] = '\0'; + pbuf = timebuf; + break; # if ENABLE_USERNAME_OR_HOMEDIR - case 'w': - /* /home/user[/something] -> ~[/something] */ - pbuf = cwd_buf; - l = strlen(home_pwd_buf); - if (l != 0 - && strncmp(home_pwd_buf, cwd_buf, l) == 0 - && (cwd_buf[l]=='/' || cwd_buf[l]=='\0') - && strlen(cwd_buf + l) < PATH_MAX - ) { - pbuf = free_me = xasprintf("~%s", cwd_buf + l); + case 'w': /* current dir */ + case 'W': /* basename of cur dir */ + if (!cwd_buf) { + cwd_buf = xrealloc_getcwd_or_warn(NULL); + if (!cwd_buf) + cwd_buf = (char *)bb_msg_unknown; + else { + /* /home/user[/something] -> ~[/something] */ + l = strlen(home_pwd_buf); + if (l != 0 + && strncmp(home_pwd_buf, cwd_buf, l) == 0 + && (cwd_buf[l] == '/' || cwd_buf[l] == '\0') + ) { + cwd_buf[0] = '~'; + overlapping_strcpy(cwd_buf + 1, cwd_buf + l); + } + } } - break; -# endif - case 'W': pbuf = cwd_buf; + if (c == 'w') + break; cp = strrchr(pbuf, '/'); if (cp != NULL && cp != pbuf) - pbuf += (cp-pbuf) + 1; - break; - case '!': - pbuf = free_me = xasprintf("%d", num_ok_lines); + pbuf = (char*)cp + 1; break; +# endif // bb_process_escape_sequence does this now: // case 'e': case 'E': /* \e \E = \033 */ // c = '\033'; @@ -1912,8 +1920,10 @@ static void parse_and_put_prompt(const char *prmt_ptr) free(free_me); } /* while */ +# if ENABLE_USERNAME_OR_HOMEDIR if (cwd_buf != (char *)bb_msg_unknown) free(cwd_buf); +# endif cmdedit_prompt = prmt_mem_ptr; put_prompt(); } -- cgit v1.2.3-55-g6feb From 8172d054db64bc80a355db111b484719930045bc Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 29 Mar 2013 13:21:53 +0100 Subject: lineedit: \W on "/bin" should show "bin", not "/bin" function old new delta parse_and_put_prompt 793 785 -8 Signed-off-by: Denys Vlasenko --- libbb/lineedit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 45448dfab..93ab86426 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -1875,7 +1875,7 @@ static void parse_and_put_prompt(const char *prmt_ptr) if (c == 'w') break; cp = strrchr(pbuf, '/'); - if (cp != NULL && cp != pbuf) + if (cp) pbuf = (char*)cp + 1; break; # endif -- cgit v1.2.3-55-g6feb