3
0

xreadlink.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * xreadlink.c - safe implementation of readlink.
  4. * Returns a NULL on failure...
  5. *
  6. * Licensed under GPLv2, see file LICENSE in this source tree.
  7. */
  8. #include "libbb.h"
  9. /*
  10. * NOTE: This function returns a malloced char* that you will have to free
  11. * yourself.
  12. */
  13. char* FAST_FUNC xmalloc_readlink(const char *path)
  14. {
  15. enum { GROWBY = 80 }; /* how large we will grow strings by */
  16. char *buf = NULL;
  17. int bufsize = 0, readsize = 0;
  18. do {
  19. bufsize += GROWBY;
  20. buf = xrealloc(buf, bufsize);
  21. readsize = readlink(path, buf, bufsize);
  22. if (readsize == -1) {
  23. free(buf);
  24. return NULL;
  25. }
  26. } while (bufsize < readsize + 1);
  27. buf[readsize] = '\0';
  28. return buf;
  29. }
  30. /*
  31. * This routine is not the same as realpath(), which
  32. * canonicalizes the given path completely. This routine only
  33. * follows trailing symlinks until a real file is reached and
  34. * returns its name. If the path ends in a dangling link or if
  35. * the target doesn't exist, the path is returned in any case.
  36. * Intermediate symlinks in the path are not expanded -- only
  37. * those at the tail.
  38. * A malloced char* is returned, which must be freed by the caller.
  39. */
  40. char* FAST_FUNC xmalloc_follow_symlinks(const char *path)
  41. {
  42. char *buf;
  43. char *lpc;
  44. char *linkpath;
  45. int bufsize;
  46. int looping = MAXSYMLINKS + 1;
  47. buf = xstrdup(path);
  48. goto jump_in;
  49. while (1) {
  50. linkpath = xmalloc_readlink(buf);
  51. if (!linkpath) {
  52. /* not a symlink, or doesn't exist */
  53. if (errno == EINVAL || errno == ENOENT)
  54. return buf;
  55. goto free_buf_ret_null;
  56. }
  57. if (!--looping) {
  58. free(linkpath);
  59. free_buf_ret_null:
  60. free(buf);
  61. return NULL;
  62. }
  63. if (*linkpath != '/') {
  64. bufsize += strlen(linkpath);
  65. buf = xrealloc(buf, bufsize);
  66. lpc = bb_get_last_path_component_strip(buf);
  67. strcpy(lpc, linkpath);
  68. free(linkpath);
  69. } else {
  70. free(buf);
  71. buf = linkpath;
  72. jump_in:
  73. bufsize = strlen(buf) + 1;
  74. }
  75. }
  76. }
  77. char* FAST_FUNC xmalloc_readlink_or_warn(const char *path)
  78. {
  79. char *buf = xmalloc_readlink(path);
  80. if (!buf) {
  81. /* EINVAL => "file: Invalid argument" => puzzled user */
  82. const char *errmsg = "not a symlink";
  83. int err = errno;
  84. if (err != EINVAL)
  85. errmsg = strerror(err);
  86. bb_error_msg("%s: cannot read link: %s", path, errmsg);
  87. }
  88. return buf;
  89. }
  90. char* FAST_FUNC xmalloc_realpath(const char *path)
  91. {
  92. #if defined(__GLIBC__) && !defined(__UCLIBC__)
  93. /* glibc provides a non-standard extension */
  94. /* new: POSIX.1-2008 specifies this behavior as well */
  95. return realpath(path, NULL);
  96. #else
  97. char buf[PATH_MAX+1];
  98. /* on error returns NULL (xstrdup(NULL) == NULL) */
  99. return xstrdup(realpath(path, buf));
  100. #endif
  101. }