mirror of
https://git.FreeBSD.org/src.git
synced 2025-02-03 17:11:32 +00:00
Add -L option (SUSv3) to pwd(1). Fix a bug, where realpath(1) would
complain about paths starting with `-', by not calling getopt(3). Submitted by: Tim J. Robbins <tim@robbins.dropbear.id.au> Obtained from: NetBSD (partially) MFC after: 1 month
This commit is contained in:
parent
c9fbdd0fa4
commit
7a396ef4d5
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=90170
@ -35,7 +35,7 @@
|
|||||||
.\" @(#)pwd.1 8.2 (Berkeley) 4/28/95
|
.\" @(#)pwd.1 8.2 (Berkeley) 4/28/95
|
||||||
.\" $FreeBSD$
|
.\" $FreeBSD$
|
||||||
.\"
|
.\"
|
||||||
.Dd April 28, 1995
|
.Dd February 4, 2002
|
||||||
.Dt PWD 1
|
.Dt PWD 1
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
@ -43,6 +43,7 @@
|
|||||||
.Nd return working directory name
|
.Nd return working directory name
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
|
.Op Fl L | P
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
.Nm Pwd
|
.Nm Pwd
|
||||||
writes the absolute pathname of the current working directory to
|
writes the absolute pathname of the current working directory to
|
||||||
@ -54,17 +55,32 @@ command which is similar or identical to this utility.
|
|||||||
Consult the
|
Consult the
|
||||||
.Xr builtin 1
|
.Xr builtin 1
|
||||||
manual page.
|
manual page.
|
||||||
|
.Pp
|
||||||
|
The options are as follows:
|
||||||
|
.Bl -tag -width indent
|
||||||
|
.It Fl L
|
||||||
|
Display the logical current working directory.
|
||||||
|
.It Fl P
|
||||||
|
Display the physical current working directory (all symbolic links resolved).
|
||||||
|
.El
|
||||||
|
.Pp
|
||||||
|
If no options are specified, the
|
||||||
|
.Fl P
|
||||||
|
option is assumed.
|
||||||
|
.Sh ENVIRONMENT
|
||||||
|
Environment variables used by
|
||||||
|
.Nm :
|
||||||
|
.Bl -tag -width PWD
|
||||||
|
.It Ev PWD
|
||||||
|
Logical current working directory.
|
||||||
|
.El
|
||||||
.Sh DIAGNOSTICS
|
.Sh DIAGNOSTICS
|
||||||
.Ex -std
|
.Ex -std
|
||||||
.Sh STANDARDS
|
.Sh STANDARDS
|
||||||
The
|
The
|
||||||
.Nm
|
.Nm
|
||||||
utility is expected to be
|
utility conforms to
|
||||||
.St -p1003.2
|
.St -p1003.1-2001 .
|
||||||
compatible.
|
|
||||||
The
|
|
||||||
.Fl L
|
|
||||||
flag is not supported.
|
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr builtin 1 ,
|
.Xr builtin 1 ,
|
||||||
.Xr cd 1 ,
|
.Xr cd 1 ,
|
||||||
@ -80,3 +96,9 @@ is always faster because it is built into that shell.
|
|||||||
However, it can give a different answer in the rare case
|
However, it can give a different answer in the rare case
|
||||||
that the current directory or a containing directory was moved after
|
that the current directory or a containing directory was moved after
|
||||||
the shell descended into it.
|
the shell descended into it.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fl L
|
||||||
|
option does not work unless the
|
||||||
|
.Ev PWD
|
||||||
|
environment variable is exported by the shell.
|
||||||
|
@ -45,31 +45,47 @@ static const char rcsid[] =
|
|||||||
"$FreeBSD$";
|
"$FreeBSD$";
|
||||||
#endif /* not lint */
|
#endif /* not lint */
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
|
||||||
|
extern char *__progname;
|
||||||
|
|
||||||
|
static char *getcwd_logical(void);
|
||||||
void usage(void);
|
void usage(void);
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
int Lflag, Pflag;
|
||||||
int ch;
|
int ch;
|
||||||
char *p;
|
char *p;
|
||||||
char buf[PATH_MAX];
|
char buf[PATH_MAX];
|
||||||
|
|
||||||
/*
|
if (strcmp(__progname, "realpath") == 0) {
|
||||||
* Flags for pwd are a bit strange. The POSIX 1003.2B/D9 document
|
if (argc != 2)
|
||||||
* has an optional -P flag for physical, which is what this program
|
usage();
|
||||||
* will produce by default. The logical flag, -L, should fail, as
|
if ((p = realpath(argv[1], buf)) == NULL)
|
||||||
* there's no way to display a logical path after forking.
|
err(1, "%s", argv[1]);
|
||||||
*/
|
(void)printf("%s\n", p);
|
||||||
while ((ch = getopt(argc, argv, "P")) != -1)
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Lflag = Pflag = 0;
|
||||||
|
while ((ch = getopt(argc, argv, "LP")) != -1)
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
|
case 'L':
|
||||||
|
Lflag = 1;
|
||||||
|
break;
|
||||||
case 'P':
|
case 'P':
|
||||||
|
Pflag = 1;
|
||||||
break;
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
default:
|
default:
|
||||||
@ -78,19 +94,13 @@ main(int argc, char *argv[])
|
|||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
if (argc == 1) {
|
if (argc != 0 || (Lflag && Pflag))
|
||||||
p = realpath(argv[0], buf);
|
|
||||||
if (p == NULL)
|
|
||||||
err(1, "%s", argv[0]);
|
|
||||||
(void)printf("%s\n", p);
|
|
||||||
} else if (argc == 0) {
|
|
||||||
p = getcwd(NULL, (size_t)0);
|
|
||||||
if (p == NULL)
|
|
||||||
err(1, ".");
|
|
||||||
(void)printf("%s\n", p);
|
|
||||||
} else {
|
|
||||||
usage();
|
usage();
|
||||||
}
|
|
||||||
|
p = Lflag ? getcwd_logical() : getcwd(NULL, 0);
|
||||||
|
if (p == NULL)
|
||||||
|
err(1, ".");
|
||||||
|
(void)printf("%s\n", p);
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
@ -99,6 +109,30 @@ void
|
|||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
(void)fprintf(stderr, "usage: pwd\n");
|
if (strcmp(__progname, "realpath") == 0)
|
||||||
exit(1);
|
(void)fprintf(stderr, "usage: realpath [path]\n");
|
||||||
|
else
|
||||||
|
(void)fprintf(stderr, "usage: pwd [-L | -P]\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *
|
||||||
|
getcwd_logical(void)
|
||||||
|
{
|
||||||
|
struct stat log, phy;
|
||||||
|
char *pwd;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check that $PWD is an absolute logical pathname referring to
|
||||||
|
* the current working directory.
|
||||||
|
*/
|
||||||
|
if ((pwd = getenv("PWD")) != NULL && *pwd == '/') {
|
||||||
|
if (stat(pwd, &log) == -1 || stat(".", &phy) == -1)
|
||||||
|
return (NULL);
|
||||||
|
if (log.st_dev == phy.st_dev && log.st_ino == phy.st_ino)
|
||||||
|
return (pwd);
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = ENOENT;
|
||||||
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user