xreadlink.c 2.4 KB

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