annotate toys/posix/pwd.c @ 776:cf101d432225

Getting the pwd -L behavior right is fiddly.
author Rob Landley <rob@landley.net>
date Sun, 30 Dec 2012 04:43:11 -0600
parents adab8adf5f6d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
1 /* pwd.c - Print working directory.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
2 *
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
3 * Copyright 2006 Rob Landley <rob@landley.net>
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
4 *
776
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/pwd.html
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
6
776
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
7 USE_PWD(NEWTOY(pwd, ">0LP[-LP]", TOYFLAG_BIN))
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
8
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
9 config PWD
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
10 bool "pwd"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
11 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
12 help
774
adab8adf5f6d Add options -L and -P to pwd.
Felix Janda <felix.janda@posteo.de>
parents: 694
diff changeset
13 usage: pwd [-L|-P]
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
14
776
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
15 Print working (current) directory.
774
adab8adf5f6d Add options -L and -P to pwd.
Felix Janda <felix.janda@posteo.de>
parents: 694
diff changeset
16
776
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
17 -L Use shell's path from $PWD (when applicable)
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
18 -P Print cannonical absolute path
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
19 */
21
6475d6c46066 Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
20
774
adab8adf5f6d Add options -L and -P to pwd.
Felix Janda <felix.janda@posteo.de>
parents: 694
diff changeset
21 #define FOR_pwd
21
6475d6c46066 Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 #include "toys.h"
6475d6c46066 Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
23
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 90
diff changeset
24 void pwd_main(void)
21
6475d6c46066 Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 {
776
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
26 char *s, *pwd = getcwd(0, 0), *PWD;
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
27
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
28 // Only use $PWD if it's an absolute path alias for cwd with no "." or ".."
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
29 if (!(toys.optflags & FLAG_P) && (s = PWD = getenv("PWD"))) {
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
30 struct stat st1, st2;
21
6475d6c46066 Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
31
776
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
32 while (*s == '/') {
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
33 if (*(++s) == '.') {
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
34 if (s[1] == '/' || !s[1]) break;
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
35 if (s[1] == '.' && (s[2] == '/' || !s[2])) break;
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
36 }
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
37 while (*s && *s != '/') s++;
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
38 }
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
39 if (!*s && s != PWD) s = PWD;
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
40 else s = NULL;
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
41
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
42 // If current directory exists, make sure it matches.
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
43 if (s && pwd)
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
44 if (stat(pwd, &st1) || stat(PWD, &st2) || st1.st_ino != st2.st_ino ||
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
45 st1.st_dev != st2.st_dev) s = NULL;
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
46 } else s = NULL;
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
47
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
48 // If -L didn't give us a valid path, use cwd.
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
49 if (!s && !(s = pwd)) perror_exit("xgetcwd");
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
50
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
51 xprintf("%s\n", s);
cf101d432225 Getting the pwd -L behavior right is fiddly.
Rob Landley <rob@landley.net>
parents: 774
diff changeset
52
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
53 if (CFG_TOYBOX_FREE) free(pwd);
21
6475d6c46066 Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 }