sbase/pwd.c

51 lines
810 B
C
Raw Normal View History

2011-05-23 19:15:19 +00:00
/* See LICENSE file for copyright and license details. */
2015-02-14 20:02:41 +00:00
#include <sys/stat.h>
2011-05-23 19:15:19 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2011-05-23 19:15:19 +00:00
#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;
}
2011-05-28 14:37:42 +00:00
2013-06-14 18:20:47 +00:00
static void
usage(void)
{
eprintf("usage: %s [-LP]\n", argv0);
}
2011-05-23 19:15:19 +00:00
int
2011-05-28 14:37:42 +00:00
main(int argc, char *argv[])
2011-05-23 19:15:19 +00:00
{
char cwd[PATH_MAX];
2011-05-28 14:37:42 +00:00
char mode = 'L';
2013-06-14 18:20:47 +00:00
ARGBEGIN {
case 'L':
case 'P':
mode = ARGC();
break;
default:
usage();
} ARGEND;
if (!getcwd(cwd, sizeof(cwd)))
eprintf("getcwd:");
2011-05-28 14:37:42 +00:00
puts((mode == 'L') ? getpwd(cwd) : cwd);
2013-06-14 18:20:47 +00:00
2014-10-02 22:46:04 +00:00
return 0;
2011-05-23 19:15:19 +00:00
}