Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame^] | 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 | |
| 15 | extern 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 | |