2011-05-23 15:15:19 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-05-28 10:37:42 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2011-05-23 15:15:19 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2011-05-28 10:37:42 -04:00
|
|
|
static const char *getpwd(const char *cwd);
|
|
|
|
|
2011-05-23 15:15:19 -04:00
|
|
|
int
|
2011-05-28 10:37:42 -04:00
|
|
|
main(int argc, char *argv[])
|
2011-05-23 15:15:19 -04:00
|
|
|
{
|
2011-05-28 10:37:42 -04:00
|
|
|
char *cwd, c;
|
|
|
|
char mode = 'L';
|
|
|
|
|
|
|
|
while((c = getopt(argc, argv, "LP")) != -1)
|
|
|
|
switch(c) {
|
|
|
|
case 'L':
|
|
|
|
case 'P':
|
|
|
|
mode = c;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
cwd = agetcwd();
|
|
|
|
puts((mode == 'L') ? getpwd(cwd) : cwd);
|
2011-05-23 15:15:19 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2011-05-28 10:37:42 -04:00
|
|
|
|
|
|
|
const char *
|
|
|
|
getpwd(const char *cwd)
|
|
|
|
{
|
|
|
|
const char *pwd;
|
|
|
|
struct stat cst, pst;
|
|
|
|
|
2011-06-04 07:20:41 -04:00
|
|
|
if(!(pwd = getenv("PWD")) || pwd[0] != '/' || stat(pwd, &pst) == -1)
|
2011-05-28 10:37:42 -04:00
|
|
|
return cwd;
|
2011-06-04 07:20:41 -04:00
|
|
|
if(stat(cwd, &cst) == -1)
|
2011-05-28 10:37:42 -04:00
|
|
|
eprintf("stat %s:", cwd);
|
|
|
|
if(pst.st_dev == cst.st_dev && pst.st_ino == cst.st_ino)
|
|
|
|
return pwd;
|
|
|
|
else
|
|
|
|
return cwd;
|
|
|
|
}
|