blob: 4c42284f4d027df106353e390d4c7ae751e640a2 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
3 * Mini pwd implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenc4996011999-10-20 22:08:37 +00008 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config PWD
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "pwd (3.7 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: pwd is used to print the current directory.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010014
15//applet:IF_PWD(APPLET_NOFORK(pwd, pwd, BB_DIR_BIN, BB_SUID_DROP, pwd))
16
17//kbuild:lib-$(CONFIG_PWD) += pwd.o
Eric Andersenc4996011999-10-20 22:08:37 +000018
Pere Orga34425382011-03-31 14:43:25 +020019//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 Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000029
Denys Vlasenko4c77ad72011-10-16 05:16:50 +020030static 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 Vlasenko73d249e2011-10-28 14:07:44 +020053 return 0; /* "/./" or "/../" component: bad */
Denys Vlasenko4c77ad72011-10-16 05:16:50 +020054 }
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 Vlasenko9b49a5e2007-10-11 10:05:36 +000069int pwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000070int pwd_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Eric Andersencc8ed391999-10-05 16:24:54 +000071{
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 char *buf;
73
Denys Vlasenko4c77ad72011-10-16 05:16:50 +020074 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 Vlasenko6ca04442007-02-11 16:19:28 +000084 buf = xrealloc_getcwd_or_warn(NULL);
Denys Vlasenko4c77ad72011-10-16 05:16:50 +020085
86 if (buf) {
Eric Andersene5dfced2001-04-09 22:48:12 +000087 puts(buf);
Denis Vlasenko99912ca2007-04-10 15:43:37 +000088 free(buf);
Denys Vlasenko8131eea2009-11-02 14:19:51 +010089 return fflush_all();
Eric Andersene5dfced2001-04-09 22:48:12 +000090 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000091
Eric Andersene5dfced2001-04-09 22:48:12 +000092 return EXIT_FAILURE;
Eric Andersencc8ed391999-10-05 16:24:54 +000093}