Made new xreadlink function for libbb and changed applets to use it instead of

readlink(2).
This commit is contained in:
Mark Whitley 2001-04-30 18:17:00 +00:00
parent 6f34394454
commit 8a633268ef
13 changed files with 75 additions and 88 deletions

View File

@ -247,8 +247,8 @@ parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c print_file.c \
process_escape_sequence.c read_package_field.c read_text_file_to_buffer.c \ 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 \ 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 \ 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 \ verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xreadlink.c\
remove_file.c last_char_is.c xregcomp.c interface.c remove_file.c last_char_is.c
LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
LIBBB_CFLAGS = -I$(LIBBB) LIBBB_CFLAGS = -I$(LIBBB)
ifneq ($(strip $(BB_SRC_DIR)),) ifneq ($(strip $(BB_SRC_DIR)),)

View File

@ -37,21 +37,10 @@ typedef int (*__link_f)(const char *, const char *);
*/ */
static char *busybox_fullpath() static char *busybox_fullpath()
{ {
pid_t pid;
char path[256];
char proc[256]; char proc[256];
int len;
pid = getpid(); sprintf(proc, "/proc/%d/exe", getpid());
sprintf(proc, "/proc/%d/exe", pid); return xreadlink(proc);
len = readlink(proc, path, 256);
if (len != -1) {
path[len] = 0;
} else {
perror_msg("%s", proc);
return NULL;
}
return strdup(path);
} }
/* create (sym)links for each applet */ /* create (sym)links for each applet */

View File

