diff --git a/Makefile b/Makefile index 11cec64b1..dcf6bd641 100644 --- a/Makefile +++ b/Makefile @@ -248,7 +248,7 @@ process_escape_sequence.c read_package_field.c read_text_file_to_buffer.c \ recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \ syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \ verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c interface.c \ -remove_file.c +remove_file.c last_char_is.c LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) LIBBB_CFLAGS = -I$(LIBBB) ifneq ($(strip $(BB_SRC_DIR)),) diff --git a/archival/dpkg.c b/archival/dpkg.c index e5ed95cb8..996809a6f 100644 --- a/archival/dpkg.c +++ b/archival/dpkg.c @@ -583,7 +583,7 @@ static int status_merge(void *status, package_t *pkgs) */ if ((fin = fopen(statusfile, "r")) != NULL) { while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { - line[strlen(line) - 1] = '\0'; /* trim newline */ + chomp(line); /* trim newline */ /* If we see a package header, find out if it's a package * that we have processed. if so, we skip that block for * now (write it at the end). diff --git a/archival/tar.c b/archival/tar.c index 48284c00a..716f4ac30 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -706,7 +706,7 @@ static int readTarFile(int tarFd, int extractFlag, int listFlag, case REGTYPE0: /* If the name ends in a '/' then assume it is * supposed to be a directory, and fall through */ - if (header.name[strlen(header.name)-1] != '/') { + if (last_char_is(header.name,'/')) { if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) errorFlag=TRUE; break; diff --git a/coreutils/cut.c b/coreutils/cut.c index 7e9a72e3f..d852ab3be 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c @@ -75,7 +75,7 @@ static void decompose_list(const char *list) /* handle multi-value cases */ else if (nminus == 1) { /* handle 'N-' case */ - if (list[strlen(list) - 1] == '-') { + if (last_char_is(list,'-')) { startpos = strtol(list, &ptr, 10); } /* handle '-M' case */ diff --git a/cut.c b/cut.c index 7e9a72e3f..d852ab3be 100644 --- a/cut.c +++ b/cut.c @@ -75,7 +75,7 @@ static void decompose_list(const char *list) /* handle multi-value cases */ else if (nminus == 1) { /* handle 'N-' case */ - if (list[strlen(list) - 1] == '-') { + if (last_char_is(list,'-')) { startpos = strtol(list, &ptr, 10); } /* handle '-M' case */ diff --git a/dpkg.c b/dpkg.c index e5ed95cb8..996809a6f 100644 --- a/dpkg.c +++ b/dpkg.c @@ -583,7 +583,7 @@ static int status_merge(void *status, package_t *pkgs) */ if ((fin = fopen(statusfile, "r")) != NULL) { while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { - line[strlen(line) - 1] = '\0'; /* trim newline */ + chomp(line); /* trim newline */ /* If we see a package header, find out if it's a package * that we have processed. if so, we skip that block for * now (write it at the end). diff --git a/editors/vi.c b/editors/vi.c index 6a93fc1fe..96fc96559 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -19,7 +19,7 @@ */ char *vi_Version = - "$Id: vi.c,v 1.4 2001/04/16 15:46:44 andersen Exp $"; + "$Id: vi.c,v 1.5 2001/04/26 15:56:47 andersen Exp $"; /* * To compile for standalone use: @@ -1745,7 +1745,7 @@ static void colon(Byte * buf) while (isblnk(*buf)) buf++; strcpy((char *) args, (char *) buf); - if (cmd[strlen((char *) cmd) - 1] == '!') { + if (last_char_is((char *)cmd,'!')) { useforce = TRUE; cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of ! } diff --git a/include/libbb.h b/include/libbb.h index 2937b4863..0ceb983cd 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -218,6 +218,7 @@ int klogctl(int type, char * b, int len); char *xgetcwd(char *cwd); char *concat_path_file(const char *path, const char *filename); +int last_char_is(const char *s, const int c); typedef struct ar_headers_s { char *name; diff --git a/libbb/last_char_is.c b/libbb/last_char_is.c new file mode 100644 index 000000000..b4bb7ec32 --- /dev/null +++ b/libbb/last_char_is.c @@ -0,0 +1,33 @@ +/* + * busybox library eXtended function + * + * Copyright (C) 2001 Larry Doolittle, + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "libbb.h" + +/* Find out if the last character of a string matches the one given Don't + * underrun the buffer if the string length is 0. Also avoids a possible + * space-hogging inline of strlen() per usage. + */ +int last_char_is(const char *s, const int c) +{ + int l = strlen(s); + if (l==0) return 0; + return (s[l-1] == c); +} diff --git a/libbb/libbb.h b/libbb/libbb.h index 2937b4863..0ceb983cd 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h @@ -218,6 +218,7 @@ int klogctl(int type, char * b, int len); char *xgetcwd(char *cwd); char *concat_path_file(const char *path, const char *filename); +int last_char_is(const char *s, const int c); typedef struct ar_headers_s { char *name; diff --git a/tar.c b/tar.c index 48284c00a..716f4ac30 100644 --- a/tar.c +++ b/tar.c @@ -706,7 +706,7 @@ static int readTarFile(int tarFd, int extractFlag, int listFlag, case REGTYPE0: /* If the name ends in a '/' then assume it is * supposed to be a directory, and fall through */ - if (header.name[strlen(header.name)-1] != '/') { + if (last_char_is(header.name,'/')) { if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) errorFlag=TRUE; break; diff --git a/vi.c b/vi.c index 6a93fc1fe..96fc96559 100644 --- a/vi.c +++ b/vi.c @@ -19,7 +19,7 @@ */ char *vi_Version = - "$Id: vi.c,v 1.4 2001/04/16 15:46:44 andersen Exp $"; + "$Id: vi.c,v 1.5 2001/04/26 15:56:47 andersen Exp $"; /* * To compile for standalone use: @@ -1745,7 +1745,7 @@ static void colon(Byte * buf) while (isblnk(*buf)) buf++; strcpy((char *) args, (char *) buf); - if (cmd[strlen((char *) cmd) - 1] == '!') { + if (last_char_is((char *)cmd,'!')) { useforce = TRUE; cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of ! }