xref: /openbsd/bin/pwd/pwd.c (revision 3d8817e4)
1 /*	$OpenBSD: pwd.c,v 1.11 2009/10/27 23:59:22 deraadt Exp $	*/
2 /*	$NetBSD: pwd.c,v 1.7 1995/03/21 09:08:18 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1991, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <err.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 extern	char *__progname;
39 
40 void usage(void);
41 
42 int
43 main(int argc, char *argv[])
44 {
45 	int ch;
46 	char *p;
47 
48 	/*
49 	 * Flags for pwd are a bit strange.  The POSIX 1003.2B/D9 document
50 	 * has an optional -P flag for physical, which is what this program
51 	 * will produce by default.  The logical flag, -L, should fail, as
52 	 * there's no way to display a logical path after forking.  We don't
53 	 * document either flag, only adding -P for future portability.
54 	 */
55 	while ((ch = getopt(argc, argv, "P")) != -1)
56 		switch (ch) {
57 		case 'P':
58 			break;
59 		default:
60 			usage();
61 		}
62 	argc -= optind;
63 	argv += optind;
64 
65 	if (argc != 0)
66 		usage();
67 
68 	if ((p = getcwd(NULL, (size_t)0)) == NULL)
69 		err(1, "getcwd");
70 	(void)printf("%s\n", p);
71 	exit(0);
72 }
73 
74 void
75 usage(void)
76 {
77 	(void)fprintf(stderr, "usage: %s\n", __progname);
78 	exit(1);
79 }
80