[BusyBox] last_char_is, which... last_patch10
Vladimir N. Oleynik
dzo at simtreas.ru
Mon May 7 10:19:44 UTC 2001
Larry,
I found small error in your patch to which.
Complex patch N10 in attach.
(I act not absolutely fairly but and what to do, previous patch was ignored. ;-(
)
--w
vodz
-------------- next part --------------
diff -ru busybox.orig/cmdedit.c busybox/cmdedit.c
--- busybox.orig/cmdedit.c Mon Apr 23 19:28:28 2001
+++ busybox/cmdedit.c Wed May 2 16:10:09 2001
@@ -1499,7 +1499,7 @@
#ifdef TEST
const char *applet_name = "debug stuff usage";
-const char *memory_exhausted = "Memory exhausted";
+const char memory_exhausted[] = "Memory exhausted";
#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
#include <locale.h>
diff -ru busybox.orig/du.c busybox/du.c
--- busybox.orig/du.c Wed Apr 25 09:39:18 2001
+++ busybox/du.c Wed May 2 15:47:34 2001
@@ -77,7 +77,6 @@
{
struct stat statbuf;
long sum;
- int len;
if ((lstat(filename, &statbuf)) != 0) {
perror_msg("%s", filename);
@@ -96,6 +95,7 @@
if (S_ISDIR(statbuf.st_mode)) {
DIR *dir;
struct dirent *entry;
+ char *newfile;
dir = opendir(filename);
if (!dir) {
@@ -103,12 +103,11 @@
return 0;
}
- len = strlen(filename);
- if (filename[len - 1] == '/')
- filename[--len] = '\0';
+ newfile = (char *)last_char_is(filename, '/');
+ if (newfile)
+ *newfile = '\0';
while ((entry = readdir(dir))) {
- char *newfile;
char *name = entry->d_name;
if ((strcmp(name, "..") == 0)
diff -ru busybox.orig/libbb/chomp.c busybox/libbb/chomp.c
--- busybox.orig/libbb/chomp.c Fri Mar 23 20:02:05 2001
+++ busybox/libbb/chomp.c Wed May 2 15:10:51 2001
@@ -32,13 +32,10 @@
void chomp(char *s)
{
- size_t len = strlen(s);
-
- if (len == 0)
- return;
-
- if (s[len-1] == '\n')
- s[len-1] = '\0';
+ char *lc = (char *)last_char_is(s, '\n');
+
+ if(lc)
+ *lc = 0;
}
diff -ru busybox.orig/libbb/concat_path_file.c busybox/libbb/concat_path_file.c
--- busybox.orig/libbb/concat_path_file.c Mon May 7 19:58:07 2001
+++ busybox/libbb/concat_path_file.c Mon May 7 20:14:00 2001
@@ -11,16 +11,12 @@
extern char *concat_path_file(const char *path, const char *filename)
{
char *outbuf;
- int l;
- int flg_slash = 1;
-
- l = strlen(path);
- if (l>0 && path[l-1] == '/')
- flg_slash--;
- l += strlen(filename);
- if (l>0 && filename[0] == '/')
- flg_slash--;
- outbuf = xmalloc(l+1+flg_slash);
- sprintf(outbuf, (flg_slash ? "%s/%s" : "%s%s"), path, filename);
+ const char *lc;
+
+ lc = last_char_is(path, '/');
+ if (filename[0] == '/')
+ filename++;
+ outbuf = xmalloc(strlen(path)+strlen(filename)+1+(lc==NULL));
+ sprintf(outbuf, (lc==NULL ? "%s/%s" : "%s%s"), path, filename);
return outbuf;
}
diff -ru busybox.orig/libbb/last_char_is.c busybox/libbb/last_char_is.c
--- busybox.orig/libbb/last_char_is.c Thu Apr 26 19:56:47 2001
+++ busybox/libbb/last_char_is.c Wed May 2 15:05:09 2001
@@ -25,9 +25,10 @@
* 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)
+const char * last_char_is(const char *s, const int c)
{
- int l = strlen(s);
- if (l==0) return 0;
- return (s[l-1] == c);
+ const char *sret = s+strlen(s)-1;
+ if (sret>=s && *sret == c)
+ return sret;
+ else return NULL;
}
diff -ru busybox.orig/libbb/libbb.h busybox/libbb/libbb.h
--- busybox.orig/libbb/libbb.h Mon Apr 30 22:18:02 2001
+++ busybox/libbb/libbb.h Wed May 2 15:57:39 2001
@@ -219,7 +219,7 @@
char *xgetcwd(char *cwd);
char *xreadlink(const char *path);
char *concat_path_file(const char *path, const char *filename);
-int last_char_is(const char *s, const int c);
+const char * last_char_is(const char *s, const int c);
typedef struct ar_headers_s {
char *name;
@@ -270,17 +270,17 @@
};
extern const char *applet_name;
-extern const char * const full_version;
-extern const char * const name_too_long;
-extern const char * const omitting_directory;
-extern const char * const not_a_directory;
-extern const char * const memory_exhausted;
-extern const char * const invalid_date;
-extern const char * const invalid_option;
-extern const char * const io_error;
-extern const char * const dash_dash_help;
-extern const char * const write_error;
-extern const char * const too_few_args;
-extern const char * const name_longer_than_foo;
+extern const char full_version[];
+extern const char name_too_long[];
+extern const char omitting_directory[];
+extern const char not_a_directory[];
+extern const char memory_exhausted[];
+extern const char invalid_date[];
+extern const char invalid_option[];
+extern const char io_error[];
+extern const char dash_dash_help[];
+extern const char write_error[];
+extern const char too_few_args[];
+extern const char name_longer_than_foo[];
#endif /* __LIBBB_H__ */
diff -ru busybox.orig/libbb/messages.c busybox/libbb/messages.c
--- busybox.orig/libbb/messages.c Mon Apr 30 22:08:03 2001
+++ busybox/libbb/messages.c Wed May 2 15:56:40 2001
@@ -22,40 +22,39 @@
#include "libbb.h"
#ifdef L_full_version
- const char * const full_version = BB_BANNER " multi-call binary";
+ const char full_version[] = BB_BANNER " multi-call binary";
#endif
#ifdef L_name_too_long
- const char * const name_too_long = "file name too long";
+ const char name_too_long[] = "file name too long";
#endif
#ifdef L_omitting_directory
- const char * const omitting_directory = "%s: omitting directory";
+ const char omitting_directory[] = "%s: omitting directory";
#endif
#ifdef L_not_a_directory
- const char * const not_a_directory = "%s: not a directory";
+ const char not_a_directory[] = "%s: not a directory";
#endif
#ifdef L_memory_exhausted
- const char * const memory_exhausted = "memory exhausted";
+ const char memory_exhausted[] = "memory exhausted";
#endif
#ifdef L_invalid_date
- const char * const invalid_date = "invalid date `%s'";
+ const char invalid_date[] = "invalid date `%s'";
#endif
#ifdef L_invalid_option
- const char * const invalid_option = "invalid option -- %c";
+ const char invalid_option[] = "invalid option -- %c";
#endif
#ifdef L_io_error
- const char * const io_error = "%s: input/output error -- %s";
+ const char io_error[] = "%s: input/output error -- %s";
#endif
#ifdef L_dash_dash_help
- const char * const dash_dash_help = "--help";
+ const char dash_dash_help[] = "--help";
#endif
#ifdef L_write_error
- const char * const write_error = "Write Error";
+ const char write_error[] = "Write Error";
#endif
#ifdef L_too_few_args
- const char * const too_few_args = "too few arguments";
+ const char too_few_args[] = "too few arguments";
#endif
#ifdef L_name_longer_than_foo
- const char * const name_longer_than_foo = "Names longer than %d chars not supported.";
+ const char name_longer_than_foo[] = "Names longer than %d chars not supported.";
#endif
-
diff -ru busybox.orig/stty.c busybox/stty.c
--- busybox.orig/stty.c Fri Mar 2 23:00:54 2001
+++ busybox/stty.c Wed May 2 16:12:21 2001
@@ -1362,75 +1362,8 @@
}
#ifdef TEST
-unsigned long parse_number(const char *numstr,
- const struct suffix_mult *suffixes)
-{
- const struct suffix_mult *sm;
- unsigned long int ret;
- int len;
- char *end;
-
- ret = strtoul(numstr, &end, 10);
- if (numstr == end)
- error_msg_and_die("invalid number `%s'", numstr);
- while (end[0] != '\0') {
- sm = suffixes;
- while ( sm != 0 ) {
- if(sm->suffix) {
- len = strlen(sm->suffix);
- if (strncmp(sm->suffix, end, len) == 0) {
- ret *= sm->mult;
- end += len;
- break;
- }
- sm++;
-
- } else
- sm = 0;
- }
- if (sm == 0)
- error_msg_and_die("invalid number `%s'", numstr);
- }
- return ret;
-}
const char *applet_name = "stty";
-
-static void verror_msg(const char *s, va_list p)
-{
- fflush(stdout);
- fprintf(stderr, "%s: ", applet_name);
- vfprintf(stderr, s, p);
-}
-
-extern void error_msg_and_die(const char *s, ...)
-{
- va_list p;
-
- va_start(p, s);
- verror_msg(s, p);
- va_end(p);
- putc('\n', stderr);
- exit(EXIT_FAILURE);
-}
-
-static void vperror_msg(const char *s, va_list p)
-{
- int err=errno;
- verror_msg(s, p);
- if (*s) s = ": ";
- fprintf(stderr, "%s%s\n", s, strerror(err));
-}
-
-extern void perror_msg_and_die(const char *s, ...)
-{
- va_list p;
-
- va_start(p, s);
- vperror_msg(s, p);
- va_end(p);
- exit(EXIT_FAILURE);
-}
#endif
diff -ru busybox.orig/vi.c busybox/vi.c
--- busybox.orig/vi.c Thu Apr 26 19:56:47 2001
+++ busybox/vi.c Wed May 2 15:19:38 2001
@@ -1745,9 +1745,10 @@
while (isblnk(*buf))
buf++;
strcpy((char *) args, (char *) buf);
- if (last_char_is((char *)cmd,'!')) {
+ buf1 = (Byte *)last_char_is((char *)cmd, '!');
+ if (buf1) {
useforce = TRUE;
- cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of !
+ *buf1 = '\0'; // get rid of !
}
if (b >= 0) {
// if there is only one addr, then the addr
diff -ru busybox.orig/which.c busybox/which.c
--- busybox.orig/which.c Mon May 7 19:52:42 2001
+++ busybox/which.c Mon May 7 19:55:42 2001
@@ -54,8 +54,7 @@
found = 0;
for (i = 0; i < count; i++) {
char *buf;
- buf = concat_path_file(buf, path_n);
- buf = concat_path_file(buf, *argv);
+ buf = concat_path_file(path_n, *argv);
if (stat (buf, &filestat) == 0
&& filestat.st_mode & S_IXUSR)
{
@@ -63,6 +62,7 @@
found = 1;
break;
}
+ free(buf);
path_n += (strlen(path_n) + 1);
}
if (!found)
More information about the busybox
mailing list