From 05e782ddd3dc58245c889529bb8aeeaddf24bf71 Mon Sep 17 00:00:00 2001 From: Matt Kraai Date: Thu, 1 Feb 2001 16:49:30 +0000 Subject: [PATCH] Fix wget error message and add (and use) chomp library function. --- archival/tar.c | 3 +-- busybox.h | 1 + editors/sed.c | 3 +-- findutils/grep.c | 3 +-- findutils/xargs.c | 3 +-- grep.c | 3 +-- hostname.c | 5 ++--- include/busybox.h | 1 + networking/hostname.c | 5 ++--- networking/wget.c | 3 ++- sed.c | 3 +-- tar.c | 3 +-- utility.c | 13 +++++++++++++ wget.c | 3 ++- xargs.c | 3 +-- 15 files changed, 31 insertions(+), 24 deletions(-) diff --git a/archival/tar.c b/archival/tar.c index 60744e8db..fb0fcc614 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -274,8 +274,7 @@ extern int tar_main(int argc, char **argv) while (fgets(file, sizeof(file), fileList) != NULL) { excludeList = xrealloc(excludeList, sizeof(char *) * (excludeListSize+2)); - if (file[strlen(file)-1] == '\n') - file[strlen(file)-1] = '\0'; + chomp(file); excludeList[excludeListSize] = xstrdup(file); /* Tack a NULL onto the end of the list */ excludeList[++excludeListSize] = NULL; diff --git a/busybox.h b/busybox.h index 5f9425a5b..e332ed412 100644 --- a/busybox.h +++ b/busybox.h @@ -152,6 +152,7 @@ extern char process_escape_sequence(char **ptr); extern char *get_last_path_component(char *path); extern FILE *wfopen(const char *path, const char *mode); extern FILE *xfopen(const char *path, const char *mode); +extern void chomp(char *s); #ifndef DMALLOC extern void *xmalloc (size_t size); diff --git a/editors/sed.c b/editors/sed.c index e0351c3b8..0ce9f45e0 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -436,8 +436,7 @@ static void load_cmd_file(char *filename) } /* eat trailing newline (if any) --if I don't do this, edit commands * (aic) will print an extra newline */ - if (line[strlen(line)-1] == '\n') - line[strlen(line)-1] = 0; + chomp(line); add_cmd_str(line); free(line); } diff --git a/findutils/grep.c b/findutils/grep.c index 320655bf4..fec8d0913 100644 --- a/findutils/grep.c +++ b/findutils/grep.c @@ -69,8 +69,7 @@ static void grep_file(FILE *file) int nmatches = 0; while ((line = get_line_from_file(file)) != NULL) { - if (line[strlen(line)-1] == '\n') - line[strlen(line)-1] = '\0'; + chomp(line); linenum++; ret = regexec(®ex, line, 0, NULL, 0); if (ret == 0 && !invert_search) { /* match */ diff --git a/findutils/xargs.c b/findutils/xargs.c index 89bcfc156..f05efd917 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c @@ -68,8 +68,7 @@ int xargs_main(int argc, char **argv) char *execstr = NULL; /* eat the newline off the filename. */ - if (file_to_act_on[strlen(file_to_act_on)-1] == '\n') - file_to_act_on[strlen(file_to_act_on)-1] = '\0'; + chomp(file_to_act_on); /* eat blank lines */ if (strlen(file_to_act_on) == 0) diff --git a/grep.c b/grep.c index 320655bf4..fec8d0913 100644 --- a/grep.c +++ b/grep.c @@ -69,8 +69,7 @@ static void grep_file(FILE *file) int nmatches = 0; while ((line = get_line_from_file(file)) != NULL) { - if (line[strlen(line)-1] == '\n') - line[strlen(line)-1] = '\0'; + chomp(line); linenum++; ret = regexec(®ex, line, 0, NULL, 0); if (ret == 0 && !invert_search) { /* match */ diff --git a/hostname.c b/hostname.c index 3dba64154..ce17ba91c 100644 --- a/hostname.c +++ b/hostname.c @@ -1,6 +1,6 @@ /* vi: set sw=4 ts=4: */ /* - * $Id: hostname.c,v 1.22 2001/01/31 19:00:20 kraai Exp $ + * $Id: hostname.c,v 1.23 2001/02/01 16:49:29 kraai Exp $ * Mini hostname implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -49,8 +49,7 @@ void do_sethostname(char *s, int isfile) f = xfopen(s, "r"); fgets(buf, 255, f); fclose(f); - if (buf[strlen(buf) - 1] == '\n') - buf[strlen(buf) - 1] = 0; + chomp(buf); if (sethostname(buf, strlen(buf)) < 0) perror_msg_and_die("sethostname"); } diff --git a/include/busybox.h b/include/busybox.h index 5f9425a5b..e332ed412 100644 --- a/include/busybox.h +++ b/include/busybox.h @@ -152,6 +152,7 @@ extern char process_escape_sequence(char **ptr); extern char *get_last_path_component(char *path); extern FILE *wfopen(const char *path, const char *mode); extern FILE *xfopen(const char *path, const char *mode); +extern void chomp(char *s); #ifndef DMALLOC extern void *xmalloc (size_t size); diff --git a/networking/hostname.c b/networking/hostname.c index 3dba64154..ce17ba91c 100644 --- a/networking/hostname.c +++ b/networking/hostname.c @@ -1,6 +1,6 @@ /* vi: set sw=4 ts=4: */ /* - * $Id: hostname.c,v 1.22 2001/01/31 19:00:20 kraai Exp $ + * $Id: hostname.c,v 1.23 2001/02/01 16:49:29 kraai Exp $ * Mini hostname implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -49,8 +49,7 @@ void do_sethostname(char *s, int isfile) f = xfopen(s, "r"); fgets(buf, 255, f); fclose(f); - if (buf[strlen(buf) - 1] == '\n') - buf[strlen(buf) - 1] = 0; + chomp(buf); if (sethostname(buf, strlen(buf)) < 0) perror_msg_and_die("sethostname"); } diff --git a/networking/wget.c b/networking/wget.c index 70f8d1b89..c134427e4 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -216,6 +216,7 @@ int wget_main(int argc, char **argv) /*FALLTHRU*/ default: close_and_delete_outfile(output, fname_out, do_continue); + chomp(buf); error_msg_and_die("server returned error %d: %s", atoi(s), buf); } @@ -532,7 +533,7 @@ progressmeter(int flag) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: wget.c,v 1.25 2001/01/31 19:00:21 kraai Exp $ + * $Id: wget.c,v 1.26 2001/02/01 16:49:30 kraai Exp $ */ diff --git a/sed.c b/sed.c index e0351c3b8..0ce9f45e0 100644 --- a/sed.c +++ b/sed.c @@ -436,8 +436,7 @@ static void load_cmd_file(char *filename) } /* eat trailing newline (if any) --if I don't do this, edit commands * (aic) will print an extra newline */ - if (line[strlen(line)-1] == '\n') - line[strlen(line)-1] = 0; + chomp(line); add_cmd_str(line); free(line); } diff --git a/tar.c b/tar.c index 60744e8db..fb0fcc614 100644 --- a/tar.c +++ b/tar.c @@ -274,8 +274,7 @@ extern int tar_main(int argc, char **argv) while (fgets(file, sizeof(file), fileList) != NULL) { excludeList = xrealloc(excludeList, sizeof(char *) * (excludeListSize+2)); - if (file[strlen(file)-1] == '\n') - file[strlen(file)-1] = '\0'; + chomp(file); excludeList[excludeListSize] = xstrdup(file); /* Tack a NULL onto the end of the list */ excludeList[++excludeListSize] = NULL; diff --git a/utility.c b/utility.c index 311926938..0a0e652af 100644 --- a/utility.c +++ b/utility.c @@ -1760,6 +1760,19 @@ char *format(unsigned long val, unsigned long hr) } #endif +#if defined(BB_GREP) || defined(BB_HOSTNAME) || defined(BB_SED) || defined(BB_TAR) || defined(BB_WGET) || defined(BB_XARGS) +void chomp(char *s) +{ + size_t len = strlen(s); + + if (len == 0) + return; + + if (s[len-1] == '\n') + s[len-1] = '\0'; +} +#endif + /* END CODE */ /* Local Variables: diff --git a/wget.c b/wget.c index 70f8d1b89..c134427e4 100644 --- a/wget.c +++ b/wget.c @@ -216,6 +216,7 @@ int wget_main(int argc, char **argv) /*FALLTHRU*/ default: close_and_delete_outfile(output, fname_out, do_continue); + chomp(buf); error_msg_and_die("server returned error %d: %s", atoi(s), buf); } @@ -532,7 +533,7 @@ progressmeter(int flag) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: wget.c,v 1.25 2001/01/31 19:00:21 kraai Exp $ + * $Id: wget.c,v 1.26 2001/02/01 16:49:30 kraai Exp $ */ diff --git a/xargs.c b/xargs.c index 89bcfc156..f05efd917 100644 --- a/xargs.c +++ b/xargs.c @@ -68,8 +68,7 @@ int xargs_main(int argc, char **argv) char *execstr = NULL; /* eat the newline off the filename. */ - if (file_to_act_on[strlen(file_to_act_on)-1] == '\n') - file_to_act_on[strlen(file_to_act_on)-1] = '\0'; + chomp(file_to_act_on); /* eat blank lines */ if (strlen(file_to_act_on) == 0)