summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griebl <griebl@gmx.de>2002-07-19 00:05:54 +0000
committerRobert Griebl <griebl@gmx.de>2002-07-19 00:05:54 +0000
commitd378c3149c6c24e7788f04a6d20ba360f3ea407e (patch)
tree945ff88bbbde5a32c2b3342b22d801be90ea40f2
parent88947dd05e28a3b793b16dfd6db1b5414ca99017 (diff)
downloadbusybox-w32-d378c3149c6c24e7788f04a6d20ba360f3ea407e.tar.gz
busybox-w32-d378c3149c6c24e7788f04a6d20ba360f3ea407e.tar.bz2
busybox-w32-d378c3149c6c24e7788f04a6d20ba360f3ea407e.zip
Applied vodz' patches #49 and #50 (with a small correction in runshell.c)
#49: I found one memory overflow and memory leak in "ln" applet. Last patch reduced also 54 bytes. ;) #50: I found bug in loginutils/Makefile.in. New patch have also new function to libbb and aplied this to applets and other cosmetic changes.
-rw-r--r--Makefile2
-rw-r--r--archival/gunzip.c3
-rw-r--r--archival/libunarchive/get_header_ar.c3
-rw-r--r--archival/libunarchive/unarchive.c4
-rw-r--r--coreutils/expr.c10
-rw-r--r--coreutils/ln.c32
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/Makefile.in2
-rw-r--r--libbb/concat_path_file.c3
-rw-r--r--libbb/run_shell.c5
-rw-r--r--libbb/xconnect.c1
-rw-r--r--libbb/xreadlink.c3
-rw-r--r--loginutils/Makefile.in2
-rw-r--r--loginutils/getty.c11
-rw-r--r--loginutils/passwd.c2
-rw-r--r--modutils/insmod.c29
-rw-r--r--shell/cmdedit.c3
-rw-r--r--util-linux/getopt.c26
18 files changed, 64 insertions, 79 deletions
diff --git a/Makefile b/Makefile
index a112e5781..1e1eddbc5 100644
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,7 @@ endif
47 47
48 48
49busybox: depend $(libraries-y) 49busybox: depend $(libraries-y)
50 $(CC) $(LDFLAGS) $(libraries-y) $(LIBRARIES) -o $@ 50 $(CC) $(LDFLAGS) -o $@ $(libraries-y) $(LIBRARIES)
51 $(STRIPCMD) $@ 51 $(STRIPCMD) $@
52 52
53busybox.links: applets/busybox.mkll 53busybox.links: applets/busybox.mkll
diff --git a/archival/gunzip.c b/archival/gunzip.c
index 83ed5e84a..4ab197f09 100644
--- a/archival/gunzip.c
+++ b/archival/gunzip.c
@@ -119,8 +119,7 @@ static int gunzip_file (const char *path, int flags)
119 } else { 119 } else {
120 error_msg_and_die("Invalid extension"); 120 error_msg_and_die("Invalid extension");
121 } 121 }
122 out_path = (char *) xcalloc(sizeof(char), length + 1); 122 out_path = xstrndup(path, length);
123 strncpy(out_path, path, length);
124 123
125 /* Open output file */ 124 /* Open output file */
126 out_file = xfopen(out_path, "w"); 125 out_file = xfopen(out_path, "w");
diff --git a/archival/libunarchive/get_header_ar.c b/archival/libunarchive/get_header_ar.c
index 1618b767f..f172fa7c9 100644
--- a/archival/libunarchive/get_header_ar.c
+++ b/archival/libunarchive/get_header_ar.c
@@ -91,8 +91,7 @@ file_header_t *get_header_ar(FILE *src_stream)
91 } 91 }
92 } else { 92 } else {
93 /* short filenames */ 93 /* short filenames */
94 typed->name = xcalloc(1, 16); 94 typed->name = xstrndup(ar.formated.name, 16);
95 strncpy(typed->name, ar.formated.name, 16);
96 } 95 }
97 typed->name[strcspn(typed->name, " /")]='\0'; 96 typed->name[strcspn(typed->name, " /")]='\0';
98 97
diff --git a/archival/libunarchive/unarchive.c b/archival/libunarchive/unarchive.c
index 312b498a1..03e3c3ec2 100644
--- a/archival/libunarchive/unarchive.c
+++ b/archival/libunarchive/unarchive.c
@@ -58,9 +58,7 @@ char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *f
58 return(NULL); 58 return(NULL);
59 } 59 }
60 } 60 }
61 full_name = xmalloc(strlen(prefix) + strlen(path) + 1); 61 bb_asprintf(&full_name, "%s%s", prefix, path);
62 strcpy(full_name, prefix);
63 strcat(full_name, path);
64 } else { 62 } else {
65 full_name = file_entry->name; 63 full_name = file_entry->name;
66 } 64 }
diff --git a/coreutils/expr.c b/coreutils/expr.c
index 0299cc73a..55ae3a969 100644
--- a/coreutils/expr.c
+++ b/coreutils/expr.c
@@ -146,12 +146,8 @@ static int null (VALUE *v)
146 146
147static void tostring (VALUE *v) 147static void tostring (VALUE *v)
148{ 148{
149 char *temp;
150
151 if (v->type == integer) { 149 if (v->type == integer) {
152 temp = xmalloc (4 * (sizeof (int) / sizeof (char))); 150 bb_asprintf (&(v->u.s), "%d", v->u.i);
153 sprintf (temp, "%d", v->u.i);
154 v->u.s = temp;
155 v->type = string; 151 v->type = string;
156 } 152 }
157} 153}
@@ -377,9 +373,7 @@ static VALUE *eval6 (void)
377 else { 373 else {
378 v = xmalloc (sizeof(VALUE)); 374 v = xmalloc (sizeof(VALUE));
379 v->type = string; 375 v->type = string;
380 v->u.s = strncpy ((char *) xmalloc (i2->u.i + 1), 376 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
381 l->u.s + i1->u.i - 1, i2->u.i);
382 v->u.s[i2->u.i] = 0;
383 } 377 }
384 freev (l); 378 freev (l);
385 freev (i1); 379 freev (i1);
diff --git a/coreutils/ln.c b/coreutils/ln.c
index 1eb853d2f..427ffcc6e 100644
--- a/coreutils/ln.c
+++ b/coreutils/ln.c
@@ -43,45 +43,47 @@ static int fs_link(const char *link_destname, const char *link_srcname,
43{ 43{
44 int status; 44 int status;
45 int src_is_dir; 45 int src_is_dir;
46 char *src_name; 46 char *src_name = 0;
47 const char *src;
47 48
48 if (link_destname==NULL) 49 if (link_destname==NULL)
49 return(FALSE); 50 return(FALSE);
50 51
51 src_name = (char *) xmalloc(strlen(link_srcname)+strlen(link_destname)+1);
52
53 if (link_srcname==NULL) 52 if (link_srcname==NULL)
54 strcpy(src_name, link_destname); 53 src = link_destname;
55 else 54 else
56 strcpy(src_name, link_srcname); 55 src = link_srcname;
57 56
58 if (flag&LN_NODEREFERENCE) 57 if (flag&LN_NODEREFERENCE)
59 src_is_dir = is_directory(src_name, TRUE, NULL); 58 src_is_dir = is_directory(src, TRUE, NULL);
60 else 59 else
61 src_is_dir = is_directory(src_name, FALSE, NULL); 60 src_is_dir = is_directory(src, FALSE, NULL);
62 61
63 if ((src_is_dir==TRUE)&&((flag&LN_NODEREFERENCE)==0)) { 62 if ((src_is_dir==TRUE)&&((flag&LN_NODEREFERENCE)==0)) {
64 char* srcdir_name; 63 char* srcdir_name;
65 64
66 srcdir_name = xstrdup(link_destname); 65 srcdir_name = xstrdup(link_destname);
67 strcat(src_name, "/"); 66 src_name = concat_path_file(src, get_last_path_component(srcdir_name));
68 strcat(src_name, get_last_path_component(srcdir_name)); 67 src = src_name;
69 free(srcdir_name); 68 free(srcdir_name);
70 } 69 }
71 70
72 if (flag&LN_FORCE) 71 if (flag&LN_FORCE)
73 unlink(src_name); 72 unlink(src);
74 73
75 if (flag&LN_SYMLINK) 74 if (flag&LN_SYMLINK)
76 status = symlink(link_destname, src_name); 75 status = symlink(link_destname, src);
77 else 76 else
78 status = link(link_destname, src_name); 77 status = link(link_destname, src);
79 78
80 if (status != 0) { 79 if (status != 0) {
81 perror_msg(src_name); 80 perror_msg(src);
82 return(FALSE); 81 status = FALSE;
82 } else {
83 status = TRUE;
83 } 84 }
84 return(TRUE); 85 free(src_name);
86 return status;
85} 87}
86 88
87extern int ln_main(int argc, char **argv) 89extern int ln_main(int argc, char **argv)
diff --git a/include/libbb.h b/include/libbb.h
index 02babb884..55a0ff809 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -321,6 +321,8 @@ void reset_ino_dev_hashtable(void);
321extern size_t xstrlen(const char *string); 321extern size_t xstrlen(const char *string);
322#define strlen(x) xstrlen(x) 322#define strlen(x) xstrlen(x)
323 323
324void bb_asprintf(char **string_ptr, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
325
324 326
325#define FAIL_DELAY 3 327#define FAIL_DELAY 3
326extern void change_identity ( const struct passwd *pw ); 328extern void change_identity ( const struct passwd *pw );
diff --git a/libbb/Makefile.in b/libbb/Makefile.in
index 70cc26dc1..e7ca9aa55 100644
--- a/libbb/Makefile.in
+++ b/libbb/Makefile.in
@@ -41,7 +41,7 @@ LIBBB_SRC:= \
41 simplify_path.c inet_common.c inode_hash.c obscure.c pwd2spwd.c xfuncs.c \ 41 simplify_path.c inet_common.c inode_hash.c obscure.c pwd2spwd.c xfuncs.c \
42 correct_password.c change_identity.c setup_environment.c run_shell.c \ 42 correct_password.c change_identity.c setup_environment.c run_shell.c \
43 pw_encrypt.c restricted_shell.c xgethostbyname2.c create_icmp6_socket.c \ 43 pw_encrypt.c restricted_shell.c xgethostbyname2.c create_icmp6_socket.c \
44 xconnect.c 44 xconnect.c bb_asprintf.c
45 45
46LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) 46LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC))
47 47
diff --git a/libbb/concat_path_file.c b/libbb/concat_path_file.c
index e62b99ef6..0146606a1 100644
--- a/libbb/concat_path_file.c
+++ b/libbb/concat_path_file.c
@@ -38,8 +38,7 @@ extern char *concat_path_file(const char *path, const char *filename)
38 lc = last_char_is(path, '/'); 38 lc = last_char_is(path, '/');
39 while (*filename == '/') 39 while (*filename == '/')
40 filename++; 40 filename++;
41 outbuf = xmalloc(strlen(path)+strlen(filename)+1+(lc==NULL)); 41 bb_asprintf(&outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename);
42 sprintf(outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename);
43 42
44 return outbuf; 43 return outbuf;
45} 44}
diff --git a/libbb/run_shell.c b/libbb/run_shell.c
index 30050fecb..b26eba115 100644
--- a/libbb/run_shell.c
+++ b/libbb/run_shell.c
@@ -60,9 +60,8 @@ void run_shell ( const char *shell, int loginshell, const char *command, const c
60 args [0] = get_last_path_component ( xstrdup ( shell )); 60 args [0] = get_last_path_component ( xstrdup ( shell ));
61 61
62 if ( loginshell ) { 62 if ( loginshell ) {
63 char *args0 = xmalloc ( xstrlen ( args [0] ) + 2 ); 63 char *args0;
64 args0 [0] = '-'; 64 bb_asprintf ( &args0, "-%s", args [0] );
65 strcpy ( args0 + 1, args [0] );
66 args [0] = args0; 65 args [0] = args0;
67 } 66 }
68 67
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index d6d144f3f..9e771495d 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -6,6 +6,7 @@
6 * 6 *
7 */ 7 */
8 8
9#include "inet_common.h"
9#include <unistd.h> 10#include <unistd.h>
10#include <string.h> 11#include <string.h>
11#include <stdlib.h> 12#include <stdlib.h>
diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c
index 932e487a5..9944b5129 100644
--- a/libbb/xreadlink.c
+++ b/libbb/xreadlink.c
@@ -24,7 +24,7 @@ extern char *xreadlink(const char *path)
24 buf = xrealloc(buf, bufsize += GROWBY); 24 buf = xrealloc(buf, bufsize += GROWBY);
25 readsize = readlink(path, buf, bufsize); /* 1st try */ 25 readsize = readlink(path, buf, bufsize); /* 1st try */
26 if (readsize == -1) { 26 if (readsize == -1) {
27 perror_msg("%s:%s", applet_name, path); 27 perror_msg("%s", path);
28 return NULL; 28 return NULL;
29 } 29 }
30 } 30 }
@@ -34,4 +34,3 @@ extern char *xreadlink(const char *path)
34 34
35 return buf; 35 return buf;
36} 36}
37
diff --git a/loginutils/Makefile.in b/loginutils/Makefile.in
index cb6452c39..1c5680aa5 100644
--- a/loginutils/Makefile.in
+++ b/loginutils/Makefile.in
@@ -40,7 +40,7 @@ needcrypt-$(CONFIG_LOGIN) := y
40needcrypt-$(CONFIG_SU) := y 40needcrypt-$(CONFIG_SU) := y
41 41
42ifeq ($(needcrypt-y),y) 42ifeq ($(needcrypt-y),y)
43 libraries-y +=-lcrypt 43 LIBRARIES += -lcrypt
44endif 44endif
45 45
46$(LOGINUTILS_DIR)$(LOGINUTILS_AR): $(patsubst %,$(LOGINUTILS_DIR)%, $(LOGINUTILS-y)) 46$(LOGINUTILS_DIR)$(LOGINUTILS_AR): $(patsubst %,$(LOGINUTILS_DIR)%, $(LOGINUTILS-y))
diff --git a/loginutils/getty.c b/loginutils/getty.c
index 2144c95ff..0176d621c 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -232,7 +232,7 @@ static void termio_final(struct options *op, struct termio *tp,
232 struct chardata *cp); 232 struct chardata *cp);
233static int caps_lock(const char *s); 233static int caps_lock(const char *s);
234static int bcode(const char *s); 234static int bcode(const char *s);
235static void error(const char *fmt, ...); 235static void error(const char *fmt, ...) __attribute__ ((noreturn));
236 236
237/* The following is used for understandable diagnostics. */ 237/* The following is used for understandable diagnostics. */
238 238
@@ -299,8 +299,7 @@ int getty_main(int argc, char **argv)
299 int iv; 299 int iv;
300 300
301 iv = getpid(); 301 iv = getpid();
302 if (ioctl(0, TIOCSPGRP, &iv) < 0) 302 ioctl(0, TIOCSPGRP, &iv);
303 perror_msg("ioctl() TIOCSPGRP call failed");
304 } 303 }
305#endif 304#endif
306 /* Initialize the termio settings (raw mode, eight-bit, blocking i/o). */ 305 /* Initialize the termio settings (raw mode, eight-bit, blocking i/o). */
@@ -368,7 +367,6 @@ int getty_main(int argc, char **argv)
368 367
369 (void) execl(options.login, options.login, "--", logname, (char *) 0); 368 (void) execl(options.login, options.login, "--", logname, (char *) 0);
370 error("%s: can't exec %s: %m", options.tty, options.login); 369 error("%s: can't exec %s: %m", options.tty, options.login);
371 return (0); /* quiet GCC */
372} 370}
373 371
374/* parse-args - parse command-line arguments */ 372/* parse-args - parse command-line arguments */
@@ -382,10 +380,9 @@ static void parse_args(int argc, char **argv, struct options *op)
382 while (isascii(c = getopt(argc, argv, "I:LH:f:hil:mt:wn"))) { 380 while (isascii(c = getopt(argc, argv, "I:LH:f:hil:mt:wn"))) {
383 switch (c) { 381 switch (c) {
384 case 'I': 382 case 'I':
385 if (!(op->initstring = strdup(optarg))) { 383 if (!(op->initstring = strdup(optarg)))
386 error("can't malloc initstring"); 384 error("can't malloc initstring");
387 break; 385
388 }
389 { 386 {
390 char ch, *p, *q; 387 char ch, *p, *q;
391 int i; 388 int i;
diff --git a/loginutils/passwd.c b/loginutils/passwd.c
index 079791c4c..c8756211a 100644
--- a/loginutils/passwd.c
+++ b/loginutils/passwd.c
@@ -173,7 +173,7 @@ extern int passwd_main(int argc, char **argv)
173 ruid = getuid(); 173 ruid = getuid();
174 pw = (struct passwd *) getpwuid(ruid); 174 pw = (struct passwd *) getpwuid(ruid);
175 if (!pw) { 175 if (!pw) {
176 error_msg_and_die("Cannot determine your user name.\n"); 176 error_msg_and_die("Cannot determine your user name.");
177 } 177 }
178 myname = (char *) xstrdup(pw->pw_name); 178 myname = (char *) xstrdup(pw->pw_name);
179 if (optind < argc) { 179 if (optind < argc) {
diff --git a/modutils/insmod.c b/modutils/insmod.c
index 5a40e4199..4f2e9a779 100644
--- a/modutils/insmod.c
+++ b/modutils/insmod.c
@@ -233,7 +233,7 @@
233#ifndef MODUTILS_MODULE_H 233#ifndef MODUTILS_MODULE_H
234static const int MODUTILS_MODULE_H = 1; 234static const int MODUTILS_MODULE_H = 1;
235 235
236#ident "$Id: insmod.c,v 1.87 2002/07/02 19:14:23 andersen Exp $" 236#ident "$Id: insmod.c,v 1.88 2002/07/19 00:05:48 sandman Exp $"
237 237
238/* This file contains the structures used by the 2.0 and 2.1 kernels. 238/* This file contains the structures used by the 2.0 and 2.1 kernels.
239 We do not use the kernel headers directly because we do not wish 239 We do not use the kernel headers directly because we do not wish
@@ -454,7 +454,7 @@ int delete_module(const char *);
454#ifndef MODUTILS_OBJ_H 454#ifndef MODUTILS_OBJ_H
455static const int MODUTILS_OBJ_H = 1; 455static const int MODUTILS_OBJ_H = 1;
456 456
457#ident "$Id: insmod.c,v 1.87 2002/07/02 19:14:23 andersen Exp $" 457#ident "$Id: insmod.c,v 1.88 2002/07/19 00:05:48 sandman Exp $"
458 458
459/* The relocatable object is manipulated using elfin types. */ 459/* The relocatable object is manipulated using elfin types. */
460 460
@@ -740,7 +740,7 @@ static int n_ext_modules_used;
740extern int delete_module(const char *); 740extern int delete_module(const char *);
741 741
742static char *m_filename; 742static char *m_filename;
743static char m_fullName[FILENAME_MAX]; 743static char *m_fullName;
744 744
745 745
746 746
@@ -3503,10 +3503,8 @@ extern int insmod_main( int argc, char **argv)
3503 tmp[len] = '\0'; 3503 tmp[len] = '\0';
3504 } 3504 }
3505 3505
3506 if (len > (sizeof(m_fullName)-3)) 3506 bb_asprintf(&m_fullName, "%s.o", tmp, ".o");
3507 error_msg_and_die("%s: module name too long", tmp);
3508 3507
3509 strcat(strcpy(m_fullName, tmp), ".o");
3510 if (!m_name) { 3508 if (!m_name) {
3511 m_name = tmp; 3509 m_name = tmp;
3512 } else { 3510 } else {
@@ -3522,25 +3520,32 @@ extern int insmod_main( int argc, char **argv)
3522 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`, 3520 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3523 * but do not error out yet if we fail to find it... */ 3521 * but do not error out yet if we fail to find it... */
3524 if (uname(&myuname) == 0) { 3522 if (uname(&myuname) == 0) {
3525 char module_dir[FILENAME_MAX]; 3523 char *module_dir;
3524 char *tmdn;
3526 char real_module_dir[FILENAME_MAX]; 3525 char real_module_dir[FILENAME_MAX];
3527 snprintf (module_dir, sizeof(module_dir), "%s/%s", 3526
3528 _PATH_MODULES, myuname.release); 3527 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
3529 /* Jump through hoops in case /lib/modules/`uname -r` 3528 /* Jump through hoops in case /lib/modules/`uname -r`
3530 * is a symlink. We do not want recursive_action to 3529 * is a symlink. We do not want recursive_action to
3531 * follow symlinks, but we do want to follow the 3530 * follow symlinks, but we do want to follow the
3532 * /lib/modules/`uname -r` dir, So resolve it ourselves 3531 * /lib/modules/`uname -r` dir, So resolve it ourselves
3533 * if it is a link... */ 3532 * if it is a link... */
3534 if (realpath (module_dir, real_module_dir) == NULL) 3533 if (realpath (tmdn, real_module_dir) == NULL)
3535 strcpy(real_module_dir, module_dir); 3534 module_dir = tmdn;
3536 recursive_action(real_module_dir, TRUE, FALSE, FALSE, 3535 else
3536 module_dir = real_module_dir;
3537 recursive_action(module_dir, TRUE, FALSE, FALSE,
3537 check_module_name_match, 0, m_fullName); 3538 check_module_name_match, 0, m_fullName);
3539 free(tmdn);
3538 } 3540 }
3539 3541
3540 /* Check if we have found anything yet */ 3542 /* Check if we have found anything yet */
3541 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) 3543 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
3542 { 3544 {
3543 char module_dir[FILENAME_MAX]; 3545 char module_dir[FILENAME_MAX];
3546
3547 free(m_filename);
3548 m_filename = 0;
3544 if (realpath (_PATH_MODULES, module_dir) == NULL) 3549 if (realpath (_PATH_MODULES, module_dir) == NULL)
3545 strcpy(module_dir, _PATH_MODULES); 3550 strcpy(module_dir, _PATH_MODULES);
3546 /* No module found under /lib/modules/`uname -r`, this 3551 /* No module found under /lib/modules/`uname -r`, this
diff --git a/shell/cmdedit.c b/shell/cmdedit.c
index c5f2e9da2..5cbc81131 100644
--- a/shell/cmdedit.c
+++ b/shell/cmdedit.c
@@ -611,8 +611,7 @@ static char **username_tab_completion(char *ud, int *num_matches)
611 /* Null usernames should result in all users as possible completions. */ 611 /* Null usernames should result in all users as possible completions. */
612 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) { 612 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
613 613
614 temp = xmalloc(3 + strlen(entry->pw_name)); 614 bb_asprintf(&temp, "~%s/", entry->pw_name);
615 sprintf(temp, "~%s/", entry->pw_name);
616 matches = xrealloc(matches, (nm + 1) * sizeof(char *)); 615 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
617 616
618 matches[nm++] = temp; 617 matches[nm++] = temp;
diff --git a/util-linux/getopt.c b/util-linux/getopt.c
index 95ecba6e6..2390f3056 100644
--- a/util-linux/getopt.c
+++ b/util-linux/getopt.c
@@ -96,9 +96,7 @@ const char *normalize(const char *arg)
96 free(BUFFER); 96 free(BUFFER);
97 97
98 if (!quote) { /* Just copy arg */ 98 if (!quote) { /* Just copy arg */
99 BUFFER=xmalloc(strlen(arg)+1); 99 BUFFER=xstrdup(arg);
100
101 strcpy(BUFFER,arg);
102 return BUFFER; 100 return BUFFER;
103 } 101 }
104 102
@@ -204,7 +202,6 @@ static const int LONG_OPTIONS_INCR = 10;
204/* Register a long option. The contents of name is copied. */ 202/* Register a long option. The contents of name is copied. */
205void add_longopt(const char *name,int has_arg) 203void add_longopt(const char *name,int has_arg)
206{ 204{
207 char *tmp;
208 if (!name) { /* init */ 205 if (!name) { /* init */
209 free(long_options); 206 free(long_options);
210 long_options=NULL; 207 long_options=NULL;
@@ -228,9 +225,7 @@ void add_longopt(const char *name,int has_arg)
228 long_options[long_options_nr-1].has_arg=has_arg; 225 long_options[long_options_nr-1].has_arg=has_arg;
229 long_options[long_options_nr-1].flag=NULL; 226 long_options[long_options_nr-1].flag=NULL;
230 long_options[long_options_nr-1].val=LONG_OPT; 227 long_options[long_options_nr-1].val=LONG_OPT;
231 tmp = xmalloc(strlen(name)+1); 228 long_options[long_options_nr-1].name=xstrdup(name);
232 strcpy(tmp,name);
233 long_options[long_options_nr-1].name=tmp;
234 } 229 }
235 long_options_nr++; 230 long_options_nr++;
236} 231}
@@ -326,7 +321,7 @@ int getopt_main(int argc, char *argv[])
326 /* For some reason, the original getopt gave no error 321 /* For some reason, the original getopt gave no error
327 when there were no arguments. */ 322 when there were no arguments. */
328 printf(" --\n"); 323 printf(" --\n");
329 exit(0); 324 return 0;
330 } else 325 } else
331 error_msg_and_die("missing optstring argument"); 326 error_msg_and_die("missing optstring argument");
332 } 327 }
@@ -336,7 +331,7 @@ int getopt_main(int argc, char *argv[])
336 optstr=xmalloc(strlen(argv[1])+1); 331 optstr=xmalloc(strlen(argv[1])+1);
337 strcpy(optstr,argv[1]+strspn(argv[1],"-+")); 332 strcpy(optstr,argv[1]+strspn(argv[1],"-+"));
338 argv[1]=argv[0]; 333 argv[1]=argv[0];
339 exit(generate_output(argv+1,argc-1,optstr,long_options)); 334 return (generate_output(argv+1,argc-1,optstr,long_options));
340 } 335 }
341 336
342 while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF) 337 while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF)
@@ -347,8 +342,7 @@ int getopt_main(int argc, char *argv[])
347 case 'o': 342 case 'o':
348 if (optstr) 343 if (optstr)
349 free(optstr); 344 free(optstr);
350 optstr=xmalloc(strlen(optarg)+1); 345 optstr=xstrdup(optarg);
351 strcpy(optstr,optarg);
352 break; 346 break;
353 case 'l': 347 case 'l':
354 add_long_options(optarg); 348 add_long_options(optarg);
@@ -356,8 +350,7 @@ int getopt_main(int argc, char *argv[])
356 case 'n': 350 case 'n':
357 if (name) 351 if (name)
358 free(name); 352 free(name);
359 name=xmalloc(strlen(optarg)+1); 353 name=xstrdup(optarg);
360 strcpy(name,optarg);
361 break; 354 break;
362 case 'q': 355 case 'q':
363 quiet_errors=1; 356 quiet_errors=1;
@@ -369,7 +362,7 @@ int getopt_main(int argc, char *argv[])
369 set_shell(optarg); 362 set_shell(optarg);
370 break; 363 break;
371 case 'T': 364 case 'T':
372 exit(4); 365 return 4;
373 case 'u': 366 case 'u':
374 quote=0; 367 quote=0;
375 break; 368 break;
@@ -381,8 +374,7 @@ int getopt_main(int argc, char *argv[])
381 if (optind >= argc) 374 if (optind >= argc)
382 error_msg_and_die("missing optstring argument"); 375 error_msg_and_die("missing optstring argument");
383 else { 376 else {
384 optstr=xmalloc(strlen(argv[optind])+1); 377 optstr=xstrdup(argv[optind]);
385 strcpy(optstr,argv[optind]);
386 optind++; 378 optind++;
387 } 379 }
388 } 380 }
@@ -390,7 +382,7 @@ int getopt_main(int argc, char *argv[])
390 argv[optind-1]=name; 382 argv[optind-1]=name;
391 else 383 else
392 argv[optind-1]=argv[0]; 384 argv[optind-1]=argv[0];
393 exit(generate_output(argv+optind-1,argc-optind+1,optstr,long_options)); 385 return (generate_output(argv+optind-1,argc-optind+1,optstr,long_options));
394} 386}
395 387
396/* 388/*