pwd.c (90110) | pwd.c (90170) |
---|---|
1/* 2 * Copyright (c) 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 28 unchanged lines hidden (view full) --- 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)pwd.c 8.3 (Berkeley) 4/1/94"; 43#endif 44static const char rcsid[] = | 1/* 2 * Copyright (c) 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 28 unchanged lines hidden (view full) --- 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)pwd.c 8.3 (Berkeley) 4/1/94"; 43#endif 44static const char rcsid[] = |
45 "$FreeBSD: head/bin/pwd/pwd.c 90110 2002-02-02 06:48:10Z imp $"; | 45 "$FreeBSD: head/bin/pwd/pwd.c 90170 2002-02-04 07:26:21Z mike $"; |
46#endif /* not lint */ 47 | 46#endif /* not lint */ 47 |
48#include <sys/types.h> 49#include <sys/stat.h> 50 |
|
48#include <err.h> | 51#include <err.h> |
52#include <errno.h> |
|
49#include <limits.h> 50#include <stdio.h> 51#include <stdlib.h> 52#include <unistd.h> 53#include <sys/param.h> 54 | 53#include <limits.h> 54#include <stdio.h> 55#include <stdlib.h> 56#include <unistd.h> 57#include <sys/param.h> 58 |
59extern char *__progname; 60 61static char *getcwd_logical(void); |
|
55void usage(void); 56 57int 58main(int argc, char *argv[]) 59{ | 62void usage(void); 63 64int 65main(int argc, char *argv[]) 66{ |
67 int Lflag, Pflag; |
|
60 int ch; 61 char *p; 62 char buf[PATH_MAX]; 63 | 68 int ch; 69 char *p; 70 char buf[PATH_MAX]; 71 |
64 /* 65 * Flags for pwd are a bit strange. The POSIX 1003.2B/D9 document 66 * has an optional -P flag for physical, which is what this program 67 * will produce by default. The logical flag, -L, should fail, as 68 * there's no way to display a logical path after forking. 69 */ 70 while ((ch = getopt(argc, argv, "P")) != -1) | 72 if (strcmp(__progname, "realpath") == 0) { 73 if (argc != 2) 74 usage(); 75 if ((p = realpath(argv[1], buf)) == NULL) 76 err(1, "%s", argv[1]); 77 (void)printf("%s\n", p); 78 exit(0); 79 } 80 81 Lflag = Pflag = 0; 82 while ((ch = getopt(argc, argv, "LP")) != -1) |
71 switch (ch) { | 83 switch (ch) { |
84 case 'L': 85 Lflag = 1; 86 break; |
|
72 case 'P': | 87 case 'P': |
88 Pflag = 1; |
|
73 break; 74 case '?': 75 default: 76 usage(); 77 } 78 argc -= optind; 79 argv += optind; 80 | 89 break; 90 case '?': 91 default: 92 usage(); 93 } 94 argc -= optind; 95 argv += optind; 96 |
81 if (argc == 1) { 82 p = realpath(argv[0], buf); 83 if (p == NULL) 84 err(1, "%s", argv[0]); 85 (void)printf("%s\n", p); 86 } else if (argc == 0) { 87 p = getcwd(NULL, (size_t)0); 88 if (p == NULL) 89 err(1, "."); 90 (void)printf("%s\n", p); 91 } else { | 97 if (argc != 0 || (Lflag && Pflag)) |
92 usage(); | 98 usage(); |
93 } | |
94 | 99 |
100 p = Lflag ? getcwd_logical() : getcwd(NULL, 0); 101 if (p == NULL) 102 err(1, "."); 103 (void)printf("%s\n", p); 104 |
|
95 exit(0); 96} 97 98void 99usage(void) 100{ 101 | 105 exit(0); 106} 107 108void 109usage(void) 110{ 111 |
102 (void)fprintf(stderr, "usage: pwd\n"); 103 exit(1); | 112 if (strcmp(__progname, "realpath") == 0) 113 (void)fprintf(stderr, "usage: realpath [path]\n"); 114 else 115 (void)fprintf(stderr, "usage: pwd [-L | -P]\n"); 116 exit(1); |
104} | 117} |
118 119static char * 120getcwd_logical(void) 121{ 122 struct stat log, phy; 123 char *pwd; 124 125 /* 126 * Check that $PWD is an absolute logical pathname referring to 127 * the current working directory. 128 */ 129 if ((pwd = getenv("PWD")) != NULL && *pwd == '/') { 130 if (stat(pwd, &log) == -1 || stat(".", &phy) == -1) 131 return (NULL); 132 if (log.st_dev == phy.st_dev && log.st_ino == phy.st_ino) 133 return (pwd); 134 } 135 136 errno = ENOENT; 137 return (NULL); 138} |
|