1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include "libbb.h"
29
30static 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
44 if (*p++ != '/')
45 continue;
46 if (*p != '.')
47 continue;
48
49 p++;
50 if (*p == '.')
51 p++;
52 if (*p == '\0' || *p == '/')
53 return 0;
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
69int pwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
70int pwd_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
71{
72 char *buf;
73
74 if (ENABLE_DESKTOP) {
75
76
77
78
79 unsigned opt = getopt32(argv, "LP");
80 if ((opt & 1) && logical_getcwd())
81 return fflush_all();
82 }
83
84 buf = xrealloc_getcwd_or_warn(NULL);
85
86 if (buf) {
87 puts(buf);
88 free(buf);
89 return fflush_all();
90 }
91
92 return EXIT_FAILURE;
93}
94