xref: /netbsd-src/bin/pwd/pwd.c (revision b6dd340d3c5e64e71932c3da8bfd9bca6bc760d3)
1*b6dd340dSkre /* $NetBSD: pwd.c,v 1.23 2021/11/16 16:57:15 kre Exp $ */
249f0ad86Scgd 
361f28255Scgd /*
4667b5ea1Smycroft  * Copyright (c) 1991, 1993, 1994
5667b5ea1Smycroft  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15b5b29542Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32e06ec2b8Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
342fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
352fe2731dSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
3949f0ad86Scgd #if 0
4049f0ad86Scgd static char sccsid[] = "@(#)pwd.c	8.3 (Berkeley) 4/1/94";
4149f0ad86Scgd #else
42*b6dd340dSkre __RCSID("$NetBSD: pwd.c,v 1.23 2021/11/16 16:57:15 kre Exp $");
4349f0ad86Scgd #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
46458ed234Sjschauma #include <sys/param.h>
47b29d50b8Skleink #include <sys/stat.h>
48458ed234Sjschauma #include <sys/types.h>
493684e0c4Swiz 
50667b5ea1Smycroft #include <err.h>
51b29d50b8Skleink #include <errno.h>
529655f5c2Schristos #include <locale.h>
5385d1c9b5Sjtc #include <stdio.h>
5485d1c9b5Sjtc #include <stdlib.h>
55520d678dSwsanchez #include <string.h>
56b29d50b8Skleink #include <unistd.h>
57458ed234Sjschauma 
58db9b05afSdsl static char *getcwd_logical(void);
595bb1ddccSjoerg __dead static void usage(void);
60db9b05afSdsl 
61db9b05afSdsl /*
62db9b05afSdsl  * Note that EEE Std 1003.1, 2003 requires that the default be -L.
63db9b05afSdsl  * This is inconsistent with the historic behaviour of everything
64db9b05afSdsl  * except the ksh builtin.
65db9b05afSdsl  * To avoid breaking scripts the default has been kept as -P.
66db9b05afSdsl  * (Some scripts run /bin/pwd in order to get 'pwd -P'.)
67db9b05afSdsl  */
6861f28255Scgd 
6985d1c9b5Sjtc int
main(int argc,char * argv[])703684e0c4Swiz main(int argc, char *argv[])
7161f28255Scgd {
723684e0c4Swiz 	int ch, lFlag;
73b29d50b8Skleink 	const char *p;
7461f28255Scgd 
753684e0c4Swiz 	setprogname(argv[0]);
769655f5c2Schristos 	(void)setlocale(LC_ALL, "");
779655f5c2Schristos 
783684e0c4Swiz 	lFlag = 0;
79db9b05afSdsl 	while ((ch = getopt(argc, argv, "LP")) != -1) {
80667b5ea1Smycroft 		switch (ch) {
81520d678dSwsanchez 		case 'L':
82520d678dSwsanchez 			lFlag = 1;
83520d678dSwsanchez 			break;
84667b5ea1Smycroft 		case 'P':
85520d678dSwsanchez 			lFlag = 0;
86667b5ea1Smycroft 			break;
87667b5ea1Smycroft 		case '?':
88667b5ea1Smycroft 		default:
89667b5ea1Smycroft 			usage();
9061f28255Scgd 		}
91db9b05afSdsl 	}
92667b5ea1Smycroft 	argc -= optind;
93667b5ea1Smycroft 	argv += optind;
9485d1c9b5Sjtc 
95667b5ea1Smycroft 	if (argc != 0)
96667b5ea1Smycroft 		usage();
97667b5ea1Smycroft 
98520d678dSwsanchez 	if (lFlag)
99db9b05afSdsl 		p = getcwd_logical();
100520d678dSwsanchez 	else
101db9b05afSdsl 		p = NULL;
102db9b05afSdsl 	if (p == NULL)
103520d678dSwsanchez 		p = getcwd(NULL, 0);
104520d678dSwsanchez 
105b29d50b8Skleink 	if (p == NULL)
10685cbf55dSdrochner 		err(EXIT_FAILURE, NULL);
107520d678dSwsanchez 
1086a75fbb6Sjschauma 	(void)printf("%s\n", p);
109520d678dSwsanchez 
110*b6dd340dSkre 	(void)fflush(stdout);
111*b6dd340dSkre 	if (ferror(stdout))
112*b6dd340dSkre 		err(EXIT_FAILURE, "stdout");
113*b6dd340dSkre 
114b29d50b8Skleink 	exit(EXIT_SUCCESS);
1159dc385beSmycroft 	/* NOTREACHED */
11685d1c9b5Sjtc }
117667b5ea1Smycroft 
118b29d50b8Skleink static char *
getcwd_logical(void)119db9b05afSdsl getcwd_logical(void)
120520d678dSwsanchez {
121520d678dSwsanchez 	char *pwd;
122db9b05afSdsl 	struct stat s_pwd, s_dot;
123520d678dSwsanchez 
124520d678dSwsanchez 	/* Check $PWD -- if it's right, it's fast. */
125db9b05afSdsl 	pwd = getenv("PWD");
126db9b05afSdsl 	if (pwd == NULL)
127db9b05afSdsl 		return NULL;
128db9b05afSdsl 	if (pwd[0] != '/')
129db9b05afSdsl 		return NULL;
130db9b05afSdsl 	if (strstr(pwd, "/./") != NULL)
131db9b05afSdsl 		return NULL;
132db9b05afSdsl 	if (strstr(pwd, "/../") != NULL)
133db9b05afSdsl 		return NULL;
134db9b05afSdsl 	if (stat(pwd, &s_pwd) == -1 || stat(".", &s_dot) == -1)
135db9b05afSdsl 		return NULL;
136db9b05afSdsl 	if (s_pwd.st_dev != s_dot.st_dev || s_pwd.st_ino != s_dot.st_ino)
137db9b05afSdsl 		return NULL;
138db9b05afSdsl 	return pwd;
139520d678dSwsanchez }
140520d678dSwsanchez 
141b29d50b8Skleink static void
usage(void)1423684e0c4Swiz usage(void)
143667b5ea1Smycroft {
1443684e0c4Swiz 	(void)fprintf(stderr, "usage: %s [-LP]\n", getprogname());
145b29d50b8Skleink 	exit(EXIT_FAILURE);
1469dc385beSmycroft 	/* NOTREACHED */
147667b5ea1Smycroft }
148