Home | History | Annotate | Line # | Download | only in pwd
pwd.c revision 1.16
      1 /* $NetBSD: pwd.c,v 1.16 2003/08/07 09:05:25 agc Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)pwd.c	8.3 (Berkeley) 4/1/94";
     41 #else
     42 __RCSID("$NetBSD: pwd.c,v 1.16 2003/08/07 09:05:25 agc Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/stat.h>
     48 #include <sys/types.h>
     49 
     50 #include <err.h>
     51 #include <errno.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 #include <vis.h>
     57 
     58 int	stdout_ok;			/* stdout connected to a terminal */
     59 
     60 static char *getcwd_logical(char *, size_t);
     61 static void usage(void);
     62 int main(int, char *[]);
     63 char *printescaped(const char *);
     64 
     65 int
     66 main(int argc, char *argv[])
     67 {
     68 	int ch, lFlag;
     69 	const char *p;
     70 	char *pn;
     71 
     72 	setprogname(argv[0]);
     73 	lFlag = 0;
     74 	while ((ch = getopt(argc, argv, "LP")) != -1)
     75 		switch (ch) {
     76 		case 'L':
     77 			lFlag = 1;
     78 			break;
     79 		case 'P':
     80 			lFlag = 0;
     81 			break;
     82 		case '?':
     83 		default:
     84 			usage();
     85 		}
     86 	argc -= optind;
     87 	argv += optind;
     88 
     89 	if (argc != 0)
     90 		usage();
     91 
     92 	if (lFlag)
     93 		p = getcwd_logical(NULL, 0);
     94 	else
     95 		p = getcwd(NULL, 0);
     96 
     97 	if (p == NULL)
     98 		err(EXIT_FAILURE, NULL);
     99 
    100 	stdout_ok = isatty(STDOUT_FILENO);
    101 
    102 	pn = printescaped(p);
    103 	(void)printf("%s\n", pn);
    104 	free(pn);
    105 
    106 	exit(EXIT_SUCCESS);
    107 	/* NOTREACHED */
    108 }
    109 
    110 static char *
    111 getcwd_logical(char *pt, size_t size)
    112 {
    113 	char *pwd;
    114 	size_t pwdlen;
    115 	dev_t dev;
    116 	ino_t ino;
    117 	struct stat s;
    118 
    119 	/* Check $PWD -- if it's right, it's fast. */
    120 	if ((pwd = getenv("PWD")) != NULL && pwd[0] == '/') {
    121 		if (stat(pwd, &s) != -1) {
    122 			dev = s.st_dev;
    123 			ino = s.st_ino;
    124 			if (stat(".", &s) != -1 && dev == s.st_dev &&
    125 			    ino == s.st_ino) {
    126 				pwdlen = strlen(pwd);
    127 				if (pt) {
    128 					if (!size) {
    129 						errno = EINVAL;
    130 						return (NULL);
    131 					}
    132 					if (pwdlen + 1 > size) {
    133 						errno = ERANGE;
    134 						return (NULL);
    135 					}
    136 				} else if ((pt = malloc(pwdlen + 1)) == NULL)
    137 					return (NULL);
    138 				(void)memmove(pt, pwd, pwdlen);
    139 				pt[pwdlen] = '\0';
    140 				return (pt);
    141 			}
    142 		}
    143 	} else
    144 		errno = ENOENT;
    145 
    146 	return (NULL);
    147 }
    148 
    149 static void
    150 usage(void)
    151 {
    152 	(void)fprintf(stderr, "usage: %s [-LP]\n", getprogname());
    153 	exit(EXIT_FAILURE);
    154 	/* NOTREACHED */
    155 }
    156 
    157 char *
    158 printescaped(const char *src)
    159 {
    160 	size_t len;
    161 	char *retval;
    162 
    163 	len = strlen(src);
    164 	if (len != 0 && SIZE_T_MAX/len <= 4) {
    165 		errx(EXIT_FAILURE, "%s: name too long", src);
    166 		/* NOTREACHED */
    167 	}
    168 
    169 	retval = (char *)malloc(4*len+1);
    170 	if (retval != NULL) {
    171 		if (stdout_ok)
    172 			(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
    173 		else
    174 			(void)strcpy(retval, src);
    175 		return retval;
    176 	} else
    177 		errx(EXIT_FAILURE, "out of memory!");
    178 		/* NOTREACHED */
    179 }
    180