xref: /freebsd/bin/realpath/realpath.c (revision 5330465c439e14b7d760d8369b484a7a7459c019)
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  * 3. All advertising materials mentioning features or use of this software
145330465cSMike Barcroft  *    must display the following acknowledgement:
155330465cSMike Barcroft  *	This product includes software developed by the University of
165330465cSMike Barcroft  *	California, Berkeley and its contributors.
175330465cSMike Barcroft  * 4. Neither the name of the University nor the names of its contributors
185330465cSMike Barcroft  *    may be used to endorse or promote products derived from this software
195330465cSMike Barcroft  *    without specific prior written permission.
205330465cSMike Barcroft  *
215330465cSMike Barcroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225330465cSMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235330465cSMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245330465cSMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255330465cSMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265330465cSMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275330465cSMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285330465cSMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295330465cSMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305330465cSMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315330465cSMike Barcroft  * SUCH DAMAGE.
325330465cSMike Barcroft  */
335330465cSMike Barcroft 
345330465cSMike Barcroft #include <sys/cdefs.h>
355330465cSMike Barcroft __FBSDID("$FreeBSD$");
365330465cSMike Barcroft 
375330465cSMike Barcroft #include <sys/param.h>
385330465cSMike Barcroft 
395330465cSMike Barcroft #include <err.h>
405330465cSMike Barcroft #include <stdio.h>
415330465cSMike Barcroft #include <stdlib.h>
425330465cSMike Barcroft #include <unistd.h>
435330465cSMike Barcroft 
445330465cSMike Barcroft static void usage(void);
455330465cSMike Barcroft 
465330465cSMike Barcroft int
475330465cSMike Barcroft main(int argc, char *argv[])
485330465cSMike Barcroft {
495330465cSMike Barcroft 	char buf[PATH_MAX];
505330465cSMike Barcroft 	char *p;
515330465cSMike Barcroft 
525330465cSMike Barcroft 	if (argc == 1)
535330465cSMike Barcroft 		p = getcwd(NULL, 0);
545330465cSMike Barcroft 	else if (argc == 2) {
555330465cSMike Barcroft 		if ((p = realpath(argv[1], buf)) == NULL)
565330465cSMike Barcroft 			err(1, "%s", argv[1]);
575330465cSMike Barcroft 	} else
585330465cSMike Barcroft 		usage();
595330465cSMike Barcroft 	(void)printf("%s\n", p);
605330465cSMike Barcroft 	exit(0);
615330465cSMike Barcroft }
625330465cSMike Barcroft 
635330465cSMike Barcroft static void
645330465cSMike Barcroft usage(void)
655330465cSMike Barcroft {
665330465cSMike Barcroft 
675330465cSMike Barcroft 	(void)fprintf(stderr, "usage: realpath [path]\n");
685330465cSMike Barcroft   	exit(1);
695330465cSMike Barcroft }
70