sbase/pwd.c
FRIGN a68c2a9e6e Remove apathmax() and implicitly agetcwd()
pathconf() is just an insane interface to use. All sane operating-
systems set sane values for PATH_MAX. Due to the by-runtime-nature of
pathconf(), it actually weakens the programs depending on its values.

Given over 3 years it has still not been possible to implement a sane
and easy to use apathmax()-utility-function, and after discussing this
on IRC, we'll dump this garbage.

We are careful enough not to overflow PATH_MAX and even if, any user
is able to set another limit in config.mk if he so desires.
2015-03-18 15:20:35 +01:00

51 lines
810 B
C

/* 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 0;
}