3
0

pwd.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini pwd implementation for busybox
  4. *
  5. * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config PWD
  10. //config: bool "pwd (3.7 kb)"
  11. //config: default y
  12. //config: help
  13. //config: pwd is used to print the current directory.
  14. //applet:IF_PWD(APPLET_NOFORK(pwd, pwd, BB_DIR_BIN, BB_SUID_DROP, pwd))
  15. //kbuild:lib-$(CONFIG_PWD) += pwd.o
  16. //usage:#define pwd_trivial_usage
  17. //usage: ""
  18. //usage:#define pwd_full_usage "\n\n"
  19. //usage: "Print the full filename of the current working directory"
  20. //usage:
  21. //usage:#define pwd_example_usage
  22. //usage: "$ pwd\n"
  23. //usage: "/root\n"
  24. #include "libbb.h"
  25. static int logical_getcwd(void)
  26. {
  27. struct stat st1;
  28. struct stat st2;
  29. char *wd;
  30. char *p;
  31. wd = getenv("PWD");
  32. if (!wd || wd[0] != '/')
  33. return 0;
  34. p = wd;
  35. while (*p) {
  36. /* doing strstr(p, "/.") by hand is smaller and faster... */
  37. if (*p++ != '/')
  38. continue;
  39. if (*p != '.')
  40. continue;
  41. /* we found "/.", skip to next char */
  42. p++;
  43. if (*p == '.')
  44. p++; /* we found "/.." */
  45. if (*p == '\0' || *p == '/')
  46. return 0; /* "/./" or "/../" component: bad */
  47. }
  48. if (stat(wd, &st1) != 0)
  49. return 0;
  50. if (stat(".", &st2) != 0)
  51. return 0;
  52. if (st1.st_ino != st2.st_ino)
  53. return 0;
  54. if (st1.st_dev != st2.st_dev)
  55. return 0;
  56. puts(wd);
  57. return 1;
  58. }
  59. int pwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  60. int pwd_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  61. {
  62. char *buf;
  63. if (ENABLE_DESKTOP) {
  64. /* TODO: assume -L if $POSIXLY_CORRECT? (coreutils does that)
  65. * Rationale:
  66. * POSIX requires a default of -L, but most scripts expect -P
  67. */
  68. unsigned opt = getopt32(argv, "LP");
  69. if ((opt & 1) && logical_getcwd())
  70. return fflush_all();
  71. }
  72. buf = xrealloc_getcwd_or_warn(NULL);
  73. if (buf) {
  74. puts(buf);
  75. free(buf);
  76. return fflush_all();
  77. }
  78. return EXIT_FAILURE;
  79. }