Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini pwd implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 9 | //config:config PWD |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame^] | 10 | //config: bool "pwd (3.7 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: pwd is used to print the current directory. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 14 | |
| 15 | //applet:IF_PWD(APPLET_NOFORK(pwd, pwd, BB_DIR_BIN, BB_SUID_DROP, pwd)) |
| 16 | |
| 17 | //kbuild:lib-$(CONFIG_PWD) += pwd.o |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 18 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 19 | //usage:#define pwd_trivial_usage |
| 20 | //usage: "" |
| 21 | //usage:#define pwd_full_usage "\n\n" |
| 22 | //usage: "Print the full filename of the current working directory" |
| 23 | //usage: |
| 24 | //usage:#define pwd_example_usage |
| 25 | //usage: "$ pwd\n" |
| 26 | //usage: "/root\n" |
| 27 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 28 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | |
Denys Vlasenko | 4c77ad7 | 2011-10-16 05:16:50 +0200 | [diff] [blame] | 30 | static int logical_getcwd(void) |
| 31 | { |
| 32 | struct stat st1; |
| 33 | struct stat st2; |
| 34 | char *wd; |
| 35 | char *p; |
| 36 | |
| 37 | wd = getenv("PWD"); |
| 38 | if (!wd || wd[0] != '/') |
| 39 | return 0; |
| 40 | |
| 41 | p = wd; |
| 42 | while (*p) { |
| 43 | /* doing strstr(p, "/.") by hand is smaller and faster... */ |
| 44 | if (*p++ != '/') |
| 45 | continue; |
| 46 | if (*p != '.') |
| 47 | continue; |
| 48 | /* we found "/.", skip to next char */ |
| 49 | p++; |
| 50 | if (*p == '.') |
| 51 | p++; /* we found "/.." */ |
| 52 | if (*p == '\0' || *p == '/') |
Denys Vlasenko | 73d249e | 2011-10-28 14:07:44 +0200 | [diff] [blame] | 53 | return 0; /* "/./" or "/../" component: bad */ |
Denys Vlasenko | 4c77ad7 | 2011-10-16 05:16:50 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | if (stat(wd, &st1) != 0) |
| 57 | return 0; |
| 58 | if (stat(".", &st2) != 0) |
| 59 | return 0; |
| 60 | if (st1.st_ino != st2.st_ino) |
| 61 | return 0; |
| 62 | if (st1.st_dev != st2.st_dev) |
| 63 | return 0; |
| 64 | |
| 65 | puts(wd); |
| 66 | return 1; |
| 67 | } |
| 68 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 69 | int pwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 70 | int pwd_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 71 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | char *buf; |
| 73 | |
Denys Vlasenko | 4c77ad7 | 2011-10-16 05:16:50 +0200 | [diff] [blame] | 74 | if (ENABLE_DESKTOP) { |
| 75 | /* TODO: assume -L if $POSIXLY_CORRECT? (coreutils does that) |
| 76 | * Rationale: |
| 77 | * POSIX requires a default of -L, but most scripts expect -P |
| 78 | */ |
| 79 | unsigned opt = getopt32(argv, "LP"); |
| 80 | if ((opt & 1) && logical_getcwd()) |
| 81 | return fflush_all(); |
| 82 | } |
| 83 | |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 84 | buf = xrealloc_getcwd_or_warn(NULL); |
Denys Vlasenko | 4c77ad7 | 2011-10-16 05:16:50 +0200 | [diff] [blame] | 85 | |
| 86 | if (buf) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 87 | puts(buf); |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 88 | free(buf); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 89 | return fflush_all(); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 90 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 91 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 92 | return EXIT_FAILURE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 93 | } |