1
0
mirror of git://git.suckless.org/sbase synced 2024-12-22 15:04:53 +00:00
sbase/pwd.c
FRIGN 7cb966c170 Audit pwd(1)
Nothing special
1) Update manpage to current style.
2) Reorder functions
3) Logical trickery in getpwd()
2015-03-03 17:24:14 +01:00

49 lines
745 B
C

/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.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;
char mode = 'L';
ARGBEGIN {
case 'L':
case 'P':
mode = ARGC();
break;
default:
usage();
} ARGEND;
cwd = agetcwd();
puts((mode == 'L') ? getpwd(cwd) : cwd);
return 0;
}