190523Smike/*-
290523Smike * Copyright (c) 1991, 1993, 1994
390523Smike *	The Regents of the University of California.  All rights reserved.
490523Smike *
590523Smike * Redistribution and use in source and binary forms, with or without
690523Smike * modification, are permitted provided that the following conditions
790523Smike * are met:
890523Smike * 1. Redistributions of source code must retain the above copyright
990523Smike *    notice, this list of conditions and the following disclaimer.
1090523Smike * 2. Redistributions in binary form must reproduce the above copyright
1190523Smike *    notice, this list of conditions and the following disclaimer in the
1290523Smike *    documentation and/or other materials provided with the distribution.
1390523Smike * 4. Neither the name of the University nor the names of its contributors
1490523Smike *    may be used to endorse or promote products derived from this software
1590523Smike *    without specific prior written permission.
1690523Smike *
1790523Smike * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1890523Smike * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1990523Smike * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2090523Smike * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2190523Smike * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2290523Smike * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2390523Smike * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2490523Smike * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2590523Smike * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2690523Smike * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2790523Smike * SUCH DAMAGE.
2890523Smike */
2990523Smike
3090523Smike#include <sys/cdefs.h>
3190523Smike__FBSDID("$FreeBSD$");
3290523Smike
3390523Smike#include <sys/param.h>
3490523Smike
3590523Smike#include <err.h>
3690523Smike#include <stdio.h>
3790523Smike#include <stdlib.h>
3890523Smike#include <unistd.h>
3990523Smike
4098057Smikestatic void usage(void) __dead2;
4190523Smike
4290523Smikeint
4390523Smikemain(int argc, char *argv[])
4490523Smike{
45109331Sjohan	char buf[PATH_MAX];
4690523Smike	char *p;
47223372Sru	const char *path;
48223372Sru	int ch, qflag, rval;
4990523Smike
50176977Srwatson	qflag = 0;
51176977Srwatson	while ((ch = getopt(argc, argv, "q")) != -1) {
52176977Srwatson		switch (ch) {
53176977Srwatson		case 'q':
54176977Srwatson			qflag = 1;
55176977Srwatson			break;
56176977Srwatson		case '?':
57176977Srwatson		default:
58176977Srwatson			usage();
59176977Srwatson		}
60176977Srwatson	}
61176977Srwatson	argc -= optind;
62176977Srwatson	argv += optind;
63223372Sru	path = *argv != NULL ? *argv++ : ".";
64176977Srwatson	rval  = 0;
65223372Sru	do {
66223372Sru		if ((p = realpath(path, buf)) == NULL) {
67176977Srwatson			if (!qflag)
68223372Sru				warn("%s", path);
69176977Srwatson			rval = 1;
70176977Srwatson		} else
71176977Srwatson			(void)printf("%s\n", p);
72223372Sru	} while ((path = *argv++) != NULL);
73176977Srwatson	exit(rval);
7490523Smike}
7590523Smike
7690523Smikestatic void
7790523Smikeusage(void)
7890523Smike{
7990523Smike
80223372Sru	(void)fprintf(stderr, "usage: realpath [-q] [path ...]\n");
8190523Smike  	exit(1);
8290523Smike}
83