From 71d73136250efa7dd5b5b023df76487f6e723830 Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Fri, 3 Jun 2011 20:51:58 +0200 Subject: platform.h: support for build under Android Signed-off-by: Dan Fandrich Signed-off-by: Denys Vlasenko --- include/platform.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'include') diff --git a/include/platform.h b/include/platform.h index 2b84447bc..780568c2b 100644 --- a/include/platform.h +++ b/include/platform.h @@ -391,6 +391,12 @@ typedef unsigned smalluint; # undef HAVE_STPCPY #endif +#if defined(ANDROID) +# undef HAVE_DPRINTF +# undef HAVE_STPCPY +# undef HAVE_STRCHRNUL +#endif + /* * Now, define prototypes for all the functions defined in platform.c * These must come after all the HAVE_* macros are defined (or not) -- cgit v1.2.3-55-g6feb From f533ec876716415ed0e6ba28d13dfb6263068e82 Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Fri, 10 Jun 2011 05:17:59 +0200 Subject: *: simplify Ethernet header includes Signed-off-by: Dan Fandrich Signed-off-by: Denys Vlasenko --- include/platform.h | 13 ++++++++++--- networking/ether-wake.c | 4 +--- networking/ifconfig.c | 12 ++++-------- networking/ifplugd.c | 4 +++- networking/interface.c | 9 ++++----- networking/libiproute/ll_proto.c | 6 +----- networking/udhcp/dhcpc.c | 10 ++-------- networking/udhcp/packet.c | 13 +++---------- networking/udhcp/socket.c | 11 +---------- networking/zcip.c | 4 +--- 10 files changed, 30 insertions(+), 56 deletions(-) (limited to 'include') diff --git a/include/platform.h b/include/platform.h index 780568c2b..429220b0d 100644 --- a/include/platform.h +++ b/include/platform.h @@ -332,8 +332,8 @@ typedef unsigned smalluint; /* ---- Who misses what? ------------------------------------ */ -/* Assume all these functions exist by default. Platforms where it is not - * true will #undef them below. +/* Assume all these functions and header files exist by default. + * Platforms where it is not true will #undef them below. */ #define HAVE_CLEARENV 1 #define HAVE_FDATASYNC 1 @@ -349,9 +349,14 @@ typedef unsigned smalluint; #define HAVE_STRSEP 1 #define HAVE_STRSIGNAL 1 #define HAVE_VASPRINTF 1 +#define HAVE_XTABS 1 #define HAVE_MNTENT_H 1 +#define HAVE_NET_ETHERNET_H 1 #define HAVE_SYS_STATFS_H 1 -#define HAVE_XTABS 1 + +#if defined(__GLIBC__) && (__GLIBC__ < 2 || __GLIBC_MINOR__ < 1) +# undef HAVE_NET_ETHERNET_H +#endif #if defined(__dietlibc__) # undef HAVE_STRCHRNUL @@ -368,6 +373,7 @@ typedef unsigned smalluint; # undef HAVE_STRSEP # undef HAVE_STRSIGNAL # undef HAVE_VASPRINTF +# undef HAVE_NET_ETHERNET_H #endif #if defined(__FreeBSD__) @@ -395,6 +401,7 @@ typedef unsigned smalluint; # undef HAVE_DPRINTF # undef HAVE_STPCPY # undef HAVE_STRCHRNUL +# undef HAVE_NET_ETHERNET_H #endif /* diff --git a/networking/ether-wake.c b/networking/ether-wake.c index 260ba2463..6a88279f4 100644 --- a/networking/ether-wake.c +++ b/networking/ether-wake.c @@ -74,13 +74,11 @@ //usage: "\n -i iface Interface to use (default eth0)" //usage: "\n -p pass Append four or six byte password PW to the packet" +#include "libbb.h" #include -#include #include #include -#include "libbb.h" - /* Note: PF_INET, SOCK_DGRAM, IPPROTO_UDP would allow SIOCGIFHWADDR to * work as non-root, but we need SOCK_PACKET to specify the Ethernet * destination address. diff --git a/networking/ifconfig.c b/networking/ifconfig.c index 2a56da561..b6604f5d1 100644 --- a/networking/ifconfig.c +++ b/networking/ifconfig.c @@ -46,18 +46,14 @@ //usage: " [mem_start NN] [io_addr NN] [irq NN]\n") //usage: " [up|down] ..." +#include "libbb.h" +#include "inet_common.h" #include #include #include -#if defined(__GLIBC__) && __GLIBC__ >=2 && __GLIBC_MINOR__ >= 1 -#include -#include -#else -#include -#include +#ifdef HAVE_NET_ETHERNET_H +# include #endif -#include "libbb.h" -#include "inet_common.h" #if ENABLE_FEATURE_IFCONFIG_SLIP # include diff --git a/networking/ifplugd.c b/networking/ifplugd.c index 57d04e67b..d8358cdfd 100644 --- a/networking/ifplugd.c +++ b/networking/ifplugd.c @@ -37,7 +37,9 @@ #include #include #include -#include +#ifdef HAVE_NET_ETHERNET_H +# include +#endif #include #include #include diff --git a/networking/interface.c b/networking/interface.c index bea54c180..79c322ec0 100644 --- a/networking/interface.c +++ b/networking/interface.c @@ -30,15 +30,14 @@ * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu * (default AF was wrong) */ + +#include "libbb.h" +#include "inet_common.h" #include #include -#ifndef __UCLIBC__ +#ifdef HAVE_NET_ETHERNET_H # include -#else -# include #endif -#include "libbb.h" -#include "inet_common.h" #if ENABLE_FEATURE_HWIB /* #include */ diff --git a/networking/libiproute/ll_proto.c b/networking/libiproute/ll_proto.c index 04925ecf6..7aac8364d 100644 --- a/networking/libiproute/ll_proto.c +++ b/networking/libiproute/ll_proto.c @@ -12,11 +12,7 @@ #include "rt_names.h" #include "utils.h" -#if defined(__GLIBC__) && __GLIBC__ >=2 && __GLIBC_MINOR__ >= 1 -#include -#else -#include -#endif +#include #if !ENABLE_WERROR #warning de-bloat diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 5d3291b8c..4d755e6b8 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -25,14 +25,8 @@ #include "dhcpd.h" #include "dhcpc.h" -#include -#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined(_NEWLIB_VERSION) -# include -# include -#else -# include -# include -#endif +#include +#include #include /* struct client_config_t client_config is in bb_common_bufsiz1 */ diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c index 2b7528cc7..66b42c5e1 100644 --- a/networking/udhcp/packet.c +++ b/networking/udhcp/packet.c @@ -6,18 +6,11 @@ * * Licensed under GPLv2, see file LICENSE in this source tree. */ -#include -#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION -# include -# include -#else -# include -# include -# include -#endif - #include "common.h" #include "dhcpd.h" +#include +#include +#include void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type) { diff --git a/networking/udhcp/socket.c b/networking/udhcp/socket.c index 39f1cec54..a5220ba74 100644 --- a/networking/udhcp/socket.c +++ b/networking/udhcp/socket.c @@ -22,17 +22,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include -#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION -# include -# include -#else -# include -# include -# include -#endif - #include "common.h" +#include int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac) { diff --git a/networking/zcip.c b/networking/zcip.c index 6b7f2144f..8a35eca5d 100644 --- a/networking/zcip.c +++ b/networking/zcip.c @@ -35,14 +35,12 @@ //usage: "\nWith no -q, runs continuously monitoring for ARP conflicts," //usage: "\nexits only on I/O errors (link down etc)" +#include "libbb.h" #include -#include #include #include -#include #include -#include "libbb.h" #include /* We don't need more than 32 bits of the counter */ -- cgit v1.2.3-55-g6feb From a1a448347e71c9899ad1500cbd8739fd82e1bb91 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 17 Jun 2011 03:37:43 +0200 Subject: libbb: split bb_get_chunk_from_file and bb_get_chunk_with_continuation This also moves bb_get_chunk_with_continuation into its sole user, parse_config.c. This allows to optimize both functions separately, they need to be optimized for speed. (this need was highlighted by slow modprobe caused in part by slow bb_get_chunk_with_continuation in config parser). function old new delta bb_get_chunk_from_file 7 130 +123 config_read 457 558 +101 bb_get_chunk_with_continuation 194 - -194 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 2/0 up/down: 224/-194) Total: 30 bytes Signed-off-by: Denys Vlasenko --- include/libbb.h | 6 +++++- libbb/get_line_from_file.c | 42 +++++++------------------------------ libbb/parse_config.c | 52 ++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 56 insertions(+), 44 deletions(-) (limited to 'include') diff --git a/include/libbb.h b/include/libbb.h index 2d46061da..5d2b4c8c1 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -730,8 +730,12 @@ extern void xclose(int fd) FAST_FUNC; /* Reads and prints to stdout till eof, then closes FILE. Exits on error: */ extern void xprint_and_close_file(FILE *file) FAST_FUNC; +/* Reads a line from a text file, up to a newline or NUL byte, inclusive. + * Returns malloc'ed char*. If end is NULL '\n' isn't considered + * end of line. If end isn't NULL, length of the chunk is stored in it. + * Returns NULL if EOF/error. + */ extern char *bb_get_chunk_from_file(FILE *file, int *end) FAST_FUNC; -extern char *bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno) FAST_FUNC; /* Reads up to (and including) TERMINATING_STRING: */ extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC RETURNS_MALLOC; /* Same, with limited max size, and returns the length (excluding NUL): */ diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index 9be10687b..a98dd35eb 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c @@ -11,45 +11,24 @@ #include "libbb.h" -/* This function reads an entire line from a text file, up to a newline - * or NUL byte, inclusive. It returns a malloc'ed char * which - * must be free'ed by the caller. If end is NULL '\n' isn't considered - * end of line. If end isn't NULL, length of the chunk is stored in it. - * If lineno is not NULL, *lineno is incremented for each line, - * and also trailing '\' is recognized as line continuation. - * - * Returns NULL if EOF/error. */ -char* FAST_FUNC bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno) +char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end) { int ch; - int idx = 0; + unsigned idx = 0; char *linebuf = NULL; - int linebufsz = 0; while ((ch = getc(file)) != EOF) { /* grow the line buffer as necessary */ - if (idx >= linebufsz) { - linebufsz += 256; - linebuf = xrealloc(linebuf, linebufsz); - } + if (!(idx & 0xff)) + linebuf = xrealloc(linebuf, idx + 0x100); linebuf[idx++] = (char) ch; - if (!ch) + if (ch == '\0') + break; + if (end && ch == '\n') break; - if (end && ch == '\n') { - if (lineno == NULL) - break; - (*lineno)++; - if (idx < 2 || linebuf[idx-2] != '\\') - break; - idx -= 2; - } } - if (end) { + if (end) *end = idx; - /* handle corner case when the file is not ended with '\n' */ - if (ch == EOF && lineno != NULL) - (*lineno)++; - } if (linebuf) { // huh, does fgets discard prior data on error like this? // I don't think so.... @@ -63,11 +42,6 @@ char* FAST_FUNC bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno return linebuf; } -char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end) -{ - return bb_get_chunk_with_continuation(file, end, NULL); -} - /* Get line, including trailing \n if any */ char* FAST_FUNC xmalloc_fgets(FILE *file) { diff --git a/libbb/parse_config.c b/libbb/parse_config.c index 4b0236028..769ae5103 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c @@ -104,6 +104,44 @@ void FAST_FUNC config_close(parser_t *parser) } } +/* This function reads an entire line from a text file, up to a newline + * or NUL byte, exclusive. It returns a malloc'ed char*. + * *lineno is incremented for each line. + * Trailing '\' is recognized as line continuation. + * Returns NULL if EOF/error. + */ +static char* get_line_with_continuation(FILE *file, int *lineno) +{ + int ch; + unsigned idx = 0; + char *linebuf = NULL; + + while ((ch = getc(file)) != EOF) { + /* grow the line buffer as necessary */ + if (!(idx & 0xff)) + linebuf = xrealloc(linebuf, idx + 0x101); + if (ch == '\n') + ch = '\0'; + linebuf[idx] = (char) ch; + if (ch == '\0') { + (*lineno)++; + if (idx == 0 || linebuf[idx-1] != '\\') + break; + idx--; /* go back to '/' */ + continue; + } + idx++; + } + if (ch == EOF) { + /* handle corner case when the file is not ended with '\n' */ + (*lineno)++; + if (linebuf) + linebuf[idx] = '\0'; + } + return linebuf; +} + + /* 0. If parser is NULL return 0. 1. Read a line from config file. If nothing to read then return 0. @@ -132,28 +170,24 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const { char *line; int ntokens, mintokens; - int t, len; + int t; + + if (!parser) + return 0; ntokens = (uint8_t)flags; mintokens = (uint8_t)(flags >> 8); - if (parser == NULL) - return 0; - again: memset(tokens, 0, sizeof(tokens[0]) * ntokens); config_free_data(parser); /* Read one line (handling continuations with backslash) */ - line = bb_get_chunk_with_continuation(parser->fp, &len, &parser->lineno); + line = get_line_with_continuation(parser->fp, &parser->lineno); if (line == NULL) return 0; parser->line = line; - /* Strip trailing line-feed if any */ - if (len && line[len-1] == '\n') - line[len-1] = '\0'; - /* Skip token in the start of line? */ if (flags & PARSE_TRIM) line += strspn(line, delims + 1); -- cgit v1.2.3-55-g6feb From 12ac6287eedf45d896557b95270a6e0323951917 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 18 Jun 2011 15:49:47 +0200 Subject: procps: remove PSSCAN_STAT define, users were using it incorrectly Also contains small cleanups ps. Signed-off-by: Denys Vlasenko --- include/libbb.h | 7 ------- libbb/procps.c | 9 ++++++++- procps/ps.c | 23 +++++++++++------------ 3 files changed, 19 insertions(+), 20 deletions(-) (limited to 'include') diff --git a/include/libbb.h b/include/libbb.h index 5d2b4c8c1..2cac7e60c 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1545,13 +1545,6 @@ enum { PSSCAN_NICE = (1 << 20) * ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS, PSSCAN_RUIDGID = (1 << 21) * ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS, PSSCAN_TASKS = (1 << 22) * ENABLE_FEATURE_SHOW_THREADS, - /* These are all retrieved from proc/NN/stat in one go: */ - PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID - /**/ | PSSCAN_COMM | PSSCAN_STATE - /**/ | PSSCAN_VSZ | PSSCAN_RSS - /**/ | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME - /**/ | PSSCAN_TTY | PSSCAN_NICE - /**/ | PSSCAN_CPU }; //procps_status_t* alloc_procps_scan(void) FAST_FUNC; void free_procps_scan(procps_status_t* sp) FAST_FUNC; diff --git a/libbb/procps.c b/libbb/procps.c index 5833a1f0d..9207e9254 100644 --- a/libbb/procps.c +++ b/libbb/procps.c @@ -357,7 +357,14 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) sp->gid = sb.st_gid; } - if (flags & PSSCAN_STAT) { + /* These are all retrieved from proc/NN/stat in one go: */ + if (flags & (PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID + | PSSCAN_COMM | PSSCAN_STATE + | PSSCAN_VSZ | PSSCAN_RSS + | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME + | PSSCAN_TTY | PSSCAN_NICE + | PSSCAN_CPU) + ) { char *cp, *comm1; int tty; #if !ENABLE_FEATURE_FAST_TOP diff --git a/procps/ps.c b/procps/ps.c index 279f25e88..7537118f6 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -87,13 +87,6 @@ enum { MAX_WIDTH = 2*1024 }; * TIME The cumulative execution time for the process * CMD The command name; the full command line is shown with -f */ -#if ENABLE_SELINUX -# define SELINUX_O_PREFIX "label," -# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args") -#else -# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args") -#endif - typedef struct { uint16_t width; char name6[6]; @@ -113,7 +106,6 @@ struct globals { unsigned kernel_HZ; unsigned long long seconds_since_boot; #endif - char default_o[sizeof(DEFAULT_O_STR)]; } FIX_ALIASING; #define G (*(struct globals*)&bb_common_bufsiz1) #define out (G.out ) @@ -124,7 +116,6 @@ struct globals { #define terminal_width (G.terminal_width ) #define kernel_HZ (G.kernel_HZ ) #define seconds_since_boot (G.seconds_since_boot) -#define default_o (G.default_o ) #define INIT_G() do { } while (0) #if ENABLE_FEATURE_PS_TIME @@ -250,7 +241,7 @@ static void func_comm(char *buf, int size, const procps_status_t *ps) safe_strncpy(buf, ps->comm, size+1); } -static void func_stat(char *buf, int size, const procps_status_t *ps) +static void func_state(char *buf, int size, const procps_status_t *ps) { safe_strncpy(buf, ps->state, size+1); } @@ -394,7 +385,7 @@ static const ps_out_t out_spec[] = { { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY }, { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ }, /* Not mandated, but useful: */ - { 4 , "stat" ,"STAT" ,func_stat ,PSSCAN_STAT }, + { 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE }, { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS }, #if ENABLE_SELINUX { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT }, @@ -531,11 +522,19 @@ static void format_process(const procps_status_t *ps) printf("%.*s\n", terminal_width, buffer); } +#if ENABLE_SELINUX +# define SELINUX_O_PREFIX "label," +# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args") +#else +# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args") +#endif + int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int ps_main(int argc UNUSED_PARAM, char **argv) { procps_status_t *p; llist_t* opt_o = NULL; + char default_o[sizeof(DEFAULT_O_STR)]; int opt; enum { OPT_Z = (1 << 0), @@ -573,7 +572,7 @@ int ps_main(int argc UNUSED_PARAM, char **argv) parse_o(llist_pop(&opt_o)); } while (opt_o); } else { - /* Below: parse_o() needs char*, NOT const char*... */ + /* Below: parse_o() needs char*, NOT const char*, can't give it default_o */ #if ENABLE_SELINUX if (!(opt & OPT_Z) || !is_selinux_enabled()) { /* no -Z or no SELinux: do not show LABEL */ -- cgit v1.2.3-55-g6feb From adcabf3235a13e77a32ff2a7b7cf80be8ee3dfe3 Mon Sep 17 00:00:00 2001 From: Timo Teras Date: Mon, 20 Jun 2011 09:49:56 +0200 Subject: parse_config: use getline. BIG speedup with glibc (~40%). function old new delta config_read 559 604 +45 getline - 23 +23 config_close 29 49 +20 find_pair 169 187 +18 showmode 330 338 +8 hash_find 233 234 +1 builtin_umask 133 132 -1 lzo1x_optimize 1434 1429 -5 test_main 253 247 -6 buffer_fill_and_print 196 179 -17 create_J 1849 1826 -23 config_free_data 37 - -37 ------------------------------------------------------------------------------ (add/remove: 3/1 grow/shrink: 5/5 up/down: 138/-89) Total: 26 bytes Signed-off-by: Timo Teras Signed-off-by: Denys Vlasenko --- include/libbb.h | 3 +- libbb/parse_config.c | 92 ++++++++++++++++++++++++---------------------------- 2 files changed, 45 insertions(+), 50 deletions(-) (limited to 'include') diff --git a/include/libbb.h b/include/libbb.h index 2cac7e60c..953bec334 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1184,8 +1184,9 @@ enum { }; typedef struct parser_t { FILE *fp; - char *line; char *data; + char *line, *nline; + size_t line_alloc, nline_alloc; int lineno; } parser_t; parser_t* config_open(const char *filename) FAST_FUNC; diff --git a/libbb/parse_config.c b/libbb/parse_config.c index c0c34f312..cf5ba4deb 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c @@ -83,60 +83,55 @@ parser_t* FAST_FUNC config_open(const char *filename) return config_open2(filename, fopen_or_warn_stdin); } -static void config_free_data(parser_t *parser) -{ - free(parser->line); - parser->line = NULL; - if (PARSE_KEEP_COPY) { /* compile-time constant */ - free(parser->data); - parser->data = NULL; - } -} - void FAST_FUNC config_close(parser_t *parser) { if (parser) { - config_free_data(parser); + if (PARSE_KEEP_COPY) /* compile-time constant */ + free(parser->data); fclose(parser->fp); + free(parser->line); + free(parser->nline); free(parser); } } -/* This function reads an entire line from a text file, up to a newline - * or NUL byte, exclusive. It returns a malloc'ed char*. - * *lineno is incremented for each line. +/* This function reads an entire line from a text file, + * up to a newline, exclusive. * Trailing '\' is recognized as line continuation. - * Returns NULL if EOF/error. + * Returns -1 if EOF/error. */ -static char* get_line_with_continuation(FILE *file, int *lineno) +static int get_line_with_continuation(parser_t *parser) { - int ch; - unsigned idx = 0; - char *linebuf = NULL; - - while ((ch = getc(file)) != EOF) { - /* grow the line buffer as necessary */ - if (!(idx & 0xff)) - linebuf = xrealloc(linebuf, idx + 0x101); - if (ch == '\n') - ch = '\0'; - linebuf[idx] = (char) ch; - if (ch == '\0') { - (*lineno)++; - if (idx == 0 || linebuf[idx-1] != '\\') - break; - idx--; /* go back to '/' */ - continue; + ssize_t len, nlen; + char *line; + + len = getline(&parser->line, &parser->line_alloc, parser->fp); + if (len <= 0) + return len; + + line = parser->line; + for (;;) { + parser->lineno++; + if (line[len - 1] == '\n') + len--; + if (len == 0 || line[len - 1] != '\\') + break; + len--; + + nlen = getline(&parser->nline, &parser->nline_alloc, parser->fp); + if (nlen <= 0) + break; + + if (parser->line_alloc < len + nlen + 1) { + parser->line_alloc = len + nlen + 1; + line = parser->line = xrealloc(line, parser->line_alloc); } - idx++; - } - if (ch == EOF) { - /* handle corner case when the file is not ended with '\n' */ - (*lineno)++; - if (linebuf) - linebuf[idx] = '\0'; + memcpy(&line[len], parser->nline, nlen); + len += nlen; } - return linebuf; + + line[len] = '\0'; + return len; } @@ -176,15 +171,14 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const ntokens = (uint8_t)flags; mintokens = (uint8_t)(flags >> 8); -again: + again: memset(tokens, 0, sizeof(tokens[0]) * ntokens); - config_free_data(parser); /* Read one line (handling continuations with backslash) */ - line = get_line_with_continuation(parser->fp, &parser->lineno); - if (line == NULL) + if (get_line_with_continuation(parser) < 0) return 0; - parser->line = line; + + line = parser->line; /* Skip token in the start of line? */ if (flags & PARSE_TRIM) @@ -193,8 +187,10 @@ again: if (line[0] == '\0' || line[0] == delims[0]) goto again; - if (flags & PARSE_KEEP_COPY) + if (flags & PARSE_KEEP_COPY) { + free(parser->data); parser->data = xstrdup(line); + } /* Tokenize the line */ t = 0; @@ -240,8 +236,6 @@ again: parser->lineno, t, mintokens); if (flags & PARSE_MIN_DIE) xfunc_die(); - if (flags & PARSE_KEEP_COPY) - free(parser->data); goto again; } -- cgit v1.2.3-55-g6feb From 06448912e6af33a1ac95f985448f83cbd39bac44 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 20 Jun 2011 10:06:28 +0200 Subject: libbb: use _unlocked variants of stdio I/O. Signed-off-by: Denys Vlasenko --- include/libbb.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'include') diff --git a/include/libbb.h b/include/libbb.h index 953bec334..963ae1e47 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -160,6 +160,44 @@ int sysinfo(struct sysinfo* info); #endif +/* Busybox does not use threads, we can speed up stdio. + * But don't define foo to foo_unlocked if foo_unlocked + * is a macro (it might be defined back to foo!). + */ +#ifndef getc_unlocked +# undef getc +# define getc(stream) getc_unlocked(stream) +#endif +#ifndef getchar_unlocked +# undef getchar +# define getchar() getchar_unlocked() +#endif +#ifndef putc_unlocked +# undef putc +# define putc(c, stream) putc_unlocked(c, stream) +#endif +#ifndef putchar_unlocked +# undef putchar +# define putchar(c) putchar_unlocked(c) +#endif +#ifndef fgetc_unlocked +# undef fgetc +# define fgetc(stream) fgetc_unlocked(stream) +#endif +#ifndef fputc_unlocked +# undef fputc +# define fputc(c, stream) fputc_unlocked(c, stream) +#endif +#ifndef fgets_unlocked +# undef fgets +# define fgets(s, n, stream) fgets_unlocked(s, n, stream) +#endif +#ifndef fputs_unlocked +# undef fputs +# define fputs(s, stream) fputs_unlocked(s, stream) +#endif + + /* Make all declarations hidden (-fvisibility flag only affects definitions) */ /* (don't include system headers after this until corresponding pop!) */ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN -- cgit v1.2.3-55-g6feb From 561f9c8585e7cb0882472297f4fd2ed763dc085a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 21 Jun 2011 16:38:29 +0200 Subject: Add HAVE_STRVERSCMP, guard the only usage of strverscmp with #ifdef Signed-off-by: Denys Vlasenko --- coreutils/ls.c | 2 ++ include/platform.h | 5 +++++ 2 files changed, 7 insertions(+) (limited to 'include') diff --git a/coreutils/ls.c b/coreutils/ls.c index c42f5f3d2..6080f59b6 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -886,9 +886,11 @@ static int sortcmp(const void *a, const void *b) if (sort_opts == SORT_DIR) { dif = S_ISDIR(d2->dn_mode) - S_ISDIR(d1->dn_mode); } else +#ifdef HAVE_STRVERSCMP && HAVE_STRVERSCMP == 1 if (sort_opts == SORT_VERSION) { dif = strverscmp(d1->name, d2->name); } else +#endif if (sort_opts == SORT_EXT) { dif = strcmp(strchrnul(d1->name, '.'), strchrnul(d2->name, '.')); } diff --git a/include/platform.h b/include/platform.h index 429220b0d..d6b748844 100644 --- a/include/platform.h +++ b/include/platform.h @@ -348,6 +348,7 @@ typedef unsigned smalluint; #define HAVE_STRCHRNUL 1 #define HAVE_STRSEP 1 #define HAVE_STRSIGNAL 1 +#define HAVE_STRVERSCMP 1 #define HAVE_VASPRINTF 1 #define HAVE_XTABS 1 #define HAVE_MNTENT_H 1 @@ -372,6 +373,7 @@ typedef unsigned smalluint; # undef HAVE_STRCHRNUL # undef HAVE_STRSEP # undef HAVE_STRSIGNAL +# undef HAVE_STRVERSCMP # undef HAVE_VASPRINTF # undef HAVE_NET_ETHERNET_H #endif @@ -389,18 +391,21 @@ typedef unsigned smalluint; # undef HAVE_PTSNAME_R # undef HAVE_SYS_STATFS_H # undef HAVE_SIGHANDLER_T +# undef HAVE_STRVERSCMP # undef HAVE_XTABS # undef HAVE_DPRINTF #endif #if defined(__digital__) && defined(__unix__) # undef HAVE_STPCPY +# undef HAVE_STRVERSCMP #endif #if defined(ANDROID) # undef HAVE_DPRINTF # undef HAVE_STPCPY # undef HAVE_STRCHRNUL +# undef HAVE_STRVERSCMP # undef HAVE_NET_ETHERNET_H #endif -- cgit v1.2.3-55-g6feb From 33092f1003982fc26339c0fda66283805cfbcfb1 Mon Sep 17 00:00:00 2001 From: Tito Ragusa Date: Tue, 21 Jun 2011 17:11:40 +0200 Subject: groups: new applet Signed-off-by: Tito Ragusa Signed-off-by: Denys Vlasenko --- coreutils/Config.src | 6 ------ coreutils/Kbuild.src | 1 - coreutils/id.c | 40 ++++++++++++++++++++++++++++++++++++---- include/applets.src.h | 1 - 4 files changed, 36 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/coreutils/Config.src b/coreutils/Config.src index 1843e8b0e..9aa04e946 100644 --- a/coreutils/Config.src +++ b/coreutils/Config.src @@ -276,12 +276,6 @@ config HOSTID hostid prints the numeric identifier (in hexadecimal) for the current host. -config ID - bool "id" - default y - help - id displays the current user and group ID names. - config INSTALL bool "install" default y diff --git a/coreutils/Kbuild.src b/coreutils/Kbuild.src index 6a41c8318..0681e44b0 100644 --- a/coreutils/Kbuild.src +++ b/coreutils/Kbuild.src @@ -37,7 +37,6 @@ lib-$(CONFIG_FOLD) += fold.o lib-$(CONFIG_FSYNC) += fsync.o lib-$(CONFIG_HEAD) += head.o lib-$(CONFIG_HOSTID) += hostid.o -lib-$(CONFIG_ID) += id.o lib-$(CONFIG_INSTALL) += install.o #lib-$(CONFIG_LENGTH) += length.o lib-$(CONFIG_LN) += ln.o diff --git a/coreutils/id.c b/coreutils/id.c index aa6ae4711..71acce4a2 100644 --- a/coreutils/id.c +++ b/coreutils/id.c @@ -15,6 +15,24 @@ * Added -G option Tito Ragusa (C) 2008 for SUSv3. */ +//config:config ID +//config: bool "id" +//config: default y +//config: help +//config: id displays the current user and group ID names. + +//config:config GROUPS +//config: bool "groups" +//config: default y +//config: help +//config: Print the group names associated with current user id. + +//kbuild:lib-$(CONFIG_GROUPS) += id.o +//kbuild:lib-$(CONFIG_ID) += id.o + +//applet:IF_GROUPS(APPLET_ODDNAME(groups, id, BB_DIR_USR_BIN, BB_SUID_DROP, groups)) +//applet:IF_ID(APPLET_NOEXEC(id, id, BB_DIR_USR_BIN, BB_SUID_DROP, id)) + //usage:#define id_trivial_usage //usage: "[OPTIONS] [USER]" //usage:#define id_full_usage "\n\n" @@ -32,6 +50,15 @@ //usage: "$ id\n" //usage: "uid=1000(andersen) gid=1000(andersen)\n" +//usage:#define groups_trivial_usage +//usage: "[USER]" +//usage:#define groups_full_usage "\n\n" +//usage: "Print the group memberships of USER or for the current process" +//usage: +//usage:#define groups_example_usage +//usage: "$ groups\n" +//usage: "andersen lp dialout cdrom floppy\n" + #include "libbb.h" /* This is a NOEXEC applet. Be very careful! */ @@ -135,11 +162,16 @@ int id_main(int argc UNUSED_PARAM, char **argv) #if ENABLE_SELINUX security_context_t scontext = NULL; #endif - /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/ - /* Don't allow more than one username */ - opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG" + + if (ENABLE_GROUPS && (!ENABLE_ID || applet_name[0] == 'g')) { + option_mask32 = opt = getopt32(argv, "") | JUST_ALL_GROUPS | NAME_NOT_NUMBER; + } else { + /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/ + /* Don't allow more than one username */ + opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG" IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G"); - opt = getopt32(argv, "rnugG" IF_SELINUX("Z")); + opt = getopt32(argv, "rnugG" IF_SELINUX("Z")); + } username = argv[optind]; if (username) { diff --git a/include/applets.src.h b/include/applets.src.h index 133f376a3..c6d99fe8c 100644 --- a/include/applets.src.h +++ b/include/applets.src.h @@ -180,7 +180,6 @@ IF_HOSTID(APPLET_NOFORK(hostid, hostid, BB_DIR_USR_BIN, BB_SUID_DROP, hostid)) IF_HOSTNAME(APPLET(hostname, BB_DIR_BIN, BB_SUID_DROP)) IF_HTTPD(APPLET(httpd, BB_DIR_USR_SBIN, BB_SUID_DROP)) IF_HWCLOCK(APPLET(hwclock, BB_DIR_SBIN, BB_SUID_DROP)) -IF_ID(APPLET_NOEXEC(id, id, BB_DIR_USR_BIN, BB_SUID_DROP, id)) IF_IFCONFIG(APPLET(ifconfig, BB_DIR_SBIN, BB_SUID_DROP)) IF_IFUPDOWN(APPLET_ODDNAME(ifdown, ifupdown, BB_DIR_SBIN, BB_SUID_DROP, ifdown)) IF_IFENSLAVE(APPLET(ifenslave, BB_DIR_SBIN, BB_SUID_DROP)) -- cgit v1.2.3-55-g6feb From 1e18a01fa2965ef967b99d519018e21c9b5f15af Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 21 Jun 2011 17:12:52 +0200 Subject: ls: fix HAVE_STRVERSCMP check; add check for older uclibc versions Signed-off-by: Denys Vlasenko --- coreutils/ls.c | 2 +- include/platform.h | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/coreutils/ls.c b/coreutils/ls.c index 6080f59b6..f11eb4358 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -886,7 +886,7 @@ static int sortcmp(const void *a, const void *b) if (sort_opts == SORT_DIR) { dif = S_ISDIR(d2->dn_mode) - S_ISDIR(d1->dn_mode); } else -#ifdef HAVE_STRVERSCMP && HAVE_STRVERSCMP == 1 +#if defined(HAVE_STRVERSCMP) && HAVE_STRVERSCMP == 1 if (sort_opts == SORT_VERSION) { dif = strverscmp(d1->name, d2->name); } else diff --git a/include/platform.h b/include/platform.h index d6b748844..cbe85f469 100644 --- a/include/platform.h +++ b/include/platform.h @@ -359,6 +359,16 @@ typedef unsigned smalluint; # undef HAVE_NET_ETHERNET_H #endif +#if defined(__UCLIBC_MAJOR__) +# if __UCLIBC_MAJOR__ == 0 \ + && ( __UCLIBC_MINOR__ < 9 \ + || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 31) \ + ) +# undef HAVE_STRVERSCMP +# endif +#endif + + #if defined(__dietlibc__) # undef HAVE_STRCHRNUL #endif -- cgit v1.2.3-55-g6feb From b9ba580917b59e4770ba99db7c61288f2476eb61 Mon Sep 17 00:00:00 2001 From: Walter Harms Date: Mon, 27 Jun 2011 02:59:37 +0200 Subject: vi: fix regex search, make it selectable in config function old new delta char_search 134 214 +80 find_pair 187 169 -18 mycmp 37 - -37 Signed-off-by: Walter Harms Signed-off-by: Denys Vlasenko --- editors/Config.src | 117 -------------------------- editors/Kbuild.src | 1 - editors/vi.c | 221 +++++++++++++++++++++++++++++++++++++++++--------- include/applets.src.h | 1 - 4 files changed, 181 insertions(+), 159 deletions(-) (limited to 'include') diff --git a/editors/Config.src b/editors/Config.src index 201ee6eb9..af1e1de5e 100644 --- a/editors/Config.src +++ b/editors/Config.src @@ -67,123 +67,6 @@ config SED sed is used to perform text transformations on a file or input from a pipeline. -config VI - bool "vi" - default y - help - 'vi' is a text editor. More specifically, it is the One True - text editor . It does, however, have a rather steep - learning curve. If you are not already comfortable with 'vi' - you may wish to use something else. - -config FEATURE_VI_MAX_LEN - int "Maximum screen width in vi" - range 256 16384 - default 4096 - depends on VI - help - Contrary to what you may think, this is not eating much. - Make it smaller than 4k only if you are very limited on memory. - -config FEATURE_VI_8BIT - bool "Allow vi to display 8-bit chars (otherwise shows dots)" - default n - depends on VI - help - If your terminal can display characters with high bit set, - you may want to enable this. Note: vi is not Unicode-capable. - If your terminal combines several 8-bit bytes into one character - (as in Unicode mode), this will not work properly. - -config FEATURE_VI_COLON - bool "Enable \":\" colon commands (no \"ex\" mode)" - default y - depends on VI - help - Enable a limited set of colon commands for vi. This does not - provide an "ex" mode. - -config FEATURE_VI_YANKMARK - bool "Enable yank/put commands and mark cmds" - default y - depends on VI - help - This will enable you to use yank and put, as well as mark in - busybox vi. - -config FEATURE_VI_SEARCH - bool "Enable search and replace cmds" - default y - depends on VI - help - Select this if you wish to be able to do search and replace in - busybox vi. - -config FEATURE_VI_USE_SIGNALS - bool "Catch signals" - default y - depends on VI - help - Selecting this option will make busybox vi signal aware. This will - make busybox vi support SIGWINCH to deal with Window Changes, catch - Ctrl-Z and Ctrl-C and alarms. - -config FEATURE_VI_DOT_CMD - bool "Remember previous cmd and \".\" cmd" - default y - depends on VI - help - Make busybox vi remember the last command and be able to repeat it. - -config FEATURE_VI_READONLY - bool "Enable -R option and \"view\" mode" - default y - depends on VI - help - Enable the read-only command line option, which allows the user to - open a file in read-only mode. - -config FEATURE_VI_SETOPTS - bool "Enable set-able options, ai ic showmatch" - default y - depends on VI - help - Enable the editor to set some (ai, ic, showmatch) options. - -config FEATURE_VI_SET - bool "Support for :set" - default y - depends on VI - help - Support for ":set". - -config FEATURE_VI_WIN_RESIZE - bool "Handle window resize" - default y - depends on VI - help - Make busybox vi behave nicely with terminals that get resized. - -config FEATURE_VI_ASK_TERMINAL - bool "Use 'tell me cursor position' ESC sequence to measure window" - default y - depends on VI - help - If terminal size can't be retrieved and $LINES/$COLUMNS are not set, - this option makes vi perform a last-ditch effort to find it: - vi positions cursor to 999,999 and asks terminal to report real - cursor position using "ESC [ 6 n" escape sequence, then reads stdin. - - This is not clean but helps a lot on serial lines and such. - -config FEATURE_VI_OPTIMIZE_CURSOR - bool "Optimize cursor movement" - default y - depends on VI - help - This will make the cursor movement faster, but requires more memory - and it makes the applet a tiny bit larger. - config FEATURE_ALLOW_EXEC bool "Allow vi and awk to execute shell commands" default y diff --git a/editors/Kbuild.src b/editors/Kbuild.src index 2f23ae12f..8888cba12 100644 --- a/editors/Kbuild.src +++ b/editors/Kbuild.src @@ -12,4 +12,3 @@ lib-$(CONFIG_CMP) += cmp.o lib-$(CONFIG_DIFF) += diff.o lib-$(CONFIG_ED) += ed.o lib-$(CONFIG_SED) += sed.o -lib-$(CONFIG_VI) += vi.o diff --git a/editors/vi.c b/editors/vi.c index 3656fee95..3f4ccdcdd 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -21,6 +21,134 @@ * An "ex" line oriented mode- maybe using "cmdedit" */ +//config:config VI +//config: bool "vi" +//config: default y +//config: help +//config: 'vi' is a text editor. More specifically, it is the One True +//config: text editor . It does, however, have a rather steep +//config: learning curve. If you are not already comfortable with 'vi' +//config: you may wish to use something else. +//config: +//config:config FEATURE_VI_MAX_LEN +//config: int "Maximum screen width in vi" +//config: range 256 16384 +//config: default 4096 +//config: depends on VI +//config: help +//config: Contrary to what you may think, this is not eating much. +//config: Make it smaller than 4k only if you are very limited on memory. +//config: +//config:config FEATURE_VI_8BIT +//config: bool "Allow vi to display 8-bit chars (otherwise shows dots)" +//config: default n +//config: depends on VI +//config: help +//config: If your terminal can display characters with high bit set, +//config: you may want to enable this. Note: vi is not Unicode-capable. +//config: If your terminal combines several 8-bit bytes into one character +//config: (as in Unicode mode), this will not work properly. +//config: +//config:config FEATURE_VI_COLON +//config: bool "Enable \":\" colon commands (no \"ex\" mode)" +//config: default y +//config: depends on VI +//config: help +//config: Enable a limited set of colon commands for vi. This does not +//config: provide an "ex" mode. +//config: +//config:config FEATURE_VI_YANKMARK +//config: bool "Enable yank/put commands and mark cmds" +//config: default y +//config: depends on VI +//config: help +//config: This will enable you to use yank and put, as well as mark in +//config: busybox vi. +//config: +//config:config FEATURE_VI_SEARCH +//config: bool "Enable search and replace cmds" +//config: default y +//config: depends on VI +//config: help +//config: Select this if you wish to be able to do search and replace in +//config: busybox vi. +//config: +//config:config FEATURE_VI_REGEX_SEARCH +//config: bool "Enable regex in search and replace" +//config: default n # Uses GNU regex, which may be unavailable. FIXME +//config: depends on FEATURE_VI_SEARCH +//config: help +//config: Use extended regex search. +//config: +//config:config FEATURE_VI_USE_SIGNALS +//config: bool "Catch signals" +//config: default y +//config: depends on VI +//config: help +//config: Selecting this option will make busybox vi signal aware. This will +//config: make busybox vi support SIGWINCH to deal with Window Changes, catch +//config: Ctrl-Z and Ctrl-C and alarms. +//config: +//config:config FEATURE_VI_DOT_CMD +//config: bool "Remember previous cmd and \".\" cmd" +//config: default y +//config: depends on VI +//config: help +//config: Make busybox vi remember the last command and be able to repeat it. +//config: +//config:config FEATURE_VI_READONLY +//config: bool "Enable -R option and \"view\" mode" +//config: default y +//config: depends on VI +//config: help +//config: Enable the read-only command line option, which allows the user to +//config: open a file in read-only mode. +//config: +//config:config FEATURE_VI_SETOPTS +//config: bool "Enable set-able options, ai ic showmatch" +//config: default y +//config: depends on VI +//config: help +//config: Enable the editor to set some (ai, ic, showmatch) options. +//config: +//config:config FEATURE_VI_SET +//config: bool "Support for :set" +//config: default y +//config: depends on VI +//config: help +//config: Support for ":set". +//config: +//config:config FEATURE_VI_WIN_RESIZE +//config: bool "Handle window resize" +//config: default y +//config: depends on VI +//config: help +//config: Make busybox vi behave nicely with terminals that get resized. +//config: +//config:config FEATURE_VI_ASK_TERMINAL +//config: bool "Use 'tell me cursor position' ESC sequence to measure window" +//config: default y +//config: depends on VI +//config: help +//config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set, +//config: this option makes vi perform a last-ditch effort to find it: +//config: vi positions cursor to 999,999 and asks terminal to report real +//config: cursor position using "ESC [ 6 n" escape sequence, then reads stdin. +//config: +//config: This is not clean but helps a lot on serial lines and such. +//config: +//config:config FEATURE_VI_OPTIMIZE_CURSOR +//config: bool "Optimize cursor movement" +//config: default y +//config: depends on VI +//config: help +//config: This will make the cursor movement faster, but requires more memory +//config: and it makes the applet a tiny bit larger. + +//applet:IF_VI(APPLET(vi, BB_DIR_BIN, BB_SUID_DROP)) + +//kbuild:lib-$(CONFIG_VI) += vi.o + //usage:#define vi_trivial_usage //usage: "[OPTIONS] [FILE]..." //usage:#define vi_full_usage "\n\n" @@ -33,6 +161,7 @@ //usage: ) //usage: "\n -H Short help regarding available features" +#include #include "libbb.h" /* the CRASHME code is unmaintained, and doesn't currently build */ @@ -366,7 +495,6 @@ static void Hit_Return(void); #if ENABLE_FEATURE_VI_SEARCH static char *char_search(char *, const char *, int, int); // search for pattern starting at p -static int mycmp(const char *, const char *, int); // string cmp based in "ignorecase" #endif #if ENABLE_FEATURE_VI_COLON static char *get_one_address(char *, int *); // get colon addr, if present @@ -1561,48 +1689,16 @@ static char *new_screen(int ro, int co) } #if ENABLE_FEATURE_VI_SEARCH -static int mycmp(const char *s1, const char *s2, int len) -{ - if (ENABLE_FEATURE_VI_SETOPTS && ignorecase) { - return strncasecmp(s1, s2, len); - } - return strncmp(s1, s2, len); -} + +# if ENABLE_FEATURE_VI_REGEX_SEARCH // search for pattern starting at p static char *char_search(char *p, const char *pat, int dir, int range) { -#ifndef REGEX_SEARCH - char *start, *stop; - int len; - - len = strlen(pat); - if (dir == FORWARD) { - stop = end - 1; // assume range is p - end-1 - if (range == LIMITED) - stop = next_line(p); // range is to next line - for (start = p; start < stop; start++) { - if (mycmp(start, pat, len) == 0) { - return start; - } - } - } else if (dir == BACK) { - stop = text; // assume range is text - p - if (range == LIMITED) - stop = prev_line(p); // range is to prev line - for (start = p - len; start >= stop; start--) { - if (mycmp(start, pat, len) == 0) { - return start; - } - } - } - // pattern not found - return NULL; -#else /* REGEX_SEARCH */ char *q; struct re_pattern_buffer preg; int i; - int size, range; + int size; re_syntax_options = RE_SYNTAX_POSIX_EXTENDED; preg.translate = 0; @@ -1625,7 +1721,7 @@ static char *char_search(char *p, const char *pat, int dir, int range) // RANGE could be negative if we are searching backwards range = q - p; - q = re_compile_pattern(pat, strlen(pat), &preg); + q = (char *)re_compile_pattern(pat, strlen(pat), (struct re_pattern_buffer *)&preg); if (q != 0) { // The pattern was not compiled status_line_bold("bad search pattern: \"%s\": %s", pat, q); @@ -1659,8 +1755,53 @@ static char *char_search(char *p, const char *pat, int dir, int range) p = p - i; } return p; -#endif /* REGEX_SEARCH */ } + +# else + +# if ENABLE_FEATURE_VI_SETOPTS +static int mycmp(const char *s1, const char *s2, int len) +{ + if (ignorecase) { + return strncasecmp(s1, s2, len); + } + return strncmp(s1, s2, len); +} +# else +# define mycmp strncmp +# endif + +static char *char_search(char *p, const char *pat, int dir, int range) +{ + char *start, *stop; + int len; + + len = strlen(pat); + if (dir == FORWARD) { + stop = end - 1; // assume range is p - end-1 + if (range == LIMITED) + stop = next_line(p); // range is to next line + for (start = p; start < stop; start++) { + if (mycmp(start, pat, len) == 0) { + return start; + } + } + } else if (dir == BACK) { + stop = text; // assume range is text - p + if (range == LIMITED) + stop = prev_line(p); // range is to prev line + for (start = p - len; start >= stop; start--) { + if (mycmp(start, pat, len) == 0) { + return start; + } + } + } + // pattern not found + return NULL; +} + +# endif + #endif /* FEATURE_VI_SEARCH */ static char *char_insert(char *p, char c) // insert the char c at 'p' @@ -2022,8 +2163,8 @@ static void show_help(void) "\n\tNamed buffers with \"x" #endif #if ENABLE_FEATURE_VI_READONLY - "\n\tReadonly if vi is called as \"view\"" - "\n\tReadonly with -R command line arg" + //not implemented: "\n\tReadonly if vi is called as \"view\"" + //redundant: usage text says this too: "\n\tReadonly with -R command line arg" #endif #if ENABLE_FEATURE_VI_SET "\n\tSome colon mode commands with \':\'" diff --git a/include/applets.src.h b/include/applets.src.h index c6d99fe8c..2d55ffec7 100644 --- a/include/applets.src.h +++ b/include/applets.src.h @@ -399,7 +399,6 @@ IF_USLEEP(APPLET_NOFORK(usleep, usleep, BB_DIR_BIN, BB_SUID_DROP, usleep)) IF_UUDECODE(APPLET(uudecode, BB_DIR_USR_BIN, BB_SUID_DROP)) IF_UUENCODE(APPLET(uuencode, BB_DIR_USR_BIN, BB_SUID_DROP)) IF_VCONFIG(APPLET(vconfig, BB_DIR_SBIN, BB_SUID_DROP)) -IF_VI(APPLET(vi, BB_DIR_BIN, BB_SUID_DROP)) /* Needs to be run by root or be suid root - needs to change uid and gid: */ IF_VLOCK(APPLET(vlock, BB_DIR_USR_BIN, BB_SUID_REQUIRE)) IF_VOLNAME(APPLET(volname, BB_DIR_USR_BIN, BB_SUID_DROP)) -- cgit v1.2.3-55-g6feb From 0a5b310067abfde9bf74a42352fc421e1c27e2b9 Mon Sep 17 00:00:00 2001 From: Timo Teras Date: Wed, 29 Jun 2011 02:19:58 +0200 Subject: platform.c: provide getline implementation Signed-off-by: Timo Teras Signed-off-by: Denys Vlasenko --- include/platform.h | 5 +++++ libbb/platform.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) (limited to 'include') diff --git a/include/platform.h b/include/platform.h index cbe85f469..eafc3fcfe 100644 --- a/include/platform.h +++ b/include/platform.h @@ -350,6 +350,7 @@ typedef unsigned smalluint; #define HAVE_STRSIGNAL 1 #define HAVE_STRVERSCMP 1 #define HAVE_VASPRINTF 1 +#define HAVE_GETLINE 1 #define HAVE_XTABS 1 #define HAVE_MNTENT_H 1 #define HAVE_NET_ETHERNET_H 1 @@ -470,4 +471,8 @@ extern char *strsep(char **stringp, const char *delim) FAST_FUNC; extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC; #endif +#ifndef HAVE_GETLINE +extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC; +#endif + #endif diff --git a/libbb/platform.c b/libbb/platform.c index 04b8961de..2bf34f5bc 100644 --- a/libbb/platform.c +++ b/libbb/platform.c @@ -145,3 +145,32 @@ char* FAST_FUNC stpcpy(char *p, const char *to_add) return p; } #endif + +#ifndef HAVE_GETLINE +ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream) +{ + int ch; + char *line = *lineptr; + size_t alloced = *n; + size_t len = 0; + + do { + ch = fgetc(stream); + if (ch == EOF) + break; + if (len + 1 >= alloced) { + alloced += alloced/4 + 64; + line = xrealloc(line, alloced); + } + line[len++] = ch; + } while (ch != '\n'); + + if (len == 0) + return -1; + + line[len] = '\0'; + *lineptr = line; + *n = alloced; + return len; +} +#endif -- cgit v1.2.3-55-g6feb From 0e79e7bb420c58fa8e5d2d88c5d3a26f94c761af Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Tue, 28 Jun 2011 23:03:27 -0700 Subject: Use the built-in getline on more systems that don't have it Signed-off-by: Dan Fandrich Signed-off-by: Denys Vlasenko --- include/platform.h | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/platform.h b/include/platform.h index eafc3fcfe..d186e4911 100644 --- a/include/platform.h +++ b/include/platform.h @@ -376,6 +376,7 @@ typedef unsigned smalluint; #if defined(__WATCOMC__) # undef HAVE_DPRINTF +# undef HAVE_GETLINE # undef HAVE_MEMRCHR # undef HAVE_MKDTEMP # undef HAVE_SETBIT @@ -389,15 +390,13 @@ typedef unsigned smalluint; # undef HAVE_NET_ETHERNET_H #endif -#if defined(__FreeBSD__) -# undef HAVE_STRCHRNUL -#endif - +/* These BSD-derived OSes share many similarities */ #if (defined __digital__ && defined __unix__) \ || defined __APPLE__ \ || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ # undef HAVE_CLEARENV # undef HAVE_FDATASYNC +# undef HAVE_GETLINE # undef HAVE_MNTENT_H # undef HAVE_PTSNAME_R # undef HAVE_SYS_STATFS_H @@ -407,13 +406,21 @@ typedef unsigned smalluint; # undef HAVE_DPRINTF #endif +#if defined(__FreeBSD__) +# undef HAVE_STRCHRNUL +#endif + +#if defined(__NetBSD__) +# define HAVE_GETLINE 1 /* Recent NetBSD versions have getline() */ +#endif + #if defined(__digital__) && defined(__unix__) # undef HAVE_STPCPY -# undef HAVE_STRVERSCMP #endif #if defined(ANDROID) # undef HAVE_DPRINTF +# undef HAVE_GETLINE # undef HAVE_STPCPY # undef HAVE_STRCHRNUL # undef HAVE_STRVERSCMP @@ -472,6 +479,7 @@ extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC #endif #ifndef HAVE_GETLINE +#include /* for FILE */ extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC; #endif -- cgit v1.2.3-55-g6feb From 0c4cf42c1e4fbb5af8952cfbec926fe7d7b318d5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 30 Jun 2011 02:44:27 +0200 Subject: libbb.h: tweak _unlocked hack Signed-off-by: Denys Vlasenko --- include/libbb.h | 54 ++++++++++++++++++------------------------------------ 1 file changed, 18 insertions(+), 36 deletions(-) (limited to 'include') diff --git a/include/libbb.h b/include/libbb.h index 963ae1e47..efb925eaf 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -160,42 +160,24 @@ int sysinfo(struct sysinfo* info); #endif -/* Busybox does not use threads, we can speed up stdio. - * But don't define foo to foo_unlocked if foo_unlocked - * is a macro (it might be defined back to foo!). - */ -#ifndef getc_unlocked -# undef getc -# define getc(stream) getc_unlocked(stream) -#endif -#ifndef getchar_unlocked -# undef getchar -# define getchar() getchar_unlocked() -#endif -#ifndef putc_unlocked -# undef putc -# define putc(c, stream) putc_unlocked(c, stream) -#endif -#ifndef putchar_unlocked -# undef putchar -# define putchar(c) putchar_unlocked(c) -#endif -#ifndef fgetc_unlocked -# undef fgetc -# define fgetc(stream) fgetc_unlocked(stream) -#endif -#ifndef fputc_unlocked -# undef fputc -# define fputc(c, stream) fputc_unlocked(c, stream) -#endif -#ifndef fgets_unlocked -# undef fgets -# define fgets(s, n, stream) fgets_unlocked(s, n, stream) -#endif -#ifndef fputs_unlocked -# undef fputs -# define fputs(s, stream) fputs_unlocked(s, stream) -#endif +/* Busybox does not use threads, we can speed up stdio. */ +#undef getc +#define getc(stream) getc_unlocked(stream) +#undef getchar +#define getchar() getchar_unlocked() +#undef putc +#define putc(c, stream) putc_unlocked(c, stream) +#undef putchar +#define putchar(c) putchar_unlocked(c) +#undef fgetc +#define fgetc(stream) getc_unlocked(stream) +#undef fputc +#define fputc(c, stream) putc_unlocked(c, stream) +/* Above functions are required by POSIX.1-2008, below ones are extensions */ +#undef fgets +#define fgets(s, n, stream) fgets_unlocked(s, n, stream) +#undef fputs +#define fputs(s, stream) fputs_unlocked(s, stream) /* Make all declarations hidden (-fvisibility flag only affects definitions) */ -- cgit v1.2.3-55-g6feb From 75214cfe00c72c51c67c9ac2541f06b0540136f1 Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Thu, 30 Jun 2011 02:59:17 +0200 Subject: Use the _unlocked stdio macros only when they're all available Signed-off-by: Dan Fandrich Signed-off-by: Denys Vlasenko --- include/libbb.h | 36 ++++++++++++++++++++---------------- include/platform.h | 10 +++++----- 2 files changed, 25 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/include/libbb.h b/include/libbb.h index efb925eaf..1502272f6 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -161,23 +161,27 @@ int sysinfo(struct sysinfo* info); /* Busybox does not use threads, we can speed up stdio. */ -#undef getc -#define getc(stream) getc_unlocked(stream) -#undef getchar -#define getchar() getchar_unlocked() -#undef putc -#define putc(c, stream) putc_unlocked(c, stream) -#undef putchar -#define putchar(c) putchar_unlocked(c) -#undef fgetc -#define fgetc(stream) getc_unlocked(stream) -#undef fputc -#define fputc(c, stream) putc_unlocked(c, stream) +#ifdef HAVE_UNLOCKED_STDIO +# undef getc +# define getc(stream) getc_unlocked(stream) +# undef getchar +# define getchar() getchar_unlocked() +# undef putc +# define putc(c, stream) putc_unlocked(c, stream) +# undef putchar +# define putchar(c) putchar_unlocked(c) +# undef fgetc +# define fgetc(stream) getc_unlocked(stream) +# undef fputc +# define fputc(c, stream) putc_unlocked(c, stream) +#endif /* Above functions are required by POSIX.1-2008, below ones are extensions */ -#undef fgets -#define fgets(s, n, stream) fgets_unlocked(s, n, stream) -#undef fputs -#define fputs(s, stream) fputs_unlocked(s, stream) +#ifdef HAVE_UNLOCKED_LINE_OPS +# undef fgets +# define fgets(s, n, stream) fgets_unlocked(s, n, stream) +# undef fputs +# define fputs(s, stream) fputs_unlocked(s, stream) +#endif /* Make all declarations hidden (-fvisibility flag only affects definitions) */ diff --git a/include/platform.h b/include/platform.h index d186e4911..60864c929 100644 --- a/include/platform.h +++ b/include/platform.h @@ -350,16 +350,14 @@ typedef unsigned smalluint; #define HAVE_STRSIGNAL 1 #define HAVE_STRVERSCMP 1 #define HAVE_VASPRINTF 1 +#define HAVE_UNLOCKED_STDIO 1 +#define HAVE_UNLOCKED_LINE_OPS 1 #define HAVE_GETLINE 1 #define HAVE_XTABS 1 #define HAVE_MNTENT_H 1 #define HAVE_NET_ETHERNET_H 1 #define HAVE_SYS_STATFS_H 1 -#if defined(__GLIBC__) && (__GLIBC__ < 2 || __GLIBC_MINOR__ < 1) -# undef HAVE_NET_ETHERNET_H -#endif - #if defined(__UCLIBC_MAJOR__) # if __UCLIBC_MAJOR__ == 0 \ && ( __UCLIBC_MINOR__ < 9 \ @@ -369,7 +367,6 @@ typedef unsigned smalluint; # endif #endif - #if defined(__dietlibc__) # undef HAVE_STRCHRNUL #endif @@ -387,6 +384,8 @@ typedef unsigned smalluint; # undef HAVE_STRSIGNAL # undef HAVE_STRVERSCMP # undef HAVE_VASPRINTF +# undef HAVE_UNLOCKED_STDIO +# undef HAVE_UNLOCKED_LINE_OPS # undef HAVE_NET_ETHERNET_H #endif @@ -424,6 +423,7 @@ typedef unsigned smalluint; # undef HAVE_STPCPY # undef HAVE_STRCHRNUL # undef HAVE_STRVERSCMP +# undef HAVE_UNLOCKED_LINE_OPS # undef HAVE_NET_ETHERNET_H #endif -- cgit v1.2.3-55-g6feb