aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Whitley <markw@lineo.com>2001-04-30 18:17:00 +0000
committerMark Whitley <markw@lineo.com>2001-04-30 18:17:00 +0000
commit8a633268ef478a31bd649d582ce07e9c26a4a03a (patch)
treea90a2bc3c3815f7f0e70f009b9b175736902a848
parent6f343944548c2e41fde4a376e805bf322a8e01c9 (diff)
downloadbusybox-w32-8a633268ef478a31bd649d582ce07e9c26a4a03a.tar.gz
busybox-w32-8a633268ef478a31bd649d582ce07e9c26a4a03a.tar.bz2
busybox-w32-8a633268ef478a31bd649d582ce07e9c26a4a03a.zip
Made new xreadlink function for libbb and changed applets to use it instead of
readlink(2).
-rw-r--r--Makefile4
-rw-r--r--applets/busybox.c17
-rw-r--r--archival/tar.c12
-rw-r--r--busybox.c17
-rw-r--r--coreutils/ls.c12
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/copy_file.c13
-rw-r--r--libbb/libbb.h1
-rw-r--r--libbb/xreadlink.c34
-rw-r--r--ls.c12
-rw-r--r--miscutils/readlink.c16
-rw-r--r--readlink.c16
-rw-r--r--tar.c12
13 files changed, 77 insertions, 90 deletions
diff --git a/Makefile b/Makefile
index 5713cdc68..91ed745c6 100644
--- a/Makefile
+++ b/Makefile
@@ -247,8 +247,8 @@ parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c print_file.c \
247process_escape_sequence.c read_package_field.c read_text_file_to_buffer.c \ 247process_escape_sequence.c read_package_field.c read_text_file_to_buffer.c \
248recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \ 248recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \
249syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \ 249syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \
250verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c interface.c \ 250verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xreadlink.c\
251remove_file.c last_char_is.c 251xregcomp.c interface.c remove_file.c last_char_is.c
252LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) 252LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
253LIBBB_CFLAGS = -I$(LIBBB) 253LIBBB_CFLAGS = -I$(LIBBB)
254ifneq ($(strip $(BB_SRC_DIR)),) 254ifneq ($(strip $(BB_SRC_DIR)),)
diff --git a/applets/busybox.c b/applets/busybox.c
index b4939e19d..badd53d79 100644
--- a/applets/busybox.c
+++ b/applets/busybox.c
@@ -37,21 +37,10 @@ typedef int (*__link_f)(const char *, const char *);
37 */ 37 */
38static char *busybox_fullpath() 38static char *busybox_fullpath()
39{ 39{
40 pid_t pid;
41 char path[256];
42 char proc[256]; 40 char proc[256];
43 int len; 41
44 42 sprintf(proc, "/proc/%d/exe", getpid());
45 pid = getpid(); 43 return xreadlink(proc);
46 sprintf(proc, "/proc/%d/exe", pid);
47 len = readlink(proc, path, 256);
48 if (len != -1) {
49 path[len] = 0;
50 } else {
51 perror_msg("%s", proc);
52 return NULL;
53 }
54 return strdup(path);
55} 44}
56 45
57/* create (sym)links for each applet */ 46/* create (sym)links for each applet */
diff --git a/archival/tar.c b/archival/tar.c
index c168564ca..4bf8004ea 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -921,16 +921,10 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
921 header.typeflag = LNKTYPE; 921 header.typeflag = LNKTYPE;
922 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname)); 922 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
923 } else if (S_ISLNK(statbuf->st_mode)) { 923 } else if (S_ISLNK(statbuf->st_mode)) {
924 int link_size=0; 924 char *lpath = xreadlink(real_name);
925 char buffer[BUFSIZ];
926 header.typeflag = SYMTYPE; 925 header.typeflag = SYMTYPE;
927 link_size = readlink(real_name, buffer, sizeof(buffer) - 1); 926 strncpy(header.linkname, lpath, sizeof(header.linkname));
928 if ( link_size < 0) { 927 free(lpath);
929 perror_msg("Error reading symlink '%s'", header.name);
930 return ( FALSE);
931 }
932 buffer[link_size] = '\0';
933 strncpy(header.linkname, buffer, sizeof(header.linkname));
934 } else if (S_ISDIR(statbuf->st_mode)) { 928 } else if (S_ISDIR(statbuf->st_mode)) {
935 header.typeflag = DIRTYPE; 929 header.typeflag = DIRTYPE;
936 strncat(header.name, "/", sizeof(header.name)); 930 strncat(header.name, "/", sizeof(header.name));
diff --git a/busybox.c b/busybox.c
index b4939e19d..badd53d79 100644
--- a/busybox.c
+++ b/busybox.c
@@ -37,21 +37,10 @@ typedef int (*__link_f)(const char *, const char *);
37 */ 37 */
38static char *busybox_fullpath() 38static char *busybox_fullpath()
39{ 39{
40 pid_t pid;
41 char path[256];
42 char proc[256]; 40 char proc[256];
43 int len; 41
44 42 sprintf(proc, "/proc/%d/exe", getpid());
45 pid = getpid(); 43 return xreadlink(proc);
46 sprintf(proc, "/proc/%d/exe", pid);
47 len = readlink(proc, path, 256);
48 if (len != -1) {
49 path[len] = 0;
50 } else {
51 perror_msg("%s", proc);
52 return NULL;
53 }
54 return strdup(path);
55} 44}
56 45
57/* create (sym)links for each applet */ 46/* create (sym)links for each applet */
diff --git a/coreutils/ls.c b/coreutils/ls.c
index d24ba9866..c13b225fa 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -577,7 +577,7 @@ static struct dnode **list_dir(char *path)
577/*----------------------------------------------------------------------*/ 577/*----------------------------------------------------------------------*/
578static int list_single(struct dnode *dn) 578static int list_single(struct dnode *dn)
579{ 579{
580 int i, len; 580 int i;
581 char scratch[BUFSIZ + 1]; 581 char scratch[BUFSIZ + 1];
582#ifdef BB_FEATURE_LS_TIMESTAMPS 582#ifdef BB_FEATURE_LS_TIMESTAMPS
583 char *filetime; 583 char *filetime;
@@ -688,16 +688,16 @@ static int list_single(struct dnode *dn)
688 break; 688 break;
689 case LIST_SYMLINK: 689 case LIST_SYMLINK:
690 if (S_ISLNK(dn->dstat.st_mode)) { 690 if (S_ISLNK(dn->dstat.st_mode)) {
691 len= readlink(dn->fullname, scratch, (sizeof scratch)-1); 691 char *lpath = xreadlink(dn->fullname);
692 if (len > 0) { 692 if (lpath) {
693 scratch[len]= '\0'; 693 printf(" -> %s", lpath);
694 printf(" -> %s", scratch);
695#ifdef BB_FEATURE_LS_FILETYPES 694#ifdef BB_FEATURE_LS_FILETYPES
696 if (!stat(dn->fullname, &info)) { 695 if (!stat(dn->fullname, &info)) {
697 append = append_char(info.st_mode); 696 append = append_char(info.st_mode);
698 } 697 }
699#endif 698#endif
700 column += len+4; 699 column += strlen(lpath) + 4;
700 free(lpath);
701 } 701 }
702 } 702 }
703 break; 703 break;
diff --git a/include/libbb.h b/include/libbb.h
index a53e647d3..d2f9a9567 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -217,6 +217,7 @@ int ask_confirmation(void);
217int klogctl(int type, char * b, int len); 217int klogctl(int type, char * b, int len);
218 218
219char *xgetcwd(char *cwd); 219char *xgetcwd(char *cwd);
220char *xreadlink(const char *path);
220char *concat_path_file(const char *path, const char *filename); 221char *concat_path_file(const char *path, const char *filename);
221int last_char_is(const char *s, const int c); 222int last_char_is(const char *s, const int c);
222 223
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index 2d18b6087..22684be74 100644
--- a/libbb/copy_file.c
+++ b/libbb/copy_file.c
@@ -196,19 +196,12 @@ int copy_file(const char *source, const char *dest, int flags)
196 return -1; 196 return -1;
197 } 197 }
198 } else if (S_ISLNK(source_stat.st_mode)) { 198 } else if (S_ISLNK(source_stat.st_mode)) {
199 int size; 199 char *lpath = xreadlink(source);
200 char buf[BUFSIZ + 1]; 200 if (symlink(lpath, dest) < 0) {
201
202 if ((size = readlink(source, buf, BUFSIZ)) < 0) {
203 perror_msg("cannot read `%s'", source);
204 return -1;
205 }
206 buf[size] = '\0';
207
208 if (symlink(buf, dest) < 0) {
209 perror_msg("cannot create symlink `%s'", dest); 201 perror_msg("cannot create symlink `%s'", dest);
210 return -1; 202 return -1;
211 } 203 }
204 free(lpath);
212 205
213#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) 206#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
214 if (flags & FILEUTILS_PRESERVE_STATUS) 207 if (flags & FILEUTILS_PRESERVE_STATUS)
diff --git a/libbb/libbb.h b/libbb/libbb.h
index a53e647d3..d2f9a9567 100644
--- a/libbb/libbb.h
+++ b/libbb/libbb.h
@@ -217,6 +217,7 @@ int ask_confirmation(void);
217int klogctl(int type, char * b, int len); 217int klogctl(int type, char * b, int len);
218 218
219char *xgetcwd(char *cwd); 219char *xgetcwd(char *cwd);
220char *xreadlink(const char *path);
220char *concat_path_file(const char *path, const char *filename); 221char *concat_path_file(const char *path, const char *filename);
221int last_char_is(const char *s, const int c); 222int last_char_is(const char *s, const int c);
222 223
diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c
new file mode 100644
index 000000000..66f63b883
--- /dev/null
+++ b/libbb/xreadlink.c
@@ -0,0 +1,34 @@
1/*
2 * xreadlink.c - safe implementation of readlink
3 */
4
5#include <stdio.h>
6
7/*
8 * NOTE: This function returns a malloced char* that you will have to free
9 * yourself. You have been warned.
10 */
11
12#include <unistd.h>
13#include "libbb.h"
14
15extern char *xreadlink(const char *path)
16{
17 static const int GROWBY = 80; /* how large we will grow strings by */
18
19 char *buf = NULL;
20 int bufsize = 0, readsize = 0;
21
22 do {
23 buf = xrealloc(buf, bufsize += GROWBY);
24 readsize = readlink(path, buf, bufsize); /* 1st try */
25 if (readsize == -1)
26 perror_msg("%s:%s", applet_name, path);
27 }
28 while (bufsize < readsize + 1);
29
30 buf[readsize] = '\0';
31
32 return buf;
33}
34
diff --git a/ls.c b/ls.c
index d24ba9866..c13b225fa 100644
--- a/ls.c
+++ b/ls.c
@@ -577,7 +577,7 @@ static struct dnode **list_dir(char *path)
577/*----------------------------------------------------------------------*/ 577/*----------------------------------------------------------------------*/
578static int list_single(struct dnode *dn) 578static int list_single(struct dnode *dn)
579{ 579{
580 int i, len; 580 int i;
581 char scratch[BUFSIZ + 1]; 581 char scratch[BUFSIZ + 1];
582#ifdef BB_FEATURE_LS_TIMESTAMPS 582#ifdef BB_FEATURE_LS_TIMESTAMPS
583 char *filetime; 583 char *filetime;
@@ -688,16 +688,16 @@ static int list_single(struct dnode *dn)
688 break; 688 break;
689 case LIST_SYMLINK: 689 case LIST_SYMLINK:
690 if (S_ISLNK(dn->dstat.st_mode)) { 690 if (S_ISLNK(dn->dstat.st_mode)) {
691 len= readlink(dn->fullname, scratch, (sizeof scratch)-1); 691 char *lpath = xreadlink(dn->fullname);
692 if (len > 0) { 692 if (lpath) {
693 scratch[len]= '\0'; 693 printf(" -> %s", lpath);
694 printf(" -> %s", scratch);
695#ifdef BB_FEATURE_LS_FILETYPES 694#ifdef BB_FEATURE_LS_FILETYPES
696 if (!stat(dn->fullname, &info)) { 695 if (!stat(dn->fullname, &info)) {
697 append = append_char(info.st_mode); 696 append = append_char(info.st_mode);
698 } 697 }
699#endif 698#endif
700 column += len+4; 699 column += strlen(lpath) + 4;
700 free(lpath);
701 } 701 }
702 } 702 }
703 break; 703 break;
diff --git a/miscutils/readlink.c b/miscutils/readlink.c
index 74196e11d..226649544 100644
--- a/miscutils/readlink.c
+++ b/miscutils/readlink.c
@@ -30,21 +30,17 @@
30int readlink_main(int argc, char **argv) 30int readlink_main(int argc, char **argv)
31{ 31{
32 char *buf = NULL; 32 char *buf = NULL;
33 int bufsize = 128, size = 128; 33
34 /* no options, no getopt */
34 35
35 if (argc != 2) 36 if (argc != 2)
36 show_usage(); 37 show_usage();
37 38
38 while (bufsize < size + 1) { 39 buf = xreadlink(argv[1]);
39 bufsize *= 2;
40 buf = xrealloc(buf, bufsize);
41 size = readlink(argv[1], buf, bufsize);
42 if (size == -1)
43 perror_msg_and_die("%s", argv[1]);
44 }
45
46 buf[size] = '\0';
47 puts(buf); 40 puts(buf);
41#ifdef BB_FEATURE_CLEAN_UP
42 free(buf);
43#endif
48 44
49 return EXIT_SUCCESS; 45 return EXIT_SUCCESS;
50} 46}
diff --git a/readlink.c b/readlink.c
index 74196e11d..226649544 100644
--- a/readlink.c
+++ b/readlink.c
@@ -30,21 +30,17 @@
30int readlink_main(int argc, char **argv) 30int readlink_main(int argc, char **argv)
31{ 31{
32 char *buf = NULL; 32 char *buf = NULL;
33 int bufsize = 128, size = 128; 33
34 /* no options, no getopt */
34 35
35 if (argc != 2) 36 if (argc != 2)
36 show_usage(); 37 show_usage();
37 38
38 while (bufsize < size + 1) { 39 buf = xreadlink(argv[1]);
39 bufsize *= 2;
40 buf = xrealloc(buf, bufsize);
41 size = readlink(argv[1], buf, bufsize);
42 if (size == -1)
43 perror_msg_and_die("%s", argv[1]);
44 }
45
46 buf[size] = '\0';
47 puts(buf); 40 puts(buf);
41#ifdef BB_FEATURE_CLEAN_UP
42 free(buf);
43#endif
48 44
49 return EXIT_SUCCESS; 45 return EXIT_SUCCESS;
50} 46}
diff --git a/tar.c b/tar.c
index c168564ca..4bf8004ea 100644
--- a/tar.c
+++ b/tar.c
@@ -921,16 +921,10 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
921 header.typeflag = LNKTYPE; 921 header.typeflag = LNKTYPE;
922 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname)); 922 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
923 } else if (S_ISLNK(statbuf->st_mode)) { 923 } else if (S_ISLNK(statbuf->st_mode)) {
924 int link_size=0; 924 char *lpath = xreadlink(real_name);
925 char buffer[BUFSIZ];
926 header.typeflag = SYMTYPE; 925 header.typeflag = SYMTYPE;
927 link_size = readlink(real_name, buffer, sizeof(buffer) - 1); 926 strncpy(header.linkname, lpath, sizeof(header.linkname));
928 if ( link_size < 0) { 927 free(lpath);
929 perror_msg("Error reading symlink '%s'", header.name);
930 return ( FALSE);
931 }
932 buffer[link_size] = '\0';
933 strncpy(header.linkname, buffer, sizeof(header.linkname));
934 } else if (S_ISDIR(statbuf->st_mode)) { 928 } else if (S_ISDIR(statbuf->st_mode)) {
935 header.typeflag = DIRTYPE; 929 header.typeflag = DIRTYPE;
936 strncat(header.name, "/", sizeof(header.name)); 930 strncat(header.name, "/", sizeof(header.name));