/* * pwd.c * Implementation of SUSv3 XCU pwd utility * Copyright © 2007 Rich Felker * Licensed under the terms of the GNU General Public License, v2 or later */ /* NOTE: This implementation assumes PATH_MAX is defined and correct. */ #include #include #include #include #include #include static int my_write(int fd, const char *s, size_t l) { if (!l) l = strlen(s); while (l) { ssize_t n = write(fd, s, l); if (n<0) return -1; s += n; l -= n; } return 0; } static void my_perror(char *prog, char *msg) { char *err = strerror(errno); write(2, prog, strlen(prog)); write(2, ": ", 2); write(2, msg, strlen(msg)); write(2, ": ", 2); write(2, err, strlen(err)); write(2, "\n", 1); } static int is_rel(const char *s) { unsigned dots=0, slash=1; if (*s++ != '/') return 1; for (; *s; s++) { if (*s == '/') { if (dots-1<2) return 1; dots=0; } else slash=1; if (slash && *s == '.') dots++; else dots=slash=0; } return dots-1<2; } int main(int argc, char *argv[]) { int i, j; int p=0; char buf[PATH_MAX+2]; struct stat st1, st2; char *pwd; for (i=1; i