blob: c6a4497fbc19b9096ab15481fccb7b7e25de8096 (
plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  | 
/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "util.h"
static const char *
getpwd(const char *cwd)
{
	const char *pwd;
	struct stat cst, pst;
	if (!(pwd = getenv("PWD")) || pwd[0] != '/' || stat(pwd, &pst) < 0)
		return cwd;
	if (stat(cwd, &cst) < 0)
		eprintf("stat %s:", cwd);
	return (pst.st_dev == cst.st_dev && pst.st_ino == cst.st_ino) ? pwd : cwd;
}
static void
usage(void)
{
	eprintf("usage: %s [-LP]\n", argv0);
}
int
main(int argc, char *argv[])
{
	char cwd[PATH_MAX];
	char mode = 'L';
	ARGBEGIN {
	case 'L':
	case 'P':
		mode = ARGC();
		break;
	default:
		usage();
	} ARGEND
	if (!getcwd(cwd, sizeof(cwd)))
		eprintf("getcwd:");
	puts((mode == 'L') ? getpwd(cwd) : cwd);
	return fshut(stdout, "<stdout>");
}
  |