Mercurial > hg > toybox
view toys/posix/pwd.c @ 1531:3ff823086c99 draft
Teach ln -f to leave original target alone if link creation fails.
Suggested by Ashwini Sharma, I wound up implementing it by creating the new
link at a temporary name and renaming it over the old one instead of renaming
the old file out of the way and putting it back if it failed.
(Because "mkdir -p one/one/blah && ln -sf /bin/one one" would otherwise
rename one/one out of the way and only notice it can't delete it way at the
end when recovery's darn awkward, vs create new thing and if rename fails
(including EISDIR) that's the main error path. And yes the temporary name
is in the same directory as the destination so we never rename between mounts.)
link over the old one instead of renaming the old file and renaming it back.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 22 Oct 2014 17:11:06 -0500 |
parents | cf101d432225 |
children |
line wrap: on
line source
/* pwd.c - Print working directory. * * Copyright 2006 Rob Landley <rob@landley.net> * * See http://opengroup.org/onlinepubs/9699919799/utilities/pwd.html USE_PWD(NEWTOY(pwd, ">0LP[-LP]", TOYFLAG_BIN)) config PWD bool "pwd" default y help usage: pwd [-L|-P] Print working (current) directory. -L Use shell's path from $PWD (when applicable) -P Print cannonical absolute path */ #define FOR_pwd #include "toys.h" void pwd_main(void) { char *s, *pwd = getcwd(0, 0), *PWD; // Only use $PWD if it's an absolute path alias for cwd with no "." or ".." if (!(toys.optflags & FLAG_P) && (s = PWD = getenv("PWD"))) { struct stat st1, st2; while (*s == '/') { if (*(++s) == '.') { if (s[1] == '/' || !s[1]) break; if (s[1] == '.' && (s[2] == '/' || !s[2])) break; } while (*s && *s != '/') s++; } if (!*s && s != PWD) s = PWD; else s = NULL; // If current directory exists, make sure it matches. if (s && pwd) if (stat(pwd, &st1) || stat(PWD, &st2) || st1.st_ino != st2.st_ino || st1.st_dev != st2.st_dev) s = NULL; } else s = NULL; // If -L didn't give us a valid path, use cwd. if (!s && !(s = pwd)) perror_exit("xgetcwd"); xprintf("%s\n", s); if (CFG_TOYBOX_FREE) free(pwd); }