pwd.c - sbase - suckless unix tools
(HTM) git clone git://git.suckless.org/sbase
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
pwd.c (833B)
---
1 /* See LICENSE file for copyright and license details. */
2 #include <sys/stat.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 #include "util.h"
9
10 static const char *
11 getpwd(const char *cwd)
12 {
13 const char *pwd;
14 struct stat cst, pst;
15
16 if (!(pwd = getenv("PWD")) || pwd[0] != '/' || stat(pwd, &pst) < 0)
17 return cwd;
18 if (stat(cwd, &cst) < 0)
19 eprintf("stat %s:", cwd);
20
21 return (pst.st_dev == cst.st_dev && pst.st_ino == cst.st_ino) ? pwd : cwd;
22 }
23
24 static void
25 usage(void)
26 {
27 eprintf("usage: %s [-LP]\n", argv0);
28 }
29
30 int
31 main(int argc, char *argv[])
32 {
33 char cwd[PATH_MAX];
34 char mode = 'L';
35
36 ARGBEGIN {
37 case 'L':
38 case 'P':
39 mode = ARGC();
40 break;
41 default:
42 usage();
43 } ARGEND
44
45 if (!getcwd(cwd, sizeof(cwd)))
46 eprintf("getcwd:");
47 puts((mode == 'L') ? getpwd(cwd) : cwd);
48
49 return fshut(stdout, "<stdout>");
50 }