aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authormarkw <markw@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-04-30 18:17:00 +0000
committermarkw <markw@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-04-30 18:17:00 +0000
commitdb1ea6ee76a5106c45398bdc17c2db04da99ecf7 (patch)
treea90a2bc3c3815f7f0e70f009b9b175736902a848 /libbb
parentc9a51e8d05d64580a0f9ccce5b77c5b4ad990541 (diff)
downloadbusybox-w32-db1ea6ee76a5106c45398bdc17c2db04da99ecf7.tar.gz
busybox-w32-db1ea6ee76a5106c45398bdc17c2db04da99ecf7.tar.bz2
busybox-w32-db1ea6ee76a5106c45398bdc17c2db04da99ecf7.zip
Made new xreadlink function for libbb and changed applets to use it instead of
readlink(2). git-svn-id: svn://busybox.net/trunk/busybox@2495 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r--libbb/copy_file.c13
-rw-r--r--libbb/libbb.h1
-rw-r--r--libbb/xreadlink.c34
3 files changed, 38 insertions, 10 deletions
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