X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=utils%2Fpwd%2Fpwd.c;fp=utils%2Fpwd%2Fpwd.c;h=0000000000000000000000000000000000000000;hb=4476699b7c7de3abf9f6da71ed409b2c032dc50f;hp=41e2ff4a3a9c8c777329b26dae467d1ce2b954a7;hpb=2e838897cc574a9147bcf0b1681aeeaa44001fce;p=pdutils diff --git a/utils/pwd/pwd.c b/utils/pwd/pwd.c deleted file mode 100644 index 41e2ff4..0000000 --- a/utils/pwd/pwd.c +++ /dev/null @@ -1,94 +0,0 @@ -#include -#include -#include -#include -#include -#include - -static int has_dot_or_dotdot(char *path) { - if (!path) return 0; - - while (*path) { - if (*path == '/') { - path++; - continue; - } - if (*path == '.') { - path++; - if (*path == '.') path++; - switch (path[0]) { - case '/': - case 0: - return 1; - default: - break; - } - } - while (*path && *path != '/') { - path++; - } - } - return 0; -} - -static void fillcwd(char *s, size_t n) { - if (!getcwd(s, n)) { - switch (errno) { - case ERANGE: - fprintf(stderr, "pwd: path too long\n"); - exit(1); - default: - perror("pwd"); - exit(1); - } - } -} - -int main(int ac, char *av[]) { - int cleansym = 0; - char cwd[PATH_MAX]; - char *pwd; - char *envpwd; - int arg; - - for (arg=1; arg < ac; arg++) { - if (av[arg][0] == '-') { - if (av[arg][1] == 'P' && av[arg][2] == 0) { - cleansym = 1; - } else if (av[arg][1] == 'L' && av[arg][2] == 0) { - cleansym = 0; - } - } else { - fprintf(stderr, "usage: pwd [-L|-P]\n"); - exit(1); - } - } - - if (!getcwd(cwd, sizeof cwd)) { - switch (errno) { - case ERANGE: - fprintf(stderr, "pwd: path too long\n"); - exit(1); - default: - perror("pwd"); - exit(1); - } - } - pwd = cwd; - - if (!cleansym) { - envpwd = getenv("PWD"); - if (envpwd && !has_dot_or_dotdot(envpwd)) { - if (chdir(envpwd) != -1) { - fillcwd(cwd, sizeof cwd); - if (strcmp(cwd, envpwd)) { - pwd = envpwd; - } - } - } - } - - printf("%s\n", pwd); - return 0; -} -