xref: /freebsd/bin/realpath/realpath.c (revision 7a416f3e7dae7230b01fb5e4bd30d83f9c925d34)
15330465cSMike Barcroft /*-
25330465cSMike Barcroft  * Copyright (c) 1991, 1993, 1994
35330465cSMike Barcroft  *	The Regents of the University of California.  All rights reserved.
45330465cSMike Barcroft  *
55330465cSMike Barcroft  * Redistribution and use in source and binary forms, with or without
65330465cSMike Barcroft  * modification, are permitted provided that the following conditions
75330465cSMike Barcroft  * are met:
85330465cSMike Barcroft  * 1. Redistributions of source code must retain the above copyright
95330465cSMike Barcroft  *    notice, this list of conditions and the following disclaimer.
105330465cSMike Barcroft  * 2. Redistributions in binary form must reproduce the above copyright
115330465cSMike Barcroft  *    notice, this list of conditions and the following disclaimer in the
125330465cSMike Barcroft  *    documentation and/or other materials provided with the distribution.
135330465cSMike Barcroft  * 4. Neither the name of the University nor the names of its contributors
145330465cSMike Barcroft  *    may be used to endorse or promote products derived from this software
155330465cSMike Barcroft  *    without specific prior written permission.
165330465cSMike Barcroft  *
175330465cSMike Barcroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
185330465cSMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
195330465cSMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
205330465cSMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
215330465cSMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
225330465cSMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
235330465cSMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
245330465cSMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
255330465cSMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
265330465cSMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
275330465cSMike Barcroft  * SUCH DAMAGE.
285330465cSMike Barcroft  */
295330465cSMike Barcroft 
305330465cSMike Barcroft #include <sys/cdefs.h>
315330465cSMike Barcroft __FBSDID("$FreeBSD$");
325330465cSMike Barcroft 
335330465cSMike Barcroft #include <sys/param.h>
345330465cSMike Barcroft 
355330465cSMike Barcroft #include <err.h>
365330465cSMike Barcroft #include <stdio.h>
375330465cSMike Barcroft #include <stdlib.h>
385330465cSMike Barcroft #include <unistd.h>
395330465cSMike Barcroft 
40f3a09c6cSMike Barcroft static void usage(void) __dead2;
415330465cSMike Barcroft 
425330465cSMike Barcroft int
435330465cSMike Barcroft main(int argc, char *argv[])
445330465cSMike Barcroft {
45de216a83SJohan Karlsson 	char buf[PATH_MAX];
465330465cSMike Barcroft 	char *p;
47*7a416f3eSRuslan Ermilov 	const char *path;
48*7a416f3eSRuslan Ermilov 	int ch, qflag, rval;
495330465cSMike Barcroft 
5008995e29SRobert Watson 	qflag = 0;
5108995e29SRobert Watson 	while ((ch = getopt(argc, argv, "q")) != -1) {
5208995e29SRobert Watson 		switch (ch) {
5308995e29SRobert Watson 		case 'q':
5408995e29SRobert Watson 			qflag = 1;
5508995e29SRobert Watson 			break;
5608995e29SRobert Watson 		case '?':
5708995e29SRobert Watson 		default:
585330465cSMike Barcroft 			usage();
5908995e29SRobert Watson 		}
6008995e29SRobert Watson 	}
6108995e29SRobert Watson 	argc -= optind;
6208995e29SRobert Watson 	argv += optind;
63*7a416f3eSRuslan Ermilov 	path = *argv != NULL ? *argv++ : ".";
6408995e29SRobert Watson 	rval  = 0;
65*7a416f3eSRuslan Ermilov 	do {
66*7a416f3eSRuslan Ermilov 		if ((p = realpath(path, buf)) == NULL) {
6708995e29SRobert Watson 			if (!qflag)
68*7a416f3eSRuslan Ermilov 				warn("%s", path);
6908995e29SRobert Watson 			rval = 1;
7008995e29SRobert Watson 		} else
715330465cSMike Barcroft 			(void)printf("%s\n", p);
72*7a416f3eSRuslan Ermilov 	} while ((path = *argv++) != NULL);
7308995e29SRobert Watson 	exit(rval);
745330465cSMike Barcroft }
755330465cSMike Barcroft 
765330465cSMike Barcroft static void
775330465cSMike Barcroft usage(void)
785330465cSMike Barcroft {
795330465cSMike Barcroft 
80*7a416f3eSRuslan Ermilov 	(void)fprintf(stderr, "usage: realpath [-q] [path ...]\n");
815330465cSMike Barcroft   	exit(1);
825330465cSMike Barcroft }
83