/* abspath.c * * Use the C library to find and print the absolute path corresponding * to the given argument. * * rev 00 12/21/02 Linux version. * * Returns 0 if no arguments were given * 'n' if 'n' args were given and argv[1] was successfully * resolved to an absolute path ('n' is limited to 126). * 127 argv[1] is not a valid file or directory. */ #include #include #include int main (int argc, char *argv[]) { int exitstatus; char *result; char resolved[PATH_MAX]; if ((argc == 2) && (strcmp(argv[1], "-help") == 0)) { fprintf(stderr,"abspath: Prints the absolute pathname of its first argument, then exits\n"); fprintf(stderr,"abspath: with exit status set to its number of arguments or to 127 on error.\n"); exit (127); } if (argc < 2) { strcpy(resolved,"No argument was supplied."); exitstatus = 0; } else { result = realpath(argv[1], resolved); if (result == NULL) { strcpy(resolved,"Invalid argument; try \"abspath -help\"."); exitstatus = 127; } else { exitstatus = argc - 1; if (exitstatus > 126) exitstatus = 126; } } printf("%s\n",resolved); /* printf("exit status will be %d\n",exitstatus); */ exit (exitstatus); } /* end of main() */