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 | */ |
| 9 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 10 | //usage:#define pwd_trivial_usage |
| 11 | //usage: "" |
| 12 | //usage:#define pwd_full_usage "\n\n" |
| 13 | //usage: "Print the full filename of the current working directory" |
| 14 | //usage: |
| 15 | //usage:#define pwd_example_usage |
| 16 | //usage: "$ pwd\n" |
| 17 | //usage: "/root\n" |
| 18 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 19 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 21 | /* This is a NOFORK applet. Be very careful! */ |
| 22 | |
Denys Vlasenko | 4c77ad7 | 2011-10-16 05:16:50 +0200 | [diff] [blame] | 23 | static int logical_getcwd(void) |
| 24 | { |
| 25 | struct stat st1; |
| 26 | struct stat st2; |
| 27 | char *wd; |
| 28 | char *p; |
| 29 | |
| 30 | wd = getenv("PWD"); |
| 31 | if (!wd || wd[0] != '/') |
| 32 | return 0; |
| 33 | |
| 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 == '/') |
Denys Vlasenko | 73d249e | 2011-10-28 14:07:44 +0200 | [diff] [blame] | 46 | return 0; /* "/./" or "/../" component: bad */ |
Denys Vlasenko | 4c77ad7 | 2011-10-16 05:16:50 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | if (stat(wd, &st1) != 0) |
| 50 | return 0; |
| 51 | if (stat(".", &st2) != 0) |
| 52 | return 0; |
| 53 | if (st1.st_ino != st2.st_ino) |
| 54 | return 0; |
| 55 | if (st1.st_dev != st2.st_dev) |
| 56 | return 0; |
| 57 | |
| 58 | puts(wd); |
| 59 | return 1; |
| 60 | } |
| 61 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 62 | int pwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 63 | int pwd_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 64 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 65 | char *buf; |
| 66 | |
Denys Vlasenko | 4c77ad7 | 2011-10-16 05:16:50 +0200 | [diff] [blame] | 67 | if (ENABLE_DESKTOP) { |
| 68 | /* TODO: assume -L if $POSIXLY_CORRECT? (coreutils does that) |
| 69 | * Rationale: |
| 70 | * POSIX requires a default of -L, but most scripts expect -P |
| 71 | */ |
| 72 | unsigned opt = getopt32(argv, "LP"); |
| 73 | if ((opt & 1) && logical_getcwd()) |
| 74 | return fflush_all(); |
| 75 | } |
| 76 | |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 77 | buf = xrealloc_getcwd_or_warn(NULL); |
Denys Vlasenko | 4c77ad7 | 2011-10-16 05:16:50 +0200 | [diff] [blame] | 78 | |
| 79 | if (buf) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 80 | puts(buf); |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 81 | free(buf); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 82 | return fflush_all(); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 83 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 84 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 85 | return EXIT_FAILURE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 86 | } |