xref: /freebsd/bin/realpath/realpath.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
15330465cSMike Barcroft /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
45330465cSMike Barcroft  * Copyright (c) 1991, 1993, 1994
55330465cSMike Barcroft  *	The Regents of the University of California.  All rights reserved.
65330465cSMike Barcroft  *
75330465cSMike Barcroft  * Redistribution and use in source and binary forms, with or without
85330465cSMike Barcroft  * modification, are permitted provided that the following conditions
95330465cSMike Barcroft  * are met:
105330465cSMike Barcroft  * 1. Redistributions of source code must retain the above copyright
115330465cSMike Barcroft  *    notice, this list of conditions and the following disclaimer.
125330465cSMike Barcroft  * 2. Redistributions in binary form must reproduce the above copyright
135330465cSMike Barcroft  *    notice, this list of conditions and the following disclaimer in the
145330465cSMike Barcroft  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
165330465cSMike Barcroft  *    may be used to endorse or promote products derived from this software
175330465cSMike Barcroft  *    without specific prior written permission.
185330465cSMike Barcroft  *
195330465cSMike Barcroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205330465cSMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215330465cSMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225330465cSMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235330465cSMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245330465cSMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255330465cSMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265330465cSMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275330465cSMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285330465cSMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295330465cSMike Barcroft  * SUCH DAMAGE.
305330465cSMike Barcroft  */
315330465cSMike Barcroft 
325330465cSMike Barcroft #include <sys/param.h>
335330465cSMike Barcroft 
345330465cSMike Barcroft #include <err.h>
355330465cSMike Barcroft #include <stdio.h>
365330465cSMike Barcroft #include <stdlib.h>
375330465cSMike Barcroft #include <unistd.h>
385330465cSMike Barcroft 
39f3a09c6cSMike Barcroft static void usage(void) __dead2;
405330465cSMike Barcroft 
415330465cSMike Barcroft int
main(int argc,char * argv[])425330465cSMike Barcroft main(int argc, char *argv[])
435330465cSMike Barcroft {
44de216a83SJohan Karlsson 	char buf[PATH_MAX];
455330465cSMike Barcroft 	char *p;
467a416f3eSRuslan Ermilov 	const char *path;
477a416f3eSRuslan Ermilov 	int ch, qflag, rval;
485330465cSMike Barcroft 
4908995e29SRobert Watson 	qflag = 0;
5008995e29SRobert Watson 	while ((ch = getopt(argc, argv, "q")) != -1) {
5108995e29SRobert Watson 		switch (ch) {
5208995e29SRobert Watson 		case 'q':
5308995e29SRobert Watson 			qflag = 1;
5408995e29SRobert Watson 			break;
5508995e29SRobert Watson 		case '?':
5608995e29SRobert Watson 		default:
575330465cSMike Barcroft 			usage();
5808995e29SRobert Watson 		}
5908995e29SRobert Watson 	}
6008995e29SRobert Watson 	argc -= optind;
6108995e29SRobert Watson 	argv += optind;
627a416f3eSRuslan Ermilov 	path = *argv != NULL ? *argv++ : ".";
6308995e29SRobert Watson 	rval  = 0;
647a416f3eSRuslan Ermilov 	do {
657a416f3eSRuslan Ermilov 		if ((p = realpath(path, buf)) == NULL) {
6608995e29SRobert Watson 			if (!qflag)
677a416f3eSRuslan Ermilov 				warn("%s", path);
6808995e29SRobert Watson 			rval = 1;
6908995e29SRobert Watson 		} else
705330465cSMike Barcroft 			(void)printf("%s\n", p);
717a416f3eSRuslan Ermilov 	} while ((path = *argv++) != NULL);
7208995e29SRobert Watson 	exit(rval);
735330465cSMike Barcroft }
745330465cSMike Barcroft 
755330465cSMike Barcroft static void
usage(void)765330465cSMike Barcroft usage(void)
775330465cSMike Barcroft {
785330465cSMike Barcroft 
797a416f3eSRuslan Ermilov 	(void)fprintf(stderr, "usage: realpath [-q] [path ...]\n");
805330465cSMike Barcroft   	exit(1);
815330465cSMike Barcroft }
82