Add -L flag.

This commit is contained in:
wsanchez 1998-11-03 21:38:19 +00:00
parent 00c4adc933
commit 520d678d7a
2 changed files with 74 additions and 17 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: pwd.1,v 1.12 1997/09/14 08:51:38 lukem Exp $
.\" $NetBSD: pwd.1,v 1.13 1998/11/03 21:38:19 wsanchez Exp $
.\"
.\" Copyright (c) 1990, 1993
.\" The Regents of the University of California. All rights reserved.
@ -36,7 +36,7 @@
.\"
.\" @(#)pwd.1 8.2 (Berkeley) 4/28/95
.\"
.Dd April 28, 1995
.Dd November 2, 1998
.Dt PWD 1
.Os BSD 4
.Sh NAME
@ -44,6 +44,7 @@
.Nd return working directory name
.Sh SYNOPSIS
.Nm
.Op Fl LP
.Sh DESCRIPTION
.Nm
writes the absolute pathname of the current working directory to
@ -52,6 +53,16 @@ the standard output.
The
.Nm
utility exits 0 on success, and >0 if an error occurs.
.Pp
The following options are available:
.Bl -tag -width indent
.It Fl L
Print the logical path to the current working directory, as defined
by the shell in the evironment variable PWD.
.It Fl P
Print the physical path to the current working directory, with symbolic
links in the path resolved. This is the default.
.El
.Sh STANDARDS
The
.Nm

View File

@ -1,4 +1,4 @@
/* $NetBSD: pwd.c,v 1.10 1998/07/28 05:31:26 mycroft Exp $ */
/* $NetBSD: pwd.c,v 1.11 1998/11/03 21:38:19 wsanchez Exp $ */
/*
* Copyright (c) 1991, 1993, 1994
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)pwd.c 8.3 (Berkeley) 4/1/94";
#else
__RCSID("$NetBSD: pwd.c,v 1.10 1998/07/28 05:31:26 mycroft Exp $");
__RCSID("$NetBSD: pwd.c,v 1.11 1998/11/03 21:38:19 wsanchez Exp $");
#endif
#endif /* not lint */
@ -51,9 +51,14 @@ __RCSID("$NetBSD: pwd.c,v 1.10 1998/07/28 05:31:26 mycroft Exp $");
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
void usage __P((void));
int main __P((int, char *[]));
char *getcwd_logical __P((char *, size_t));
void usage __P((void));
int main __P((int, char *[]));
int
main(argc, argv)
@ -61,18 +66,16 @@ main(argc, argv)
char *argv[];
{
int ch;
char *p;
int lFlag=0;
char *p = NULL;
/*
* Flags for pwd are a bit strange. The POSIX 1003.2B/D9 document
* has an optional -P flag for physical, which is what this program
* will produce by default. The logical flag, -L, should fail, as
* there's no way to display a logical path after forking. We don't
* document either flag, only adding -P for future portability.
*/
while ((ch = getopt(argc, argv, "P")) != -1)
while ((ch = getopt(argc, argv, "LP")) != -1)
switch (ch) {
case 'L':
lFlag=1;
break;
case 'P':
lFlag=0;
break;
case '?':
default:
@ -84,13 +87,56 @@ main(argc, argv)
if (argc != 0)
usage();
if ((p = getcwd(NULL, 0)) == NULL)
err(1, "%s", "");
if (lFlag)
p = getcwd_logical(NULL, 0);
else
p = getcwd(NULL, 0);
if (p == NULL) err(1, "%s", "");
(void)printf("%s\n", p);
exit(0);
/* NOTREACHED */
}
char *
getcwd_logical(pt, size)
char *pt;
size_t size;
{
char *pwd;
size_t pwdlen;
dev_t dev;
ino_t ino;
struct stat s;
/* Check $PWD -- if it's right, it's fast. */
if ((pwd = getenv("PWD")) != NULL && pwd[0] == '/' && stat(pwd, &s) != -1) {
dev = s.st_dev;
ino = s.st_ino;
if (stat(".", &s) != -1 && dev == s.st_dev && ino == s.st_ino) {
pwdlen = strlen(pwd);
if (pt) {
if (!size) {
errno = EINVAL;
return (NULL);
}
if (pwdlen + 1 > size) {
errno = ERANGE;
return (NULL);
}
} else if ((pt = malloc(pwdlen + 1)) == NULL)
return (NULL);
memmove(pt, pwd, pwdlen);
pt[pwdlen] = '\0';
return (pt);
}
}
return (NULL);
}
void
usage()
{