@ -921,16 +921,10 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
header.typeflag = LNKTYPE; header.typeflag = LNKTYPE;
strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname)); strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
} else if (S_ISLNK(statbuf->st_mode)) { } else if (S_ISLNK(statbuf->st_mode)) {
int link_size=0; char *lpath = xreadlink(real_name);
char buffer[BUFSIZ];
header.typeflag = SYMTYPE; header.typeflag = SYMTYPE;
link_size = readlink(real_name, buffer, sizeof(buffer) - 1); strncpy(header.linkname, lpath, sizeof(header.linkname));
if ( link_size < 0) { free(lpath);
perror_msg("Error reading symlink '%s'", header.name);
return ( FALSE);
}
buffer[link_size] = '\0';
strncpy(header.linkname, buffer, sizeof(header.linkname));
} else if (S_ISDIR(statbuf->st_mode)) { } else if (S_ISDIR(statbuf->st_mode)) {
header.typeflag = DIRTYPE; header.typeflag = DIRTYPE;
strncat(header.name, "/", sizeof(header.name)); strncat(header.name, "/", sizeof(header.name));

View File

@ -37,21 +37,10 @@ typedef int (*__link_f)(const char *, const char *);
*/ */
static char *busybox_fullpath() static char *busybox_fullpath()
{ {
pid_t pid;
char path[256];
char proc[256]; char proc[256];
int len;
pid = getpid(); sprintf(proc, "/proc/%d/exe", getpid());
sprintf(proc, "/proc/%d/exe", pid); return xreadlink(proc);
len = readlink(proc, path, 256);
if (len != -1) {
path[len] = 0;
} else {
perror_msg("%s", proc);
return NULL;
}
return strdup(path);
} }
/* create (sym)links for each applet */ /* create (sym)links for each applet */

View File

@ -577,7 +577,7 @@ static struct dnode **list_dir(char *path)
/*----------------------------------------------------------------------*/ /*----------------------------------------------------------------------*/
static int list_single(struct dnode *dn) static int list_single(struct dnode *dn)
{ {
int i, len; int i;
char scratch[BUFSIZ + 1]; char scratch[BUFSIZ + 1];
#ifdef BB_FEATURE_LS_TIMESTAMPS #ifdef BB_FEATURE_LS_TIMESTAMPS
char *filetime; char *filetime;
@ -688,16 +688,16 @@ static int list_single(struct dnode *dn)
break; break;
case LIST_SYMLINK: case LIST_SYMLINK:
if (S_ISLNK(dn->dstat.st_mode)) { if (S_ISLNK(dn->dstat.st_mode)) {
len= readlink(dn->fullname, scratch, (sizeof scratch)-1); char *lpath = xreadlink(dn->fullname);
if (len > 0) { if (lpath) {
scratch[len]= '\0'; printf(" -> %s", lpath);
printf(" -> %s", scratch);
#ifdef BB_FEATURE_LS_FILETYPES #ifdef BB_FEATURE_LS_FILETYPES
if (!stat(dn->fullname, &info)) { if (!stat(dn->fullname, &info)) {
append = append_char(info.st_mode); append = append_char(info.st_mode);
} }
#endif #endif
column += len+4; column += strlen(lpath) + 4;
free(lpath);
} }
} }
break; break;

View File

@ -217,6 +217,7 @@ int ask_confirmation(void);
int klogctl(int type, char * b, int len); int klogctl(int type, char * b, int len);
char *xgetcwd(char *cwd); char *xgetcwd(char *cwd);
char *xreadlink(const char *path);
char *concat_path_file(const char *path, const char *filename); char *concat_path_file(const char *path, const char *filename);
int last_char_is(const char *s, const int c); int last_char_is(const char *s, const int c);

View File

@ -196,19 +196,12 @@ int copy_file(const char *source, const char *dest, int flags)
return -1; return -1;
} }
} else if (S_ISLNK(source_stat.st_mode)) { } else if (S_ISLNK(source_stat.st_mode)) {
int size; char *lpath = xreadlink(source);
char buf[BUFSIZ + 1]; if (symlink(lpath, dest) < 0) {
if ((size = readlink(source, buf, BUFSIZ)) < 0) {
perror_msg("cannot read `%s'", source);
return -1;
}
buf[size] = '\0';
if (symlink(buf, dest) < 0) {
perror_msg("cannot create symlink `%s'", dest); perror_msg("cannot create symlink `%s'", dest);
return -1; return -1;
} }
free(lpath);
#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
if (flags & FILEUTILS_PRESERVE_STATUS) if (flags & FILEUTILS_PRESERVE_STATUS)

View File

@ -217,6 +217,7 @@ int ask_confirmation(void);
int klogctl(int type, char * b, int len); int klogctl(int type, char * b, int len);
char *xgetcwd(char *cwd); char *xgetcwd(char *cwd);
char *xreadlink(const char *path);
char *concat_path_file(const char *path, const char *filename); char *concat_path_file(const char *path, const char *filename);
int last_char_is(const char *s, const int c); int last_char_is(const char *s, const int c);

34
libbb/xreadlink.c Normal file
View File

@ -0,0 +1,34 @@
/*
* xreadlink.c - safe implementation of readlink
*/
#include <stdio.h>
/*
* NOTE: This function returns a malloced char* that you will have to free
* yourself. You have been warned.
*/
#include <unistd.h>
#include "libbb.h"
extern char *xreadlink(const char *path)
{
static const int GROWBY = 80; /* how large we will grow strings by */
char *buf = NULL;
int bufsize = 0, readsize = 0;
do {
buf = xrealloc(buf, bufsize += GROWBY);
readsize = readlink(path, buf, bufsize); /* 1st try */
if (readsize == -1)
perror_msg("%s:%s", applet_name, path);
}
while (bufsize < readsize + 1);
buf[readsize] = '\0';
return buf;
}

12
ls.c
View File

@ -577,7 +577,7 @@ static struct dnode **list_dir(char *path)
/*----------------------------------------------------------------------*/ /*----------------------------------------------------------------------*/
static int list_single(struct dnode *dn) static int list_single(struct dnode *dn)
{ {
int i, len; int i;
char scratch[BUFSIZ + 1]; char scratch[BUFSIZ + 1];
#ifdef BB_FEATURE_LS_TIMESTAMPS #ifdef BB_FEATURE_LS_TIMESTAMPS
char *filetime; char *filetime;
@ -688,16 +688,16 @@ static int list_single(struct dnode *dn)
break; break;
case LIST_SYMLINK: case LIST_SYMLINK:
if (S_ISLNK(dn->dstat.st_mode)) { if (S_ISLNK(dn->dstat.st_mode)) {
len= readlink(dn->fullname, scratch, (sizeof scratch)-1); char *lpath = xreadlink(dn->fullname);
if (len > 0) { if (lpath) {
scratch[len]= '\0'; printf(" -> %s", lpath);
printf(" -> %s", scratch);
#ifdef BB_FEATURE_LS_FILETYPES #ifdef BB_FEATURE_LS_FILETYPES
if (!stat(dn->fullname, &info)) { if (!stat(dn->fullname, &info)) {
append = append_char(info.st_mode); append = append_char(info.st_mode);
} }
#endif #endif
column += len+4; column += strlen(lpath) + 4;
free(lpath);
} }
} }
break; break;

View File

@ -30,21 +30,17 @@
int readlink_main(int argc, char **argv) int readlink_main(int argc, char **argv)
{ {
char *buf = NULL; char *buf = NULL;
int bufsize = 128, size = 128;
/* no options, no getopt */
if (argc != 2) if (argc != 2)
show_usage(); show_usage();
while (bufsize < size + 1) { buf = xreadlink(argv[1]);
bufsize *= 2;
buf = xrealloc(buf, bufsize);
size = readlink(argv[1], buf, bufsize);
if (size == -1)
perror_msg_and_die("%s", argv[1]);
}
buf[size] = '\0';
puts(buf); puts(buf);
#ifdef BB_FEATURE_CLEAN_UP
free(buf);
#endif
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -30,21 +30,17 @@
int readlink_main(int argc, char **argv) int readlink_main(int argc, char **argv)
{ {
char *buf = NULL; char *buf = NULL;
int bufsize = 128, size = 128;
/* no options, no getopt */
if (argc != 2) if (argc != 2)
show_usage(); show_usage();
while (bufsize < size + 1) { buf = xreadlink(argv[1]);
bufsize *= 2;
buf = xrealloc(buf, bufsize);
size = readlink(argv[1], buf, bufsize);
if (size == -1)
perror_msg_and_die("%s", argv[1]);
}
buf[size] = '\0';
puts(buf); puts(buf);
#ifdef BB_FEATURE_CLEAN_UP
free(buf);
#endif
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

12
tar.c
View File

@ -921,16 +921,10 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
header.typeflag = LNKTYPE; header.typeflag = LNKTYPE;
strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname)); strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
} else if (S_ISLNK(statbuf->st_mode)) { } else if (S_ISLNK(statbuf->st_mode)) {
int link_size=0; char *lpath = xreadlink(real_name);
char buffer[BUFSIZ];
header.typeflag = SYMTYPE; header.typeflag = SYMTYPE;
link_size = readlink(real_name, buffer, sizeof(buffer) - 1); strncpy(header.linkname, lpath, sizeof(header.linkname));
if ( link_size < 0) { free(lpath);
perror_msg("Error reading symlink '%s'", header.name);
return ( FALSE);
}
buffer[link_size] = '\0';
strncpy(header.linkname, buffer, sizeof(header.linkname));
} else if (S_ISDIR(statbuf->st_mode)) { } else if (S_ISDIR(statbuf->st_mode)) {
header.typeflag = DIRTYPE; header.typeflag = DIRTYPE;
strncat(header.name, "/", sizeof(header.name)); strncat(header.name, "/", sizeof(header.name));