BSD 4.4 Lite bin Sources

This commit is contained in:
Rodney W. Grimes 1994-05-26 06:18:55 +00:00
parent 02fab103c2
commit 4b88c807ea
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=1556
249 changed files with 75907 additions and 0 deletions

6
bin/Makefile Normal file
View File

@ -0,0 +1,6 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
SUBDIR= cat chmod cp csh date dd df echo ed expr hostname kill ln ls \
mkdir mv pax ps pwd rcp rm rmail rmdir sh sleep stty sync test
.include <bsd.subdir.mk>

3
bin/Makefile.inc Normal file
View File

@ -0,0 +1,3 @@
# @(#)Makefile.inc 8.1 (Berkeley) 5/31/93
BINDIR?= /bin

5
bin/cat/Makefile Normal file
View File

@ -0,0 +1,5 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= cat
.include <bsd.prog.mk>

121
bin/cat/cat.1 Normal file
View File

@ -0,0 +1,121 @@
.\" Copyright (c) 1989, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)cat.1 8.1 (Berkeley) 6/29/93
.\"
.Dd June 29, 1993
.Dt CAT 1
.Os BSD 3
.Sh NAME
.Nm cat
.Nd concatenate and print files
.Sh SYNOPSIS
.Nm cat
.Op Fl benstuv
.Op Fl
.Op Ar
.Sh DESCRIPTION
The
.Nm cat
utility reads files sequentially, writing them to the standard output.
The
.Ar file
operands are processed in command line order.
A single dash represents the standard input.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl b
Implies the
.Fl n
option but doesn't number blank lines.
.It Fl e
Implies the
.Fl v
option, and displays a dollar sign
.Pq Ql \&$
at the end of each line
as well.
.It Fl n
Number the
.Ar output
lines, starting at 1.
.It Fl s
Squeeze multiple adjacent empty lines, causing the output to be
single spaced.
.It Fl t
Implies the
.Fl v
option, and displays tab characters as
.Pq Ql ^I
as well.
.It Fl u
The
.Fl u
option guarantees that the output is unbuffered.
.It Fl v
Displays non-printing characters so they are visible.
Control characters print as
.Ql ^X
for control-X; the delete
character (octal 0177) prints as
.Ql ^?
Non-ascii characters (with the high bit set) are printed as
.Ql M-
(for meta) followed by the character for the low 7 bits.
.El
.Pp
The
.Nm cat
utility exits 0 on success, and >0 if an error occurs.
.Sh BUGS
Because of the shell language mechanism used to perform output
redirection, the command
.Dq Li cat file1 file 2 > file1
will cause the original data in file1 to be destroyed!
.Sh SEE ALSO
.Xr head 1 ,
.Xr more 1 ,
.Xr pr 1 ,
.Xr tail 1
.Rs
.%A Rob Pike
.%T "UNIX Style, or cat -v Considered Harmful"
.%J "USENIX Summer Conference Proceedings"
.%D 1983
.Re
.Sh HISTORY
A
.Nm
command appeared in Version 6 AT&T UNIX.

251
bin/cat/cat.c Normal file
View File

@ -0,0 +1,251 @@
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Kevin Fall.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)cat.c 8.1 (Berkeley) 7/19/93";
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int bflag, eflag, nflag, sflag, tflag, vflag;
int rval;
char *filename;
void cook_args __P((char *argv[]));
void cook_buf __P((FILE *));
void raw_args __P((char *argv[]));
void raw_cat __P((int));
int
main(argc, argv)
int argc;
char *argv[];
{
extern int optind;
int ch;
while ((ch = getopt(argc, argv, "benstuv")) != EOF)
switch (ch) {
case 'b':
bflag = nflag = 1; /* -b implies -n */
break;
case 'e':
eflag = vflag = 1; /* -e implies -v */
break;
case 'n':
nflag = 1;
break;
case 's':
sflag = 1;
break;
case 't':
tflag = vflag = 1; /* -t implies -v */
break;
case 'u':
setbuf(stdout, (char *)NULL);
break;
case 'v':
vflag = 1;
break;
case '?':
(void)fprintf(stderr,
"usage: cat [-benstuv] [-] [file ...]\n");
exit(1);
}
argv += optind;
if (bflag || eflag || nflag || sflag || tflag || vflag)
cook_args(argv);
else
raw_args(argv);
if (fclose(stdout))
err(1, "stdout");
exit(rval);
}
void
cook_args(argv)
char **argv;
{
register FILE *fp;
fp = stdin;
filename = "stdin";
do {
if (*argv) {
if (!strcmp(*argv, "-"))
fp = stdin;
else if ((fp = fopen(*argv, "r")) == NULL) {
warn("%s", *argv);
++argv;
continue;
}
filename = *argv++;
}
cook_buf(fp);
if (fp != stdin)
(void)fclose(fp);
} while (*argv);
}
void
cook_buf(fp)
register FILE *fp;
{
register int ch, gobble, line, prev;
line = gobble = 0;
for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
if (prev == '\n') {
if (ch == '\n') {
if (sflag) {
if (!gobble && putchar(ch) == EOF)
break;
gobble = 1;
continue;
}
if (nflag && !bflag) {
(void)fprintf(stdout, "%6d\t", ++line);
if (ferror(stdout))
break;
}
} else if (nflag) {
(void)fprintf(stdout, "%6d\t", ++line);
if (ferror(stdout))
break;
}
}
gobble = 0;
if (ch == '\n') {
if (eflag)
if (putchar('$') == EOF)
break;
} else if (ch == '\t') {
if (tflag) {
if (putchar('^') == EOF || putchar('I') == EOF)
break;
continue;
}
} else if (vflag) {
if (!isascii(ch)) {
if (putchar('M') == EOF || putchar('-') == EOF)
break;
ch = toascii(ch);
}
if (iscntrl(ch)) {
if (putchar('^') == EOF ||
putchar(ch == '\177' ? '?' :
ch | 0100) == EOF)
break;
continue;
}
}
if (putchar(ch) == EOF)
break;
}
if (ferror(fp)) {
warn("%s", filename);
clearerr(fp);
}
if (ferror(stdout))
err(1, "stdout");
}
void
raw_args(argv)
char **argv;
{
register int fd;
fd = fileno(stdin);
filename = "stdin";
do {
if (*argv) {
if (!strcmp(*argv, "-"))
fd = fileno(stdin);
else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
warn("%s", *argv);
++argv;
continue;
}
filename = *argv++;
}
raw_cat(fd);
if (fd != fileno(stdin))
(void)close(fd);
} while (*argv);
}
void
raw_cat(rfd)
register int rfd;
{
register int nr, nw, off, wfd;
static int bsize;
static char *buf;
struct stat sbuf;
wfd = fileno(stdout);
if (buf == NULL) {
if (fstat(wfd, &sbuf))
err(1, "%s", filename);
bsize = MAX(sbuf.st_blksize, 1024);
if ((buf = malloc((u_int)bsize)) == NULL)
err(1, "");
}
while ((nr = read(rfd, buf, bsize)) > 0)
for (off = 0; nr; nr -= nw, off += nw)
if ((nw = write(wfd, buf + off, nr)) < 0)
err(1, "stdout");
if (nr < 0)
warn("%s", filename);
}

5
bin/chmod/Makefile Normal file
View File

@ -0,0 +1,5 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= chmod
.include <bsd.prog.mk>

294
bin/chmod/chmod.1 Normal file
View File

@ -0,0 +1,294 @@
.\" Copyright (c) 1989, 1990, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)chmod.1 8.4 (Berkeley) 3/31/94
.\"
.Dd March 31, 1994
.Dt CHMOD 1
.Os
.Sh NAME
.Nm chmod
.Nd change file modes
.Sh SYNOPSIS
.Nm chmod
.Oo
.Fl R
.Op Fl H | Fl L | Fl P
.Oc
.Ar mode
.Ar file ...
.Sh DESCRIPTION
The
.Nm chmod
utility modifies the file mode bits of the listed files
as specified by the
.Ar mode
operand.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl H
If the
.Fl R
option is specified, symbolic links on the command line are followed.
(Symbolic links encountered in the tree traversal are not followed.)
.It Fl L
If the
.Fl R
option is specified, all symbolic links are followed.
.It Fl P
If the
.Fl R
option is specified, no symbolic links are followed.
.It Fl R
Change the modes of the file hierarchies rooted in the files
instead of just the files themselves.
.El
.Pp
Symbolic links do not have modes, so unless the
.Fl H
or
.Fl L
option is set,
.Nm chmod
on a symbolic link always succeeds and has no effect.
The
.Fl H ,
.Fl L
and
.Fl P
options are ignored unless the
.Fl R
option is specified.
In addition, these options override each other and the
command's actions are determined by the last one specified.
.Pp
Only the owner of a file or the super-user is permitted to change
the mode of a file.
.Pp
The
.Nm chmod
utility exits 0 on success, and >0 if an error occurs.
.Sh MODES
Modes may be absolute or symbolic.
An absolute mode is an octal number constructed by
.Ar or-ing
the following values:
.Pp
.Bl -tag -width 6n -compact -offset indent
.It Li 4000
set-user-ID-on-execution
.It Li 2000
set-group-ID-on-execution
.It Li 1000
sticky bit, see chmod(2)
.It Li 0400
read by owner
.It Li 0200
write by owner
.It Li 0100
execute (or search for directories) by owner
.It Li 0070
read, write, execute/search by group
.It Li 0007
read, write, execute/search by others
.El
.Pp
The read, write, and execute/search values for group and others
are encoded as described for owner.
.Pp
The symbolic mode is described by the following grammar:
.Bd -literal -offset indent
mode ::= clause [, clause ...]
clause ::= [who ...] [action ...] last_action
action ::= op [perm ...]
last_action ::= op [perm ...]
who ::= a | u | g | o
op ::= + | \- | =
perm ::= r | s | t | w | x | X | u | g | o
.Ed
.Pp
The
.Ar who
symbols ``u'', ``g'', and ``o'' specify the user, group, and other parts
of the mode bits, respectively.
The
.Ar who
symbol ``a'' is equivalent to ``ugo''.
.Pp
.ne 1i
The
.Ar perm
symbols represent the portions of the mode bits as follows:
.Pp
.Bl -tag -width Ds -compact -offset indent
.It r
The read bits.
.It s
The set-user-ID-on-execution and set-group-ID-on-execution bits.
.It t
The sticky bit.
.It w
The write bits.
.It x
The execute/search bits.
.It X
The execute/search bits if the file is a directory or any of the
execute/search bits are set in the original (unmodified) mode.
Operations with the
.Ar perm
symbol ``X'' are only meaningful in conjunction with the
.Ar op
symbol ``+'', and are ignored in all other cases.
.It u
The user permission bits in the mode of the original file.
.It g
The group permission bits in the mode of the original file.
.It o
The other permission bits in the mode of the original file.
.El
.Pp
The
.Ar op
symbols represent the operation performed, as follows:
.Bl -tag -width 4n
.It +
If no value is supplied for
.Ar perm ,
the ``+'' operation has no effect.
If no value is supplied for
.Ar who ,
each permission bit specified in
.Ar perm ,
for which the corresponding bit in the file mode creation mask
is clear, is set.
Otherwise, the mode bits represented by the specified
.Ar who
and
.Ar perm
values are set.
.It \&\-
If no value is supplied for
.Ar perm ,
the ``\-'' operation has no effect.
If no value is supplied for
.Ar who ,
each permission bit specified in
.Ar perm ,
for which the corresponding bit in the file mode creation mask
is clear, is cleared.
Otherwise, the mode bits represented by the specified
.Ar who
and
.Ar perm
values are cleared.
.It =
The mode bits specified by the
.Ar who
value are cleared, or, if no who value is specified, the owner, group
and other mode bits are cleared.
Then, if no value is supplied for
.Ar who ,
each permission bit specified in
.Ar perm ,
for which the corresponding bit in the file mode creation mask
is clear, is set.
Otherwise, the mode bits represented by the specified
.Ar who
and
.Ar perm
values are set.
.El
.Pp
Each
.Ar clause
specifies one or more operations to be performed on the mode
bits, and each operation is applied to the mode bits in the
order specified.
.Pp
Operations upon the other permissions only (specified by the symbol
``o'' by itself), in combination with the
.Ar perm
symbols ``s'' or ``t'', are ignored.
.Sh EXAMPLES
.Bl -tag -width "u=rwx,go=u-w" -compact
.It Li 644
make a file readable by anyone and writable by the owner only.
.Pp
.It Li go-w
deny write permission to group and others.
.Pp
.It Li =rw,+X
set the read and write permissions to the usual defaults, but
retain any execute permissions that are currently set.
.Pp
.It Li +X
make a directory or file searchable/executable by everyone if it is
already searchable/executable by anyone.
.Pp
.It Li 755
.It Li u=rwx,go=rx
.It Li u=rwx,go=u-w
make a file readable/executable by everyone and writable by the owner only.
.Pp
.It Li go=
clear all mode bits for group and others.
.Pp
.It Li g=u-w
set the group bits equal to the user bits, but clear the group write bit.
.El
.Sh BUGS
There's no
.Ar perm
option for the naughty bits.
.Sh SEE ALSO
.Xr install 1 ,
.Xr chmod 2 ,
.Xr stat 2 ,
.Xr umask 2 ,
.Xr fts 3 ,
.Xr setmode 3 ,
.Xr symlink 7 ,
.Xr chown 8
.Sh STANDARDS
The
.Nm chmod
utility is expected to be POSIX 1003.2
compatible with the exception of the
.Ar perm
symbols
.Dq t
and
.Dq X
which are not included in that standard.

201
bin/chmod/chmod.c Normal file
View File

@ -0,0 +1,201 @@
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1989, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fts.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void usage __P((void));
int
main(argc, argv)
int argc;
char *argv[];
{
FTS *ftsp;
FTSENT *p;
mode_t *set;
long val;
int oct, omode;
int Hflag, Lflag, Pflag, Rflag, ch, fflag, fts_options, hflag, rval;
char *ep, *mode;
Hflag = Lflag = Pflag = Rflag = fflag = hflag = 0;
while ((ch = getopt(argc, argv, "HLPRXfgorstuwx")) != EOF)
switch (ch) {
case 'H':
Hflag = 1;
Lflag = Pflag = 0;
break;
case 'L':
Lflag = 1;
Hflag = Pflag = 0;
break;
case 'P':
Pflag = 1;
Hflag = Lflag = 0;
break;
case 'R':
Rflag = 1;
break;
case 'f': /* XXX: undocumented. */
fflag = 1;
break;
case 'h':
/*
* In System V (and probably POSIX.2) the -h option
* causes chmod to change the mode of the symbolic
* link. 4.4BSD's symbolic links don't have modes,
* so it's an undocumented noop. Do syntax checking,
* though.
*/
hflag = 1;
break;
/*
* XXX
* "-[rwx]" are valid mode commands. If they are the entire
* argument, getopt has moved past them, so decrement optind.
* Regardless, we're done argument processing.
*/
case 'g': case 'o': case 'r': case 's':
case 't': case 'u': case 'w': case 'X': case 'x':
if (argv[optind - 1][0] == '-' &&
argv[optind - 1][1] == ch &&
argv[optind - 1][2] == '\0')
--optind;
goto done;
case '?':
default:
usage();
}
done: argv += optind;
argc -= optind;
if (argc < 2)
usage();
fts_options = FTS_PHYSICAL;
if (Rflag) {
if (hflag)
errx(1,
"the -R and -h options may not be specified together.");
if (Hflag)
fts_options |= FTS_COMFOLLOW;
if (Lflag) {
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
}
}
mode = *argv;
if (*mode >= '0' && *mode <= '7') {
errno = 0;
val = strtol(mode, &ep, 8);
if (val > INT_MAX || val < 0)
errno = ERANGE;
if (errno)
err(1, "invalid file mode: %s", mode);
if (*ep)
errx(1, "invalid file mode: %s", mode);
omode = val;
oct = 1;
} else {
if ((set = setmode(mode)) == NULL)
errx(1, "invalid file mode: %s", mode);
oct = 0;
}
if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
err(1, NULL);
for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
switch (p->fts_info) {
case FTS_D:
if (Rflag) /* Change it at FTS_DP. */
continue;
fts_set(ftsp, p, FTS_SKIP);
break;
case FTS_DNR: /* Warn, chmod, continue. */
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
rval = 1;
break;
case FTS_ERR: /* Warn, continue. */
case FTS_NS:
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
rval = 1;
continue;
case FTS_SL: /* Ignore. */
case FTS_SLNONE:
/*
* The only symlinks that end up here are ones that
* don't point to anything and ones that we found
* doing a physical walk.
*/
continue;
default:
break;
}
if (chmod(p->fts_accpath, oct ? omode :
getmode(set, p->fts_statp->st_mode)) && !fflag) {
warn(p->fts_path);
rval = 1;
}
}
if (errno)
err(1, "fts_read");
exit(rval);
}
void
usage()
{
(void)fprintf(stderr,
"usage: chmod [-R [-H | -L | -P]] mode file ...\n");
exit(1);
}

6
bin/cp/Makefile Normal file
View File

@ -0,0 +1,6 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= cp
SRCS= cp.c utils.c
.include <bsd.prog.mk>

211
bin/cp/cp.1 Normal file
View File

@ -0,0 +1,211 @@
.\" Copyright (c) 1989, 1990, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)cp.1 8.3 (Berkeley) 4/18/94
.\"
.Dd April 18, 1994
.Dt CP 1
.Os BSD 4
.Sh NAME
.Nm cp
.Nd copy files
.Sh SYNOPSIS
.Nm cp
.Oo
.Fl R
.Op Fl H | Fl L | Fl P
.Oc
.Op Fl fip
.Ar source_file target_file
.Nm cp
.Oo
.Fl R
.Op Fl H | Fl L | Fl P
.Oc
.Op Fl fip
.Ar source_file ... target_directory
.Sh DESCRIPTION
In the first synopsis form, the
.Nm cp
utility copies the contents of the
.Ar source_file
to the
.Ar target_file .
In the second synopsis form,
the contents of each named
.Ar source_file
is copied to the destination
.Ar target_directory .
The names of the files themselves are not changed.
If
.Nm cp
detects an attempt to copy a file to itself, the copy will fail.
.Pp
The following options are available:
.Bl -tag -width flag
.It Fl H
If the
.Fl R
option is specified, symbolic links on the command line are followed.
(Symbolic links encountered in the tree traversal are not followed.)
.It Fl L
If the
.Fl R
option is specified, all symbolic links are followed.
.It Fl P
If the
.Fl R
option is specified, no symbolic links are followed.
.It Fl R
If
.Ar source_file
designates a directory,
.Nm cp
copies the directory and the entire subtree connected at that point.
This option also causes symbolic links to be copied, rather than
indirected through, and for
.Nm cp
to create special files rather than copying them as normal files.
Created directories have the same mode as the corresponding source
directory, unmodified by the process' umask.
.It Fl f
For each existing destination pathname, remove it and
create a new file, without prompting for confirmation
regardless of its permissions.
(The
.Fl i
option is ignored if the
.Fl f
option is specified.)
.It Fl i
Causes
.Nm cp
to write a prompt to the standard error output before copying a file
that would overwrite an existing file.
If the response from the standard input begins with the character
.Sq Li y ,
the file copy is attempted.
.It Fl p
Causes
.Nm cp
to preserve in the copy as many of the modification time, access time,
file flags, file mode, user ID, and group ID as allowed by permissions.
.Pp
If the user ID and group ID cannot be preserved, no error message
is displayed and the exit value is not altered.
.Pp
If the source file has its set user ID bit on and the user ID cannot
be preserved, the set user ID bit is not preserved
in the copy's permissions.
If the source file has its set group ID bit on and the group ID cannot
be preserved, the set group ID bit is not preserved
in the copy's permissions.
If the source file has both its set user ID and set group ID bits on,
and either the user ID or group ID cannot be preserved, neither
the set user ID or set group ID bits are preserved in the copy's
permissions.
.El
.Pp
For each destination file that already exists, its contents are
overwritten if permissions allow, but its mode, user ID, and group
ID are unchanged.
.Pp
In the second synopsis form,
.Ar target_directory
must exist unless there is only one named
.Ar source_file
which is a directory and the
.Fl R
flag is specified.
.Pp
If the destination file does not exist, the mode of the source file is
used as modified by the file mode creation mask
.Pf ( Ic umask ,
see
.Xr csh 1 ) .
If the source file has its set user ID bit on, that bit is removed
unless both the source file and the destination file are owned by the
same user.
If the source file has its set group ID bit on, that bit is removed
unless both the source file and the destination file are in the same
group and the user is a member of that group.
If both the set user ID and set group ID bits are set, all of the above
conditions must be fulfilled or both bits are removed.
.Pp
Appropriate permissions are required for file creation or overwriting.
.Pp
Symbolic links are always followed unless the
.Fl R
flag is set, in which case symbolic links are not followed, by default.
The
.Fl H
or
.Fl L
flags (in conjunction with the
.Fl R
flag) cause symbolic links to be followed as described above.
The
.Fl H ,
.Fl L
and
.Fl P
options are ignored unless the
.Fl R
option is specified.
In addition, these options override each other and the
command's actions are determined by the last one specified.
.Pp
.Nm Cp
exits 0 on success, >0 if an error occurred.
.Sh COMPATIBILITY
Historic versions of the
.Nm cp
utility had a
.Fl r
option.
This implementation supports that option, however, its use is strongly
discouraged, as it does not correctly copy special files, symbolic links
or fifo's.
.Sh SEE ALSO
.Xr mv 1 ,
.Xr rcp 1 ,
.Xr umask 2 ,
.Xr fts 3 ,
.Xr symlink 7
.Sh HISTORY
The
.Nm cp
command is expected to be
.St -p1003.2
compatible.

441
bin/cp/cp.c Normal file
View File

@ -0,0 +1,441 @@
/*
* Copyright (c) 1988, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* David Hitz of Auspex Systems Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1988, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)cp.c 8.2 (Berkeley) 4/1/94";
#endif /* not lint */
/*
* Cp copies source files to target files.
*
* The global PATH_T structure "to" always contains the path to the
* current target file. Since fts(3) does not change directories,
* this path can be either absolute or dot-realative.
*
* The basic algorithm is to initialize "to" and use fts(3) to traverse
* the file hierarchy rooted in the argument list. A trivial case is the
* case of 'cp file1 file2'. The more interesting case is the case of
* 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
* path (relative to the root of the traversal) is appended to dir (stored
* in "to") to form the final target path.
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
#define STRIP_TRAILING_SLASH(p) { \
while ((p).p_end > (p).p_path && (p).p_end[-1] == '/') \
*--(p).p_end = 0; \
}
PATH_T to = { to.p_path, "" };
uid_t myuid;
int Rflag, iflag, pflag, rflag;
int myumask;
enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
int copy __P((char *[], enum op, int));
int mastercmp __P((const FTSENT **, const FTSENT **));
int
main(argc, argv)
int argc;
char *argv[];
{
struct stat to_stat, tmp_stat;
enum op type;
int Hflag, Lflag, Pflag, ch, fts_options, r;
char *target;
Hflag = Lflag = Pflag = Rflag = 0;
while ((ch = getopt(argc, argv, "HLPRfipr")) != EOF)
switch (ch) {
case 'H':
Hflag = 1;
Lflag = Pflag = 0;
break;
case 'L':
Lflag = 1;
Hflag = Pflag = 0;
break;
case 'P':
Pflag = 1;
Hflag = Lflag = 0;
break;
case 'R':
Rflag = 1;
break;
case 'f':
iflag = 0;
break;
case 'i':
iflag = isatty(fileno(stdin));
break;
case 'p':
pflag = 1;
break;
case 'r':
rflag = 1;
break;
case '?':
default:
usage();
break;
}
argc -= optind;
argv += optind;
if (argc < 2)
usage();
fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
if (rflag) {
if (Rflag)
errx(1,
"the -R and -r options may not be specified together.");
if (Hflag || Lflag || Pflag)
errx(1,
"the -H, -L, and -P options may not be specified with the -r option.");
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
}
if (Rflag) {
if (Hflag)
fts_options |= FTS_COMFOLLOW;
if (Lflag) {
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
}
} else {
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
}
myuid = getuid();
/* Copy the umask for explicit mode setting. */
myumask = umask(0);
(void)umask(myumask);
/* Save the target base in "to". */
target = argv[--argc];
if (strlen(target) > MAXPATHLEN)
errx(1, "%s: name too long", target);
(void)strcpy(to.p_path, target);
to.p_end = to.p_path + strlen(to.p_path);
if (to.p_path == to.p_end) {
*to.p_end++ = '.';
*to.p_end = 0;
}
STRIP_TRAILING_SLASH(to);
to.target_end = to.p_end;
/* Set end of argument list for fts(3). */
argv[argc] = NULL;
/*
* Cp has two distinct cases:
*
* cp [-R] source target
* cp [-R] source1 ... sourceN directory
*
* In both cases, source can be either a file or a directory.
*
* In (1), the target becomes a copy of the source. That is, if the
* source is a file, the target will be a file, and likewise for
* directories.
*
* In (2), the real target is not directory, but "directory/source".
*/
r = stat(to.p_path, &to_stat);
if (r == -1 && errno != ENOENT)
err(1, "%s", to.p_path);
if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
/*
* Case (1). Target is not a directory.
*/
if (argc > 1) {
usage();
exit(1);
}
/*
* Need to detect the case:
* cp -R dir foo
* Where dir is a directory and foo does not exist, where
* we want pathname concatenations turned on but not for
* the initial mkdir().
*/
if (r == -1) {
if (rflag || (Rflag && (Lflag || Hflag)))
stat(*argv, &tmp_stat);
else
lstat(*argv, &tmp_stat);
if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
type = DIR_TO_DNE;
else
type = FILE_TO_FILE;
} else
type = FILE_TO_FILE;
} else
/*
* Case (2). Target is a directory.
*/
type = FILE_TO_DIR;
exit (copy(argv, type, fts_options));
}
int
copy(argv, type, fts_options)
char *argv[];
enum op type;
int fts_options;
{
struct stat to_stat;
FTS *ftsp;
FTSENT *curr;
int base, dne, nlen, rval;
char *p;
if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
err(1, NULL);
for (rval = 0; (curr = fts_read(ftsp)) != NULL;) {
switch (curr->fts_info) {
case FTS_NS:
case FTS_ERR:
warnx("%s: %s",
curr->fts_path, strerror(curr->fts_errno));
rval = 1;
continue;
case FTS_DC: /* Warn, continue. */
warnx("%s: directory causes a cycle", curr->fts_path);
rval = 1;
continue;
case FTS_DP: /* Ignore, continue. */
continue;
}
/*
* If we are in case (2) or (3) above, we need to append the
* source name to the target name.
*/
if (type != FILE_TO_FILE) {
if ((curr->fts_namelen +
to.target_end - to.p_path + 1) > MAXPATHLEN) {
warnx("%s/%s: name too long (not copied)",
to.p_path, curr->fts_name);
rval = 1;
continue;
}
/*
* Need to remember the roots of traversals to create
* correct pathnames. If there's a directory being
* copied to a non-existent directory, e.g.
* cp -R a/dir noexist
* the resulting path name should be noexist/foo, not
* noexist/dir/foo (where foo is a file in dir), which
* is the case where the target exists.
*
* Also, check for "..". This is for correct path
* concatentation for paths ending in "..", e.g.
* cp -R .. /tmp
* Paths ending in ".." are changed to ".". This is
* tricky, but seems the easiest way to fix the problem.
*
* XXX
* Since the first level MUST be FTS_ROOTLEVEL, base
* is always initialized.
*/
if (curr->fts_level == FTS_ROOTLEVEL)
if (type != DIR_TO_DNE) {
p = strrchr(curr->fts_path, '/');
base = (p == NULL) ? 0 :
(int)(p - curr->fts_path + 1);
if (!strcmp(&curr->fts_path[base],
".."))
base += 1;
} else
base = curr->fts_pathlen;
if (to.target_end[-1] != '/') {
*to.target_end = '/';
*(to.target_end + 1) = 0;
}
p = &curr->fts_path[base];
nlen = curr->fts_pathlen - base;
(void)strncat(to.target_end + 1, p, nlen);
to.p_end = to.target_end + nlen + 1;
*to.p_end = 0;
STRIP_TRAILING_SLASH(to);
}
/* Not an error but need to remember it happened */
if (stat(to.p_path, &to_stat) == -1)
dne = 1;
else {
if (to_stat.st_dev == curr->fts_statp->st_dev &&
to_stat.st_ino == curr->fts_statp->st_ino) {
warnx("%s and %s are identical (not copied).",
to.p_path, curr->fts_path);
rval = 1;
if (S_ISDIR(curr->fts_statp->st_mode))
(void)fts_set(ftsp, curr, FTS_SKIP);
continue;
}
dne = 0;
}
switch (curr->fts_statp->st_mode & S_IFMT) {
case S_IFLNK:
if (copy_link(curr, !dne))
rval = 1;
break;
case S_IFDIR:
if (!Rflag && !rflag) {
warnx("%s is a directory (not copied).",
curr->fts_path);
(void)fts_set(ftsp, curr, FTS_SKIP);
rval = 1;
break;
}
/*
* If the directory doesn't exist, create the new
* one with the from file mode plus owner RWX bits,
* modified by the umask. Trade-off between being
* able to write the directory (if from directory is
* 555) and not causing a permissions race. If the
* umask blocks owner writes, we fail..
*/
if (dne) {
if (mkdir(to.p_path,
curr->fts_statp->st_mode | S_IRWXU) < 0)
err(1, "%s", to.p_path);
} else if (!S_ISDIR(to_stat.st_mode)) {
errno = ENOTDIR;
err(1, "%s: %s", to.p_path);
}
/*
* If not -p and directory didn't exist, set it to be
* the same as the from directory, umodified by the
* umask; arguably wrong, but it's been that way
* forever.
*/
if (pflag && setfile(curr->fts_statp, 0))
rval = 1;
else if (dne)
(void)chmod(to.p_path,
curr->fts_statp->st_mode);
break;
case S_IFBLK:
case S_IFCHR:
if (Rflag) {
if (copy_special(curr->fts_statp, !dne))
rval = 1;
} else
if (copy_file(curr, dne))
rval = 1;
break;
case S_IFIFO:
if (Rflag)
if (copy_fifo(curr->fts_statp, !dne))
rval = 1;
else
if (copy_file(curr, dne))
rval = 1;
break;
default:
if (copy_file(curr, dne))
rval = 1;
break;
}
}
if (errno)
err(1, "fts_read");
return (rval);
}
/*
* mastercmp --
* The comparison function for the copy order. The order is to copy
* non-directory files before directory files. The reason for this
* is because files tend to be in the same cylinder group as their
* parent directory, whereas directories tend not to be. Copying the
* files first reduces seeking.
*/
int
mastercmp(a, b)
const FTSENT **a, **b;
{
int a_info, b_info;
a_info = (*a)->fts_info;
if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
return (0);
b_info = (*b)->fts_info;
if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
return (0);
if (a_info == FTS_D)
return (-1);
if (b_info == FTS_D)
return (1);
return (0);
}

55
bin/cp/extern.h Normal file
View File

@ -0,0 +1,55 @@
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)extern.h 8.2 (Berkeley) 4/1/94
*/
typedef struct {
char *p_end; /* pointer to NULL at end of path */
char *target_end; /* pointer to end of target base */
char p_path[MAXPATHLEN + 1]; /* pointer to the start of a path */
} PATH_T;
extern PATH_T to;
extern uid_t myuid;
extern int iflag, pflag, myumask;
#include <sys/cdefs.h>
__BEGIN_DECLS
int copy_fifo __P((struct stat *, int));
int copy_file __P((FTSENT *, int));
int copy_link __P((FTSENT *, int));
int copy_special __P((struct stat *, int));
int setfile __P((struct stat *, int));
void usage __P((void));
__END_DECLS

285
bin/cp/utils.c Normal file
View File

@ -0,0 +1,285 @@
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
int
copy_file(entp, dne)
FTSENT *entp;
int dne;
{
static char buf[MAXBSIZE];
struct stat to_stat, *fs;
int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
char *p;
#endif
if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
warn("%s", entp->fts_path);
return (1);
}
fs = entp->fts_statp;
/*
* If the file exists and we're interactive, verify with the user.
* If the file DNE, set the mode to be the from file, minus setuid
* bits, modified by the umask; arguably wrong, but it makes copying
* executables work right and it's been that way forever. (The
* other choice is 666 or'ed with the execute bits on the from file
* modified by the umask.)
*/
if (!dne) {
if (iflag) {
(void)fprintf(stderr, "overwrite %s? ", to.p_path);
checkch = ch = getchar();
while (ch != '\n' && ch != EOF)
ch = getchar();
if (checkch != 'y') {
(void)close(from_fd);
return (0);
}
}
to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
} else
to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
fs->st_mode & ~(S_ISUID | S_ISGID));
if (to_fd == -1) {
warn("%s", to.p_path);
(void)close(from_fd);
return (1);;
}
rval = 0;
/*
* Mmap and write if less than 8M (the limit is so we don't totally
* trash memory on big files. This is really a minor hack, but it
* wins some CPU back.
*/
#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
if (fs->st_size <= 8 * 1048576) {
if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
0, from_fd, (off_t)0)) == (char *)-1) {
warn("%s", entp->fts_path);
rval = 1;
} else {
if (write(to_fd, p, fs->st_size) != fs->st_size) {
warn("%s", to.p_path);
rval = 1;
}
/* Some systems don't unmap on close(2). */
if (munmap(p, fs->st_size) < 0) {
warn("%s", entp->fts_path);
rval = 1;
}
}
} else
#endif
{
while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
wcount = write(to_fd, buf, rcount);
if (rcount != wcount || wcount == -1) {
warn("%s", to.p_path);
rval = 1;
break;
}
}
if (rcount < 0) {
warn("%s", entp->fts_path);
rval = 1;
}
}
/* If the copy went bad, lose the file. */
if (rval == 1) {
(void)unlink(to.p_path);
(void)close(from_fd);
(void)close(to_fd);
return (1);
}
if (pflag && setfile(fs, to_fd))
rval = 1;
/*
* If the source was setuid or setgid, lose the bits unless the
* copy is owned by the same user and group.
*/
#define RETAINBITS \
(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid)
if (fstat(to_fd, &to_stat)) {
warn("%s", to.p_path);
rval = 1;
} else if (fs->st_gid == to_stat.st_gid &&
fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
warn("%s", to.p_path);
rval = 1;
}
(void)close(from_fd);
if (close(to_fd)) {
warn("%s", to.p_path);
rval = 1;
}
return (rval);
}
int
copy_link(p, exists)
FTSENT *p;
int exists;
{
int len;
char link[MAXPATHLEN];
if ((len = readlink(p->fts_path, link, sizeof(link))) == -1) {
warn("readlink: %s", p->fts_path);
return (1);
}
link[len] = '\0';
if (exists && unlink(to.p_path)) {
warn("unlink: %s", to.p_path);
return (1);
}
if (symlink(link, to.p_path)) {
warn("symlink: %s", link);
return (1);
}
return (0);
}
int
copy_fifo(from_stat, exists)
struct stat *from_stat;
int exists;
{
if (exists && unlink(to.p_path)) {
warn("unlink: %s", to.p_path);
return (1);
}
if (mkfifo(to.p_path, from_stat->st_mode)) {
warn("mkfifo: %s", to.p_path);
return (1);
}
return (pflag ? setfile(from_stat, 0) : 0);
}
int
copy_special(from_stat, exists)
struct stat *from_stat;
int exists;
{
if (exists && unlink(to.p_path)) {
warn("unlink: %s", to.p_path);
return (1);
}
if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
warn("mknod: %s", to.p_path);
return (1);
}
return (pflag ? setfile(from_stat, 0) : 0);
}
int
setfile(fs, fd)
register struct stat *fs;
int fd;
{
static struct timeval tv[2];
int rval;
rval = 0;
fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
if (utimes(to.p_path, tv)) {
warn("utimes: %s", to.p_path);
rval = 1;
}
/*
* Changing the ownership probably won't succeed, unless we're root
* or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
* the mode; current BSD behavior is to remove all setuid bits on
* chown. If chown fails, lose setuid/setgid bits.
*/
if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
chown(to.p_path, fs->st_uid, fs->st_gid)) {
if (errno != EPERM) {
warn("chown: %s", to.p_path);
rval = 1;
}
fs->st_mode &= ~(S_ISUID | S_ISGID);
}
if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
warn("chown: %s", to.p_path);
rval = 1;
}
if (fd ?
fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
warn("chflags: %s", to.p_path);
rval = 1;
}
return (rval);
}
void
usage()
{
(void)fprintf(stderr, "%s\n%s\n",
"usage: cp [-R [-H | -L | -P] [-fip] src target",
" cp [-R [-H | -L | -P] [-fip] src1 ... srcN directory");
exit(1);
}

44
bin/csh/Makefile Normal file
View File

@ -0,0 +1,44 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
#
# C Shell with process control; VM/UNIX VAX Makefile
# Bill Joy UC Berkeley; Jim Kulp IIASA, Austria
#
# To profile, put -DPROF in DEFS and -pg in CFLAGS, and recompile.
PROG= csh
DFLAGS=-DBUILTIN -DFILEC -DNLS -DSHORT_STRINGS
#CFLAGS+=-g
#CFLAGS+=-Wall -ansi -pedantic
CFLAGS+=-I${.CURDIR} -I. ${DFLAGS}
SRCS= alloc.c char.c const.c csh.c dir.c dol.c err.c exec.c exp.c file.c \
func.c glob.c hist.c init.c lex.c misc.c parse.c printf.c proc.c \
sem.c set.c str.c time.c
.PATH: ${.CURDIR}/../../usr.bin/printf
MAN1= csh.0
MLINKS= csh.1 limit.1 csh.1 alias.1 csh.1 bg.1 csh.1 dirs.1 csh.1 fg.1 \
csh.1 foreach.1 csh.1 history.1 csh.1 jobs.1 csh.1 popd.1 \
csh.1 pushd.1 csh.1 rehash.1 csh.1 repeat.1 csh.1 suspend.1 \
csh.1 stop.1 csh.1 source.1
CLEANFILES+=err.h const.h
const.h: err.h
err.h: err.c
@rm -f $@
@echo '/* Do not edit this file, make creates it. */' > $@
@echo '#ifndef _h_sh_err' >> $@
@echo '#define _h_sh_err' >> $@
egrep 'ERR_' ${.CURDIR}/$*.c | egrep '^#define' >> $@
@echo '#endif /* _h_sh_err */' >> $@
const.h: const.c
@rm -f $@
@echo '/* Do not edit this file, make creates it. */' > $@
${CC} -E ${CFLAGS} ${.CURDIR}/$*.c | egrep 'Char STR' | \
sed -e 's/Char \([a-zA-Z0-9_]*\)\(.*\)/extern Char \1[];/' | \
sort >> $@
.depend: const.h err.h
.include <bsd.prog.mk>

7
bin/csh/USD.doc/Makefile Normal file
View File

@ -0,0 +1,7 @@
# @(#)Makefile 8.1 (Berkeley) 8/14/93
DIR= usd/04.csh
SRCS= tabs csh.1 csh.2 csh.3 csh.4 csh.a csh.g
MACROS= -ms
.include <bsd.doc.mk>

1012
bin/csh/USD.doc/csh.1 Normal file

File diff suppressed because it is too large Load Diff

1305
bin/csh/USD.doc/csh.2 Normal file

File diff suppressed because it is too large Load Diff

650
bin/csh/USD.doc/csh.3 Normal file
View File

@ -0,0 +1,650 @@
.\" Copyright (c) 1980, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)csh.3 8.1 (Berkeley) 6/8/93
.\"
.nr H1 2
.NH
Shell control structures and command scripts
.NH 2
Introduction
.PP
It is possible to place commands in files and to cause shells to be
invoked to read and execute commands from these files,
which are called
.I "shell scripts."
We here detail those features of the shell useful to the writers of such
scripts.
.NH 2
Make
.PP
It is important to first note what shell scripts are
.I not
useful for.
There is a program called
.I make
which is very useful for maintaining a group of related files
or performing sets of operations on related files.
For instance a large program consisting of one or more files
can have its dependencies described in a
.I makefile
which contains definitions of the commands used to create these
different files when changes occur.
Definitions of the means for printing listings, cleaning up the directory
in which the files reside, and installing the resultant programs
are easily, and most appropriately placed in this
.I makefile.
This format is superior and preferable to maintaining a group of shell
procedures to maintain these files.
.PP
Similarly when working on a document a
.I makefile
may be created which defines how different versions of the document
are to be created and which options of
.I nroff
or
.I troff
are appropriate.
.NH 2
Invocation and the argv variable
.PP
A
.I csh
command script may be interpreted by saying
.DS
% csh script ...
.DE
where
.I script
is the name of the file containing a group of
.I csh
commands and
`\&...' is replaced by a sequence of arguments.
The shell places these arguments in the variable
.I argv
and then begins to read commands from the script.
These parameters are then available through the same mechanisms
which are used to reference any other shell variables.
.PP
If you make the file
`script'
executable by doing
.DS
chmod 755 script
.DE
and place a shell comment at the beginning of the shell script
(i.e. begin the file with a `#' character)
then a `/bin/csh' will automatically be invoked to execute `script' when
you type
.DS
script
.DE
If the file does not begin with a `#' then the standard shell
`/bin/sh' will be used to execute it.
This allows you to convert your older shell scripts to use
.I csh
at your convenience.
.NH 2
Variable substitution
.PP
After each input line is broken into words and history substitutions
are done on it, the input line is parsed into distinct commands.
Before each command is executed a mechanism know as
.I "variable substitution"
is done on these words.
Keyed by the character `$' this substitution replaces the names
of variables by their values.
Thus
.DS
echo $argv
.DE
when placed in a command script would cause the current value of the
variable
.I argv
to be echoed to the output of the shell script.
It is an error for
.I argv
to be unset at this point.
.PP
A number of notations are provided for accessing components and attributes
of variables.
The notation
.DS
$?name
.DE
expands to `1' if name is
.I set
or to `0'
if name is not
.I set.
It is the fundamental mechanism used for checking whether particular
variables have been assigned values.
All other forms of reference to undefined variables cause errors.
.PP
The notation
.DS
$#name
.DE
expands to the number of elements in the variable
.I name.
Thus
.DS
% set argv=(a b c)
% echo $?argv
1
% echo $#argv
3
% unset argv
% echo $?argv
0
% echo $argv
Undefined variable: argv.
%
.DE
.PP
It is also possible to access the components of a variable
which has several values.
Thus
.DS
$argv[1]
.DE
gives the first component of
.I argv
or in the example above `a'.
Similarly
.DS
$argv[$#argv]
.DE
would give `c',
and
.DS
$argv[1\-2]
.DE
would give `a b'. Other notations useful in shell scripts are
.DS
$\fIn\fR
.DE
where
.I n
is an integer as a shorthand for
.DS
$argv[\fIn\fR\|]
.DE
the
.I n\|th
parameter and
.DS
$*
.DE
which is a shorthand for
.DS
$argv
.DE
The form
.DS
$$
.DE
expands to the process number of the current shell.
Since this process number is unique in the system it can
be used in generation of unique temporary file names.
The form
.DS
$<
.DE
is quite special and is replaced by the next line of input read from
the shell's standard input (not the script it is reading). This is
useful for writing shell scripts that are interactive, reading
commands from the terminal, or even writing a shell script that
acts as a filter, reading lines from its input file. Thus the sequence
.DS
echo 'yes or no?\ec'
set a=($<)
.DE
would write out the prompt `yes or no?' without a newline and then
read the answer into the variable `a'. In this case `$#a' would be
`0' if either a blank line or end-of-file (^D) was typed.
.PP
One minor difference between `$\fIn\fR\|' and `$argv[\fIn\fR\|]'
should be noted here.
The form
`$argv[\fIn\fR\|]'
will yield an error if
.I n
is not in the range
`1\-$#argv'
while `$n'
will never yield an out of range subscript error.
This is for compatibility with the way older shells handled parameters.
.PP
Another important point is that it is never an error to give a subrange
of the form `n\-'; if there are less than
.I n
components of the given variable then no words are substituted.
A range of the form `m\-n' likewise returns an empty vector without giving
an error when \fIm\fR exceeds the number of elements of the given variable,
provided the subscript \fIn\fR is in range.
.NH 2
Expressions
.PP
In order for interesting shell scripts to be constructed it
must be possible to evaluate expressions in the shell based on the
values of variables.
In fact, all the arithmetic operations of the language C are available
in the shell
with the same precedence that they have in C.
In particular, the operations `==' and `!=' compare strings
and the operators `&&' and `|\|\||' implement the boolean and/or operations.
The special operators `=~' and `!~' are similar to `==' and `!=' except
that the string on the right side can have pattern matching characters
(like *, ? or []) and the test is whether the string on the left matches
the pattern on the right.
.PP
The shell also allows file enquiries of the form
.DS
\-? filename
.DE
where `?' is replace by a number of single characters.
For instance the expression primitive
.DS
\-e filename
.DE
tell whether the file
`filename'
exists.
Other primitives test for read, write and execute access to the file,
whether it is a directory, or has non-zero length.
.PP
It is possible to test whether a command terminates normally,
by a primitive of the
form `{ command }' which returns true, i.e. `1' if the command
succeeds exiting normally with exit status 0, or `0' if the command
terminates abnormally or with exit status non-zero.
If more detailed information about the execution status of a command
is required, it can be executed and the variable `$status' examined
in the next command.
Since `$status' is set by every command, it is very transient.
It can be saved if it is inconvenient to use it only in the single
immediately following command.
.PP
For a full list of expression components available see the manual
section for the shell.
.NH 2
Sample shell script
.PP
A sample shell script which makes use of the expression mechanism
of the shell and some of its control structure follows:
.DS
% cat copyc
#
# Copyc copies those C programs in the specified list
# to the directory ~/backup if they differ from the files
# already in ~/backup
#
set noglob
foreach i ($argv)
if ($i !~ *.c) continue # not a .c file so do nothing
if (! \-r ~/backup/$i:t) then
echo $i:t not in backup... not cp\e\'ed
continue
endif
cmp \-s $i ~/backup/$i:t # to set $status
if ($status != 0) then
echo new backup of $i
cp $i ~/backup/$i:t
endif
end
.DE
.PP
This script makes use of the
.I foreach
command, which causes the shell to execute the commands between the
.I foreach
and the matching
.I end
for each of the values given between `(' and `)' with the named
variable, in this case `i' set to successive values in the list.
Within this loop we may use the command
.I break
to stop executing the loop
and
.I continue
to prematurely terminate one iteration
and begin the next.
After the
.I foreach
loop the iteration variable
(\fIi\fR in this case)
has the value at the last iteration.
.PP
We set the variable
.I noglob
here to prevent filename expansion of the members of
.I argv.
This is a good idea, in general, if the arguments to a shell script
are filenames which have already been expanded or if the arguments
may contain filename expansion metacharacters.
It is also possible to quote each use of a `$' variable expansion,
but this is harder and less reliable.
.PP
The other control construct used here is a statement of the form
.DS
\fBif\fR ( expression ) \fBthen\fR
command
...
\fBendif\fR
.DE
The placement of the keywords here is
.B not
flexible due to the current implementation of the shell.\(dg
.FS
\(dgThe following two formats are not currently acceptable to the shell:
.sp
.in +5
.nf
\fBif\fR ( expression ) # \fBWon't work!\fR
\fBthen\fR
command
...
\fBendif\fR
.fi
.in -5
.sp
and
.sp
.in +5
.nf
\fBif\fR ( expression ) \fBthen\fR command \fBendif\fR # \fBWon't work\fR
.in -5
.fi
.FE
.PP
The shell does have another form of the if statement of the form
.DS
\fBif\fR ( expression ) \fBcommand\fR
.DE
which can be written
.DS
\fBif\fR ( expression ) \e
command
.DE
Here we have escaped the newline for the sake of appearance.
The command must not involve `\||\|', `&' or `;'
and must not be another control command.
The second form requires the final `\e' to
.B immediately
precede the end-of-line.
.PP
The more general
.I if
statements above also admit a sequence of
.I else\-if
pairs followed by a single
.I else
and an
.I endif,
e.g.:
.DS
\fBif\fR ( expression ) \fBthen\fR
commands
\fBelse\fR \fBif\fR (expression ) \fBthen\fR
commands
\&...
\fBelse\fR
commands
\fBendif\fR
.DE
.PP
Another important mechanism used in shell scripts is the `:' modifier.
We can use the modifier `:r' here to extract a root of a filename or
`:e' to extract the
.I extension.
Thus if the variable
.I i
has the value
`/mnt/foo.bar'
then
.sp
.in +5
.nf
% echo $i $i:r $i:e
/mnt/foo.bar /mnt/foo bar
%
.sp
.in -5
.fi
shows how the `:r' modifier strips off the trailing `.bar' and the
the `:e' modifier leaves only the `bar'.
Other modifiers will take off the last component of a pathname leaving
the head `:h' or all but the last component of a pathname leaving the
tail `:t'.
These modifiers are fully described in the
.I csh
manual pages in the User's Reference Manual.
It is also possible to use the
.I "command substitution"
mechanism described in the next major section to perform modifications
on strings to then reenter the shell's environment.
Since each usage of this mechanism involves the creation of a new process,
it is much more expensive to use than the `:' modification mechanism.\(dd
.FS
\(dd It is also important to note that
the current implementation of the shell limits the number of `:' modifiers
on a `$' substitution to 1.
Thus
.sp
.nf
.in +5
% echo $i $i:h:t
/a/b/c /a/b:t
%
.in -5
.fi
.sp
does not do what one would expect.
.FE
Finally, we note that the character `#' lexically introduces a shell
comment in shell scripts (but not from the terminal).
All subsequent characters on the input line after a `#' are discarded
by the shell.
This character can be quoted using `\'' or `\e' to place it in
an argument word.
.NH 2
Other control structures
.PP
The shell also has control structures
.I while
and
.I switch
similar to those of C.
These take the forms
.DS
\fBwhile\fR ( expression )
commands
\fBend\fR
.DE
and
.DS
\fBswitch\fR ( word )
\fBcase\fR str1:
commands
\fBbreaksw\fR
\& ...
\fBcase\fR strn:
commands
\fBbreaksw\fR
\fBdefault:\fR
commands
\fBbreaksw\fR
\fBendsw\fR
.DE
For details see the manual section for
.I csh.
C programmers should note that we use
.I breaksw
to exit from a
.I switch
while
.I break
exits a
.I while
or
.I foreach
loop.
A common mistake to make in
.I csh
scripts is to use
.I break
rather than
.I breaksw
in switches.
.PP
Finally,
.I csh
allows a
.I goto
statement, with labels looking like they do in C, i.e.:
.DS
loop:
commands
\fBgoto\fR loop
.DE
.NH 2
Supplying input to commands
.PP
Commands run from shell scripts receive by default the standard
input of the shell which is running the script.
This is different from previous shells running
under \s-2UNIX\s0. It allows shell scripts to fully participate
in pipelines, but mandates extra notation for commands which are to take
inline data.
.PP
Thus we need a metanotation for supplying inline data to commands in
shell scripts.
As an example, consider this script which runs the editor to
delete leading blanks from the lines in each argument file:
.DS
% cat deblank
# deblank \-\- remove leading blanks
foreach i ($argv)
ed \- $i << \'EOF\'
1,$s/^[ ]*//
w
q
\&\'EOF\'
end
%
.DE
The notation `<< \'EOF\''
means that the standard input for the
.I ed
command is to come from the text in the shell script file
up to the next line consisting of exactly `\'EOF\''.
The fact that the `EOF' is enclosed in `\'' characters, i.e. quoted,
causes the shell to not perform variable substitution on the
intervening lines.
In general, if any part of the word following the `<<' which the
shell uses to terminate the text to be given to the command is quoted
then these substitutions will not be performed.
In this case since we used the form `1,$' in our editor script
we needed to insure that this `$' was not variable substituted.
We could also have insured this by preceding the `$' here with a `\e',
i.e.:
.DS
1,\e$s/^[ ]*//
.DE
but quoting the `EOF' terminator is a more reliable way of achieving the
same thing.
.NH 2
Catching interrupts
.PP
If our shell script creates temporary files, we may wish to catch
interruptions of the shell script so that we can clean up
these files.
We can then do
.DS
onintr label
.DE
where
.I label
is a label in our program.
If an interrupt is received the shell will do a
`goto label'
and we can remove the temporary files and then do an
.I exit
command (which is built in to the shell)
to exit from the shell script.
If we wish to exit with a non-zero status we can do
.DS
exit(1)
.DE
e.g. to exit with status `1'.
.NH 2
What else?
.PP
There are other features of the shell useful to writers of shell
procedures.
The
.I verbose
and
.I echo
options and the related
.I \-v
and
.I \-x
command line options can be used to help trace the actions of the shell.
The
.I \-n
option causes the shell only to read commands and not to execute
them and may sometimes be of use.
.PP
One other thing to note is that
.I csh
will not execute shell scripts which do not begin with the
character `#', that is shell scripts that do not begin with a comment.
Similarly, the `/bin/sh' on your system may well defer to `csh'
to interpret shell scripts which begin with `#'.
This allows shell scripts for both shells to live in harmony.
.PP
There is also another quotation mechanism using `"' which allows
only some of the expansion mechanisms we have so far discussed to occur
on the quoted string and serves to make this string into a single word
as `\'' does.
.bp

178
bin/csh/USD.doc/csh.4 Normal file
View File

@ -0,0 +1,178 @@
.\" Copyright (c) 1980, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)csh.4 8.1 (Berkeley) 6/8/93
.\"
.nr H1 3
.NH
Other, less commonly used, shell features
.NH 2
Loops at the terminal; variables as vectors
.PP
It is occasionally useful to use the
.I foreach
control structure at the terminal to aid in performing a number
of similar commands.
For instance, there were at one point three shells in use on the Cory \s-2UNIX\s0
system at Cory Hall,
`/bin/sh',
`/bin/nsh',
and
`/bin/csh'.
To count the number of persons using each shell one could have issued
the commands
.DS
% grep \-c csh$ /etc/passwd
27
% grep \-c nsh$ /etc/passwd
128
% grep \-c \-v sh$ /etc/passwd
430
%
.DE
Since these commands are very similar we can use
.I foreach
to do this more easily.
.DS
% foreach i (\'sh$\' \'csh$\' \'\-v sh$\')
? grep \-c $i /etc/passwd
? end
27
128
430
%
.DE
Note here that the shell prompts for
input with `? ' when reading the body of the loop.
.PP
Very useful with loops are variables which contain lists of filenames
or other words.
You can, for example, do
.DS
% set a=(\`ls\`)
% echo $a
csh.n csh.rm
% ls
csh.n
csh.rm
% echo $#a
2
%
.DE
The
.I set
command here gave the variable
.I a
a list of all the filenames in the current directory as value.
We can then iterate over these names to perform any chosen function.
.PP
The output of a command within `\`' characters is converted by
the shell to a list of words.
You can also place the `\`' quoted string within `"' characters
to take each (non-empty) line as a component of the variable;
preventing the lines from being split into words at blanks and tabs.
A modifier `:x' exists which can be used later to expand each component
of the variable into another variable splitting it into separate words
at embedded blanks and tabs.
.NH 2
Braces { ... } in argument expansion
.PP
Another form of filename expansion, alluded
to before involves the characters `{' and `}'.
These characters specify that the contained strings, separated by `,'
are to be consecutively substituted into the containing characters
and the results expanded left to right.
Thus
.DS
A{str1,str2,...strn}B
.DE
expands to
.DS
Astr1B Astr2B ... AstrnB
.DE
This expansion occurs before the other filename expansions, and may
be applied recursively (i.e. nested).
The results of each expanded string are sorted separately, left
to right order being preserved.
The resulting filenames are not required to exist if no other expansion
mechanisms are used.
This means that this mechanism can be used to generate arguments which are
not filenames, but which have common parts.
.PP
A typical use of this would be
.DS
mkdir ~/{hdrs,retrofit,csh}
.DE
to make subdirectories `hdrs', `retrofit' and `csh'
in your home directory.
This mechanism is most useful when the common prefix is longer
than in this example, i.e.
.DS
chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
.DE
.NH 2
Command substitution
.PP
A command enclosed in `\`' characters is replaced, just before
filenames are expanded, by the output from that command.
Thus it is possible to do
.DS
set pwd=\`pwd\`
.DE
to save the current directory in the variable
.I pwd
or to do
.DS
ex \`grep \-l TRACE *.c\`
.DE
to run the editor
.I ex
supplying as arguments those files whose names end in `.c'
which have the string `TRACE' in them.*
.FS
*Command expansion also occurs in input redirected with `<<'
and within `"' quotations.
Refer to the shell manual section for full details.
.FE
.NH 2
Other details not covered here
.PP
In particular circumstances it may be necessary to know the exact
nature and order of different substitutions performed by the shell.
The exact meaning of certain combinations of quotations is also
occasionally important.
These are detailed fully in its manual section.
.PP
The shell has a number of command line option flags mostly of use
in writing \s-2UNIX\s0 programs,
and debugging shell scripts.
See the csh(1) manual section for a list of these options.
.bp

95
bin/csh/USD.doc/csh.a Normal file
View File

@ -0,0 +1,95 @@
.\" Copyright (c) 1980, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)csh.a 8.1 (Berkeley) 6/8/93
.\"
.SH
Appendix \- Special characters
.LP
The following table lists the special characters of
.I csh
and the \s-2UNIX\s0 system, giving for each the section(s) in which it
is discussed.
A number of these characters also have special meaning in expressions.
See the
.I csh
manual section
for a complete list.
.ta .75i 1.5i 2.25i
.LP
Syntactic metacharacters
.DS
; 2.4 separates commands to be executed sequentially
| 1.5 separates commands in a pipeline
( ) 2.2,3.6 brackets expressions and variable values
& 2.5 follows commands to be executed without waiting for completion
.DE
.LP
Filename metacharacters
.DS
/ 1.6 separates components of a file's pathname
\. 1.6 separates root parts of a file name from extensions
? 1.6 expansion character matching any single character
* 1.6 expansion character matching any sequence of characters
[ ] 1.6 expansion sequence matching any single character from a set
~ 1.6 used at the beginning of a filename to indicate home directories
{ } 4.2 used to specify groups of arguments with common parts
.DE
.LP
Quotation metacharacters
.DS
\e 1.7 prevents meta-meaning of following single character
\' 1.7 prevents meta-meaning of a group of characters
" 4.3 like \', but allows variable and command expansion
.DE
.LP
Input/output metacharacters
.DS
< 1.5 indicates redirected input
> 1.3 indicates redirected output
.DE
.LP
Expansion/substitution metacharacters
.DS
$ 3.4 indicates variable substitution
! 2.3 indicates history substitution
: 3.6 precedes substitution modifiers
^ 2.3 used in special forms of history substitution
\` 4.3 indicates command substitution
.DE
.LP
Other metacharacters
.DS
# 1.3,3.6 begins scratch file names; indicates shell comments
\- 1.2 prefixes option (flag) arguments to commands
% 2.6 prefixes job name specifications
.DE
.bp

1721
bin/csh/USD.doc/csh.g Normal file

File diff suppressed because it is too large Load Diff

34
bin/csh/USD.doc/tabs Normal file
View File

@ -0,0 +1,34 @@
.\" Copyright (c) 1980, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)tabs 8.1 (Berkeley) 6/8/93
.\"
.ta 5n 10n 15n 20n 25n 30n 35n 40n 45n 50n 55n 60n 65n 70n 75n 80n

124
bin/csh/alloc.c Normal file
View File

@ -0,0 +1,124 @@
/*-
* Copyright (c) 1983, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)alloc.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
char *memtop = NULL; /* PWP: top of current memory */
char *membot = NULL; /* PWP: bottom of allocatable memory */
ptr_t
Malloc(n)
size_t n;
{
ptr_t ptr;
if (membot == NULL)
memtop = membot = sbrk(0);
if ((ptr = malloc(n)) == (ptr_t) 0) {
child++;
stderror(ERR_NOMEM);
}
return (ptr);
}
ptr_t
Realloc(p, n)
ptr_t p;
size_t n;
{
ptr_t ptr;
if (membot == NULL)
memtop = membot = sbrk(0);
if ((ptr = realloc(p, n)) == (ptr_t) 0) {
child++;
stderror(ERR_NOMEM);
}
return (ptr);
}
ptr_t
Calloc(s, n)
size_t s, n;
{
ptr_t ptr;
if (membot == NULL)
memtop = membot = sbrk(0);
if ((ptr = calloc(s, n)) == (ptr_t) 0) {
child++;
stderror(ERR_NOMEM);
}
return (ptr);
}
void
Free(p)
ptr_t p;
{
if (p)
free(p);
}
/*
* mstats - print out statistics about malloc
*
* Prints two lines of numbers, one showing the length of the free list
* for each size category, the second showing the number of mallocs -
* frees for each size category.
*/
void
/*ARGSUSED*/
showall(v, t)
Char **v;
struct command *t;
{
memtop = (char *) sbrk(0);
(void) fprintf(cshout, "Allocated memory from 0x%lx to 0x%lx (%ld).\n",
(unsigned long) membot, (unsigned long) memtop, memtop - membot);
}

311
bin/csh/char.c Normal file
View File

@ -0,0 +1,311 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)char.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include "char.h"
unsigned short _cmap[256] = {
/* nul soh stx etx */
_CTR, _CTR, _CTR, _CTR,
/* eot enq ack bel */
_CTR, _CTR, _CTR, _CTR,
/* bs ht nl vt */
_CTR, _CTR|_SP|_META, _CTR|_NL|_META, _CTR,
/* np cr so si */
_CTR, _CTR, _CTR, _CTR,
/* dle dc1 dc2 dc3 */
_CTR, _CTR, _CTR, _CTR,
/* dc4 nak syn etb */
_CTR, _CTR, _CTR, _CTR,
/* can em sub esc */
_CTR, _CTR, _CTR, _CTR,
/* fs gs rs us */
_CTR, _CTR, _CTR, _CTR,
/* sp ! " # */
_SP|_META, 0, _QF, _META,
/* $ % & ' */
_DOL, 0, _META|_CMD, _QF,
/* ( ) * + */
_META|_CMD, _META, _GLOB, 0,
/* , - . / */
0, 0, 0, 0,
/* 0 1 2 3 */
_DIG|_XD, _DIG|_XD, _DIG|_XD, _DIG|_XD,
/* 4 5 6 7 */
_DIG|_XD, _DIG|_XD, _DIG|_XD, _DIG|_XD,
/* 8 9 : ; */
_DIG|_XD, _DIG|_XD, 0, _META|_CMD,
/* < = > ? */
_META, 0, _META, _GLOB,
/* @ A B C */
0, _LET|_UP|_XD, _LET|_UP|_XD, _LET|_UP|_XD,
/* D E F G */
_LET|_UP|_XD, _LET|_UP|_XD, _LET|_UP|_XD, _LET|_UP,
/* H I J K */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* L M N O */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* P Q R S */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* T U V W */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* X Y Z [ */
_LET|_UP, _LET|_UP, _LET|_UP, _GLOB,
/* \ ] ^ _ */
_ESC, 0, 0, 0,
/* ` a b c */
_QB|_GLOB|_META, _LET|_LOW|_XD, _LET|_LOW|_XD, _LET|_LOW|_XD,
/* d e f g */
_LET|_LOW|_XD, _LET|_LOW|_XD, _LET|_LOW|_XD, _LET|_LOW,
/* h i j k */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* l m n o */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* p q r s */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* t u v w */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* x y z { */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _GLOB,
/* | } ~ del */
_META|_CMD, 0, 0, _CTR,
#if defined(SHORT_STRINGS) && !defined(KANJI)
/****************************************************************/
/* 128 - 255 The below is supposedly ISO 8859/1 */
/****************************************************************/
/* (undef) (undef) (undef) (undef) */
_CTR, _CTR, _CTR, _CTR,
/* (undef) (undef) (undef) (undef) */
_CTR, _CTR, _CTR, _CTR,
/* (undef) (undef) (undef) (undef) */
_CTR, _CTR, _CTR, _CTR,
/* (undef) (undef) (undef) (undef) */
_CTR, _CTR, _CTR, _CTR,
/* (undef) (undef) (undef) (undef) */
_CTR, _CTR, _CTR, _CTR,
/* (undef) (undef) (undef) (undef) */
_CTR, _CTR, _CTR, _CTR,
/* (undef) (undef) (undef) (undef) */
_CTR, _CTR, _CTR, _CTR,
/* (undef) (undef) (undef) (undef) */
_CTR, _CTR, _CTR, _CTR,
/* nobreakspace exclamdown cent sterling */
_SP, 0, 0, 0,
/* currency yen brokenbar section */
0, 0, 0, 0,
/* diaeresis copyright ordfeminine guillemotleft */
0, 0, 0, 0,
/* notsign hyphen registered macron */
0, 0, 0, 0,
/* degree plusminus twosuperior threesuperior */
0, 0, 0, 0,
/* acute mu paragraph periodcentered */
0, 0, 0, 0,
/* cedilla onesuperior masculine guillemotright */
0, 0, 0, 0,
/* onequarter onehalf threequarters questiondown */
0, 0, 0, 0,
/* Agrave Aacute Acircumflex Atilde */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* Adiaeresis Aring AE Ccedilla */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* Egrave Eacute Ecircumflex Ediaeresis */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* Igrave Iacute Icircumflex Idiaeresis */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* ETH Ntilde Ograve Oacute */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* Ocircumflex Otilde Odiaeresis multiply */
_LET|_UP, _LET|_UP, _LET|_UP, 0,
/* Ooblique Ugrave Uacute Ucircumflex */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* Udiaeresis Yacute THORN ssharp */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_LOW,
/* agrave aacute acircumflex atilde */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* adiaeresis aring ae ccedilla */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* egrave eacute ecircumflex ediaeresis */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* igrave iacute icircumflex idiaeresis */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* eth ntilde ograve oacute */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* ocircumflex otilde odiaeresis division */
_LET|_LOW, _LET|_LOW, _LET|_LOW, 0,
/* oslash ugrave uacute ucircumflex */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* udiaeresis yacute thorn ydiaeresis */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
#endif /* SHORT_STRINGS && !KANJI */
};
#ifndef NLS
/* _cmap_lower, _cmap_upper for ISO 8859/1 */
unsigned char _cmap_lower[256] = {
0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
0100, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
0170, 0171, 0172, 0133, 0134, 0135, 0136, 0137,
0140, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
0170, 0171, 0172, 0173, 0174, 0175, 0176, 0177,
0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0327,
0370, 0371, 0372, 0373, 0374, 0375, 0376, 0337,
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377,
};
unsigned char _cmap_upper[256] = {
0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
0320, 0321, 0322, 0323, 0324, 0325, 0326, 0367,
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0377,
};
#endif /* NLS */

96
bin/csh/char.h Normal file
View File

@ -0,0 +1,96 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)char.h 8.1 (Berkeley) 5/31/93
*/
#include <ctype.h>
extern unsigned short _cmap[];
#ifndef NLS
extern unsigned char _cmap_lower[], _cmap_upper[];
#endif
#define _QF 0x0001 /* '" (Forward quotes) */
#define _QB 0x0002 /* ` (Backquote) */
#define _SP 0x0004 /* space and tab */
#define _NL 0x0008 /* \n */
#define _META 0x0010 /* lex meta characters, sp #'`";&<>()|\t\n */
#define _GLOB 0x0020 /* glob characters, *?{[` */
#define _ESC 0x0040 /* \ */
#define _DOL 0x0080 /* $ */
#define _DIG 0x0100 /* 0-9 */
#define _LET 0x0200 /* a-z, A-Z, _ */
#define _UP 0x0400 /* A-Z */
#define _LOW 0x0800 /* a-z */
#define _XD 0x1000 /* 0-9, a-f, A-F */
#define _CMD 0x2000 /* lex end of command chars, ;&(|` */
#define _CTR 0x4000 /* control */
#define cmap(c, bits) \
(((c) & QUOTE) ? 0 : (_cmap[(unsigned char)(c)] & (bits)))
#define isglob(c) cmap(c, _GLOB)
#define isspc(c) cmap(c, _SP)
#define ismeta(c) cmap(c, _META)
#define iscmdmeta(c) cmap(c, _CMD)
#define letter(c) (((c) & QUOTE) ? 0 : \
(isalpha((unsigned char) (c)) || (c) == '_'))
#define alnum(c) (((c) & QUOTE) ? 0 : \
(isalnum((unsigned char) (c)) || (c) == '_'))
#ifdef NLS
#define Isspace(c) (((c) & QUOTE) ? 0 : isspace((unsigned char) (c)))
#define Isdigit(c) (((c) & QUOTE) ? 0 : isdigit((unsigned char) (c)))
#define Isalpha(c) (((c) & QUOTE) ? 0 : isalpha((unsigned char) (c)))
#define Islower(c) (((c) & QUOTE) ? 0 : islower((unsigned char) (c)))
#define Isupper(c) (((c) & QUOTE) ? 0 : isupper((unsigned char) (c)))
#define Tolower(c) (((c) & QUOTE) ? 0 : tolower((unsigned char) (c)))
#define Toupper(c) (((c) & QUOTE) ? 0 : toupper((unsigned char) (c)))
#define Isxdigit(c) (((c) & QUOTE) ? 0 : isxdigit((unsigned char) (c)))
#define Isalnum(c) (((c) & QUOTE) ? 0 : isalnum((unsigned char) (c)))
#define Iscntrl(c) (((c) & QUOTE) ? 0 : iscntrl((unsigned char) (c)))
#define Isprint(c) (((c) & QUOTE) ? 0 : isprint((unsigned char) (c)))
#else
#define Isspace(c) cmap(c, _SP|_NL)
#define Isdigit(c) cmap(c, _DIG)
#define Isalpha(c) (cmap(c,_LET) && !(((c) & META) && AsciiOnly))
#define Islower(c) (cmap(c,_LOW) && !(((c) & META) && AsciiOnly))
#define Isupper(c) (cmap(c, _UP) && !(((c) & META) && AsciiOnly))
#define Tolower(c) (_cmap_lower[(unsigned char)(c)])
#define Toupper(c) (_cmap_upper[(unsigned char)(c)])
#define Isxdigit(c) cmap(c, _XD)
#define Isalnum(c) (cmap(c, _DIG|_LET) && !(((c) & META) && AsciiOnly))
#define Iscntrl(c) (cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
#define Isprint(c) (!cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
#endif

160
bin/csh/const.c Normal file
View File

@ -0,0 +1,160 @@
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)const.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
/*
* tc.const.c: String constants for csh.
*/
#include "csh.h"
Char STR0[] = { '0', '\0' };
Char STR1[] = { '1', '\0' };
Char STRHOME[] = { 'H', 'O', 'M', 'E', '\0' };
Char STRLANG[] = { 'L', 'A', 'N', 'G', '\0' };
Char STRLC_CTYPE[] = { 'L', 'C', '_', 'C', 'T', 'Y', 'P', 'E' ,'\0' };
Char STRLOGNAME[] = { 'L', 'O', 'G', 'N', 'A', 'M', 'E', '\0' };
Char STRLbrace[] = { '{', '\0' };
Char STRLparen[] = { '(', '\0' };
Char STRLparensp[] = { '(', ' ', '\0' };
Char STRNULL[] = { '\0' };
Char STRPATH[] = { 'P', 'A', 'T', 'H', '\0' };
Char STRPWD[] = { 'P', 'W', 'D', '\0' };
Char STRQNULL[] = { '\0' | QUOTE, '\0' };
Char STRRbrace[] = { '}', '\0' };
Char STRspRparen[] = { ' ', ')', '\0' };
Char STRTERM[] = { 'T', 'E', 'R', 'M', '\0' };
Char STRUSER[] = { 'U', 'S', 'E', 'R', '\0' };
Char STRalias[] = { 'a', 'l', 'i', 'a', 's', '\0' };
Char STRand[] = { '&', '\0' };
Char STRand2[] = { '&', '&', '\0' };
Char STRaout[] = { 'a', '.', 'o', 'u', 't', '\0' };
Char STRargv[] = { 'a', 'r', 'g', 'v', '\0' };
Char STRbang[] = { '!', '\0' };
Char STRcaret[] = { '^', '\0' };
Char STRcdpath[] = { 'c', 'd', 'p', 'a', 't', 'h', '\0' };
Char STRcent2[] = { '%', '%', '\0' };
Char STRcenthash[] = { '%', '#', '\0' };
Char STRcentplus[] = { '%', '+', '\0' };
Char STRcentminus[] = { '%', '-', '\0' };
Char STRchase_symlinks[] = { 'c', 'h', 'a', 's', 'e', '_', 's', 'y', 'm', 'l',
'i', 'n', 'k', 's', '\0' };
Char STRchild[] = { 'c', 'h', 'i', 'l', 'd', '\0' };
Char STRcolon[] = { ':', '\0' };
Char STRcwd[] = { 'c', 'w', 'd', '\0' };
Char STRdefault[] = { 'd', 'e', 'f', 'a', 'u', 'l', 't', '\0' };
Char STRdot[] = { '.', '\0' };
Char STRdotdotsl[] = { '.', '.', '/', '\0' };
Char STRdotsl[] = { '.', '/', '\0' };
Char STRecho[] = { 'e', 'c', 'h', 'o', '\0' };
Char STRequal[] = { '=', '\0' };
Char STRfakecom[] = { '{', ' ', '.', '.', '.', ' ', '}', '\0' };
Char STRfakecom1[] = { '`', ' ', '.', '.', '.', ' ', '`', '\0' };
Char STRfignore[] = { 'f', 'i', 'g', 'n', 'o', 'r', 'e', '\0' };
#ifdef FILEC
Char STRfilec[] = { 'f', 'i', 'l', 'e', 'c', '\0' };
#endif /* FILEC */
Char STRhistchars[] = { 'h', 'i', 's', 't', 'c', 'h', 'a', 'r', 's', '\0' };
Char STRtildothist[] = { '~', '/', '.', 'h', 'i', 's', 't', 'o', 'r',
'y', '\0' };
Char STRhistfile[] = { 'h', 'i', 's', 't', 'f', 'i', 'l', 'e', '\0' };
Char STRhistory[] = { 'h', 'i', 's', 't', 'o', 'r', 'y', '\0' };
Char STRhome[] = { 'h', 'o', 'm', 'e', '\0' };
Char STRignore_symlinks[] = { 'i', 'g', 'n', 'o', 'r', 'e', '_', 's', 'y', 'm',
'l', 'i', 'n', 'k', 's', '\0' };
Char STRignoreeof[] = { 'i', 'g', 'n', 'o', 'r', 'e', 'e', 'o', 'f', '\0' };
Char STRjobs[] = { 'j', 'o', 'b', 's', '\0' };
Char STRlistjobs[] = { 'l', 'i', 's', 't', 'j', 'o', 'b', 's', '\0' };
Char STRlogout[] = { 'l', 'o', 'g', 'o', 'u', 't', '\0' };
Char STRlong[] = { 'l', 'o', 'n', 'g', '\0' };
Char STRmail[] = { 'm', 'a', 'i', 'l', '\0' };
Char STRmh[] = { '-', 'h', '\0' };
Char STRminus[] = { '-', '\0' };
Char STRml[] = { '-', 'l', '\0' };
Char STRmn[] = { '-', 'n', '\0' };
Char STRmquestion[] = { '?' | QUOTE, ' ', '\0' };
Char STRnice[] = { 'n', 'i', 'c', 'e', '\0' };
Char STRnoambiguous[] = { 'n', 'o', 'a', 'm', 'b', 'i', 'g', 'u', 'o', 'u',
's', '\0' };
Char STRnobeep[] = { 'n', 'o', 'b', 'e', 'e', 'p', '\0' };
Char STRnoclobber[] = { 'n', 'o', 'c', 'l', 'o', 'b', 'b', 'e', 'r', '\0' };
Char STRnoglob[] = { 'n', 'o', 'g', 'l', 'o', 'b', '\0' };
Char STRnohup[] = { 'n', 'o', 'h', 'u', 'p', '\0' };
Char STRnonomatch[] = { 'n', 'o', 'n', 'o', 'm', 'a', 't', 'c', 'h', '\0' };
Char STRnormal[] = { 'n', 'o', 'r', 'm', 'a', 'l', '\0' };
Char STRnotify[] = { 'n', 'o', 't', 'i', 'f', 'y', '\0' };
Char STRor[] = { '|', '\0' };
Char STRor2[] = { '|', '|', '\0' };
Char STRpath[] = { 'p', 'a', 't', 'h', '\0' };
Char STRprintexitvalue[] = { 'p', 'r', 'i', 'n', 't', 'e', 'x', 'i', 't', 'v',
'a', 'l', 'u', 'e', '\0' };
Char STRprompt[] = { 'p', 'r', 'o', 'm', 'p', 't', '\0' };
Char STRprompt2[] = { 'p', 'r', 'o', 'm', 'p', 't', '2', '\0' };
Char STRpushdsilent[] = { 'p', 'u', 's', 'h', 'd', 's', 'i', 'l', 'e', 'n',
't', '\0' };
Char STRret[] = { '\n', '\0' };
Char STRsavehist[] = { 's', 'a', 'v', 'e', 'h', 'i', 's', 't', '\0' };
Char STRsemisp[] = { ';', ' ', '\0' };
Char STRshell[] = { 's', 'h', 'e', 'l', 'l', '\0' };
Char STRslash[] = { '/', '\0' };
Char STRsldotcshrc[] = { '/', '.', 'c', 's', 'h', 'r', 'c', '\0' };
Char STRsldotlogin[] = { '/', '.', 'l', 'o', 'g', 'i', 'n', '\0' };
Char STRsldthist[] = { '/', '.', 'h', 'i', 's', 't', 'o', 'r', 'y', '\0' };
Char STRsldtlogout[] = { '/', '.', 'l', 'o', 'g', 'o', 'u', 't', '\0' };
Char STRsource[] = { 's', 'o', 'u', 'r', 'c', 'e', '\0' };
Char STRsp3dots[] = { ' ', '.', '.', '.', '\0' };
Char STRspLarrow2sp[] = { ' ', '<', '<', ' ', '\0' };
Char STRspLarrowsp[] = { ' ', '<', ' ', '\0' };
Char STRspRarrow[] = { ' ', '>', '\0' };
Char STRspRarrow2[] = { ' ', '>', '>', '\0' };
Char STRRparen[] = { ')', '\0' };
Char STRspace[] = { ' ', '\0' };
Char STRspand2sp[] = { ' ', '&', '&', ' ', '\0' };
Char STRspor2sp[] = { ' ', '|', '|', ' ', '\0' };
Char STRsporsp[] = { ' ', '|', ' ', '\0' };
Char STRstar[] = { '*', '\0' };
Char STRstatus[] = { 's', 't', 'a', 't', 'u', 's', '\0' };
Char STRsymcent[] = { '%', ' ', '\0' };
Char STRsymhash[] = { '#', ' ', '\0' };
Char STRterm[] = { 't', 'e', 'r', 'm', '\0' };
Char STRthen[] = { 't', 'h', 'e', 'n', '\0' };
Char STRtilde[] = { '~', '\0' };
Char STRtime[] = { 't', 'i', 'm', 'e', '\0' };
Char STRtmpsh[] = { '/', 't', 'm', 'p', '/', 's', 'h', '\0' };
Char STRunalias[] = { 'u', 'n', 'a', 'l', 'i', 'a', 's', '\0' };
Char STRuser[] = { 'u', 's', 'e', 'r', '\0' };
Char STRverbose[] = { 'v', 'e', 'r', 'b', 'o', 's', 'e', '\0' };
Char STRwordchars[] = { 'w', 'o', 'r', 'd', 'c', 'h', 'a', 'r', 's', '\0' };

2175
bin/csh/csh.1 Normal file

File diff suppressed because it is too large Load Diff

1352
bin/csh/csh.c Normal file

File diff suppressed because it is too large Load Diff

552
bin/csh/csh.h Normal file
View File

@ -0,0 +1,552 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)csh.h 8.1 (Berkeley) 5/31/93
*/
/*
* Fundamental definitions which may vary from system to system.
*
* BUFSIZ The i/o buffering size; also limits word size
* MAILINTVL How often to mailcheck; more often is more expensive
*/
#ifndef BUFSIZ
#define BUFSIZ 1024 /* default buffer size */
#endif /* BUFSIZ */
#define FORKSLEEP 10 /* delay loop on non-interactive fork failure */
#define MAILINTVL 600 /* 10 minutes */
/*
* The shell moves std in/out/diag and the old std input away from units
* 0, 1, and 2 so that it is easy to set up these standards for invoked
* commands.
*/
#define FSHTTY 15 /* /dev/tty when manip pgrps */
#define FSHIN 16 /* Preferred desc for shell input */
#define FSHOUT 17 /* ... shell output */
#define FSHERR 18 /* ... shell diagnostics */
#define FOLDSTD 19 /* ... old std input */
#ifdef PROF
#define xexit(n) done(n)
#endif
#ifdef SHORT_STRINGS
typedef short Char;
#define SAVE(a) (Strsave(str2short(a)))
#else
typedef char Char;
#define SAVE(a) (strsave(a))
#endif
/*
* Make sure a variable is not stored in a register by taking its address
* This is used where variables might be clobbered by longjmp.
*/
#define UNREGISTER(a) (void) &a
typedef void *ioctl_t; /* Third arg of ioctl */
typedef void *ptr_t;
#include "const.h"
#include "char.h"
#include "err.h"
#define xmalloc(i) Malloc(i)
#define xrealloc(p, i) Realloc(p, i)
#define xcalloc(n, s) Calloc(n, s)
#define xfree(p) Free(p)
#include <stdio.h>
FILE *cshin, *cshout, *csherr;
#define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR)
typedef int bool;
#define eq(a, b) (Strcmp(a, b) == 0)
/* globone() flags */
#define G_ERROR 0 /* default action: error if multiple words */
#define G_IGNORE 1 /* ignore the rest of the words */
#define G_APPEND 2 /* make a sentence by cat'ing the words */
/*
* Global flags
*/
bool chkstop; /* Warned of stopped jobs... allow exit */
bool didfds; /* Have setup i/o fd's for child */
bool doneinp; /* EOF indicator after reset from readc */
bool exiterr; /* Exit if error or non-zero exit status */
bool child; /* Child shell ... errors cause exit */
bool haderr; /* Reset was because of an error */
bool intty; /* Input is a tty */
bool intact; /* We are interactive... therefore prompt */
bool justpr; /* Just print because of :p hist mod */
bool loginsh; /* We are a loginsh -> .login/.logout */
bool neednote; /* Need to pnotify() */
bool noexec; /* Don't execute, just syntax check */
bool pjobs; /* want to print jobs if interrupted */
bool setintr; /* Set interrupts on/off -> Wait intr... */
bool timflg; /* Time the next waited for command */
bool havhash; /* path hashing is available */
#ifdef FILEC
bool filec; /* doing filename expansion */
#endif
/*
* Global i/o info
*/
Char *arginp; /* Argument input for sh -c and internal `xx` */
int onelflg; /* 2 -> need line for -t, 1 -> exit on read */
Char *ffile; /* Name of shell file for $0 */
char *seterr; /* Error message from scanner/parser */
Char *shtemp; /* Temp name for << shell files in /tmp */
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
struct timeval time0; /* Time at which the shell started */
struct rusage ru0;
/*
* Miscellany
*/
Char *doldol; /* Character pid for $$ */
int backpid; /* Pid of the last background process */
int uid, euid; /* Invokers uid */
int gid, egid; /* Invokers gid */
time_t chktim; /* Time mail last checked */
int shpgrp; /* Pgrp of shell */
int tpgrp; /* Terminal process group */
/* If tpgrp is -1, leave tty alone! */
int opgrp; /* Initial pgrp and tty pgrp */
/*
* To be able to redirect i/o for builtins easily, the shell moves the i/o
* descriptors it uses away from 0,1,2.
* Ideally these should be in units which are closed across exec's
* (this saves work) but for version 6, this is not usually possible.
* The desired initial values for these descriptors are F{SHIN,...}.
*/
int SHIN; /* Current shell input (script) */
int SHOUT; /* Shell output */
int SHERR; /* Diagnostic output... shell errs go here */
int OLDSTD; /* Old standard input (def for cmds) */
/*
* Error control
*
* Errors in scanning and parsing set up an error message to be printed
* at the end and complete. Other errors always cause a reset.
* Because of source commands and .cshrc we need nested error catches.
*/
#include <setjmp.h>
jmp_buf reslab;
#define setexit() (setjmp(reslab))
#define reset() longjmp(reslab, 1)
/* Should use structure assignment here */
#define getexit(a) bcopy((char *)reslab, ((char *)(a)), sizeof reslab)
#define resexit(a) bcopy((char *)(a), (char *)reslab, sizeof reslab)
Char *gointr; /* Label for an onintr transfer */
#include <signal.h>
sig_t parintr; /* Parents interrupt catch */
sig_t parterm; /* Parents terminate catch */
/*
* Lexical definitions.
*
* All lexical space is allocated dynamically.
* The eighth/sixteenth bit of characters is used to prevent recognition,
* and eventually stripped.
*/
#define META 0200
#define ASCII 0177
#ifdef SHORT_STRINGS
#define CHAR 0377
#define QUOTE 0100000 /* 16nth char bit used for 'ing */
#define TRIM 0077777 /* Mask to strip quote bit */
#else
#define CHAR 0177
#define QUOTE 0200 /* Eighth char bit used for 'ing */
#define TRIM 0177 /* Mask to strip quote bit */
#endif
int AsciiOnly; /* If set only 7 bits is expected in characters */
/*
* Each level of input has a buffered input structure.
* There are one or more blocks of buffered input for each level,
* exactly one if the input is seekable and tell is available.
* In other cases, the shell buffers enough blocks to keep all loops
* in the buffer.
*/
struct Bin {
off_t Bfseekp; /* Seek pointer */
off_t Bfbobp; /* Seekp of beginning of buffers */
off_t Bfeobp; /* Seekp of end of buffers */
int Bfblocks; /* Number of buffer blocks */
Char **Bfbuf; /* The array of buffer blocks */
} B;
/*
* This structure allows us to seek inside aliases
*/
struct Ain {
int type;
#define I_SEEK -1 /* Invalid seek */
#define A_SEEK 0 /* Alias seek */
#define F_SEEK 1 /* File seek */
#define E_SEEK 2 /* Eval seek */
union {
off_t _f_seek;
Char* _c_seek;
} fc;
#define f_seek fc._f_seek
#define c_seek fc._c_seek
Char **a_seek;
} ;
extern int aret; /* What was the last character returned */
#define SEEKEQ(a, b) ((a)->type == (b)->type && \
(a)->f_seek == (b)->f_seek && \
(a)->a_seek == (b)->a_seek)
#define fseekp B.Bfseekp
#define fbobp B.Bfbobp
#define feobp B.Bfeobp
#define fblocks B.Bfblocks
#define fbuf B.Bfbuf
/*
* The shell finds commands in loops by reseeking the input
* For whiles, in particular, it reseeks to the beginning of the
* line the while was on; hence the while placement restrictions.
*/
struct Ain lineloc;
bool cantell; /* Is current source tellable ? */
/*
* Input lines are parsed into doubly linked circular
* lists of words of the following form.
*/
struct wordent {
Char *word;
struct wordent *prev;
struct wordent *next;
};
/*
* During word building, both in the initial lexical phase and
* when expanding $ variable substitutions, expansion by `!' and `$'
* must be inhibited when reading ahead in routines which are themselves
* processing `!' and `$' expansion or after characters such as `\' or in
* quotations. The following flags are passed to the getC routines
* telling them which of these substitutions are appropriate for the
* next character to be returned.
*/
#define DODOL 1
#define DOEXCL 2
#define DOALL DODOL|DOEXCL
/*
* Labuf implements a general buffer for lookahead during lexical operations.
* Text which is to be placed in the input stream can be stuck here.
* We stick parsed ahead $ constructs during initial input,
* process id's from `$$', and modified variable values (from qualifiers
* during expansion in sh.dol.c) here.
*/
Char *lap;
/*
* Parser structure
*
* Each command is parsed to a tree of command structures and
* flags are set bottom up during this process, to be propagated down
* as needed during the semantics/exeuction pass (sh.sem.c).
*/
struct command {
short t_dtyp; /* Type of node */
#define NODE_COMMAND 1 /* t_dcom <t_dlef >t_drit */
#define NODE_PAREN 2 /* ( t_dspr ) <t_dlef >t_drit */
#define NODE_PIPE 3 /* t_dlef | t_drit */
#define NODE_LIST 4 /* t_dlef ; t_drit */
#define NODE_OR 5 /* t_dlef || t_drit */
#define NODE_AND 6 /* t_dlef && t_drit */
short t_dflg; /* Flags, e.g. F_AMPERSAND|... */
#define F_SAVE (F_NICE|F_TIME|F_NOHUP) /* save these when re-doing */
#define F_AMPERSAND (1<<0) /* executes in background */
#define F_APPEND (1<<1) /* output is redirected >> */
#define F_PIPEIN (1<<2) /* input is a pipe */
#define F_PIPEOUT (1<<3) /* output is a pipe */
#define F_NOFORK (1<<4) /* don't fork, last ()ized cmd */
#define F_NOINTERRUPT (1<<5) /* should be immune from intr's */
/* spare */
#define F_STDERR (1<<7) /* redirect unit 2 with unit 1 */
#define F_OVERWRITE (1<<8) /* output was ! */
#define F_READ (1<<9) /* input redirection is << */
#define F_REPEAT (1<<10) /* reexec aft if, repeat,... */
#define F_NICE (1<<11) /* t_nice is meaningful */
#define F_NOHUP (1<<12) /* nohup this command */
#define F_TIME (1<<13) /* time this command */
union {
Char *T_dlef; /* Input redirect word */
struct command *T_dcar; /* Left part of list/pipe */
} L;
union {
Char *T_drit; /* Output redirect word */
struct command *T_dcdr; /* Right part of list/pipe */
} R;
#define t_dlef L.T_dlef
#define t_dcar L.T_dcar
#define t_drit R.T_drit
#define t_dcdr R.T_dcdr
Char **t_dcom; /* Command/argument vector */
struct command *t_dspr; /* Pointer to ()'d subtree */
int t_nice;
};
/*
* These are declared here because they want to be
* initialized in sh.init.c (to allow them to be made readonly)
*/
extern struct biltins {
char *bname;
void (*bfunct) __P((Char **, struct command *));
short minargs, maxargs;
} bfunc[];
extern int nbfunc;
extern struct srch {
char *s_name;
short s_value;
} srchn[];
extern int nsrchn;
/*
* The keywords for the parser
*/
#define T_BREAK 0
#define T_BRKSW 1
#define T_CASE 2
#define T_DEFAULT 3
#define T_ELSE 4
#define T_END 5
#define T_ENDIF 6
#define T_ENDSW 7
#define T_EXIT 8
#define T_FOREACH 9
#define T_GOTO 10
#define T_IF 11
#define T_LABEL 12
#define T_LET 13
#define T_SET 14
#define T_SWITCH 15
#define T_TEST 16
#define T_THEN 17
#define T_WHILE 18
/*
* Structure defining the existing while/foreach loops at this
* source level. Loops are implemented by seeking back in the
* input. For foreach (fe), the word list is attached here.
*/
struct whyle {
struct Ain w_start; /* Point to restart loop */
struct Ain w_end; /* End of loop (0 if unknown) */
Char **w_fe, **w_fe0; /* Current/initial wordlist for fe */
Char *w_fename; /* Name for fe */
struct whyle *w_next; /* Next (more outer) loop */
} *whyles;
/*
* Variable structure
*
* Aliases and variables are stored in AVL balanced binary trees.
*/
struct varent {
Char **vec; /* Array of words which is the value */
Char *v_name; /* Name of variable/alias */
struct varent *v_link[3]; /* The links, see below */
int v_bal; /* Balance factor */
} shvhed, aliases;
#define v_left v_link[0]
#define v_right v_link[1]
#define v_parent v_link[2]
struct varent *adrof1();
#define adrof(v) adrof1(v, &shvhed)
#define value(v) value1(v, &shvhed)
/*
* The following are for interfacing redo substitution in
* aliases to the lexical routines.
*/
struct wordent *alhistp; /* Argument list (first) */
struct wordent *alhistt; /* Node after last in arg list */
Char **alvec, *alvecp; /* The (remnants of) alias vector */
/*
* Filename/command name expansion variables
*/
int gflag; /* After tglob -> is globbing needed? */
#define MAXVARLEN 30 /* Maximum number of char in a variable name */
/*
* Variables for filename expansion
*/
extern Char **gargv; /* Pointer to the (stack) arglist */
extern long gargc; /* Number args in gargv */
/*
* Variables for command expansion.
*/
extern Char **pargv; /* Pointer to the argv list space */
extern long pargc; /* Count of arguments in pargv */
Char *pargs; /* Pointer to start current word */
long pnleft; /* Number of chars left in pargs */
Char *pargcp; /* Current index into pargs */
/*
* History list
*
* Each history list entry contains an embedded wordlist
* from the scanner, a number for the event, and a reference count
* to aid in discarding old entries.
*
* Essentially "invisible" entries are put on the history list
* when history substitution includes modifiers, and thrown away
* at the next discarding since their event numbers are very negative.
*/
struct Hist {
struct wordent Hlex;
int Hnum;
int Href;
struct Hist *Hnext;
} Histlist;
struct wordent paraml; /* Current lexical word list */
int eventno; /* Next events number */
int lastev; /* Last event reference (default) */
Char HIST; /* history invocation character */
Char HISTSUB; /* auto-substitute character */
/*
* strings.h:
*/
#ifndef SHORT_STRINGS
#define Strchr(a, b) strchr(a, b)
#define Strrchr(a, b) strrchr(a, b)
#define Strcat(a, b) strcat(a, b)
#define Strncat(a, b, c) strncat(a, b, c)
#define Strcpy(a, b) strcpy(a, b)
#define Strncpy(a, b, c) strncpy(a, b, c)
#define Strlen(a) strlen(a)
#define Strcmp(a, b) strcmp(a, b)
#define Strncmp(a, b, c) strncmp(a, b, c)
#define Strspl(a, b) strspl(a, b)
#define Strsave(a) strsave(a)
#define Strend(a) strend(a)
#define Strstr(a, b) strstr(a, b)
#define str2short(a) (a)
#define blk2short(a) saveblk(a)
#define short2blk(a) saveblk(a)
#define short2str(a) strip(a)
#else
#define Strchr(a, b) s_strchr(a, b)
#define Strrchr(a, b) s_strrchr(a, b)
#define Strcat(a, b) s_strcat(a, b)
#define Strncat(a, b, c) s_strncat(a, b, c)
#define Strcpy(a, b) s_strcpy(a, b)
#define Strncpy(a, b, c) s_strncpy(a, b, c)
#define Strlen(a) s_strlen(a)
#define Strcmp(a, b) s_strcmp(a, b)
#define Strncmp(a, b, c) s_strncmp(a, b, c)
#define Strspl(a, b) s_strspl(a, b)
#define Strsave(a) s_strsave(a)
#define Strend(a) s_strend(a)
#define Strstr(a, b) s_strstr(a, b)
#endif
/*
* setname is a macro to save space (see sh.err.c)
*/
char *bname;
#define setname(a) (bname = (a))
Char *Vsav;
Char *Vdp;
Char *Vexpath;
char **Vt;
Char **evalvec;
Char *evalp;
/* word_chars is set by default to WORD_CHARS but can be overridden by
the worchars variable--if unset, reverts to WORD_CHARS */
Char *word_chars;
#define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */
Char *STR_SHELLPATH;
#include <paths.h>
#ifdef _PATH_BSHELL
Char *STR_BSHELL;
#endif
Char *STR_WORD_CHARS;
Char **STR_environ;

930
bin/csh/dir.c Normal file
View File

@ -0,0 +1,930 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)dir.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "dir.h"
#include "extern.h"
/* Directory management. */
static struct directory
*dfind __P((Char *));
static Char *dfollow __P((Char *));
static void printdirs __P((void));
static Char *dgoto __P((Char *));
static void dnewcwd __P((struct directory *));
static void dset __P((Char *));
struct directory dhead; /* "head" of loop */
int printd; /* force name to be printed */
static int dirflag = 0;
/*
* dinit - initialize current working directory
*/
void
dinit(hp)
Char *hp;
{
register char *tcp;
register Char *cp;
register struct directory *dp;
char path[MAXPATHLEN];
static char *emsg = "csh: Trying to start from \"%s\"\n";
/* Don't believe the login shell home, because it may be a symlink */
tcp = getwd(path); /* see ngetwd.c for System V version */
if (tcp == NULL || *tcp == '\0') {
(void) fprintf(csherr, "csh: %s\n", path);
if (hp && *hp) {
tcp = short2str(hp);
if (chdir(tcp) == -1)
cp = NULL;
else
cp = hp;
(void) fprintf(csherr, emsg, vis_str(hp));
}
else
cp = NULL;
if (cp == NULL) {
(void) fprintf(csherr, emsg, "/");
if (chdir("/") == -1)
/* I am not even try to print an error message! */
xexit(1);
cp = SAVE("/");
}
}
else {
struct stat swd, shp;
/*
* See if $HOME is the working directory we got and use that
*/
if (hp && *hp &&
stat(tcp, &swd) != -1 && stat(short2str(hp), &shp) != -1 &&
swd.st_dev == shp.st_dev && swd.st_ino == shp.st_ino)
cp = hp;
else {
char *cwd;
/*
* use PWD if we have it (for subshells)
*/
if ((cwd = getenv("PWD")) != NULL) {
if (stat(cwd, &shp) != -1 && swd.st_dev == shp.st_dev &&
swd.st_ino == shp.st_ino)
tcp = cwd;
}
cp = dcanon(SAVE(tcp), STRNULL);
}
}
dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
dp->di_name = Strsave(cp);
dp->di_count = 0;
dhead.di_next = dhead.di_prev = dp;
dp->di_next = dp->di_prev = &dhead;
printd = 0;
dnewcwd(dp);
}
static void
dset(dp)
Char *dp;
{
/*
* Don't call set() directly cause if the directory contains ` or
* other junk characters glob will fail.
*/
register Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
vec[0] = Strsave(dp);
vec[1] = 0;
setq(STRcwd, vec, &shvhed);
Setenv(STRPWD, dp);
}
#define DIR_LONG 1
#define DIR_VERT 2
#define DIR_LINE 4
static void
skipargs(v, str)
Char ***v;
char *str;
{
Char **n = *v, *s;
dirflag = 0;
for (n++; *n != NULL && (*n)[0] == '-'; n++)
for (s = &((*n)[1]); *s; s++)
switch (*s) {
case 'l':
dirflag |= DIR_LONG;
break;
case 'v':
dirflag |= DIR_VERT;
break;
case 'n':
dirflag |= DIR_LINE;
break;
default:
stderror(ERR_DIRUS, vis_str(**v), str);
break;
}
*v = n;
}
/*
* dodirs - list all directories in directory loop
*/
void
/*ARGSUSED*/
dodirs(v, t)
Char **v;
struct command *t;
{
skipargs(&v, "");
if (*v != NULL)
stderror(ERR_DIRUS, "dirs", "");
printdirs();
}
static void
printdirs()
{
register struct directory *dp;
Char *s, *hp = value(STRhome);
int idx, len, cur;
if (*hp == '\0')
hp = NULL;
dp = dcwd;
idx = 0;
cur = 0;
do {
if (dp == &dhead)
continue;
if (dirflag & DIR_VERT) {
(void) fprintf(cshout, "%d\t", idx++);
cur = 0;
}
if (!(dirflag & DIR_LONG) && hp != NULL && !eq(hp, STRslash) &&
(len = Strlen(hp), Strncmp(hp, dp->di_name, len) == 0) &&
(dp->di_name[len] == '\0' || dp->di_name[len] == '/'))
len = Strlen(s = (dp->di_name + len)) + 2;
else
len = Strlen(s = dp->di_name) + 1;
cur += len;
if ((dirflag & DIR_LINE) && cur >= 80 - 1 && len < 80) {
(void) fprintf(cshout, "\n");
cur = len;
}
(void) fprintf(cshout, s != dp->di_name ? "~%s%c" : "%s%c",
vis_str(s), (dirflag & DIR_VERT) ? '\n' : ' ');
} while ((dp = dp->di_prev) != dcwd);
if (!(dirflag & DIR_VERT))
(void) fprintf(cshout, "\n");
}
void
dtildepr(home, dir)
register Char *home, *dir;
{
if (!eq(home, STRslash) && prefix(home, dir))
(void) fprintf(cshout, "~%s", vis_str(dir + Strlen(home)));
else
(void) fprintf(cshout, "%s", vis_str(dir));
}
void
dtilde()
{
struct directory *d = dcwd;
do {
if (d == &dhead)
continue;
d->di_name = dcanon(d->di_name, STRNULL);
} while ((d = d->di_prev) != dcwd);
dset(dcwd->di_name);
}
/* dnormalize():
* If the name starts with . or .. then we might need to normalize
* it depending on the symbolic link flags
*/
Char *
dnormalize(cp)
Char *cp;
{
#define UC (unsigned char)
#define ISDOT(c) (UC(c)[0] == '.' && ((UC(c)[1] == '\0') || (UC(c)[1] == '/')))
#define ISDOTDOT(c) (UC(c)[0] == '.' && ISDOT(&((c)[1])))
if ((unsigned char) cp[0] == '/')
return (Strsave(cp));
if (adrof(STRignore_symlinks)) {
int dotdot = 0;
Char *dp, *cwd;
cwd = (Char *) xmalloc((size_t) ((Strlen(dcwd->di_name) + 3) *
sizeof(Char)));
(void) Strcpy(cwd, dcwd->di_name);
/*
* Ignore . and count ..'s
*/
while (*cp) {
if (ISDOT(cp)) {
if (*++cp)
cp++;
}
else if (ISDOTDOT(cp)) {
dotdot++;
cp += 2;
if (*cp)
cp++;
}
else
break;
}
while (dotdot > 0)
if ((dp = Strrchr(cwd, '/'))) {
*dp = '\0';
dotdot--;
}
else
break;
if (*cp) {
cwd[dotdot = Strlen(cwd)] = '/';
cwd[dotdot + 1] = '\0';
dp = Strspl(cwd, cp);
xfree((ptr_t) cwd);
return dp;
}
else {
if (!*cwd) {
cwd[0] = '/';
cwd[1] = '\0';
}
return cwd;
}
}
return Strsave(cp);
}
/*
* dochngd - implement chdir command.
*/
void
/*ARGSUSED*/
dochngd(v, t)
Char **v;
struct command *t;
{
register Char *cp;
register struct directory *dp;
skipargs(&v, " [<dir>]");
printd = 0;
if (*v == NULL) {
if ((cp = value(STRhome)) == NULL || *cp == 0)
stderror(ERR_NAME | ERR_NOHOMEDIR);
if (chdir(short2str(cp)) < 0)
stderror(ERR_NAME | ERR_CANTCHANGE);
cp = Strsave(cp);
}
else if (v[1] != NULL) {
stderror(ERR_NAME | ERR_TOOMANY);
/* NOTREACHED */
return;
}
else if ((dp = dfind(*v)) != 0) {
char *tmp;
printd = 1;
if (chdir(tmp = short2str(dp->di_name)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
dcwd->di_prev->di_next = dcwd->di_next;
dcwd->di_next->di_prev = dcwd->di_prev;
dfree(dcwd);
dnewcwd(dp);
return;
}
else
cp = dfollow(*v);
dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
dp->di_name = cp;
dp->di_count = 0;
dp->di_next = dcwd->di_next;
dp->di_prev = dcwd->di_prev;
dp->di_prev->di_next = dp;
dp->di_next->di_prev = dp;
dfree(dcwd);
dnewcwd(dp);
}
static Char *
dgoto(cp)
Char *cp;
{
Char *dp;
if (*cp != '/') {
register Char *p, *q;
int cwdlen;
for (p = dcwd->di_name; *p++;)
continue;
if ((cwdlen = p - dcwd->di_name - 1) == 1) /* root */
cwdlen = 0;
for (p = cp; *p++;)
continue;
dp = (Char *) xmalloc((size_t)((cwdlen + (p - cp) + 1) * sizeof(Char)));
for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
continue;
if (cwdlen)
p[-1] = '/';
else
p--; /* don't add a / after root */
for (q = cp; (*p++ = *q++) != '\0';)
continue;
xfree((ptr_t) cp);
cp = dp;
dp += cwdlen;
}
else
dp = cp;
cp = dcanon(cp, dp);
return cp;
}
/*
* dfollow - change to arg directory; fall back on cdpath if not valid
*/
static Char *
dfollow(cp)
register Char *cp;
{
register Char *dp;
struct varent *c;
char ebuf[MAXPATHLEN];
int serrno;
cp = globone(cp, G_ERROR);
/*
* if we are ignoring symlinks, try to fix relatives now.
*/
dp = dnormalize(cp);
if (chdir(short2str(dp)) >= 0) {
xfree((ptr_t) cp);
return dgoto(dp);
}
else {
xfree((ptr_t) dp);
if (chdir(short2str(cp)) >= 0)
return dgoto(cp);
serrno = errno;
}
if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
&& (c = adrof(STRcdpath))) {
Char **cdp;
register Char *p;
Char buf[MAXPATHLEN];
for (cdp = c->vec; *cdp; cdp++) {
for (dp = buf, p = *cdp; (*dp++ = *p++) != '\0';)
continue;
dp[-1] = '/';
for (p = cp; (*dp++ = *p++) != '\0';)
continue;
if (chdir(short2str(buf)) >= 0) {
printd = 1;
xfree((ptr_t) cp);
cp = Strsave(buf);
return dgoto(cp);
}
}
}
dp = value(cp);
if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
xfree((ptr_t) cp);
cp = Strsave(dp);
printd = 1;
return dgoto(cp);
}
(void) strcpy(ebuf, short2str(cp));
xfree((ptr_t) cp);
stderror(ERR_SYSTEM, ebuf, strerror(serrno));
return (NULL);
}
/*
* dopushd - push new directory onto directory stack.
* with no arguments exchange top and second.
* with numeric argument (+n) bring it to top.
*/
void
/*ARGSUSED*/
dopushd(v, t)
Char **v;
struct command *t;
{
register struct directory *dp;
skipargs(&v, " [<dir>|+<n>]");
printd = 1;
if (*v == NULL) {
char *tmp;
if ((dp = dcwd->di_prev) == &dhead)
dp = dhead.di_prev;
if (dp == dcwd)
stderror(ERR_NAME | ERR_NODIR);
if (chdir(tmp = short2str(dp->di_name)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
dp->di_prev->di_next = dp->di_next;
dp->di_next->di_prev = dp->di_prev;
dp->di_next = dcwd->di_next;
dp->di_prev = dcwd;
dcwd->di_next->di_prev = dp;
dcwd->di_next = dp;
}
else if (v[1] != NULL) {
stderror(ERR_NAME | ERR_TOOMANY);
/* NOTREACHED */
return;
}
else if ((dp = dfind(*v)) != NULL) {
char *tmp;
if (chdir(tmp = short2str(dp->di_name)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
}
else {
register Char *ccp;
ccp = dfollow(*v);
dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
dp->di_name = ccp;
dp->di_count = 0;
dp->di_prev = dcwd;
dp->di_next = dcwd->di_next;
dcwd->di_next = dp;
dp->di_next->di_prev = dp;
}
dnewcwd(dp);
}
/*
* dfind - find a directory if specified by numeric (+n) argument
*/
static struct directory *
dfind(cp)
register Char *cp;
{
register struct directory *dp;
register int i;
register Char *ep;
if (*cp++ != '+')
return (0);
for (ep = cp; Isdigit(*ep); ep++)
continue;
if (*ep)
return (0);
i = getn(cp);
if (i <= 0)
return (0);
for (dp = dcwd; i != 0; i--) {
if ((dp = dp->di_prev) == &dhead)
dp = dp->di_prev;
if (dp == dcwd)
stderror(ERR_NAME | ERR_DEEP);
}
return (dp);
}
/*
* dopopd - pop a directory out of the directory stack
* with a numeric argument just discard it.
*/
void
/*ARGSUSED*/
dopopd(v, t)
Char **v;
struct command *t;
{
register struct directory *dp, *p = NULL;
skipargs(&v, " [+<n>]");
printd = 1;
if (*v == NULL)
dp = dcwd;
else if (v[1] != NULL) {
stderror(ERR_NAME | ERR_TOOMANY);
/* NOTREACHED */
return;
}
else if ((dp = dfind(*v)) == 0)
stderror(ERR_NAME | ERR_BADDIR);
if (dp->di_prev == &dhead && dp->di_next == &dhead)
stderror(ERR_NAME | ERR_EMPTY);
if (dp == dcwd) {
char *tmp;
if ((p = dp->di_prev) == &dhead)
p = dhead.di_prev;
if (chdir(tmp = short2str(p->di_name)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
}
dp->di_prev->di_next = dp->di_next;
dp->di_next->di_prev = dp->di_prev;
if (dp == dcwd)
dnewcwd(p);
else {
printdirs();
}
dfree(dp);
}
/*
* dfree - free the directory (or keep it if it still has ref count)
*/
void
dfree(dp)
register struct directory *dp;
{
if (dp->di_count != 0) {
dp->di_next = dp->di_prev = 0;
}
else {
xfree((char *) dp->di_name);
xfree((ptr_t) dp);
}
}
/*
* dcanon - canonicalize the pathname, removing excess ./ and ../ etc.
* we are of course assuming that the file system is standardly
* constructed (always have ..'s, directories have links)
*/
Char *
dcanon(cp, p)
register Char *cp, *p;
{
register Char *sp;
register Char *p1, *p2; /* general purpose */
bool slash;
Char link[MAXPATHLEN];
char tlink[MAXPATHLEN];
int cc;
Char *newcp;
/*
* christos: if the path given does not start with a slash prepend cwd. If
* cwd does not start with a path or the result would be too long abort().
*/
if (*cp != '/') {
Char tmpdir[MAXPATHLEN];
p1 = value(STRcwd);
if (p1 == NULL || *p1 != '/')
abort();
if (Strlen(p1) + Strlen(cp) + 1 >= MAXPATHLEN)
abort();
(void) Strcpy(tmpdir, p1);
(void) Strcat(tmpdir, STRslash);
(void) Strcat(tmpdir, cp);
xfree((ptr_t) cp);
cp = p = Strsave(tmpdir);
}
while (*p) { /* for each component */
sp = p; /* save slash address */
while (*++p == '/') /* flush extra slashes */
continue;
if (p != ++sp)
for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
continue;
p = sp; /* save start of component */
slash = 0;
while (*++p) /* find next slash or end of path */
if (*p == '/') {
slash = 1;
*p = 0;
break;
}
if (*sp == '\0') /* if component is null */
if (--sp == cp) /* if path is one char (i.e. /) */
break;
else
*sp = '\0';
else if (sp[0] == '.' && sp[1] == 0) {
if (slash) {
for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
continue;
p = --sp;
}
else if (--sp != cp)
*sp = '\0';
}
else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
/*
* We have something like "yyy/xxx/..", where "yyy" can be null or
* a path starting at /, and "xxx" is a single component. Before
* compressing "xxx/..", we want to expand "yyy/xxx", if it is a
* symbolic link.
*/
*--sp = 0; /* form the pathname for readlink */
if (sp != cp && !adrof(STRignore_symlinks) &&
(cc = readlink(short2str(cp), tlink,
sizeof tlink)) >= 0) {
(void) Strcpy(link, str2short(tlink));
link[cc] = '\0';
if (slash)
*p = '/';
/*
* Point p to the '/' in "/..", and restore the '/'.
*/
*(p = sp) = '/';
/*
* find length of p
*/
for (p1 = p; *p1++;)
continue;
if (*link != '/') {
/*
* Relative path, expand it between the "yyy/" and the
* "/..". First, back sp up to the character past "yyy/".
*/
while (*--sp != '/')
continue;
sp++;
*sp = 0;
/*
* New length is "yyy/" + link + "/.." and rest
*/
p1 = newcp = (Char *) xmalloc((size_t)
(((sp - cp) + cc + (p1 - p)) *
sizeof(Char)));
/*
* Copy new path into newcp
*/
for (p2 = cp; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
continue;
/*
* Restart canonicalization at expanded "/xxx".
*/
p = sp - cp - 1 + newcp;
}
else {
/*
* New length is link + "/.." and rest
*/
p1 = newcp = (Char *) xmalloc((size_t)
((cc + (p1 - p)) * sizeof(Char)));
/*
* Copy new path into newcp
*/
for (p2 = link; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
continue;
/*
* Restart canonicalization at beginning
*/
p = newcp;
}
xfree((ptr_t) cp);
cp = newcp;
continue; /* canonicalize the link */
}
*sp = '/';
if (sp != cp)
while (*--sp != '/')
continue;
if (slash) {
for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
continue;
p = sp;
}
else if (cp == sp)
*++sp = '\0';
else
*sp = '\0';
}
else { /* normal dir name (not . or .. or nothing) */
if (sp != cp && adrof(STRchase_symlinks) &&
!adrof(STRignore_symlinks) &&
(cc = readlink(short2str(cp), tlink,
sizeof tlink)) >= 0) {
(void) Strcpy(link, str2short(tlink));
link[cc] = '\0';
/*
* restore the '/'.
*/
if (slash)
*p = '/';
/*
* point sp to p (rather than backing up).
*/
sp = p;
/*
* find length of p
*/
for (p1 = p; *p1++;)
continue;
if (*link != '/') {
/*
* Relative path, expand it between the "yyy/" and the
* remainder. First, back sp up to the character past
* "yyy/".
*/
while (*--sp != '/')
continue;
sp++;
*sp = 0;
/*
* New length is "yyy/" + link + "/.." and rest
*/
p1 = newcp = (Char *) xmalloc((size_t)
(((sp - cp) + cc + (p1 - p))
* sizeof(Char)));
/*
* Copy new path into newcp
*/
for (p2 = cp; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
continue;
/*
* Restart canonicalization at expanded "/xxx".
*/
p = sp - cp - 1 + newcp;
}
else {
/*
* New length is link + the rest
*/
p1 = newcp = (Char *) xmalloc((size_t)
((cc + (p1 - p)) * sizeof(Char)));
/*
* Copy new path into newcp
*/
for (p2 = link; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
continue;
/*
* Restart canonicalization at beginning
*/
p = newcp;
}
xfree((ptr_t) cp);
cp = newcp;
continue; /* canonicalize the link */
}
if (slash)
*p = '/';
}
}
/*
* fix home...
*/
p1 = value(STRhome);
cc = Strlen(p1);
/*
* See if we're not in a subdir of STRhome
*/
if (p1 && *p1 == '/' &&
(Strncmp(p1, cp, cc) != 0 || (cp[cc] != '/' && cp[cc] != '\0'))) {
static ino_t home_ino = -1;
static dev_t home_dev = -1;
static Char *home_ptr = NULL;
struct stat statbuf;
/*
* Get dev and ino of STRhome
*/
if (home_ptr != p1 &&
stat(short2str(p1), &statbuf) != -1) {
home_dev = statbuf.st_dev;
home_ino = statbuf.st_ino;
home_ptr = p1;
}
/*
* Start comparing dev & ino backwards
*/
p2 = Strcpy(link, cp);
for (sp = NULL; *p2 && stat(short2str(p2), &statbuf) != -1;) {
if (statbuf.st_dev == home_dev &&
statbuf.st_ino == home_ino) {
sp = (Char *) - 1;
break;
}
if ((sp = Strrchr(p2, '/')) != NULL)
*sp = '\0';
}
/*
* See if we found it
*/
if (*p2 && sp == (Char *) -1) {
/*
* Use STRhome to make '~' work
*/
newcp = Strspl(p1, cp + Strlen(p2));
xfree((ptr_t) cp);
cp = newcp;
}
}
return cp;
}
/*
* dnewcwd - make a new directory in the loop the current one
*/
static void
dnewcwd(dp)
register struct directory *dp;
{
dcwd = dp;
dset(dcwd->di_name);
if (printd && !(adrof(STRpushdsilent)))
printdirs();
}

45
bin/csh/dir.h Normal file
View File

@ -0,0 +1,45 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)dir.h 8.1 (Berkeley) 5/31/93
*/
/*
* Structure for entries in directory stack.
*/
struct directory {
struct directory *di_next; /* next in loop */
struct directory *di_prev; /* prev in loop */
unsigned short *di_count; /* refcount of processes */
Char *di_name; /* actual name */
};
struct directory *dcwd; /* the one we are in now */

985
bin/csh/dol.c Normal file
View File

@ -0,0 +1,985 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)dol.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
/*
* These routines perform variable substitution and quoting via ' and ".
* To this point these constructs have been preserved in the divided
* input words. Here we expand variables and turn quoting via ' and " into
* QUOTE bits on characters (which prevent further interpretation).
* If the `:q' modifier was applied during history expansion, then
* some QUOTEing may have occurred already, so we dont "trim()" here.
*/
static int Dpeekc, Dpeekrd; /* Peeks for DgetC and Dreadc */
static Char *Dcp, **Dvp; /* Input vector for Dreadc */
#define DEOF -1
#define unDgetC(c) Dpeekc = c
#define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
/*
* The following variables give the information about the current
* $ expansion, recording the current word position, the remaining
* words within this expansion, the count of remaining words, and the
* information about any : modifier which is being applied.
*/
#define MAXWLEN (BUFSIZ - 4)
#define MAXMOD MAXWLEN /* This cannot overflow */
static Char *dolp; /* Remaining chars from this word */
static Char **dolnxt; /* Further words */
static int dolcnt; /* Count of further words */
static Char dolmod[MAXMOD]; /* : modifier character */
static int dolnmod; /* Number of modifiers */
static int dolmcnt; /* :gx -> 10000, else 1 */
static int dolwcnt; /* :wx -> 10000, else 1 */
static void Dfix2 __P((Char **));
static Char *Dpack __P((Char *, Char *));
static int Dword __P((void));
static void dolerror __P((Char *));
static int DgetC __P((int));
static void Dgetdol __P((void));
static void fixDolMod __P((void));
static void setDolp __P((Char *));
static void unDredc __P((int));
static int Dredc __P((void));
static void Dtestq __P((int));
/*
* Fix up the $ expansions and quotations in the
* argument list to command t.
*/
void
Dfix(t)
register struct command *t;
{
register Char **pp;
register Char *p;
if (noexec)
return;
/* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
for (pp = t->t_dcom; (p = *pp++) != NULL;)
for (; *p; p++) {
if (cmap(*p, _DOL | QUOTES)) { /* $, \, ', ", ` */
Dfix2(t->t_dcom); /* found one */
blkfree(t->t_dcom);
t->t_dcom = gargv;
gargv = 0;
return;
}
}
}
/*
* $ substitute one word, for i/o redirection
*/
Char *
Dfix1(cp)
register Char *cp;
{
Char *Dv[2];
if (noexec)
return (0);
Dv[0] = cp;
Dv[1] = NULL;
Dfix2(Dv);
if (gargc != 1) {
setname(vis_str(cp));
stderror(ERR_NAME | ERR_AMBIG);
}
cp = Strsave(gargv[0]);
blkfree(gargv), gargv = 0;
return (cp);
}
/*
* Subroutine to do actual fixing after state initialization.
*/
static void
Dfix2(v)
Char **v;
{
ginit(); /* Initialize glob's area pointers */
Dvp = v;
Dcp = STRNULL; /* Setup input vector for Dreadc */
unDgetC(0);
unDredc(0); /* Clear out any old peeks (at error) */
dolp = 0;
dolcnt = 0; /* Clear out residual $ expands (...) */
while (Dword())
continue;
}
/*
* Pack up more characters in this word
*/
static Char *
Dpack(wbuf, wp)
Char *wbuf, *wp;
{
register int c;
register int i = MAXWLEN - (wp - wbuf);
for (;;) {
c = DgetC(DODOL);
if (c == '\\') {
c = DgetC(0);
if (c == DEOF) {
unDredc(c);
*wp = 0;
Gcat(STRNULL, wbuf);
return (NULL);
}
if (c == '\n')
c = ' ';
else
c |= QUOTE;
}
if (c == DEOF) {
unDredc(c);
*wp = 0;
Gcat(STRNULL, wbuf);
return (NULL);
}
if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */
unDgetC(c);
if (cmap(c, QUOTES))
return (wp);
*wp++ = 0;
Gcat(STRNULL, wbuf);
return (NULL);
}
if (--i <= 0)
stderror(ERR_WTOOLONG);
*wp++ = c;
}
}
/*
* Get a word. This routine is analogous to the routine
* word() in sh.lex.c for the main lexical input. One difference
* here is that we don't get a newline to terminate our expansion.
* Rather, DgetC will return a DEOF when we hit the end-of-input.
*/
static int
Dword()
{
register int c, c1;
Char wbuf[BUFSIZ];
register Char *wp = wbuf;
register int i = MAXWLEN;
register bool dolflg;
bool sofar = 0, done = 0;
while (!done) {
done = 1;
c = DgetC(DODOL);
switch (c) {
case DEOF:
if (sofar == 0)
return (0);
/* finish this word and catch the code above the next time */
unDredc(c);
/* fall into ... */
case '\n':
*wp = 0;
Gcat(STRNULL, wbuf);
return (1);
case ' ':
case '\t':
done = 0;
break;
case '`':
/* We preserve ` quotations which are done yet later */
*wp++ = c, --i;
case '\'':
case '"':
/*
* Note that DgetC never returns a QUOTES character from an
* expansion, so only true input quotes will get us here or out.
*/
c1 = c;
dolflg = c1 == '"' ? DODOL : 0;
for (;;) {
c = DgetC(dolflg);
if (c == c1)
break;
if (c == '\n' || c == DEOF)
stderror(ERR_UNMATCHED, c1);
if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE))
--wp, ++i;
if (--i <= 0)
stderror(ERR_WTOOLONG);
switch (c1) {
case '"':
/*
* Leave any `s alone for later. Other chars are all
* quoted, thus `...` can tell it was within "...".
*/
*wp++ = c == '`' ? '`' : c | QUOTE;
break;
case '\'':
/* Prevent all further interpretation */
*wp++ = c | QUOTE;
break;
case '`':
/* Leave all text alone for later */
*wp++ = c;
break;
default:
break;
}
}
if (c1 == '`')
*wp++ = '`' /* i--; eliminated */;
sofar = 1;
if ((wp = Dpack(wbuf, wp)) == NULL)
return (1);
else {
i = MAXWLEN - (wp - wbuf);
done = 0;
}
break;
case '\\':
c = DgetC(0); /* No $ subst! */
if (c == '\n' || c == DEOF) {
done = 0;
break;
}
c |= QUOTE;
break;
default:
break;
}
if (done) {
unDgetC(c);
sofar = 1;
if ((wp = Dpack(wbuf, wp)) == NULL)
return (1);
else {
i = MAXWLEN - (wp - wbuf);
done = 0;
}
}
}
/* Really NOTREACHED */
return (0);
}
/*
* Get a character, performing $ substitution unless flag is 0.
* Any QUOTES character which is returned from a $ expansion is
* QUOTEd so that it will not be recognized above.
*/
static int
DgetC(flag)
register int flag;
{
register int c;
top:
if ((c = Dpeekc) != '\0') {
Dpeekc = 0;
return (c);
}
if (lap) {
c = *lap++ & (QUOTE | TRIM);
if (c == 0) {
lap = 0;
goto top;
}
quotspec:
if (cmap(c, QUOTES))
return (c | QUOTE);
return (c);
}
if (dolp) {
if ((c = *dolp++ & (QUOTE | TRIM)) != '\0')
goto quotspec;
if (dolcnt > 0) {
setDolp(*dolnxt++);
--dolcnt;
return (' ');
}
dolp = 0;
}
if (dolcnt > 0) {
setDolp(*dolnxt++);
--dolcnt;
goto top;
}
c = Dredc();
if (c == '$' && flag) {
Dgetdol();
goto top;
}
return (c);
}
static Char *nulvec[] = {0};
static struct varent nulargv = {nulvec, STRargv, { NULL, NULL, NULL }, 0};
static void
dolerror(s)
Char *s;
{
setname(vis_str(s));
stderror(ERR_NAME | ERR_RANGE);
}
/*
* Handle the multitudinous $ expansion forms.
* Ugh.
*/
static void
Dgetdol()
{
register Char *np;
register struct varent *vp = NULL;
Char name[4 * MAXVARLEN + 1];
int c, sc;
int subscr = 0, lwb = 1, upb = 0;
bool dimen = 0, bitset = 0;
char tnp;
Char wbuf[BUFSIZ];
static Char *dolbang = NULL;
dolnmod = dolmcnt = dolwcnt = 0;
c = sc = DgetC(0);
if (c == '{')
c = DgetC(0); /* sc is { to take } later */
if ((c & TRIM) == '#')
dimen++, c = DgetC(0); /* $# takes dimension */
else if (c == '?')
bitset++, c = DgetC(0); /* $? tests existence */
switch (c) {
case '!':
if (dimen || bitset)
stderror(ERR_SYNTAX);
if (backpid != 0) {
if (dolbang)
xfree((ptr_t) dolbang);
setDolp(dolbang = putn(backpid));
}
goto eatbrac;
case '$':
if (dimen || bitset)
stderror(ERR_SYNTAX);
setDolp(doldol);
goto eatbrac;
case '<' | QUOTE:
if (bitset)
stderror(ERR_NOTALLOWED, "$?<");
if (dimen)
stderror(ERR_NOTALLOWED, "$?#");
for (np = wbuf; read(OLDSTD, &tnp, 1) == 1; np++) {
*np = (unsigned char) tnp;
if (np >= &wbuf[BUFSIZ - 1])
stderror(ERR_LTOOLONG);
if (tnp == '\n')
break;
}
*np = 0;
/*
* KLUDGE: dolmod is set here because it will cause setDolp to call
* domod and thus to copy wbuf. Otherwise setDolp would use it
* directly. If we saved it ourselves, no one would know when to free
* it. The actual function of the 'q' causes filename expansion not to
* be done on the interpolated value.
*/
dolmod[dolnmod++] = 'q';
dolmcnt = 10000;
setDolp(wbuf);
goto eatbrac;
case DEOF:
case '\n':
stderror(ERR_SYNTAX);
/* NOTREACHED */
break;
case '*':
(void) Strcpy(name, STRargv);
vp = adrof(STRargv);
subscr = -1; /* Prevent eating [...] */
break;
default:
np = name;
if (Isdigit(c)) {
if (dimen)
stderror(ERR_NOTALLOWED, "$#<num>");
subscr = 0;
do {
subscr = subscr * 10 + c - '0';
c = DgetC(0);
} while (Isdigit(c));
unDredc(c);
if (subscr < 0) {
dolerror(vp->v_name);
return;
}
if (subscr == 0) {
if (bitset) {
dolp = ffile ? STR1 : STR0;
goto eatbrac;
}
if (ffile == 0)
stderror(ERR_DOLZERO);
fixDolMod();
setDolp(ffile);
goto eatbrac;
}
if (bitset)
stderror(ERR_DOLQUEST);
vp = adrof(STRargv);
if (vp == 0) {
vp = &nulargv;
goto eatmod;
}
break;
}
if (!alnum(c))
stderror(ERR_VARALNUM);
for (;;) {
*np++ = c;
c = DgetC(0);
if (!alnum(c))
break;
if (np >= &name[MAXVARLEN])
stderror(ERR_VARTOOLONG);
}
*np++ = 0;
unDredc(c);
vp = adrof(name);
}
if (bitset) {
dolp = (vp || getenv(short2str(name))) ? STR1 : STR0;
goto eatbrac;
}
if (vp == 0) {
np = str2short(getenv(short2str(name)));
if (np) {
fixDolMod();
setDolp(np);
goto eatbrac;
}
udvar(name);
/* NOTREACHED */
}
c = DgetC(0);
upb = blklen(vp->vec);
if (dimen == 0 && subscr == 0 && c == '[') {
np = name;
for (;;) {
c = DgetC(DODOL); /* Allow $ expand within [ ] */
if (c == ']')
break;
if (c == '\n' || c == DEOF)
stderror(ERR_INCBR);
if (np >= &name[sizeof(name) / sizeof(Char) - 2])
stderror(ERR_VARTOOLONG);
*np++ = c;
}
*np = 0, np = name;
if (dolp || dolcnt) /* $ exp must end before ] */
stderror(ERR_EXPORD);
if (!*np)
stderror(ERR_SYNTAX);
if (Isdigit(*np)) {
int i;
for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0')
continue;
if ((i < 0 || i > upb) && !any("-*", *np)) {
dolerror(vp->v_name);
return;
}
lwb = i;
if (!*np)
upb = lwb, np = STRstar;
}
if (*np == '*')
np++;
else if (*np != '-')
stderror(ERR_MISSING, '-');
else {
register int i = upb;
np++;
if (Isdigit(*np)) {
i = 0;
while (Isdigit(*np))
i = i * 10 + *np++ - '0';
if (i < 0 || i > upb) {
dolerror(vp->v_name);
return;
}
}
if (i < lwb)
upb = lwb - 1;
else
upb = i;
}
if (lwb == 0) {
if (upb != 0) {
dolerror(vp->v_name);
return;
}
upb = -1;
}
if (*np)
stderror(ERR_SYNTAX);
}
else {
if (subscr > 0)
if (subscr > upb)
lwb = 1, upb = 0;
else
lwb = upb = subscr;
unDredc(c);
}
if (dimen) {
Char *cp = putn(upb - lwb + 1);
addla(cp);
xfree((ptr_t) cp);
}
else {
eatmod:
fixDolMod();
dolnxt = &vp->vec[lwb - 1];
dolcnt = upb - lwb + 1;
}
eatbrac:
if (sc == '{') {
c = Dredc();
if (c != '}')
stderror(ERR_MISSING, '}');
}
}
static void
fixDolMod()
{
register int c;
c = DgetC(0);
if (c == ':') {
do {
c = DgetC(0), dolmcnt = 1, dolwcnt = 1;
if (c == 'g' || c == 'a') {
if (c == 'g')
dolmcnt = 10000;
else
dolwcnt = 10000;
c = DgetC(0);
}
if ((c == 'g' && dolmcnt != 10000) ||
(c == 'a' && dolwcnt != 10000)) {
if (c == 'g')
dolmcnt = 10000;
else
dolwcnt = 10000;
c = DgetC(0);
}
if (c == 's') { /* [eichin:19910926.0755EST] */
int delimcnt = 2;
int delim = DgetC(0);
dolmod[dolnmod++] = c;
dolmod[dolnmod++] = delim;
if (!delim || letter(delim)
|| Isdigit(delim) || any(" \t\n", delim)) {
seterror(ERR_BADSUBST);
break;
}
while ((c = DgetC(0)) != (-1)) {
dolmod[dolnmod++] = c;
if(c == delim) delimcnt--;
if(!delimcnt) break;
}
if(delimcnt) {
seterror(ERR_BADSUBST);
break;
}
continue;
}
if (!any("htrqxes", c))
stderror(ERR_BADMOD, c);
dolmod[dolnmod++] = c;
if (c == 'q')
dolmcnt = 10000;
}
while ((c = DgetC(0)) == ':');
unDredc(c);
}
else
unDredc(c);
}
static void
setDolp(cp)
register Char *cp;
{
register Char *dp;
int i;
if (dolnmod == 0 || dolmcnt == 0) {
dolp = cp;
return;
}
dp = cp = Strsave(cp);
for (i = 0; i < dolnmod; i++) {
/* handle s// [eichin:19910926.0510EST] */
if(dolmod[i] == 's') {
int delim;
Char *lhsub, *rhsub, *np;
size_t lhlen = 0, rhlen = 0;
int didmod = 0;
delim = dolmod[++i];
if (!delim || letter(delim)
|| Isdigit(delim) || any(" \t\n", delim)) {
seterror(ERR_BADSUBST);
break;
}
lhsub = &dolmod[++i];
while(dolmod[i] != delim && dolmod[++i]) {
lhlen++;
}
dolmod[i] = 0;
rhsub = &dolmod[++i];
while(dolmod[i] != delim && dolmod[++i]) {
rhlen++;
}
dolmod[i] = 0;
do {
dp = Strstr(cp, lhsub);
if (dp) {
np = (Char *) xmalloc((size_t)
((Strlen(cp) + 1 - lhlen + rhlen) *
sizeof(Char)));
(void) Strncpy(np, cp, dp - cp);
(void) Strcpy(np + (dp - cp), rhsub);
(void) Strcpy(np + (dp - cp) + rhlen, dp + lhlen);
xfree((ptr_t) cp);
dp = cp = np;
didmod = 1;
} else {
/* should this do a seterror? */
break;
}
}
while (dolwcnt == 10000);
/*
* restore dolmod for additional words
*/
dolmod[i] = rhsub[-1] = delim;
if (didmod)
dolmcnt--;
else
break;
} else {
int didmod = 0;
do {
if ((dp = domod(cp, dolmod[i]))) {
didmod = 1;
if (Strcmp(cp, dp) == 0) {
xfree((ptr_t) cp);
cp = dp;
break;
}
else {
xfree((ptr_t) cp);
cp = dp;
}
}
else
break;
}
while (dolwcnt == 10000);
dp = cp;
if (didmod)
dolmcnt--;
else
break;
}
}
if (dp) {
addla(dp);
xfree((ptr_t) dp);
}
else
addla(cp);
dolp = STRNULL;
if (seterr)
stderror(ERR_OLD);
}
static void
unDredc(c)
int c;
{
Dpeekrd = c;
}
static int
Dredc()
{
register int c;
if ((c = Dpeekrd) != '\0') {
Dpeekrd = 0;
return (c);
}
if (Dcp && (c = *Dcp++))
return (c & (QUOTE | TRIM));
if (*Dvp == 0) {
Dcp = 0;
return (DEOF);
}
Dcp = *Dvp++;
return (' ');
}
static void
Dtestq(c)
register int c;
{
if (cmap(c, QUOTES))
gflag = 1;
}
/*
* Form a shell temporary file (in unit 0) from the words
* of the shell input up to EOF or a line the same as "term".
* Unit 0 should have been closed before this call.
*/
void
/*ARGSUSED*/
heredoc(term)
Char *term;
{
register int c;
Char *Dv[2];
Char obuf[BUFSIZ], lbuf[BUFSIZ], mbuf[BUFSIZ];
int ocnt, lcnt, mcnt;
register Char *lbp, *obp, *mbp;
Char **vp;
bool quoted;
char *tmp;
if (creat(tmp = short2str(shtemp), 0600) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
(void) close(0);
if (open(tmp, O_RDWR) < 0) {
int oerrno = errno;
(void) unlink(tmp);
errno = oerrno;
stderror(ERR_SYSTEM, tmp, strerror(errno));
}
(void) unlink(tmp); /* 0 0 inode! */
Dv[0] = term;
Dv[1] = NULL;
gflag = 0;
trim(Dv);
rscan(Dv, Dtestq);
quoted = gflag;
ocnt = BUFSIZ;
obp = obuf;
for (;;) {
/*
* Read up a line
*/
lbp = lbuf;
lcnt = BUFSIZ - 4;
for (;;) {
c = readc(1); /* 1 -> Want EOF returns */
if (c < 0 || c == '\n')
break;
if ((c &= TRIM) != '\0') {
*lbp++ = c;
if (--lcnt < 0) {
setname("<<");
stderror(ERR_NAME | ERR_OVERFLOW);
}
}
}
*lbp = 0;
/*
* Check for EOF or compare to terminator -- before expansion
*/
if (c < 0 || eq(lbuf, term)) {
(void) write(0, short2str(obuf), (size_t) (BUFSIZ - ocnt));
(void) lseek(0, 0l, L_SET);
return;
}
/*
* If term was quoted or -n just pass it on
*/
if (quoted || noexec) {
*lbp++ = '\n';
*lbp = 0;
for (lbp = lbuf; (c = *lbp++) != '\0';) {
*obp++ = c;
if (--ocnt == 0) {
(void) write(0, short2str(obuf), BUFSIZ);
obp = obuf;
ocnt = BUFSIZ;
}
}
continue;
}
/*
* Term wasn't quoted so variable and then command expand the input
* line
*/
Dcp = lbuf;
Dvp = Dv + 1;
mbp = mbuf;
mcnt = BUFSIZ - 4;
for (;;) {
c = DgetC(DODOL);
if (c == DEOF)
break;
if ((c &= TRIM) == 0)
continue;
/* \ quotes \ $ ` here */
if (c == '\\') {
c = DgetC(0);
if (!any("$\\`", c))
unDgetC(c | QUOTE), c = '\\';
else
c |= QUOTE;
}
*mbp++ = c;
if (--mcnt == 0) {
setname("<<");
stderror(ERR_NAME | ERR_OVERFLOW);
}
}
*mbp++ = 0;
/*
* If any ` in line do command substitution
*/
mbp = mbuf;
if (any(short2str(mbp), '`')) {
/*
* 1 arg to dobackp causes substitution to be literal. Words are
* broken only at newlines so that all blanks and tabs are
* preserved. Blank lines (null words) are not discarded.
*/
vp = dobackp(mbuf, 1);
}
else
/* Setup trivial vector similar to return of dobackp */
Dv[0] = mbp, Dv[1] = NULL, vp = Dv;
/*
* Resurrect the words from the command substitution each separated by
* a newline. Note that the last newline of a command substitution
* will have been discarded, but we put a newline after the last word
* because this represents the newline after the last input line!
*/
for (; *vp; vp++) {
for (mbp = *vp; *mbp; mbp++) {
*obp++ = *mbp & TRIM;
if (--ocnt == 0) {
(void) write(0, short2str(obuf), BUFSIZ);
obp = obuf;
ocnt = BUFSIZ;
}
}
*obp++ = '\n';
if (--ocnt == 0) {
(void) write(0, short2str(obuf), BUFSIZ);
obp = obuf;
ocnt = BUFSIZ;
}
}
if (pargv)
blkfree(pargv), pargv = 0;
}
}

407
bin/csh/err.c Normal file
View File

@ -0,0 +1,407 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
char *seterr = NULL; /* Holds last error if there was one */
#define ERR_FLAGS 0xf0000000
#define ERR_NAME 0x10000000
#define ERR_SILENT 0x20000000
#define ERR_OLD 0x40000000
static char *errorlist[] =
{
#define ERR_SYNTAX 0
"Syntax Error",
#define ERR_NOTALLOWED 1
"%s is not allowed",
#define ERR_WTOOLONG 2
"Word too long",
#define ERR_LTOOLONG 3
"$< line too long",
#define ERR_DOLZERO 4
"No file for $0",
#define ERR_DOLQUEST 5
"$? not allowed here",
#define ERR_INCBR 6
"Incomplete [] modifier",
#define ERR_EXPORD 7
"$ expansion must end before ]",
#define ERR_BADMOD 8
"Bad : modifier in $ (%c)",
#define ERR_SUBSCRIPT 9
"Subscript error",
#define ERR_BADNUM 10
"Badly formed number",
#define ERR_NOMORE 11
"No more words",
#define ERR_FILENAME 12
"Missing file name",
#define ERR_GLOB 13
"Internal glob error",
#define ERR_COMMAND 14
"Command not found",
#define ERR_TOOFEW 15
"Too few arguments",
#define ERR_TOOMANY 16
"Too many arguments",
#define ERR_DANGER 17
"Too dangerous to alias that",
#define ERR_EMPTYIF 18
"Empty if",
#define ERR_IMPRTHEN 19
"Improper then",
#define ERR_NOPAREN 20
"Words not parenthesized",
#define ERR_NOTFOUND 21
"%s not found",
#define ERR_MASK 22
"Improper mask",
#define ERR_LIMIT 23
"No such limit",
#define ERR_TOOLARGE 24
"Argument too large",
#define ERR_SCALEF 25
"Improper or unknown scale factor",
#define ERR_UNDVAR 26
"Undefined variable",
#define ERR_DEEP 27
"Directory stack not that deep",
#define ERR_BADSIG 28
"Bad signal number",
#define ERR_UNKSIG 29
"Unknown signal; kill -l lists signals",
#define ERR_VARBEGIN 30
"Variable name must begin with a letter",
#define ERR_VARTOOLONG 31
"Variable name too long",
#define ERR_VARALNUM 32
"Variable name must contain alphanumeric characters",
#define ERR_JOBCONTROL 33
"No job control in this shell",
#define ERR_EXPRESSION 34
"Expression Syntax",
#define ERR_NOHOMEDIR 35
"No home directory",
#define ERR_CANTCHANGE 36
"Can't change to home directory",
#define ERR_NULLCOM 37
"Invalid null command",
#define ERR_ASSIGN 38
"Assignment missing expression",
#define ERR_UNKNOWNOP 39
"Unknown operator",
#define ERR_AMBIG 40
"Ambiguous",
#define ERR_EXISTS 41
"%s: File exists",
#define ERR_INTR 42
"Interrupted",
#define ERR_RANGE 43
"Subscript out of range",
#define ERR_OVERFLOW 44
"Line overflow",
#define ERR_VARMOD 45
"Unknown variable modifier",
#define ERR_NOSUCHJOB 46
"No such job",
#define ERR_TERMINAL 47
"Can't from terminal",
#define ERR_NOTWHILE 48
"Not in while/foreach",
#define ERR_NOPROC 49
"No more processes",
#define ERR_NOMATCH 50
"No match",
#define ERR_MISSING 51
"Missing %c",
#define ERR_UNMATCHED 52
"Unmatched %c",
#define ERR_NOMEM 53
"Out of memory",
#define ERR_PIPE 54
"Can't make pipe",
#define ERR_SYSTEM 55
"%s: %s",
#define ERR_STRING 56
"%s",
#define ERR_JOBS 57
"Usage: jobs [ -l ]",
#define ERR_JOBARGS 58
"Arguments should be jobs or process id's",
#define ERR_JOBCUR 59
"No current job",
#define ERR_JOBPREV 60
"No previous job",
#define ERR_JOBPAT 61
"No job matches pattern",
#define ERR_NESTING 62
"Fork nesting > %d; maybe `...` loop",
#define ERR_JOBCTRLSUB 63
"No job control in subshells",
#define ERR_BADPLPS 64
"Badly placed ()'s",
#define ERR_STOPPED 65
"%sThere are suspended jobs",
#define ERR_NODIR 66
"No other directory",
#define ERR_EMPTY 67
"Directory stack empty",
#define ERR_BADDIR 68
"Bad directory",
#define ERR_DIRUS 69
"Usage: %s [-lvn]%s",
#define ERR_HFLAG 70
"No operand for -h flag",
#define ERR_NOTLOGIN 71
"Not a login shell",
#define ERR_DIV0 72
"Division by 0",
#define ERR_MOD0 73
"Mod by 0",
#define ERR_BADSCALE 74
"Bad scaling; did you mean \"%s\"?",
#define ERR_SUSPLOG 75
"Can't suspend a login shell (yet)",
#define ERR_UNKUSER 76
"Unknown user: %s",
#define ERR_NOHOME 77
"No $home variable set",
#define ERR_HISTUS 78
"Usage: history [-rh] [# number of events]",
#define ERR_SPDOLLT 79
"$, ! or < not allowed with $# or $?",
#define ERR_NEWLINE 80
"Newline in variable name",
#define ERR_SPSTAR 81
"* not allowed with $# or $?",
#define ERR_DIGIT 82
"$?<digit> or $#<digit> not allowed",
#define ERR_VARILL 83
"Illegal variable name",
#define ERR_NLINDEX 84
"Newline in variable index",
#define ERR_EXPOVFL 85
"Expansion buffer overflow",
#define ERR_VARSYN 86
"Variable syntax",
#define ERR_BADBANG 87
"Bad ! form",
#define ERR_NOSUBST 88
"No previous substitute",
#define ERR_BADSUBST 89
"Bad substitute",
#define ERR_LHS 90
"No previous left hand side",
#define ERR_RHSLONG 91
"Right hand side too long",
#define ERR_BADBANGMOD 92
"Bad ! modifier: %c",
#define ERR_MODFAIL 93
"Modifier failed",
#define ERR_SUBOVFL 94
"Substitution buffer overflow",
#define ERR_BADBANGARG 95
"Bad ! arg selector",
#define ERR_NOSEARCH 96
"No prev search",
#define ERR_NOEVENT 97
"%s: Event not found",
#define ERR_TOOMANYRP 98
"Too many )'s",
#define ERR_TOOMANYLP 99
"Too many ('s",
#define ERR_BADPLP 100
"Badly placed (",
#define ERR_MISRED 101
"Missing name for redirect",
#define ERR_OUTRED 102
"Ambiguous output redirect",
#define ERR_REDPAR 103
"Can't << within ()'s",
#define ERR_INRED 104
"Ambiguous input redirect",
#define ERR_ALIASLOOP 105
"Alias loop",
#define ERR_HISTLOOP 106
"!# History loop",
#define ERR_ARCH 107
"%s: %s. Wrong Architecture",
#define ERR_FILEINQ 108
"Malformed file inquiry",
#define ERR_SELOVFL 109
"Selector overflow",
#define ERR_INVALID 110
"Invalid Error"
};
/*
* The parser and scanner set up errors for later by calling seterr,
* which sets the variable err as a side effect; later to be tested,
* e.g. in process.
*/
void
#if __STDC__
seterror(int id, ...)
#else
seterror(id, va_alist)
int id;
va_dcl
#endif
{
if (seterr == 0) {
char berr[BUFSIZ];
va_list va;
#if __STDC__
va_start(va, id);
#else
va_start(va);
#endif
if (id < 0 || id > sizeof(errorlist) / sizeof(errorlist[0]))
id = ERR_INVALID;
vsprintf(berr, errorlist[id], va);
va_end(va);
seterr = strsave(berr);
}
}
/*
* Print the error with the given id.
*
* Special ids:
* ERR_SILENT: Print nothing.
* ERR_OLD: Print the previously set error if one was there.
* otherwise return.
* ERR_NAME: If this bit is set, print the name of the function
* in bname
*
* This routine always resets or exits. The flag haderr
* is set so the routine who catches the unwind can propogate
* it if they want.
*
* Note that any open files at the point of error will eventually
* be closed in the routine process in sh.c which is the only
* place error unwinds are ever caught.
*/
void
#if __STDC__
stderror(int id, ...)
#else
stderror(id, va_alist)
int id;
va_dcl
#endif
{
va_list va;
register Char **v;
int flags = id & ERR_FLAGS;
id &= ~ERR_FLAGS;
if ((flags & ERR_OLD) && seterr == NULL)
return;
if (id < 0 || id > sizeof(errorlist) / sizeof(errorlist[0]))
id = ERR_INVALID;
(void) fflush(cshout);
(void) fflush(csherr);
haderr = 1; /* Now to diagnostic output */
timflg = 0; /* This isn't otherwise reset */
if (!(flags & ERR_SILENT)) {
if (flags & ERR_NAME)
(void) fprintf(csherr, "%s: ", bname);
if ((flags & ERR_OLD))
/* Old error. */
(void) fprintf(csherr, "%s.\n", seterr);
else {
#if __STDC__
va_start(va, id);
#else
va_start(va);
#endif
(void) vfprintf(csherr, errorlist[id], va);
va_end(va);
(void) fprintf(csherr, ".\n");
}
}
if (seterr) {
xfree((ptr_t) seterr);
seterr = NULL;
}
if ((v = pargv) != NULL)
pargv = 0, blkfree(v);
if ((v = gargv) != NULL)
gargv = 0, blkfree(v);
(void) fflush(cshout);
(void) fflush(csherr);
didfds = 0; /* Forget about 0,1,2 */
/*
* Go away if -e or we are a child shell
*/
if (exiterr || child)
xexit(1);
/*
* Reset the state of the input. This buffered seek to end of file will
* also clear the while/foreach stack.
*/
btoeof();
set(STRstatus, Strsave(STR1));
if (tpgrp > 0)
(void) tcsetpgrp(FSHTTY, tpgrp);
reset(); /* Unwind */
}

736
bin/csh/exec.c Normal file
View File

@ -0,0 +1,736 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <sys/param.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
/*
* System level search and execute of a command. We look in each directory
* for the specified command name. If the name contains a '/' then we
* execute only the full path name. If there is no search path then we
* execute only full path names.
*/
extern char **environ;
/*
* As we search for the command we note the first non-trivial error
* message for presentation to the user. This allows us often
* to show that a file has the wrong mode/no access when the file
* is not in the last component of the search path, so we must
* go on after first detecting the error.
*/
static char *exerr; /* Execution error message */
static Char *expath; /* Path for exerr */
/*
* Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
* to hash execs. If it is allocated (havhash true), then to tell
* whether ``name'' is (possibly) present in the i'th component
* of the variable path, you look at the bit in xhash indexed by
* hash(hashname("name"), i). This is setup automatically
* after .login is executed, and recomputed whenever ``path'' is
* changed.
* The two part hash function is designed to let texec() call the
* more expensive hashname() only once and the simple hash() several
* times (once for each path component checked).
* Byte size is assumed to be 8.
*/
#define HSHSIZ 8192 /* 1k bytes */
#define HSHMASK (HSHSIZ - 1)
#define HSHMUL 243
static char xhash[HSHSIZ / 8];
#define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
#define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */
#define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7)) /* bit set */
static int hits, misses;
/* Dummy search path for just absolute search when no path */
static Char *justabs[] = {STRNULL, 0};
static void pexerr __P((void));
static void texec __P((Char *, Char **));
static int hashname __P((Char *));
static void tellmewhat __P((struct wordent *));
static int executable __P((Char *, Char *, bool));
static int iscommand __P((Char *));
void
/*ARGSUSED*/
doexec(v, t)
Char **v;
struct command *t;
{
register Char *dp, **pv, **av, *sav;
register struct varent *pathv;
register bool slash;
register int hashval = 0, hashval1, i;
Char *blk[2];
/*
* Glob the command name. We will search $path even if this does something,
* as in sh but not in csh. One special case: if there is no PATH, then we
* execute only commands which start with '/'.
*/
blk[0] = t->t_dcom[0];
blk[1] = 0;
gflag = 0, tglob(blk);
if (gflag) {
pv = globall(blk);
if (pv == 0) {
setname(vis_str(blk[0]));
stderror(ERR_NAME | ERR_NOMATCH);
}
gargv = 0;
}
else
pv = saveblk(blk);
trim(pv);
exerr = 0;
expath = Strsave(pv[0]);
Vexpath = expath;
pathv = adrof(STRpath);
if (pathv == 0 && expath[0] != '/') {
blkfree(pv);
pexerr();
}
slash = any(short2str(expath), '/');
/*
* Glob the argument list, if necessary. Otherwise trim off the quote bits.
*/
gflag = 0;
av = &t->t_dcom[1];
tglob(av);
if (gflag) {
av = globall(av);
if (av == 0) {
blkfree(pv);
setname(vis_str(expath));
stderror(ERR_NAME | ERR_NOMATCH);
}
gargv = 0;
}
else
av = saveblk(av);
blkfree(t->t_dcom);
t->t_dcom = blkspl(pv, av);
xfree((ptr_t) pv);
xfree((ptr_t) av);
av = t->t_dcom;
trim(av);
if (*av == NULL || **av == '\0')
pexerr();
xechoit(av); /* Echo command if -x */
/*
* Since all internal file descriptors are set to close on exec, we don't
* need to close them explicitly here. Just reorient ourselves for error
* messages.
*/
SHIN = 0;
SHOUT = 1;
SHERR = 2;
OLDSTD = 0;
/*
* We must do this AFTER any possible forking (like `foo` in glob) so that
* this shell can still do subprocesses.
*/
(void) sigsetmask((sigset_t) 0);
/*
* If no path, no words in path, or a / in the filename then restrict the
* command search.
*/
if (pathv == 0 || pathv->vec[0] == 0 || slash)
pv = justabs;
else
pv = pathv->vec;
sav = Strspl(STRslash, *av);/* / command name for postpending */
Vsav = sav;
if (havhash)
hashval = hashname(*av);
i = 0;
hits++;
do {
/*
* Try to save time by looking at the hash table for where this command
* could be. If we are doing delayed hashing, then we put the names in
* one at a time, as the user enters them. This is kinda like Korn
* Shell's "tracked aliases".
*/
if (!slash && pv[0][0] == '/' && havhash) {
hashval1 = hash(hashval, i);
if (!bit(xhash, hashval1))
goto cont;
}
if (pv[0][0] == 0 || eq(pv[0], STRdot)) /* don't make ./xxx */
texec(*av, av);
else {
dp = Strspl(*pv, sav);
Vdp = dp;
texec(dp, av);
Vdp = 0;
xfree((ptr_t) dp);
}
misses++;
cont:
pv++;
i++;
} while (*pv);
hits--;
Vsav = 0;
xfree((ptr_t) sav);
pexerr();
}
static void
pexerr()
{
/* Couldn't find the damn thing */
if (expath) {
setname(vis_str(expath));
Vexpath = 0;
xfree((ptr_t) expath);
expath = 0;
}
else
setname("");
if (exerr)
stderror(ERR_NAME | ERR_STRING, exerr);
stderror(ERR_NAME | ERR_COMMAND);
}
/*
* Execute command f, arg list t.
* Record error message if not found.
* Also do shell scripts here.
*/
static void
texec(sf, st)
Char *sf;
register Char **st;
{
register char **t;
register char *f;
register struct varent *v;
register Char **vp;
Char *lastsh[2];
int fd;
unsigned char c;
Char *st0, **ost;
/* The order for the conversions is significant */
t = short2blk(st);
f = short2str(sf);
Vt = t;
errno = 0; /* don't use a previous error */
(void) execve(f, t, environ);
Vt = 0;
blkfree((Char **) t);
switch (errno) {
case ENOEXEC:
/*
* From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
* it, don't feed it to the shell if it looks like a binary!
*/
if ((fd = open(f, O_RDONLY)) != -1) {
if (read(fd, (char *) &c, 1) == 1) {
if (!Isprint(c) && (c != '\n' && c != '\t')) {
(void) close(fd);
/*
* We *know* what ENOEXEC means.
*/
stderror(ERR_ARCH, f, strerror(errno));
}
}
#ifdef _PATH_BSHELL
else
c = '#';
#endif
(void) close(fd);
}
/*
* If there is an alias for shell, then put the words of the alias in
* front of the argument list replacing the command name. Note no
* interpretation of the words at this point.
*/
v = adrof1(STRshell, &aliases);
if (v == 0) {
vp = lastsh;
vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH;
vp[1] = NULL;
#ifdef _PATH_BSHELL
if (fd != -1 && c != '#')
vp[0] = STR_BSHELL;
#endif
}
else
vp = v->vec;
st0 = st[0];
st[0] = sf;
ost = st;
st = blkspl(vp, st); /* Splice up the new arglst */
ost[0] = st0;
sf = *st;
/* The order for the conversions is significant */
t = short2blk(st);
f = short2str(sf);
xfree((ptr_t) st);
Vt = t;
(void) execve(f, t, environ);
Vt = 0;
blkfree((Char **) t);
/* The sky is falling, the sky is falling! */
case ENOMEM:
stderror(ERR_SYSTEM, f, strerror(errno));
case ENOENT:
break;
default:
if (exerr == 0) {
exerr = strerror(errno);
if (expath)
xfree((ptr_t) expath);
expath = Strsave(sf);
Vexpath = expath;
}
}
}
/*ARGSUSED*/
void
execash(t, kp)
Char **t;
register struct command *kp;
{
int saveIN, saveOUT, saveDIAG, saveSTD;
int oSHIN;
int oSHOUT;
int oSHERR;
int oOLDSTD;
jmp_buf osetexit;
int my_reenter;
int odidfds;
sig_t osigint, osigquit, osigterm;
if (chkstop == 0 && setintr)
panystop(0);
/*
* Hmm, we don't really want to do that now because we might
* fail, but what is the choice
*/
rechist();
osigint = signal(SIGINT, parintr);
osigquit = signal(SIGQUIT, parintr);
osigterm = signal(SIGTERM, parterm);
odidfds = didfds;
oSHIN = SHIN;
oSHOUT = SHOUT;
oSHERR = SHERR;
oOLDSTD = OLDSTD;
saveIN = dcopy(SHIN, -1);
saveOUT = dcopy(SHOUT, -1);
saveDIAG = dcopy(SHERR, -1);
saveSTD = dcopy(OLDSTD, -1);
lshift(kp->t_dcom, 1);
getexit(osetexit);
if ((my_reenter = setexit()) == 0) {
SHIN = dcopy(0, -1);
SHOUT = dcopy(1, -1);
SHERR = dcopy(2, -1);
didfds = 0;
doexec(t, kp);
}
(void) signal(SIGINT, osigint);
(void) signal(SIGQUIT, osigquit);
(void) signal(SIGTERM, osigterm);
doneinp = 0;
didfds = odidfds;
(void) close(SHIN);
(void) close(SHOUT);
(void) close(SHERR);
(void) close(OLDSTD);
SHIN = dmove(saveIN, oSHIN);
SHOUT = dmove(saveOUT, oSHOUT);
SHERR = dmove(saveDIAG, oSHERR);
OLDSTD = dmove(saveSTD, oOLDSTD);
resexit(osetexit);
if (my_reenter)
stderror(ERR_SILENT);
}
void
xechoit(t)
Char **t;
{
if (adrof(STRecho)) {
(void) fflush(csherr);
blkpr(csherr, t);
(void) fputc('\n', csherr);
}
}
void
/*ARGSUSED*/
dohash(v, t)
Char **v;
struct command *t;
{
DIR *dirp;
register struct dirent *dp;
register int cnt;
int i = 0;
struct varent *pathv = adrof(STRpath);
Char **pv;
int hashval;
havhash = 1;
for (cnt = 0; cnt < sizeof xhash; cnt++)
xhash[cnt] = 0;
if (pathv == 0)
return;
for (pv = pathv->vec; *pv; pv++, i++) {
if (pv[0][0] != '/')
continue;
dirp = opendir(short2str(*pv));
if (dirp == NULL)
continue;
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_ino == 0)
continue;
if (dp->d_name[0] == '.' &&
(dp->d_name[1] == '\0' ||
(dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
continue;
hashval = hash(hashname(str2short(dp->d_name)), i);
bis(xhash, hashval);
/* tw_add_comm_name (dp->d_name); */
}
(void) closedir(dirp);
}
}
void
/*ARGSUSED*/
dounhash(v, t)
Char **v;
struct command *t;
{
havhash = 0;
}
void
/*ARGSUSED*/
hashstat(v, t)
Char **v;
struct command *t;
{
if (hits + misses)
(void) fprintf(cshout, "%d hits, %d misses, %d%%\n",
hits, misses, 100 * hits / (hits + misses));
}
/*
* Hash a command name.
*/
static int
hashname(cp)
register Char *cp;
{
register long h = 0;
while (*cp)
h = hash(h, *cp++);
return ((int) h);
}
static int
iscommand(name)
Char *name;
{
register Char **pv;
register Char *sav;
register struct varent *v;
register bool slash = any(short2str(name), '/');
register int hashval = 0, hashval1, i;
v = adrof(STRpath);
if (v == 0 || v->vec[0] == 0 || slash)
pv = justabs;
else
pv = v->vec;
sav = Strspl(STRslash, name); /* / command name for postpending */
if (havhash)
hashval = hashname(name);
i = 0;
do {
if (!slash && pv[0][0] == '/' && havhash) {
hashval1 = hash(hashval, i);
if (!bit(xhash, hashval1))
goto cont;
}
if (pv[0][0] == 0 || eq(pv[0], STRdot)) { /* don't make ./xxx */
if (executable(NULL, name, 0)) {
xfree((ptr_t) sav);
return i + 1;
}
}
else {
if (executable(*pv, sav, 0)) {
xfree((ptr_t) sav);
return i + 1;
}
}
cont:
pv++;
i++;
} while (*pv);
xfree((ptr_t) sav);
return 0;
}
/* Also by:
* Andreas Luik <luik@isaak.isa.de>
* I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
* Azenberstr. 35
* D-7000 Stuttgart 1
* West-Germany
* is the executable() routine below and changes to iscommand().
* Thanks again!!
*/
/*
* executable() examines the pathname obtained by concatenating dir and name
* (dir may be NULL), and returns 1 either if it is executable by us, or
* if dir_ok is set and the pathname refers to a directory.
* This is a bit kludgy, but in the name of optimization...
*/
static int
executable(dir, name, dir_ok)
Char *dir, *name;
bool dir_ok;
{
struct stat stbuf;
Char path[MAXPATHLEN + 1], *dp, *sp;
char *strname;
if (dir && *dir) {
for (dp = path, sp = dir; *sp; *dp++ = *sp++)
if (dp == &path[MAXPATHLEN + 1]) {
*--dp = '\0';
break;
}
for (sp = name; *sp; *dp++ = *sp++)
if (dp == &path[MAXPATHLEN + 1]) {
*--dp = '\0';
break;
}
*dp = '\0';
strname = short2str(path);
}
else
strname = short2str(name);
return (stat(strname, &stbuf) != -1 &&
((S_ISREG(stbuf.st_mode) &&
/* save time by not calling access() in the hopeless case */
(stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
access(strname, X_OK) == 0) ||
(dir_ok && S_ISDIR(stbuf.st_mode))));
}
/* The dowhich() is by:
* Andreas Luik <luik@isaak.isa.de>
* I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
* Azenberstr. 35
* D-7000 Stuttgart 1
* West-Germany
* Thanks!!
*/
/*ARGSUSED*/
void
dowhich(v, c)
register Char **v;
struct command *c;
{
struct wordent lex[3];
struct varent *vp;
lex[0].next = &lex[1];
lex[1].next = &lex[2];
lex[2].next = &lex[0];
lex[0].prev = &lex[2];
lex[1].prev = &lex[0];
lex[2].prev = &lex[1];
lex[0].word = STRNULL;
lex[2].word = STRret;
while (*++v) {
if ((vp = adrof1(*v, &aliases)) != NULL) {
(void) fprintf(cshout, "%s: \t aliased to ", vis_str(*v));
blkpr(cshout, vp->vec);
(void) fputc('\n', cshout);
}
else {
lex[1].word = *v;
tellmewhat(lex);
}
}
}
static void
tellmewhat(lex)
struct wordent *lex;
{
register int i;
register struct biltins *bptr;
register struct wordent *sp = lex->next;
bool aliased = 0;
Char *s0, *s1, *s2;
Char qc;
if (adrof1(sp->word, &aliases)) {
alias(lex);
sp = lex->next;
aliased = 1;
}
s0 = sp->word; /* to get the memory freeing right... */
/* handle quoted alias hack */
if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
(sp->word)++;
/* do quoting, if it hasn't been done */
s1 = s2 = sp->word;
while (*s2)
switch (*s2) {
case '\'':
case '"':
qc = *s2++;
while (*s2 && *s2 != qc)
*s1++ = *s2++ | QUOTE;
if (*s2)
s2++;
break;
case '\\':
if (*++s2)
*s1++ = *s2++ | QUOTE;
break;
default:
*s1++ = *s2++;
}
*s1 = '\0';
for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
if (eq(sp->word, str2short(bptr->bname))) {
if (aliased)
prlex(cshout, lex);
(void) fprintf(cshout, "%s: shell built-in command.\n",
vis_str(sp->word));
sp->word = s0; /* we save and then restore this */
return;
}
}
if ((i = iscommand(strip(sp->word))) != 0) {
register Char **pv;
register struct varent *v;
bool slash = any(short2str(sp->word), '/');
v = adrof(STRpath);
if (v == 0 || v->vec[0] == 0 || slash)
pv = justabs;
else
pv = v->vec;
while (--i)
pv++;
if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
sp->word = Strspl(STRdotsl, sp->word);
prlex(cshout, lex);
xfree((ptr_t) sp->word);
sp->word = s0; /* we save and then restore this */
return;
}
s1 = Strspl(*pv, STRslash);
sp->word = Strspl(s1, sp->word);
xfree((ptr_t) s1);
prlex(cshout, lex);
xfree((ptr_t) sp->word);
}
else {
if (aliased)
prlex(cshout, lex);
(void) fprintf(csherr, "%s: Command not found.\n", vis_str(sp->word));
}
sp->word = s0; /* we save and then restore this */
}

711
bin/csh/exp.c Normal file
View File

@ -0,0 +1,711 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)exp.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef SHORT_STRINGS
#include <string.h>
#endif /* SHORT_STRINGS */
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
#define IGNORE 1 /* in ignore, it means to ignore value, just parse */
#define NOGLOB 2 /* in ignore, it means not to globone */
#define ADDOP 1
#define MULOP 2
#define EQOP 4
#define RELOP 8
#define RESTOP 16
#define ANYOP 31
#define EQEQ 1
#define GTR 2
#define LSS 4
#define NOTEQ 6
#define EQMATCH 7
#define NOTEQMATCH 8
static int exp1 __P((Char ***, bool));
static int exp2 __P((Char ***, bool));
static int exp2a __P((Char ***, bool));
static int exp2b __P((Char ***, bool));
static int exp2c __P((Char ***, bool));
static Char * exp3 __P((Char ***, bool));
static Char * exp3a __P((Char ***, bool));
static Char * exp4 __P((Char ***, bool));
static Char * exp5 __P((Char ***, bool));
static Char * exp6 __P((Char ***, bool));
static void evalav __P((Char **));
static int isa __P((Char *, int));
static int egetn __P((Char *));
#ifdef EDEBUG
static void etracc __P((char *, Char *, Char ***));
static void etraci __P((char *, int, Char ***));
#endif
int
expr(vp)
register Char ***vp;
{
return (exp0(vp, 0));
}
int
exp0(vp, ignore)
register Char ***vp;
bool ignore;
{
register int p1 = exp1(vp, ignore);
#ifdef EDEBUG
etraci("exp0 p1", p1, vp);
#endif
if (**vp && eq(**vp, STRor2)) {
register int p2;
(*vp)++;
p2 = exp0(vp, (ignore & IGNORE) || p1);
#ifdef EDEBUG
etraci("exp0 p2", p2, vp);
#endif
return (p1 || p2);
}
return (p1);
}
static int
exp1(vp, ignore)
register Char ***vp;
bool ignore;
{
register int p1 = exp2(vp, ignore);
#ifdef EDEBUG
etraci("exp1 p1", p1, vp);
#endif
if (**vp && eq(**vp, STRand2)) {
register int p2;
(*vp)++;
p2 = exp1(vp, (ignore & IGNORE) || !p1);
#ifdef EDEBUG
etraci("exp1 p2", p2, vp);
#endif
return (p1 && p2);
}
return (p1);
}
static int
exp2(vp, ignore)
register Char ***vp;
bool ignore;
{
register int p1 = exp2a(vp, ignore);
#ifdef EDEBUG
etraci("exp3 p1", p1, vp);
#endif
if (**vp && eq(**vp, STRor)) {
register int p2;
(*vp)++;
p2 = exp2(vp, ignore);
#ifdef EDEBUG
etraci("exp3 p2", p2, vp);
#endif
return (p1 | p2);
}
return (p1);
}
static int
exp2a(vp, ignore)
register Char ***vp;
bool ignore;
{
register int p1 = exp2b(vp, ignore);
#ifdef EDEBUG
etraci("exp2a p1", p1, vp);
#endif
if (**vp && eq(**vp, STRcaret)) {
register int p2;
(*vp)++;
p2 = exp2a(vp, ignore);
#ifdef EDEBUG
etraci("exp2a p2", p2, vp);
#endif
return (p1 ^ p2);
}
return (p1);
}
static int
exp2b(vp, ignore)
register Char ***vp;
bool ignore;
{
register int p1 = exp2c(vp, ignore);
#ifdef EDEBUG
etraci("exp2b p1", p1, vp);
#endif
if (**vp && eq(**vp, STRand)) {
register int p2;
(*vp)++;
p2 = exp2b(vp, ignore);
#ifdef EDEBUG
etraci("exp2b p2", p2, vp);
#endif
return (p1 & p2);
}
return (p1);
}
static int
exp2c(vp, ignore)
register Char ***vp;
bool ignore;
{
register Char *p1 = exp3(vp, ignore);
register Char *p2;
register int i;
#ifdef EDEBUG
etracc("exp2c p1", p1, vp);
#endif
if ((i = isa(**vp, EQOP)) != 0) {
(*vp)++;
if (i == EQMATCH || i == NOTEQMATCH)
ignore |= NOGLOB;
p2 = exp3(vp, ignore);
#ifdef EDEBUG
etracc("exp2c p2", p2, vp);
#endif
if (!(ignore & IGNORE))
switch (i) {
case EQEQ:
i = eq(p1, p2);
break;
case NOTEQ:
i = !eq(p1, p2);
break;
case EQMATCH:
i = Gmatch(p1, p2);
break;
case NOTEQMATCH:
i = !Gmatch(p1, p2);
break;
}
xfree((ptr_t) p1);
xfree((ptr_t) p2);
return (i);
}
i = egetn(p1);
xfree((ptr_t) p1);
return (i);
}
static Char *
exp3(vp, ignore)
register Char ***vp;
bool ignore;
{
register Char *p1, *p2;
register int i;
p1 = exp3a(vp, ignore);
#ifdef EDEBUG
etracc("exp3 p1", p1, vp);
#endif
if ((i = isa(**vp, RELOP)) != 0) {
(*vp)++;
if (**vp && eq(**vp, STRequal))
i |= 1, (*vp)++;
p2 = exp3(vp, ignore);
#ifdef EDEBUG
etracc("exp3 p2", p2, vp);
#endif
if (!(ignore & IGNORE))
switch (i) {
case GTR:
i = egetn(p1) > egetn(p2);
break;
case GTR | 1:
i = egetn(p1) >= egetn(p2);
break;
case LSS:
i = egetn(p1) < egetn(p2);
break;
case LSS | 1:
i = egetn(p1) <= egetn(p2);
break;
}
xfree((ptr_t) p1);
xfree((ptr_t) p2);
return (putn(i));
}
return (p1);
}
static Char *
exp3a(vp, ignore)
register Char ***vp;
bool ignore;
{
register Char *p1, *p2, *op;
register int i;
p1 = exp4(vp, ignore);
#ifdef EDEBUG
etracc("exp3a p1", p1, vp);
#endif
op = **vp;
if (op && any("<>", op[0]) && op[0] == op[1]) {
(*vp)++;
p2 = exp3a(vp, ignore);
#ifdef EDEBUG
etracc("exp3a p2", p2, vp);
#endif
if (op[0] == '<')
i = egetn(p1) << egetn(p2);
else
i = egetn(p1) >> egetn(p2);
xfree((ptr_t) p1);
xfree((ptr_t) p2);
return (putn(i));
}
return (p1);
}
static Char *
exp4(vp, ignore)
register Char ***vp;
bool ignore;
{
register Char *p1, *p2;
register int i = 0;
p1 = exp5(vp, ignore);
#ifdef EDEBUG
etracc("exp4 p1", p1, vp);
#endif
if (isa(**vp, ADDOP)) {
register Char *op = *(*vp)++;
p2 = exp4(vp, ignore);
#ifdef EDEBUG
etracc("exp4 p2", p2, vp);
#endif
if (!(ignore & IGNORE))
switch (op[0]) {
case '+':
i = egetn(p1) + egetn(p2);
break;
case '-':
i = egetn(p1) - egetn(p2);
break;
}
xfree((ptr_t) p1);
xfree((ptr_t) p2);
return (putn(i));
}
return (p1);
}
static Char *
exp5(vp, ignore)
register Char ***vp;
bool ignore;
{
register Char *p1, *p2;
register int i = 0;
p1 = exp6(vp, ignore);
#ifdef EDEBUG
etracc("exp5 p1", p1, vp);
#endif
if (isa(**vp, MULOP)) {
register Char *op = *(*vp)++;
p2 = exp5(vp, ignore);
#ifdef EDEBUG
etracc("exp5 p2", p2, vp);
#endif
if (!(ignore & IGNORE))
switch (op[0]) {
case '*':
i = egetn(p1) * egetn(p2);
break;
case '/':
i = egetn(p2);
if (i == 0)
stderror(ERR_DIV0);
i = egetn(p1) / i;
break;
case '%':
i = egetn(p2);
if (i == 0)
stderror(ERR_MOD0);
i = egetn(p1) % i;
break;
}
xfree((ptr_t) p1);
xfree((ptr_t) p2);
return (putn(i));
}
return (p1);
}
static Char *
exp6(vp, ignore)
register Char ***vp;
bool ignore;
{
int ccode, i = 0;
register Char *cp, *dp, *ep;
if (**vp == 0)
stderror(ERR_NAME | ERR_EXPRESSION);
if (eq(**vp, STRbang)) {
(*vp)++;
cp = exp6(vp, ignore);
#ifdef EDEBUG
etracc("exp6 ! cp", cp, vp);
#endif
i = egetn(cp);
xfree((ptr_t) cp);
return (putn(!i));
}
if (eq(**vp, STRtilde)) {
(*vp)++;
cp = exp6(vp, ignore);
#ifdef EDEBUG
etracc("exp6 ~ cp", cp, vp);
#endif
i = egetn(cp);
xfree((ptr_t) cp);
return (putn(~i));
}
if (eq(**vp, STRLparen)) {
(*vp)++;
ccode = exp0(vp, ignore);
#ifdef EDEBUG
etraci("exp6 () ccode", ccode, vp);
#endif
if (*vp == 0 || **vp == 0 || ***vp != ')')
stderror(ERR_NAME | ERR_EXPRESSION);
(*vp)++;
return (putn(ccode));
}
if (eq(**vp, STRLbrace)) {
register Char **v;
struct command faket;
Char *fakecom[2];
faket.t_dtyp = NODE_COMMAND;
faket.t_dflg = 0;
faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
faket.t_dcom = fakecom;
fakecom[0] = STRfakecom;
fakecom[1] = NULL;
(*vp)++;
v = *vp;
for (;;) {
if (!**vp)
stderror(ERR_NAME | ERR_MISSING, '}');
if (eq(*(*vp)++, STRRbrace))
break;
}
if (ignore & IGNORE)
return (Strsave(STRNULL));
psavejob();
if (pfork(&faket, -1) == 0) {
*--(*vp) = 0;
evalav(v);
exitstat();
}
pwait();
prestjob();
#ifdef EDEBUG
etraci("exp6 {} status", egetn(value(STRstatus)), vp);
#endif
return (putn(egetn(value(STRstatus)) == 0));
}
if (isa(**vp, ANYOP))
return (Strsave(STRNULL));
cp = *(*vp)++;
if (*cp == '-' && any("erwxfdzopls", cp[1])) {
struct stat stb;
if (cp[2] != '\0')
stderror(ERR_NAME | ERR_FILEINQ);
/*
* Detect missing file names by checking for operator in the file name
* position. However, if an operator name appears there, we must make
* sure that there's no file by that name (e.g., "/") before announcing
* an error. Even this check isn't quite right, since it doesn't take
* globbing into account.
*/
if (isa(**vp, ANYOP) && stat(short2str(**vp), &stb))
stderror(ERR_NAME | ERR_FILENAME);
dp = *(*vp)++;
if (ignore & IGNORE)
return (Strsave(STRNULL));
ep = globone(dp, G_ERROR);
switch (cp[1]) {
case 'r':
i = !access(short2str(ep), R_OK);
break;
case 'w':
i = !access(short2str(ep), W_OK);
break;
case 'x':
i = !access(short2str(ep), X_OK);
break;
default:
if (
#ifdef S_IFLNK
cp[1] == 'l' ? lstat(short2str(ep), &stb) :
#endif
stat(short2str(ep), &stb)) {
xfree((ptr_t) ep);
return (Strsave(STR0));
}
switch (cp[1]) {
case 'f':
i = S_ISREG(stb.st_mode);
break;
case 'd':
i = S_ISDIR(stb.st_mode);
break;
case 'p':
#ifdef S_ISFIFO
i = S_ISFIFO(stb.st_mode);
#else
i = 0;
#endif
break;
case 'l':
#ifdef S_ISLNK
i = S_ISLNK(stb.st_mode);
#else
i = 0;
#endif
break;
case 's':
#ifdef S_ISSOCK
i = S_ISSOCK(stb.st_mode);
#else
i = 0;
#endif
break;
case 'z':
i = stb.st_size == 0;
break;
case 'e':
i = 1;
break;
case 'o':
i = stb.st_uid == uid;
break;
}
}
#ifdef EDEBUG
etraci("exp6 -? i", i, vp);
#endif
xfree((ptr_t) ep);
return (putn(i));
}
#ifdef EDEBUG
etracc("exp6 default", cp, vp);
#endif
return (ignore & NOGLOB ? Strsave(cp) : globone(cp, G_ERROR));
}
static void
evalav(v)
register Char **v;
{
struct wordent paraml1;
register struct wordent *hp = &paraml1;
struct command *t;
register struct wordent *wdp = hp;
set(STRstatus, Strsave(STR0));
hp->prev = hp->next = hp;
hp->word = STRNULL;
while (*v) {
register struct wordent *new =
(struct wordent *) xcalloc(1, sizeof *wdp);
new->prev = wdp;
new->next = hp;
wdp->next = new;
wdp = new;
wdp->word = Strsave(*v++);
}
hp->prev = wdp;
alias(&paraml1);
t = syntax(paraml1.next, &paraml1, 0);
if (seterr)
stderror(ERR_OLD);
execute(t, -1, NULL, NULL);
freelex(&paraml1), freesyn(t);
}
static int
isa(cp, what)
register Char *cp;
register int what;
{
if (cp == 0)
return ((what & RESTOP) != 0);
if (cp[1] == 0) {
if (what & ADDOP && (*cp == '+' || *cp == '-'))
return (1);
if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
return (1);
if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
*cp == '~' || *cp == '^' || *cp == '"'))
return (1);
}
else if (cp[2] == 0) {
if (what & RESTOP) {
if (cp[0] == '|' && cp[1] == '&')
return (1);
if (cp[0] == '<' && cp[1] == '<')
return (1);
if (cp[0] == '>' && cp[1] == '>')
return (1);
}
if (what & EQOP) {
if (cp[0] == '=') {
if (cp[1] == '=')
return (EQEQ);
if (cp[1] == '~')
return (EQMATCH);
}
else if (cp[0] == '!') {
if (cp[1] == '=')
return (NOTEQ);
if (cp[1] == '~')
return (NOTEQMATCH);
}
}
}
if (what & RELOP) {
if (*cp == '<')
return (LSS);
if (*cp == '>')
return (GTR);
}
return (0);
}
static int
egetn(cp)
register Char *cp;
{
if (*cp && *cp != '-' && !Isdigit(*cp))
stderror(ERR_NAME | ERR_EXPRESSION);
return (getn(cp));
}
/* Phew! */
#ifdef EDEBUG
static void
etraci(str, i, vp)
char *str;
int i;
Char ***vp;
{
(void) fprintf(csherr, "%s=%d\t", str, i);
blkpr(csherr, *vp);
(void) fprintf(csherr, "\n");
}
static void
etracc(str, cp, vp)
char *str;
Char *cp;
Char ***vp;
{
(void) fprintf(csherr, "%s=%s\t", str, vis_str(cp));
blkpr(csherr, *vp);
(void) fprintf(csherr, "\n");
}
#endif

338
bin/csh/extern.h Normal file
View File

@ -0,0 +1,338 @@
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)extern.h 8.1 (Berkeley) 5/31/93
*/
#include <sys/cdefs.h>
/*
* csh.c
*/
int gethdir __P((Char *));
void dosource __P((Char **, struct command *));
void exitstat __P((void));
void goodbye __P((void));
void importpath __P((Char *));
void initdesc __P((void));
void pintr __P((int));
void pintr1 __P((bool));
void printprompt __P((void));
void process __P((bool));
void rechist __P((void));
void untty __P((void));
int vis_fputc __P((int, FILE *));
#ifdef PROF
void done __P((int));
#else
void xexit __P((int));
#endif
/*
* dir.c
*/
void dinit __P((Char *));
void dodirs __P((Char **, struct command *));
Char *dcanon __P((Char *, Char *));
void dtildepr __P((Char *, Char *));
void dtilde __P((void));
void dochngd __P((Char **, struct command *));
Char *dnormalize __P((Char *));
void dopushd __P((Char **, struct command *));
void dopopd __P((Char **, struct command *));
struct directory;
void dfree __P((struct directory *));
/*
* dol.c
*/
void Dfix __P((struct command *));
Char *Dfix1 __P((Char *));
void heredoc __P((Char *));
/*
* err.c
*/
void seterror __P((int, ...));
void stderror __P((int, ...));
/*
* exec.c
*/
void doexec __P((Char **, struct command *));
void dohash __P((Char **, struct command *));
void dounhash __P((Char **, struct command *));
void dowhich __P((Char **, struct command *));
void execash __P((Char **, struct command *));
void hashstat __P((Char **, struct command *));
void xechoit __P((Char **));
/*
* exp.c
*/
int expr __P((Char ***));
int exp0 __P((Char ***, bool));
/*
* file.c
*/
#ifdef FILEC
int tenex __P((Char *, int));
#endif
/*
* func.c
*/
void Setenv __P((Char *, Char *));
void doalias __P((Char **, struct command *));
void dobreak __P((Char **, struct command *));
void docontin __P((Char **, struct command *));
void doecho __P((Char **, struct command *));
void doelse __P((Char **, struct command *));
void doend __P((Char **, struct command *));
void doeval __P((Char **, struct command *));
void doexit __P((Char **, struct command *));
void doforeach __P((Char **, struct command *));
void doglob __P((Char **, struct command *));
void dogoto __P((Char **, struct command *));
void doif __P((Char **, struct command *));
void dolimit __P((Char **, struct command *));
void dologin __P((Char **, struct command *));
void dologout __P((Char **, struct command *));
void donohup __P((Char **, struct command *));
void doonintr __P((Char **, struct command *));
void doprintf __P((Char **, struct command *));
void dorepeat __P((Char **, struct command *));
void dosetenv __P((Char **, struct command *));
void dosuspend __P((Char **, struct command *));
void doswbrk __P((Char **, struct command *));
void doswitch __P((Char **, struct command *));
void doumask __P((Char **, struct command *));
void dounlimit __P((Char **, struct command *));
void dounsetenv __P((Char **, struct command *));
void dowhile __P((Char **, struct command *));
void dozip __P((Char **, struct command *));
void func __P((struct command *, struct biltins *));
struct biltins *
isbfunc __P((struct command *));
void prvars __P((void));
void gotolab __P((Char *));
int srchx __P((Char *));
void unalias __P((Char **, struct command *));
void wfree __P((void));
/*
* glob.c
*/
Char **dobackp __P((Char *, bool));
void Gcat __P((Char *, Char *));
Char *globone __P((Char *, int));
int Gmatch __P((Char *, Char *));
void ginit __P((void));
Char **globall __P((Char **));
void rscan __P((Char **, void (*)()));
void tglob __P((Char **));
void trim __P((Char **));
#ifdef FILEC
int sortscmp __P((const ptr_t, const ptr_t));
#endif /* FILEC */
/*
* hist.c
*/
void dohist __P((Char **, struct command *));
struct Hist *
enthist __P((int, struct wordent *, bool));
void savehist __P((struct wordent *));
/*
* lex.c
*/
void addla __P((Char *));
void bseek __P((struct Ain *));
void btell __P((struct Ain *));
void btoeof __P((void));
void copylex __P((struct wordent *, struct wordent *));
Char *domod __P((Char *, int));
void freelex __P((struct wordent *));
int lex __P((struct wordent *));
void prlex __P((FILE *, struct wordent *));
int readc __P((bool));
void settell __P((void));
void unreadc __P((int));
/*
* misc.c
*/
int any __P((char *, int));
Char **blkcat __P((Char **, Char **));
Char **blkcpy __P((Char **, Char **));
Char **blkend __P((Char **));
void blkfree __P((Char **));
int blklen __P((Char **));
void blkpr __P((FILE *, Char **));
Char **blkspl __P((Char **, Char **));
void closem __P((void));
Char **copyblk __P((Char **));
int dcopy __P((int, int));
int dmove __P((int, int));
void donefds __P((void));
Char lastchr __P((Char *));
void lshift __P((Char **, int));
int number __P((Char *));
int prefix __P((Char *, Char *));
Char **saveblk __P((Char **));
void setzero __P((char *, int));
Char *strip __P((Char *));
char *strsave __P((char *));
char *strspl __P((char *, char *));
void udvar __P((Char *));
#ifndef SHORT_STRINGS
# ifdef NOTUSED
char *strstr __P((const char *, const char *));
# endif /* NOTUSED */
char *strend __P((char *));
#endif
/*
* parse.c
*/
void alias __P((struct wordent *));
void freesyn __P((struct command *));
struct command *
syntax __P((struct wordent *, struct wordent *, int));
/*
* proc.c
*/
void dobg __P((Char **, struct command *));
void dobg1 __P((Char **, struct command *));
void dofg __P((Char **, struct command *));
void dofg1 __P((Char **, struct command *));
void dojobs __P((Char **, struct command *));
void dokill __P((Char **, struct command *));
void donotify __P((Char **, struct command *));
void dostop __P((Char **, struct command *));
void dowait __P((Char **, struct command *));
void palloc __P((int, struct command *));
void panystop __P((bool));
void pchild __P((int));
void pendjob __P((void));
struct process *
pfind __P((Char *));
int pfork __P((struct command *, int));
void pgetty __P((int, int));
void pjwait __P((struct process *));
void pnote __P((void));
void prestjob __P((void));
void psavejob __P((void));
void pstart __P((struct process *, int));
void pwait __P((void));
/*
* sem.c
*/
void execute __P((struct command *, int, int *, int *));
void mypipe __P((int *));
/*
* set.c
*/
struct varent
*adrof1 __P((Char *, struct varent *));
void doset __P((Char **, struct command *));
void dolet __P((Char **, struct command *));
Char *putn __P((int));
int getn __P((Char *));
Char *value1 __P((Char *, struct varent *));
void set __P((Char *, Char *));
void set1 __P((Char *, Char **, struct varent *));
void setq __P((Char *, Char **, struct varent *));
void unset __P((Char **, struct command *));
void unset1 __P((Char *[], struct varent *));
void unsetv __P((Char *));
void setNS __P((Char *));
void shift __P((Char **, struct command *));
void plist __P((struct varent *));
/*
* time.c
*/
void donice __P((Char **, struct command *));
void dotime __P((Char **, struct command *));
void prusage __P((struct rusage *, struct rusage *,
struct timeval *, struct timeval *));
void ruadd __P((struct rusage *, struct rusage *));
void settimes __P((void));
void tvadd __P((struct timeval *, struct timeval *));
void tvsub __P((struct timeval *, struct timeval *, struct timeval *));
void pcsecs __P((long));
void psecs __P((long));
/*
* alloc.c
*/
void Free __P((ptr_t));
ptr_t Malloc __P((size_t));
ptr_t Realloc __P((ptr_t, size_t));
ptr_t Calloc __P((size_t, size_t));
void showall __P((Char **, struct command *));
/*
* str.c:
*/
#ifdef SHORT_STRINGS
Char *s_strchr __P((Char *, int));
Char *s_strrchr __P((Char *, int));
Char *s_strcat __P((Char *, Char *));
#ifdef NOTUSED
Char *s_strncat __P((Char *, Char *, size_t));
#endif
Char *s_strcpy __P((Char *, Char *));
Char *s_strncpy __P((Char *, Char *, size_t));
Char *s_strspl __P((Char *, Char *));
size_t s_strlen __P((Char *));
int s_strcmp __P((Char *, Char *));
int s_strncmp __P((Char *, Char *, size_t));
Char *s_strsave __P((Char *));
Char *s_strend __P((Char *));
Char *s_strstr __P((Char *, Char *));
Char *str2short __P((char *));
Char **blk2short __P((char **));
char *short2str __P((Char *));
char **short2blk __P((Char **));
#endif
char *short2qstr __P((Char *));
char *vis_str __P((Char *));

684
bin/csh/file.c Normal file
View File

@ -0,0 +1,684 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)file.c 8.2 (Berkeley) 3/19/94";
#endif /* not lint */
#ifdef FILEC
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <termios.h>
#include <dirent.h>
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef SHORT_STRINGS
#include <string.h>
#endif /* SHORT_STRINGS */
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
/*
* Tenex style file name recognition, .. and more.
* History:
* Author: Ken Greer, Sept. 1975, CMU.
* Finally got around to adding to the Cshell., Ken Greer, Dec. 1981.
*/
#define ON 1
#define OFF 0
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define ESC '\033'
typedef enum {
LIST, RECOGNIZE
} COMMAND;
static void setup_tty __P((int));
static void back_to_col_1 __P((void));
static void pushback __P((Char *));
static void catn __P((Char *, Char *, int));
static void copyn __P((Char *, Char *, int));
static Char filetype __P((Char *, Char *));
static void print_by_column __P((Char *, Char *[], int));
static Char *tilde __P((Char *, Char *));
static void retype __P((void));
static void beep __P((void));
static void print_recognized_stuff __P((Char *));
static void extract_dir_and_name __P((Char *, Char *, Char *));
static Char *getentry __P((DIR *, int));
static void free_items __P((Char **));
static int tsearch __P((Char *, COMMAND, int));
static int recognize __P((Char *, Char *, int, int));
static int is_prefix __P((Char *, Char *));
static int is_suffix __P((Char *, Char *));
static int ignored __P((Char *));
/*
* Put this here so the binary can be patched with adb to enable file
* completion by default. Filec controls completion, nobeep controls
* ringing the terminal bell on incomplete expansions.
*/
bool filec = 0;
static void
setup_tty(on)
int on;
{
static struct termios tchars;
(void) tcgetattr(SHIN, &tchars);
if (on) {
tchars.c_cc[VEOL] = ESC;
if (tchars.c_lflag & ICANON)
on = TCSANOW;
else {
on = TCSAFLUSH;
tchars.c_lflag |= ICANON;
}
}
else {
tchars.c_cc[VEOL] = _POSIX_VDISABLE;
on = TCSANOW;
}
(void) tcsetattr(SHIN, TCSANOW, &tchars);
}
/*
* Move back to beginning of current line
*/
static void
back_to_col_1()
{
struct termios tty, tty_normal;
int omask;
omask = sigblock(sigmask(SIGINT));
(void) tcgetattr(SHOUT, &tty);
tty_normal = tty;
tty.c_iflag &= ~INLCR;
tty.c_oflag &= ~ONLCR;
(void) tcsetattr(SHOUT, TCSANOW, &tty);
(void) write(SHOUT, "\r", 1);
(void) tcsetattr(SHOUT, TCSANOW, &tty_normal);
(void) sigsetmask(omask);
}
/*
* Push string contents back into tty queue
*/
static void
pushback(string)
Char *string;
{
register Char *p;
struct termios tty, tty_normal;
int omask;
char c;
omask = sigblock(sigmask(SIGINT));
(void) tcgetattr(SHOUT, &tty);
tty_normal = tty;
tty.c_lflag &= ~(ECHOKE | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOCTL);
(void) tcsetattr(SHOUT, TCSANOW, &tty);
for (p = string; (c = *p) != '\0'; p++)
(void) ioctl(SHOUT, TIOCSTI, (ioctl_t) & c);
(void) tcsetattr(SHOUT, TCSANOW, &tty_normal);
(void) sigsetmask(omask);
}
/*
* Concatenate src onto tail of des.
* Des is a string whose maximum length is count.
* Always null terminate.
*/
static void
catn(des, src, count)
register Char *des, *src;
register int count;
{
while (--count >= 0 && *des)
des++;
while (--count >= 0)
if ((*des++ = *src++) == 0)
return;
*des = '\0';
}
/*
* Like strncpy but always leave room for trailing \0
* and always null terminate.
*/
static void
copyn(des, src, count)
register Char *des, *src;
register int count;
{
while (--count >= 0)
if ((*des++ = *src++) == 0)
return;
*des = '\0';
}
static Char
filetype(dir, file)
Char *dir, *file;
{
Char path[MAXPATHLEN];
struct stat statb;
catn(Strcpy(path, dir), file, sizeof(path) / sizeof(Char));
if (lstat(short2str(path), &statb) == 0) {
switch (statb.st_mode & S_IFMT) {
case S_IFDIR:
return ('/');
case S_IFLNK:
if (stat(short2str(path), &statb) == 0 && /* follow it out */
S_ISDIR(statb.st_mode))
return ('>');
else
return ('@');
case S_IFSOCK:
return ('=');
default:
if (statb.st_mode & 0111)
return ('*');
}
}
return (' ');
}
static struct winsize win;
/*
* Print sorted down columns
*/
static void
print_by_column(dir, items, count)
Char *dir, *items[];
int count;
{
register int i, rows, r, c, maxwidth = 0, columns;
if (ioctl(SHOUT, TIOCGWINSZ, (ioctl_t) & win) < 0 || win.ws_col == 0)
win.ws_col = 80;
for (i = 0; i < count; i++)
maxwidth = maxwidth > (r = Strlen(items[i])) ? maxwidth : r;
maxwidth += 2; /* for the file tag and space */
columns = win.ws_col / maxwidth;
if (columns == 0)
columns = 1;
rows = (count + (columns - 1)) / columns;
for (r = 0; r < rows; r++) {
for (c = 0; c < columns; c++) {
i = c * rows + r;
if (i < count) {
register int w;
(void) fprintf(cshout, "%s", vis_str(items[i]));
(void) fputc(dir ? filetype(dir, items[i]) : ' ', cshout);
if (c < columns - 1) { /* last column? */
w = Strlen(items[i]) + 1;
for (; w < maxwidth; w++)
(void) fputc(' ', cshout);
}
}
}
(void) fputc('\r', cshout);
(void) fputc('\n', cshout);
}
}
/*
* Expand file name with possible tilde usage
* ~person/mumble
* expands to
* home_directory_of_person/mumble
*/
static Char *
tilde(new, old)
Char *new, *old;
{
register Char *o, *p;
register struct passwd *pw;
static Char person[40];
if (old[0] != '~')
return (Strcpy(new, old));
for (p = person, o = &old[1]; *o && *o != '/'; *p++ = *o++)
continue;
*p = '\0';
if (person[0] == '\0')
(void) Strcpy(new, value(STRhome));
else {
pw = getpwnam(short2str(person));
if (pw == NULL)
return (NULL);
(void) Strcpy(new, str2short(pw->pw_dir));
}
(void) Strcat(new, o);
return (new);
}
/*
* Cause pending line to be printed
*/
static void
retype()
{
struct termios tty;
(void) tcgetattr(SHOUT, &tty);
tty.c_lflag |= PENDIN;
(void) tcsetattr(SHOUT, TCSANOW, &tty);
}
static void
beep()
{
if (adrof(STRnobeep) == 0)
(void) write(SHOUT, "\007", 1);
}
/*
* Erase that silly ^[ and
* print the recognized part of the string
*/
static void
print_recognized_stuff(recognized_part)
Char *recognized_part;
{
/* An optimized erasing of that silly ^[ */
(void) fputc('\b', cshout);
(void) fputc('\b', cshout);
switch (Strlen(recognized_part)) {
case 0: /* erase two Characters: ^[ */
(void) fputc(' ', cshout);
(void) fputc(' ', cshout);
(void) fputc('\b', cshout);
(void) fputc('\b', cshout);
break;
case 1: /* overstrike the ^, erase the [ */
(void) fprintf(cshout, "%s", vis_str(recognized_part));
(void) fputc(' ', cshout);
(void) fputc('\b', cshout);
break;
default: /* overstrike both Characters ^[ */
(void) fprintf(cshout, "%s", vis_str(recognized_part));
break;
}
(void) fflush(cshout);
}
/*
* Parse full path in file into 2 parts: directory and file names
* Should leave final slash (/) at end of dir.
*/
static void
extract_dir_and_name(path, dir, name)
Char *path, *dir, *name;
{
register Char *p;
p = Strrchr(path, '/');
if (p == NULL) {
copyn(name, path, MAXNAMLEN);
dir[0] = '\0';
}
else {
copyn(name, ++p, MAXNAMLEN);
copyn(dir, path, p - path);
}
}
static Char *
getentry(dir_fd, looking_for_lognames)
DIR *dir_fd;
int looking_for_lognames;
{
register struct passwd *pw;
register struct dirent *dirp;
if (looking_for_lognames) {
if ((pw = getpwent()) == NULL)
return (NULL);
return (str2short(pw->pw_name));
}
if ((dirp = readdir(dir_fd)) != NULL)
return (str2short(dirp->d_name));
return (NULL);
}
static void
free_items(items)
register Char **items;
{
register int i;
for (i = 0; items[i]; i++)
xfree((ptr_t) items[i]);
xfree((ptr_t) items);
}
#define FREE_ITEMS(items) { \
int omask;\
\
omask = sigblock(sigmask(SIGINT));\
free_items(items);\
items = NULL;\
(void) sigsetmask(omask);\
}
/*
* Perform a RECOGNIZE or LIST command on string "word".
*/
static int
tsearch(word, command, max_word_length)
Char *word;
COMMAND command;
int max_word_length;
{
static Char **items = NULL;
register DIR *dir_fd;
register numitems = 0, ignoring = TRUE, nignored = 0;
register name_length, looking_for_lognames;
Char tilded_dir[MAXPATHLEN + 1], dir[MAXPATHLEN + 1];
Char name[MAXNAMLEN + 1], extended_name[MAXNAMLEN + 1];
Char *entry;
#define MAXITEMS 1024
if (items != NULL)
FREE_ITEMS(items);
looking_for_lognames = (*word == '~') && (Strchr(word, '/') == NULL);
if (looking_for_lognames) {
(void) setpwent();
copyn(name, &word[1], MAXNAMLEN); /* name sans ~ */
dir_fd = NULL;
}
else {
extract_dir_and_name(word, dir, name);
if (tilde(tilded_dir, dir) == 0)
return (0);
dir_fd = opendir(*tilded_dir ? short2str(tilded_dir) : ".");
if (dir_fd == NULL)
return (0);
}
again: /* search for matches */
name_length = Strlen(name);
for (numitems = 0; (entry = getentry(dir_fd, looking_for_lognames)) != NULL;) {
if (!is_prefix(name, entry))
continue;
/* Don't match . files on null prefix match */
if (name_length == 0 && entry[0] == '.' &&
!looking_for_lognames)
continue;
if (command == LIST) {
if (numitems >= MAXITEMS) {
(void) fprintf(csherr, "\nYikes!! Too many %s!!\n",
looking_for_lognames ?
"names in password file" : "files");
break;
}
if (items == NULL)
items = (Char **) xcalloc(sizeof(items[0]), MAXITEMS);
items[numitems] = (Char *) xmalloc((size_t) (Strlen(entry) + 1) *
sizeof(Char));
copyn(items[numitems], entry, MAXNAMLEN);
numitems++;
}
else { /* RECOGNIZE command */
if (ignoring && ignored(entry))
nignored++;
else if (recognize(extended_name,
entry, name_length, ++numitems))
break;
}
}
if (ignoring && numitems == 0 && nignored > 0) {
ignoring = FALSE;
nignored = 0;
if (looking_for_lognames)
(void) setpwent();
else
rewinddir(dir_fd);
goto again;
}
if (looking_for_lognames)
(void) endpwent();
else
(void) closedir(dir_fd);
if (numitems == 0)
return (0);
if (command == RECOGNIZE) {
if (looking_for_lognames)
copyn(word, STRtilde, 1);
else
/* put back dir part */
copyn(word, dir, max_word_length);
/* add extended name */
catn(word, extended_name, max_word_length);
return (numitems);
}
else { /* LIST */
qsort((ptr_t) items, numitems, sizeof(items[0]),
(int (*) __P((const void *, const void *))) sortscmp);
print_by_column(looking_for_lognames ? NULL : tilded_dir,
items, numitems);
if (items != NULL)
FREE_ITEMS(items);
}
return (0);
}
/*
* Object: extend what user typed up to an ambiguity.
* Algorithm:
* On first match, copy full entry (assume it'll be the only match)
* On subsequent matches, shorten extended_name to the first
* Character mismatch between extended_name and entry.
* If we shorten it back to the prefix length, stop searching.
*/
static int
recognize(extended_name, entry, name_length, numitems)
Char *extended_name, *entry;
int name_length, numitems;
{
if (numitems == 1) /* 1st match */
copyn(extended_name, entry, MAXNAMLEN);
else { /* 2nd & subsequent matches */
register Char *x, *ent;
register int len = 0;
x = extended_name;
for (ent = entry; *x && *x == *ent++; x++, len++)
continue;
*x = '\0'; /* Shorten at 1st Char diff */
if (len == name_length) /* Ambiguous to prefix? */
return (-1); /* So stop now and save time */
}
return (0);
}
/*
* Return true if check matches initial Chars in template.
* This differs from PWB imatch in that if check is null
* it matches anything.
*/
static int
is_prefix(check, template)
register Char *check, *template;
{
do
if (*check == 0)
return (TRUE);
while (*check++ == *template++);
return (FALSE);
}
/*
* Return true if the Chars in template appear at the
* end of check, I.e., are it's suffix.
*/
static int
is_suffix(check, template)
Char *check, *template;
{
register Char *c, *t;
for (c = check; *c++;)
continue;
for (t = template; *t++;)
continue;
for (;;) {
if (t == template)
return 1;
if (c == check || *--t != *--c)
return 0;
}
}
int
tenex(inputline, inputline_size)
Char *inputline;
int inputline_size;
{
register int numitems, num_read;
char tinputline[BUFSIZ];
setup_tty(ON);
while ((num_read = read(SHIN, tinputline, BUFSIZ)) > 0) {
int i;
static Char delims[] = {' ', '\'', '"', '\t', ';', '&', '<',
'>', '(', ')', '|', '^', '%', '\0'};
register Char *str_end, *word_start, last_Char, should_retype;
register int space_left;
COMMAND command;
for (i = 0; i < num_read; i++)
inputline[i] = (unsigned char) tinputline[i];
last_Char = inputline[num_read - 1] & ASCII;
if (last_Char == '\n' || num_read == inputline_size)
break;
command = (last_Char == ESC) ? RECOGNIZE : LIST;
if (command == LIST)
(void) fputc('\n', cshout);
str_end = &inputline[num_read];
if (last_Char == ESC)
--str_end; /* wipeout trailing cmd Char */
*str_end = '\0';
/*
* Find LAST occurence of a delimiter in the inputline. The word start
* is one Character past it.
*/
for (word_start = str_end; word_start > inputline; --word_start)
if (Strchr(delims, word_start[-1]))
break;
space_left = inputline_size - (word_start - inputline) - 1;
numitems = tsearch(word_start, command, space_left);
if (command == RECOGNIZE) {
/* print from str_end on */
print_recognized_stuff(str_end);
if (numitems != 1) /* Beep = No match/ambiguous */
beep();
}
/*
* Tabs in the input line cause trouble after a pushback. tty driver
* won't backspace over them because column positions are now
* incorrect. This is solved by retyping over current line.
*/
should_retype = FALSE;
if (Strchr(inputline, '\t')) { /* tab Char in input line? */
back_to_col_1();
should_retype = TRUE;
}
if (command == LIST) /* Always retype after a LIST */
should_retype = TRUE;
if (should_retype)
printprompt();
pushback(inputline);
if (should_retype)
retype();
}
setup_tty(OFF);
return (num_read);
}
static int
ignored(entry)
register Char *entry;
{
struct varent *vp;
register Char **cp;
if ((vp = adrof(STRfignore)) == NULL || (cp = vp->vec) == NULL)
return (FALSE);
for (; *cp != NULL; cp++)
if (is_suffix(entry, *cp))
return (TRUE);
return (FALSE);
}
#endif /* FILEC */

1498
bin/csh/func.c Normal file

File diff suppressed because it is too large Load Diff

939
bin/csh/glob.c Normal file
View File

@ -0,0 +1,939 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)glob.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/param.h>
#include <glob.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
static int noglob;
static int pargsiz, gargsiz;
/*
* Values for gflag
*/
#define G_NONE 0 /* No globbing needed */
#define G_GLOB 1 /* string contains *?[] characters */
#define G_CSH 2 /* string contains ~`{ characters */
#define GLOBSPACE 100 /* Alloc increment */
#define LBRC '{'
#define RBRC '}'
#define LBRK '['
#define RBRK ']'
#define EOS '\0'
Char **gargv = NULL;
long gargc = 0;
Char **pargv = NULL;
long pargc = 0;
/*
* globbing is now done in two stages. In the first pass we expand
* csh globbing idioms ~`{ and then we proceed doing the normal
* globbing if needed ?*[
*
* Csh type globbing is handled in globexpand() and the rest is
* handled in glob() which is part of the 4.4BSD libc.
*
*/
static Char *globtilde __P((Char **, Char *));
static Char **libglob __P((Char **));
static Char **globexpand __P((Char **));
static int globbrace __P((Char *, Char *, Char ***));
static void expbrace __P((Char ***, Char ***, int));
static int pmatch __P((Char *, Char *));
static void pword __P((void));
static void psave __P((int));
static void backeval __P((Char *, bool));
static Char *
globtilde(nv, s)
Char **nv, *s;
{
Char gbuf[MAXPATHLEN], *gstart, *b, *u, *e;
gstart = gbuf;
*gstart++ = *s++;
u = s;
for (b = gstart, e = &gbuf[MAXPATHLEN - 1];
*s && *s != '/' && *s != ':' && b < e;
*b++ = *s++)
continue;
*b = EOS;
if (gethdir(gstart)) {
blkfree(nv);
if (*gstart)
stderror(ERR_UNKUSER, vis_str(gstart));
else
stderror(ERR_NOHOME);
}
b = &gstart[Strlen(gstart)];
while (*s)
*b++ = *s++;
*b = EOS;
--u;
xfree((ptr_t) u);
return (Strsave(gstart));
}
static int
globbrace(s, p, bl)
Char *s, *p, ***bl;
{
int i, len;
Char *pm, *pe, *lm, *pl;
Char **nv, **vl;
Char gbuf[MAXPATHLEN];
int size = GLOBSPACE;
nv = vl = (Char **) xmalloc((size_t) sizeof(Char *) * size);
*vl = NULL;
len = 0;
/* copy part up to the brace */
for (lm = gbuf, p = s; *p != LBRC; *lm++ = *p++)
continue;
/* check for balanced braces */
for (i = 0, pe = ++p; *pe; pe++)
if (*pe == LBRK) {
/* Ignore everything between [] */
for (++pe; *pe != RBRK && *pe != EOS; pe++)
continue;
if (*pe == EOS) {
blkfree(nv);
return (-RBRK);
}
}
else if (*pe == LBRC)
i++;
else if (*pe == RBRC) {
if (i == 0)
break;
i--;
}
if (i != 0 || *pe == '\0') {
blkfree(nv);
return (-RBRC);
}
for (i = 0, pl = pm = p; pm <= pe; pm++)
switch (*pm) {
case LBRK:
for (++pm; *pm != RBRK && *pm != EOS; pm++)
continue;
if (*pm == EOS) {
*vl = NULL;
blkfree(nv);
return (-RBRK);
}
break;
case LBRC:
i++;
break;
case RBRC:
if (i) {
i--;
break;
}
/* FALLTHROUGH */
case ',':
if (i && *pm == ',')
break;
else {
Char savec = *pm;
*pm = EOS;
(void) Strcpy(lm, pl);
(void) Strcat(gbuf, pe + 1);
*pm = savec;
*vl++ = Strsave(gbuf);
len++;
pl = pm + 1;
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = (Char **) xrealloc((ptr_t) nv, (size_t)
size * sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
break;
default:
break;
}
*vl = NULL;
*bl = nv;
return (len);
}
static void
expbrace(nvp, elp, size)
Char ***nvp, ***elp;
int size;
{
Char **vl, **el, **nv, *s;
vl = nv = *nvp;
if (elp != NULL)
el = *elp;
else
for (el = vl; *el; el++)
continue;
for (s = *vl; s; s = *++vl) {
Char *b;
Char **vp, **bp;
/* leave {} untouched for find */
if (s[0] == '{' && (s[1] == '\0' || (s[1] == '}' && s[2] == '\0')))
continue;
if ((b = Strchr(s, '{')) != NULL) {
Char **bl;
int len;
if ((len = globbrace(s, b, &bl)) < 0) {
xfree((ptr_t) nv);
stderror(ERR_MISSING, -len);
}
xfree((ptr_t) s);
if (len == 1) {
*vl-- = *bl;
xfree((ptr_t) bl);
continue;
}
len = blklen(bl);
if (&el[len] >= &nv[size]) {
int l, e;
l = &el[len] - &nv[size];
size += GLOBSPACE > l ? GLOBSPACE : l;
l = vl - nv;
e = el - nv;
nv = (Char **) xrealloc((ptr_t) nv, (size_t)
size * sizeof(Char *));
vl = nv + l;
el = nv + e;
}
vp = vl--;
*vp = *bl;
len--;
for (bp = el; bp != vp; bp--)
bp[len] = *bp;
el += len;
vp++;
for (bp = bl + 1; *bp; *vp++ = *bp++)
continue;
xfree((ptr_t) bl);
}
}
if (elp != NULL)
*elp = el;
*nvp = nv;
}
static Char **
globexpand(v)
Char **v;
{
Char *s;
Char **nv, **vl, **el;
int size = GLOBSPACE;
nv = vl = (Char **) xmalloc((size_t) sizeof(Char *) * size);
*vl = NULL;
/*
* Step 1: expand backquotes.
*/
while ((s = *v++) != NULL) {
if (Strchr(s, '`')) {
int i;
(void) dobackp(s, 0);
for (i = 0; i < pargc; i++) {
*vl++ = pargv[i];
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = (Char **) xrealloc((ptr_t) nv,
(size_t) size * sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
xfree((ptr_t) pargv);
pargv = NULL;
}
else {
*vl++ = Strsave(s);
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = (Char **) xrealloc((ptr_t) nv, (size_t)
size * sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
}
*vl = NULL;
if (noglob)
return (nv);
/*
* Step 2: expand braces
*/
el = vl;
expbrace(&nv, &el, size);
/*
* Step 3: expand ~
*/
vl = nv;
for (s = *vl; s; s = *++vl)
if (*s == '~')
*vl = globtilde(nv, s);
vl = nv;
return (vl);
}
static Char *
handleone(str, vl, action)
Char *str, **vl;
int action;
{
Char *cp, **vlp = vl;
switch (action) {
case G_ERROR:
setname(vis_str(str));
blkfree(vl);
stderror(ERR_NAME | ERR_AMBIG);
break;
case G_APPEND:
trim(vlp);
str = Strsave(*vlp++);
do {
cp = Strspl(str, STRspace);
xfree((ptr_t) str);
str = Strspl(cp, *vlp);
xfree((ptr_t) cp);
}
while (*++vlp);
blkfree(vl);
break;
case G_IGNORE:
str = Strsave(strip(*vlp));
blkfree(vl);
break;
default:
break;
}
return (str);
}
static Char **
libglob(vl)
Char **vl;
{
int gflgs = GLOB_QUOTE | GLOB_NOMAGIC;
glob_t globv;
char *ptr;
int nonomatch = adrof(STRnonomatch) != 0, magic = 0, match = 0;
if (!vl || !vl[0])
return (vl);
globv.gl_offs = 0;
globv.gl_pathv = 0;
globv.gl_pathc = 0;
if (nonomatch)
gflgs |= GLOB_NOCHECK;
do {
ptr = short2qstr(*vl);
switch (glob(ptr, gflgs, 0, &globv)) {
case GLOB_ABEND:
setname(vis_str(*vl));
stderror(ERR_NAME | ERR_GLOB);
/* NOTREACHED */
case GLOB_NOSPACE:
stderror(ERR_NOMEM);
/* NOTREACHED */
default:
break;
}
if (globv.gl_flags & GLOB_MAGCHAR) {
match |= (globv.gl_matchc != 0);
magic = 1;
}
gflgs |= GLOB_APPEND;
}
while (*++vl);
vl = (globv.gl_pathc == 0 || (magic && !match && !nonomatch)) ?
NULL : blk2short(globv.gl_pathv);
globfree(&globv);
return (vl);
}
Char *
globone(str, action)
Char *str;
int action;
{
Char *v[2], **vl, **vo;
int gflg;
noglob = adrof(STRnoglob) != 0;
gflag = 0;
v[0] = str;
v[1] = 0;
tglob(v);
gflg = gflag;
if (gflg == G_NONE)
return (strip(Strsave(str)));
if (gflg & G_CSH) {
/*
* Expand back-quote, tilde and brace
*/
vo = globexpand(v);
if (noglob || (gflg & G_GLOB) == 0) {
if (vo[0] == NULL) {
xfree((ptr_t) vo);
return (Strsave(STRNULL));
}
if (vo[1] != NULL)
return (handleone(str, vo, action));
else {
str = strip(vo[0]);
xfree((ptr_t) vo);
return (str);
}
}
}
else if (noglob || (gflg & G_GLOB) == 0)
return (strip(Strsave(str)));
else
vo = v;
vl = libglob(vo);
if ((gflg & G_CSH) && vl != vo)
blkfree(vo);
if (vl == NULL) {
setname(vis_str(str));
stderror(ERR_NAME | ERR_NOMATCH);
}
if (vl[0] == NULL) {
xfree((ptr_t) vl);
return (Strsave(STRNULL));
}
if (vl[1] != NULL)
return (handleone(str, vl, action));
else {
str = strip(*vl);
xfree((ptr_t) vl);
return (str);
}
}
Char **
globall(v)
Char **v;
{
Char **vl, **vo;
int gflg = gflag;
if (!v || !v[0]) {
gargv = saveblk(v);
gargc = blklen(gargv);
return (gargv);
}
noglob = adrof(STRnoglob) != 0;
if (gflg & G_CSH)
/*
* Expand back-quote, tilde and brace
*/
vl = vo = globexpand(v);
else
vl = vo = saveblk(v);
if (!noglob && (gflg & G_GLOB)) {
vl = libglob(vo);
if ((gflg & G_CSH) && vl != vo)
blkfree(vo);
}
else
trim(vl);
gargc = vl ? blklen(vl) : 0;
return (gargv = vl);
}
void
ginit()
{
gargsiz = GLOBSPACE;
gargv = (Char **) xmalloc((size_t) sizeof(Char *) * gargsiz);
gargv[0] = 0;
gargc = 0;
}
void
rscan(t, f)
register Char **t;
void (*f) ();
{
register Char *p;
while ((p = *t++) != NULL)
while (*p)
(*f) (*p++);
}
void
trim(t)
register Char **t;
{
register Char *p;
while ((p = *t++) != NULL)
while (*p)
*p++ &= TRIM;
}
void
tglob(t)
register Char **t;
{
register Char *p, c;
while ((p = *t++) != NULL) {
if (*p == '~' || *p == '=')
gflag |= G_CSH;
else if (*p == '{' &&
(p[1] == '\0' || (p[1] == '}' && p[2] == '\0')))
continue;
while ((c = *p++) != '\0') {
/*
* eat everything inside the matching backquotes
*/
if (c == '`') {
gflag |= G_CSH;
while (*p && *p != '`')
if (*p++ == '\\') {
if (*p) /* Quoted chars */
p++;
else
break;
}
if (*p) /* The matching ` */
p++;
else
break;
}
else if (c == '{')
gflag |= G_CSH;
else if (isglob(c))
gflag |= G_GLOB;
}
}
}
/*
* Command substitute cp. If literal, then this is a substitution from a
* << redirection, and so we should not crunch blanks and tabs, separating
* words only at newlines.
*/
Char **
dobackp(cp, literal)
Char *cp;
bool literal;
{
register Char *lp, *rp;
Char *ep, word[MAXPATHLEN];
if (pargv) {
#ifdef notdef
abort();
#endif
blkfree(pargv);
}
pargsiz = GLOBSPACE;
pargv = (Char **) xmalloc((size_t) sizeof(Char *) * pargsiz);
pargv[0] = NULL;
pargcp = pargs = word;
pargc = 0;
pnleft = MAXPATHLEN - 4;
for (;;) {
for (lp = cp; *lp != '`'; lp++) {
if (*lp == 0) {
if (pargcp != pargs)
pword();
return (pargv);
}
psave(*lp);
}
lp++;
for (rp = lp; *rp && *rp != '`'; rp++)
if (*rp == '\\') {
rp++;
if (!*rp)
goto oops;
}
if (!*rp)
oops: stderror(ERR_UNMATCHED, '`');
ep = Strsave(lp);
ep[rp - lp] = 0;
backeval(ep, literal);
cp = rp + 1;
}
}
static void
backeval(cp, literal)
Char *cp;
bool literal;
{
register int icnt, c;
register Char *ip;
struct command faket;
bool hadnl;
int pvec[2], quoted;
Char *fakecom[2], ibuf[BUFSIZ];
char tibuf[BUFSIZ];
hadnl = 0;
icnt = 0;
quoted = (literal || (cp[0] & QUOTE)) ? QUOTE : 0;
faket.t_dtyp = NODE_COMMAND;
faket.t_dflg = 0;
faket.t_dlef = 0;
faket.t_drit = 0;
faket.t_dspr = 0;
faket.t_dcom = fakecom;
fakecom[0] = STRfakecom1;
fakecom[1] = 0;
/*
* We do the psave job to temporarily change the current job so that the
* following fork is considered a separate job. This is so that when
* backquotes are used in a builtin function that calls glob the "current
* job" is not corrupted. We only need one level of pushed jobs as long as
* we are sure to fork here.
*/
psavejob();
/*
* It would be nicer if we could integrate this redirection more with the
* routines in sh.sem.c by doing a fake execute on a builtin function that
* was piped out.
*/
mypipe(pvec);
if (pfork(&faket, -1) == 0) {
struct wordent paraml;
struct command *t;
(void) close(pvec[0]);
(void) dmove(pvec[1], 1);
(void) dmove(SHERR, 2);
initdesc();
/*
* Bugfix for nested backquotes by Michael Greim <greim@sbsvax.UUCP>,
* posted to comp.bugs.4bsd 12 Sep. 1989.
*/
if (pargv) /* mg, 21.dec.88 */
blkfree(pargv), pargv = 0, pargsiz = 0;
/* mg, 21.dec.88 */
arginp = cp;
while (*cp)
*cp++ &= TRIM;
/*
* In the child ``forget'' everything about current aliases or
* eval vectors.
*/
alvec = NULL;
evalvec = NULL;
alvecp = NULL;
evalp = NULL;
(void) lex(&paraml);
if (seterr)
stderror(ERR_OLD);
alias(&paraml);
t = syntax(paraml.next, &paraml, 0);
if (seterr)
stderror(ERR_OLD);
if (t)
t->t_dflg |= F_NOFORK;
(void) signal(SIGTSTP, SIG_IGN);
(void) signal(SIGTTIN, SIG_IGN);
(void) signal(SIGTTOU, SIG_IGN);
execute(t, -1, NULL, NULL);
exitstat();
}
xfree((ptr_t) cp);
(void) close(pvec[1]);
c = 0;
ip = NULL;
do {
int cnt = 0;
for (;;) {
if (icnt == 0) {
int i;
ip = ibuf;
do
icnt = read(pvec[0], tibuf, BUFSIZ);
while (icnt == -1 && errno == EINTR);
if (icnt <= 0) {
c = -1;
break;
}
for (i = 0; i < icnt; i++)
ip[i] = (unsigned char) tibuf[i];
}
if (hadnl)
break;
--icnt;
c = (*ip++ & TRIM);
if (c == 0)
break;
if (c == '\n') {
/*
* Continue around the loop one more time, so that we can eat
* the last newline without terminating this word.
*/
hadnl = 1;
continue;
}
if (!quoted && (c == ' ' || c == '\t'))
break;
cnt++;
psave(c | quoted);
}
/*
* Unless at end-of-file, we will form a new word here if there were
* characters in the word, or in any case when we take text literally.
* If we didn't make empty words here when literal was set then we
* would lose blank lines.
*/
if (c != -1 && (cnt || literal))
pword();
hadnl = 0;
} while (c >= 0);
(void) close(pvec[0]);
pwait();
prestjob();
}
static void
psave(c)
int c;
{
if (--pnleft <= 0)
stderror(ERR_WTOOLONG);
*pargcp++ = c;
}
static void
pword()
{
psave(0);
if (pargc == pargsiz - 1) {
pargsiz += GLOBSPACE;
pargv = (Char **) xrealloc((ptr_t) pargv,
(size_t) pargsiz * sizeof(Char *));
}
pargv[pargc++] = Strsave(pargs);
pargv[pargc] = NULL;
pargcp = pargs;
pnleft = MAXPATHLEN - 4;
}
int
Gmatch(string, pattern)
Char *string, *pattern;
{
Char **blk, **p;
int gpol = 1, gres = 0;
if (*pattern == '^') {
gpol = 0;
pattern++;
}
blk = (Char **) xmalloc(GLOBSPACE * sizeof(Char *));
blk[0] = Strsave(pattern);
blk[1] = NULL;
expbrace(&blk, NULL, GLOBSPACE);
for (p = blk; *p; p++)
gres |= pmatch(string, *p);
blkfree(blk);
return(gres == gpol);
}
static int
pmatch(string, pattern)
register Char *string, *pattern;
{
register Char stringc, patternc;
int match, negate_range;
Char rangec;
for (;; ++string) {
stringc = *string & TRIM;
patternc = *pattern++;
switch (patternc) {
case 0:
return (stringc == 0);
case '?':
if (stringc == 0)
return (0);
break;
case '*':
if (!*pattern)
return (1);
while (*string)
if (Gmatch(string++, pattern))
return (1);
return (0);
case '[':
match = 0;
if ((negate_range = (*pattern == '^')) != 0)
pattern++;
while ((rangec = *pattern++) != '\0') {
if (rangec == ']')
break;
if (match)
continue;
if (rangec == '-' && *(pattern-2) != '[' && *pattern != ']') {
match = (stringc <= (*pattern & TRIM) &&
(*(pattern-2) & TRIM) <= stringc);
pattern++;
}
else
match = (stringc == (rangec & TRIM));
}
if (rangec == 0)
stderror(ERR_NAME | ERR_MISSING, ']');
if (match == negate_range)
return (0);
break;
default:
if ((patternc & TRIM) != stringc)
return (0);
break;
}
}
}
void
Gcat(s1, s2)
Char *s1, *s2;
{
register Char *p, *q;
int n;
for (p = s1; *p++;)
continue;
for (q = s2; *q++;)
continue;
n = (p - s1) + (q - s2) - 1;
if (++gargc >= gargsiz) {
gargsiz += GLOBSPACE;
gargv = (Char **) xrealloc((ptr_t) gargv,
(size_t) gargsiz * sizeof(Char *));
}
gargv[gargc] = 0;
p = gargv[gargc - 1] = (Char *) xmalloc((size_t) n * sizeof(Char));
for (q = s1; (*p++ = *q++) != '\0';)
continue;
for (p--, q = s2; (*p++ = *q++) != '\0';)
continue;
}
#ifdef FILEC
int
sortscmp(a, b)
register const ptr_t a, b;
{
#if defined(NLS) && !defined(NOSTRCOLL)
char buf[2048];
#endif
if (!a) /* check for NULL */
return (b ? 1 : 0);
if (!b)
return (-1);
if (!*(Char **)a) /* check for NULL */
return (*(Char **)b ? 1 : 0);
if (!*(Char **)b)
return (-1);
#if defined(NLS) && !defined(NOSTRCOLL)
(void) strcpy(buf, short2str(*(Char **)a));
return ((int) strcoll(buf, short2str(*(Char **)b)));
#else
return ((int) Strcmp(*(Char **)a, *(Char **)b));
#endif
}
#endif /* FILEC */

184
bin/csh/hist.c Normal file
View File

@ -0,0 +1,184 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <stdlib.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
static void hfree __P((struct Hist *));
static void dohist1 __P((struct Hist *, int *, int, int));
static void phist __P((struct Hist *, int));
void
savehist(sp)
struct wordent *sp;
{
register struct Hist *hp, *np;
register int histlen = 0;
Char *cp;
/* throw away null lines */
if (sp->next->word[0] == '\n')
return;
cp = value(STRhistory);
if (*cp) {
register Char *p = cp;
while (*p) {
if (!Isdigit(*p)) {
histlen = 0;
break;
}
histlen = histlen * 10 + *p++ - '0';
}
}
for (hp = &Histlist; (np = hp->Hnext) != NULL;)
if (eventno - np->Href >= histlen || histlen == 0)
hp->Hnext = np->Hnext, hfree(np);
else
hp = np;
(void) enthist(++eventno, sp, 1);
}
struct Hist *
enthist(event, lp, docopy)
int event;
register struct wordent *lp;
bool docopy;
{
register struct Hist *np;
np = (struct Hist *) xmalloc((size_t) sizeof(*np));
np->Hnum = np->Href = event;
if (docopy) {
copylex(&np->Hlex, lp);
}
else {
np->Hlex.next = lp->next;
lp->next->prev = &np->Hlex;
np->Hlex.prev = lp->prev;
lp->prev->next = &np->Hlex;
}
np->Hnext = Histlist.Hnext;
Histlist.Hnext = np;
return (np);
}
static void
hfree(hp)
register struct Hist *hp;
{
freelex(&hp->Hlex);
xfree((ptr_t) hp);
}
void
/*ARGSUSED*/
dohist(v, t)
Char **v;
struct command *t;
{
int n, rflg = 0, hflg = 0;
if (getn(value(STRhistory)) == 0)
return;
if (setintr)
(void) sigsetmask(sigblock((sigset_t) 0) & ~sigmask(SIGINT));
while (*++v && **v == '-') {
Char *vp = *v;
while (*++vp)
switch (*vp) {
case 'h':
hflg++;
break;
case 'r':
rflg++;
break;
case '-': /* ignore multiple '-'s */
break;
default:
stderror(ERR_HISTUS);
break;
}
}
if (*v)
n = getn(*v);
else {
n = getn(value(STRhistory));
}
dohist1(Histlist.Hnext, &n, rflg, hflg);
}
static void
dohist1(hp, np, rflg, hflg)
struct Hist *hp;
int *np, rflg, hflg;
{
bool print = (*np) > 0;
for (; hp != 0; hp = hp->Hnext) {
(*np)--;
hp->Href++;
if (rflg == 0) {
dohist1(hp->Hnext, np, rflg, hflg);
if (print)
phist(hp, hflg);
return;
}
if (*np >= 0)
phist(hp, hflg);
}
}
static void
phist(hp, hflg)
register struct Hist *hp;
int hflg;
{
if (hflg == 0)
(void) fprintf(cshout, "%6d\t", hp->Hnum);
prlex(cshout, &hp->Hlex);
}

135
bin/csh/init.c Normal file
View File

@ -0,0 +1,135 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
/*###9 [cc] warning: `sccsid' defined but not used%%%*/
static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
#define INF 1000
struct biltins bfunc[] =
{
{ "@", dolet, 0, INF },
{ "alias", doalias, 0, INF },
{ "alloc", showall, 0, 1 },
{ "bg", dobg, 0, INF },
{ "break", dobreak, 0, 0 },
{ "breaksw", doswbrk, 0, 0 },
{ "case", dozip, 0, 1 },
{ "cd", dochngd, 0, INF },
{ "chdir", dochngd, 0, INF },
{ "continue", docontin, 0, 0 },
{ "default", dozip, 0, 0 },
{ "dirs", dodirs, 0, INF },
{ "echo", doecho, 0, INF },
{ "else", doelse, 0, INF },
{ "end", doend, 0, 0 },
{ "endif", dozip, 0, 0 },
{ "endsw", dozip, 0, 0 },
{ "eval", doeval, 0, INF },
{ "exec", execash, 1, INF },
{ "exit", doexit, 0, INF },
{ "fg", dofg, 0, INF },
{ "foreach", doforeach, 3, INF },
{ "glob", doglob, 0, INF },
{ "goto", dogoto, 1, 1 },
{ "hashstat", hashstat, 0, 0 },
{ "history", dohist, 0, 2 },
{ "if", doif, 1, INF },
{ "jobs", dojobs, 0, 1 },
{ "kill", dokill, 1, INF },
{ "limit", dolimit, 0, 3 },
{ "linedit", doecho, 0, INF },
{ "login", dologin, 0, 1 },
{ "logout", dologout, 0, 0 },
{ "nice", donice, 0, INF },
{ "nohup", donohup, 0, INF },
{ "notify", donotify, 0, INF },
{ "onintr", doonintr, 0, 2 },
{ "popd", dopopd, 0, INF },
{ "printf", doprintf, 1, INF },
{ "pushd", dopushd, 0, INF },
{ "rehash", dohash, 0, 0 },
{ "repeat", dorepeat, 2, INF },
{ "set", doset, 0, INF },
{ "setenv", dosetenv, 0, 2 },
{ "shift", shift, 0, 1 },
{ "source", dosource, 1, 2 },
{ "stop", dostop, 1, INF },
{ "suspend", dosuspend, 0, 0 },
{ "switch", doswitch, 1, INF },
{ "time", dotime, 0, INF },
{ "umask", doumask, 0, 1 },
{ "unalias", unalias, 1, INF },
{ "unhash", dounhash, 0, 0 },
{ "unlimit", dounlimit, 0, INF },
{ "unset", unset, 1, INF },
{ "unsetenv", dounsetenv, 1, INF },
{ "wait", dowait, 0, 0 },
{ "which", dowhich, 1, INF },
{ "while", dowhile, 1, INF }
};
int nbfunc = sizeof bfunc / sizeof *bfunc;
struct srch srchn[] =
{
{ "@", T_LET },
{ "break", T_BREAK },
{ "breaksw", T_BRKSW },
{ "case", T_CASE },
{ "default", T_DEFAULT },
{ "else", T_ELSE },
{ "end", T_END },
{ "endif", T_ENDIF },
{ "endsw", T_ENDSW },
{ "exit", T_EXIT },
{ "foreach", T_FOREACH },
{ "goto", T_GOTO },
{ "if", T_IF },
{ "label", T_LABEL },
{ "set", T_SET },
{ "switch", T_SWITCH },
{ "while", T_WHILE }
};
int nsrchn = sizeof srchn / sizeof *srchn;

1639
bin/csh/lex.c Normal file

File diff suppressed because it is too large Load Diff

420
bin/csh/misc.c Normal file
View File

@ -0,0 +1,420 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/param.h>
#include <stdlib.h>
#include <unistd.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
static int renum __P((int, int));
int
any(s, c)
register char *s;
register int c;
{
if (!s)
return (0); /* Check for nil pointer */
while (*s)
if (*s++ == c)
return (1);
return (0);
}
void
setzero(cp, i)
char *cp;
int i;
{
if (i != 0)
do
*cp++ = 0;
while (--i);
}
char *
strsave(s)
register char *s;
{
char *n;
register char *p;
if (s == NULL)
s = "";
for (p = s; *p++;)
continue;
n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
while ((*p++ = *s++) != '\0')
continue;
return (n);
}
Char **
blkend(up)
register Char **up;
{
while (*up)
up++;
return (up);
}
void
blkpr(fp, av)
FILE *fp;
register Char **av;
{
for (; *av; av++) {
(void) fprintf(fp, "%s", vis_str(*av));
if (av[1])
(void) fprintf(fp, " ");
}
}
int
blklen(av)
register Char **av;
{
register int i = 0;
while (*av++)
i++;
return (i);
}
Char **
blkcpy(oav, bv)
Char **oav;
register Char **bv;
{
register Char **av = oav;
while ((*av++ = *bv++) != NULL)
continue;
return (oav);
}
Char **
blkcat(up, vp)
Char **up, **vp;
{
(void) blkcpy(blkend(up), vp);
return (up);
}
void
blkfree(av0)
Char **av0;
{
register Char **av = av0;
if (!av0)
return;
for (; *av; av++)
xfree((ptr_t) * av);
xfree((ptr_t) av0);
}
Char **
saveblk(v)
register Char **v;
{
register Char **newv =
(Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
Char **onewv = newv;
while (*v)
*newv++ = Strsave(*v++);
return (onewv);
}
#ifdef NOTUSED
char *
strstr(s, t)
register char *s, *t;
{
do {
register char *ss = s;
register char *tt = t;
do
if (*tt == '\0')
return (s);
while (*ss++ == *tt++);
} while (*s++ != '\0');
return (NULL);
}
#endif /* NOTUSED */
#ifndef SHORT_STRINGS
char *
strspl(cp, dp)
char *cp, *dp;
{
char *ep;
register char *p, *q;
if (!cp)
cp = "";
if (!dp)
dp = "";
for (p = cp; *p++;)
continue;
for (q = dp; *q++;)
continue;
ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
for (p = ep, q = cp; *p++ = *q++;)
continue;
for (p--, q = dp; *p++ = *q++;)
continue;
return (ep);
}
#endif
Char **
blkspl(up, vp)
register Char **up, **vp;
{
register Char **wp =
(Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
sizeof(Char **));
(void) blkcpy(wp, up);
return (blkcat(wp, vp));
}
Char
lastchr(cp)
register Char *cp;
{
if (!cp)
return (0);
if (!*cp)
return (0);
while (cp[1])
cp++;
return (*cp);
}
/*
* This routine is called after an error to close up
* any units which may have been left open accidentally.
*/
void
closem()
{
register int f;
for (f = 0; f < NOFILE; f++)
if (f != SHIN && f != SHOUT && f != SHERR && f != OLDSTD &&
f != FSHTTY)
(void) close(f);
}
void
donefds()
{
(void) close(0);
(void) close(1);
(void) close(2);
didfds = 0;
}
/*
* Move descriptor i to j.
* If j is -1 then we just want to get i to a safe place,
* i.e. to a unit > 2. This also happens in dcopy.
*/
int
dmove(i, j)
register int i, j;
{
if (i == j || i < 0)
return (i);
if (j >= 0) {
(void) dup2(i, j);
if (j != i)
(void) close(i);
return (j);
}
j = dcopy(i, j);
if (j != i)
(void) close(i);
return (j);
}
int
dcopy(i, j)
register int i, j;
{
if (i == j || i < 0 || (j < 0 && i > 2))
return (i);
if (j >= 0) {
(void) dup2(i, j);
return (j);
}
(void) close(j);
return (renum(i, j));
}
static int
renum(i, j)
register int i, j;
{
register int k = dup(i);
if (k < 0)
return (-1);
if (j == -1 && k > 2)
return (k);
if (k != j) {
j = renum(k, j);
(void) close(k);
return (j);
}
return (k);
}
/*
* Left shift a command argument list, discarding
* the first c arguments. Used in "shift" commands
* as well as by commands like "repeat".
*/
void
lshift(v, c)
register Char **v;
register int c;
{
register Char **u;
for (u = v; *u && --c >= 0; u++)
xfree((ptr_t) *u);
(void) blkcpy(v, u);
}
int
number(cp)
Char *cp;
{
if (!cp)
return(0);
if (*cp == '-') {
cp++;
if (!Isdigit(*cp))
return (0);
cp++;
}
while (*cp && Isdigit(*cp))
cp++;
return (*cp == 0);
}
Char **
copyblk(v)
register Char **v;
{
Char **nv = (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
return (blkcpy(nv, v));
}
#ifndef SHORT_STRINGS
char *
strend(cp)
register char *cp;
{
if (!cp)
return (cp);
while (*cp)
cp++;
return (cp);
}
#endif /* SHORT_STRINGS */
Char *
strip(cp)
Char *cp;
{
register Char *dp = cp;
if (!cp)
return (cp);
while ((*dp++ &= TRIM) != '\0')
continue;
return (cp);
}
void
udvar(name)
Char *name;
{
setname(vis_str(name));
stderror(ERR_NAME | ERR_UNDVAR);
}
int
prefix(sub, str)
register Char *sub, *str;
{
for (;;) {
if (*sub == 0)
return (1);
if (*str == 0)
return (0);
if (*sub++ != *str++)
return (0);
}
}

698
bin/csh/parse.c Normal file
View File

@ -0,0 +1,698 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
static void asyntax __P((struct wordent *, struct wordent *));
static void asyn0 __P((struct wordent *, struct wordent *));
static void asyn3 __P((struct wordent *, struct wordent *));
static struct wordent
*freenod __P((struct wordent *, struct wordent *));
static struct command
*syn0 __P((struct wordent *, struct wordent *, int));
static struct command
*syn1 __P((struct wordent *, struct wordent *, int));
static struct command
*syn1a __P((struct wordent *, struct wordent *, int));
static struct command
*syn1b __P((struct wordent *, struct wordent *, int));
static struct command
*syn2 __P((struct wordent *, struct wordent *, int));
static struct command
*syn3 __P((struct wordent *, struct wordent *, int));
#define ALEFT 21 /* max of 20 alias expansions */
#define HLEFT 11 /* max of 10 history expansions */
/*
* Perform aliasing on the word list lex
* Do a (very rudimentary) parse to separate into commands.
* If word 0 of a command has an alias, do it.
* Repeat a maximum of 20 times.
*/
static int aleft;
extern int hleft;
void
alias(lex)
register struct wordent *lex;
{
jmp_buf osetexit;
aleft = ALEFT;
hleft = HLEFT;
getexit(osetexit);
(void) setexit();
if (haderr) {
resexit(osetexit);
reset();
}
if (--aleft == 0)
stderror(ERR_ALIASLOOP);
asyntax(lex->next, lex);
resexit(osetexit);
}
static void
asyntax(p1, p2)
register struct wordent *p1, *p2;
{
while (p1 != p2)
if (any(";&\n", p1->word[0]))
p1 = p1->next;
else {
asyn0(p1, p2);
return;
}
}
static void
asyn0(p1, p2)
struct wordent *p1;
register struct wordent *p2;
{
register struct wordent *p;
register int l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
if (l < 0)
stderror(ERR_TOOMANYRP);
continue;
case '>':
if (p->next != p2 && eq(p->next->word, STRand))
p = p->next;
continue;
case '&':
case '|':
case ';':
case '\n':
if (l != 0)
continue;
asyn3(p1, p);
asyntax(p->next, p2);
return;
}
if (l == 0)
asyn3(p1, p2);
}
static void
asyn3(p1, p2)
struct wordent *p1;
register struct wordent *p2;
{
register struct varent *ap;
struct wordent alout;
register bool redid;
if (p1 == p2)
return;
if (p1->word[0] == '(') {
for (p2 = p2->prev; p2->word[0] != ')'; p2 = p2->prev)
if (p2 == p1)
return;
if (p2 == p1->next)
return;
asyn0(p1->next, p2);
return;
}
ap = adrof1(p1->word, &aliases);
if (ap == 0)
return;
alhistp = p1->prev;
alhistt = p2;
alvec = ap->vec;
redid = lex(&alout);
alhistp = alhistt = 0;
alvec = 0;
if (seterr) {
freelex(&alout);
stderror(ERR_OLD);
}
if (p1->word[0] && eq(p1->word, alout.next->word)) {
Char *cp = alout.next->word;
alout.next->word = Strspl(STRQNULL, cp);
xfree((ptr_t) cp);
}
p1 = freenod(p1, redid ? p2 : p1->next);
if (alout.next != &alout) {
p1->next->prev = alout.prev->prev;
alout.prev->prev->next = p1->next;
alout.next->prev = p1;
p1->next = alout.next;
xfree((ptr_t) alout.prev->word);
xfree((ptr_t) (alout.prev));
}
reset(); /* throw! */
}
static struct wordent *
freenod(p1, p2)
register struct wordent *p1, *p2;
{
register struct wordent *retp = p1->prev;
while (p1 != p2) {
xfree((ptr_t) p1->word);
p1 = p1->next;
xfree((ptr_t) (p1->prev));
}
retp->next = p2;
p2->prev = retp;
return (retp);
}
#define PHERE 1
#define PIN 2
#define POUT 4
#define PERR 8
/*
* syntax
* empty
* syn0
*/
struct command *
syntax(p1, p2, flags)
register struct wordent *p1, *p2;
int flags;
{
while (p1 != p2)
if (any(";&\n", p1->word[0]))
p1 = p1->next;
else
return (syn0(p1, p2, flags));
return (0);
}
/*
* syn0
* syn1
* syn1 & syntax
*/
static struct command *
syn0(p1, p2, flags)
struct wordent *p1, *p2;
int flags;
{
register struct wordent *p;
register struct command *t, *t1;
int l;
l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
if (l < 0)
seterror(ERR_TOOMANYRP);
continue;
case '|':
if (p->word[1] == '|')
continue;
/* fall into ... */
case '>':
if (p->next != p2 && eq(p->next->word, STRand))
p = p->next;
continue;
case '&':
if (l != 0)
break;
if (p->word[1] == '&')
continue;
t1 = syn1(p1, p, flags);
if (t1->t_dtyp == NODE_LIST ||
t1->t_dtyp == NODE_AND ||
t1->t_dtyp == NODE_OR) {
t = (struct command *) xcalloc(1, sizeof(*t));
t->t_dtyp = NODE_PAREN;
t->t_dflg = F_AMPERSAND | F_NOINTERRUPT;
t->t_dspr = t1;
t1 = t;
}
else
t1->t_dflg |= F_AMPERSAND | F_NOINTERRUPT;
t = (struct command *) xcalloc(1, sizeof(*t));
t->t_dtyp = NODE_LIST;
t->t_dflg = 0;
t->t_dcar = t1;
t->t_dcdr = syntax(p, p2, flags);
return (t);
}
if (l == 0)
return (syn1(p1, p2, flags));
seterror(ERR_TOOMANYLP);
return (0);
}
/*
* syn1
* syn1a
* syn1a ; syntax
*/
static struct command *
syn1(p1, p2, flags)
struct wordent *p1, *p2;
int flags;
{
register struct wordent *p;
register struct command *t;
int l;
l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
continue;
case ';':
case '\n':
if (l != 0)
break;
t = (struct command *) xcalloc(1, sizeof(*t));
t->t_dtyp = NODE_LIST;
t->t_dcar = syn1a(p1, p, flags);
t->t_dcdr = syntax(p->next, p2, flags);
if (t->t_dcdr == 0)
t->t_dcdr = t->t_dcar, t->t_dcar = 0;
return (t);
}
return (syn1a(p1, p2, flags));
}
/*
* syn1a
* syn1b
* syn1b || syn1a
*/
static struct command *
syn1a(p1, p2, flags)
struct wordent *p1, *p2;
int flags;
{
register struct wordent *p;
register struct command *t;
register int l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
continue;
case '|':
if (p->word[1] != '|')
continue;
if (l == 0) {
t = (struct command *) xcalloc(1, sizeof(*t));
t->t_dtyp = NODE_OR;
t->t_dcar = syn1b(p1, p, flags);
t->t_dcdr = syn1a(p->next, p2, flags);
t->t_dflg = 0;
return (t);
}
continue;
}
return (syn1b(p1, p2, flags));
}
/*
* syn1b
* syn2
* syn2 && syn1b
*/
static struct command *
syn1b(p1, p2, flags)
struct wordent *p1, *p2;
int flags;
{
register struct wordent *p;
register struct command *t;
register int l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
continue;
case '&':
if (p->word[1] == '&' && l == 0) {
t = (struct command *) xcalloc(1, sizeof(*t));
t->t_dtyp = NODE_AND;
t->t_dcar = syn2(p1, p, flags);
t->t_dcdr = syn1b(p->next, p2, flags);
t->t_dflg = 0;
return (t);
}
continue;
}
return (syn2(p1, p2, flags));
}
/*
* syn2
* syn3
* syn3 | syn2
* syn3 |& syn2
*/
static struct command *
syn2(p1, p2, flags)
struct wordent *p1, *p2;
int flags;
{
register struct wordent *p, *pn;
register struct command *t;
register int l = 0;
int f;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
continue;
case '|':
if (l != 0)
continue;
t = (struct command *) xcalloc(1, sizeof(*t));
f = flags | POUT;
pn = p->next;
if (pn != p2 && pn->word[0] == '&') {
f |= PERR;
t->t_dflg |= F_STDERR;
}
t->t_dtyp = NODE_PIPE;
t->t_dcar = syn3(p1, p, f);
if (pn != p2 && pn->word[0] == '&')
p = pn;
t->t_dcdr = syn2(p->next, p2, flags | PIN);
return (t);
}
return (syn3(p1, p2, flags));
}
static char RELPAR[] = {'<', '>', '(', ')', '\0'};
/*
* syn3
* ( syn0 ) [ < in ] [ > out ]
* word word* [ < in ] [ > out ]
* KEYWORD ( word* ) word* [ < in ] [ > out ]
*
* KEYWORD = (@ exit foreach if set switch test while)
*/
static struct command *
syn3(p1, p2, flags)
struct wordent *p1, *p2;
int flags;
{
register struct wordent *p;
struct wordent *lp, *rp;
register struct command *t;
register int l;
Char **av;
int n, c;
bool specp = 0;
if (p1 != p2) {
p = p1;
again:
switch (srchx(p->word)) {
case T_ELSE:
p = p->next;
if (p != p2)
goto again;
break;
case T_EXIT:
case T_FOREACH:
case T_IF:
case T_LET:
case T_SET:
case T_SWITCH:
case T_WHILE:
specp = 1;
break;
}
}
n = 0;
l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
if (specp)
n++;
l++;
continue;
case ')':
if (specp)
n++;
l--;
continue;
case '>':
case '<':
if (l != 0) {
if (specp)
n++;
continue;
}
if (p->next == p2)
continue;
if (any(RELPAR, p->next->word[0]))
continue;
n--;
continue;
default:
if (!specp && l != 0)
continue;
n++;
continue;
}
if (n < 0)
n = 0;
t = (struct command *) xcalloc(1, sizeof(*t));
av = (Char **) xcalloc((size_t) (n + 1), sizeof(Char **));
t->t_dcom = av;
n = 0;
if (p2->word[0] == ')')
t->t_dflg = F_NOFORK;
lp = 0;
rp = 0;
l = 0;
for (p = p1; p != p2; p = p->next) {
c = p->word[0];
switch (c) {
case '(':
if (l == 0) {
if (lp != 0 && !specp)
seterror(ERR_BADPLP);
lp = p->next;
}
l++;
goto savep;
case ')':
l--;
if (l == 0)
rp = p;
goto savep;
case '>':
if (l != 0)
goto savep;
if (p->word[1] == '>')
t->t_dflg |= F_APPEND;
if (p->next != p2 && eq(p->next->word, STRand)) {
t->t_dflg |= F_STDERR, p = p->next;
if (flags & (POUT | PERR)) {
seterror(ERR_OUTRED);
continue;
}
}
if (p->next != p2 && eq(p->next->word, STRbang))
t->t_dflg |= F_OVERWRITE, p = p->next;
if (p->next == p2) {
seterror(ERR_MISRED);
continue;
}
p = p->next;
if (any(RELPAR, p->word[0])) {
seterror(ERR_MISRED);
continue;
}
if ((flags & POUT) && ((flags & PERR) == 0 || t->t_drit))
seterror(ERR_OUTRED);
else
t->t_drit = Strsave(p->word);
continue;
case '<':
if (l != 0)
goto savep;
if (p->word[1] == '<')
t->t_dflg |= F_READ;
if (p->next == p2) {
seterror(ERR_MISRED);
continue;
}
p = p->next;
if (any(RELPAR, p->word[0])) {
seterror(ERR_MISRED);
continue;
}
if ((flags & PHERE) && (t->t_dflg & F_READ))
seterror(ERR_REDPAR);
else if ((flags & PIN) || t->t_dlef)
seterror(ERR_INRED);
else
t->t_dlef = Strsave(p->word);
continue;
savep:
if (!specp)
continue;
default:
if (l != 0 && !specp)
continue;
if (seterr == 0)
av[n] = Strsave(p->word);
n++;
continue;
}
}
if (lp != 0 && !specp) {
if (n != 0)
seterror(ERR_BADPLPS);
t->t_dtyp = NODE_PAREN;
t->t_dspr = syn0(lp, rp, PHERE);
}
else {
if (n == 0)
seterror(ERR_NULLCOM);
t->t_dtyp = NODE_COMMAND;
}
return (t);
}
void
freesyn(t)
register struct command *t;
{
register Char **v;
if (t == 0)
return;
switch (t->t_dtyp) {
case NODE_COMMAND:
for (v = t->t_dcom; *v; v++)
xfree((ptr_t) * v);
xfree((ptr_t) (t->t_dcom));
xfree((ptr_t) t->t_dlef);
xfree((ptr_t) t->t_drit);
break;
case NODE_PAREN:
freesyn(t->t_dspr);
xfree((ptr_t) t->t_dlef);
xfree((ptr_t) t->t_drit);
break;
case NODE_AND:
case NODE_OR:
case NODE_PIPE:
case NODE_LIST:
freesyn(t->t_dcar), freesyn(t->t_dcdr);
break;
}
xfree((ptr_t) t);
}

41
bin/csh/pathnames.h Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)pathnames.h 8.1 (Berkeley) 5/31/93
*/
#define _PATH_BIN "/bin"
#define _PATH_DOTCSHRC "/etc/csh.cshrc"
#define _PATH_DOTLOGIN "/etc/csh.login"
#define _PATH_DOTLOGOUT "/etc/csh.logout"
#define _PATH_LOGIN "/usr/bin/login"
#define _PATH_USRBIN "/usr/bin"

1356
bin/csh/proc.c Normal file

File diff suppressed because it is too large Load Diff

101
bin/csh/proc.h Normal file
View File

@ -0,0 +1,101 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)proc.h 8.1 (Berkeley) 5/31/93
*/
/*
* Structure for each process the shell knows about:
* allocated and filled by pcreate.
* flushed by pflush; freeing always happens at top level
* so the interrupt level has less to worry about.
* processes are related to "friends" when in a pipeline;
* p_friends links makes a circular list of such jobs
*/
struct process {
struct process *p_next; /* next in global "proclist" */
struct process *p_friends; /* next in job list (or self) */
struct directory *p_cwd; /* cwd of the job (only in head) */
short unsigned p_flags; /* various job status flags */
char p_reason; /* reason for entering this state */
int p_index; /* shorthand job index */
int p_pid;
int p_jobid; /* pid of job leader */
/* if a job is stopped/background p_jobid gives its pgrp */
struct timeval p_btime; /* begin time */
struct timeval p_etime; /* end time */
struct rusage p_rusage;
Char *p_command; /* first PMAXLEN chars of command */
};
/* flag values for p_flags */
#define PRUNNING (1<<0) /* running */
#define PSTOPPED (1<<1) /* stopped */
#define PNEXITED (1<<2) /* normally exited */
#define PAEXITED (1<<3) /* abnormally exited */
#define PSIGNALED (1<<4) /* terminated by a signal != SIGINT */
#define PALLSTATES (PRUNNING|PSTOPPED|PNEXITED|PAEXITED|PSIGNALED|PINTERRUPTED)
#define PNOTIFY (1<<5) /* notify async when done */
#define PTIME (1<<6) /* job times should be printed */
#define PAWAITED (1<<7) /* top level is waiting for it */
#define PFOREGND (1<<8) /* started in shells pgrp */
#define PDUMPED (1<<9) /* process dumped core */
#define PERR (1<<10) /* diagnostic output also piped out */
#define PPOU (1<<11) /* piped output */
#define PREPORTED (1<<12) /* status has been reported */
#define PINTERRUPTED (1<<13) /* job stopped via interrupt signal */
#define PPTIME (1<<14) /* time individual process */
#define PNEEDNOTE (1<<15) /* notify as soon as practical */
#define PMAXLEN 80
/* defines for arguments to pprint */
#define NUMBER 01
#define NAME 02
#define REASON 04
#define AMPERSAND 010
#define FANCY 020
#define SHELLDIR 040 /* print shell's dir if not the same */
#define JOBDIR 0100 /* print job's dir if not the same */
#define AREASON 0200
struct process proclist; /* list head of all processes */
bool pnoprocesses; /* pchild found nothing to wait for */
struct process *pholdjob; /* one level stack of current jobs */
struct process *pcurrjob; /* current job */
struct process *pcurrent; /* current job in table */
struct process *pprevious; /* previous job in table */
int pmaxindex; /* current maximum job index */

645
bin/csh/sem.c Normal file
View File

@ -0,0 +1,645 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)sem.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "proc.h"
#include "extern.h"
static void vffree __P((int));
static Char *splicepipe __P((struct command *t, Char *));
static void doio __P((struct command *t, int *, int *));
static void chkclob __P((char *));
void
execute(t, wanttty, pipein, pipeout)
register struct command *t;
int wanttty, *pipein, *pipeout;
{
bool forked = 0;
struct biltins *bifunc;
int pid = 0;
int pv[2];
static sigset_t csigmask;
static sigset_t ocsigmask;
static int onosigchld = 0;
static int nosigchld = 0;
UNREGISTER(forked);
UNREGISTER(bifunc);
UNREGISTER(wanttty);
if (t == 0)
return;
if (t->t_dflg & F_AMPERSAND)
wanttty = 0;
switch (t->t_dtyp) {
case NODE_COMMAND:
if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
(void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
if ((t->t_dflg & F_REPEAT) == 0)
Dfix(t); /* $ " ' \ */
if (t->t_dcom[0] == 0)
return;
/* fall into... */
case NODE_PAREN:
if (t->t_dflg & F_PIPEOUT)
mypipe(pipeout);
/*
* Must do << early so parent will know where input pointer should be.
* If noexec then this is all we do.
*/
if (t->t_dflg & F_READ) {
(void) close(0);
heredoc(t->t_dlef);
if (noexec)
(void) close(0);
}
set(STRstatus, Strsave(STR0));
/*
* This mess is the necessary kludge to handle the prefix builtins:
* nice, nohup, time. These commands can also be used by themselves,
* and this is not handled here. This will also work when loops are
* parsed.
*/
while (t->t_dtyp == NODE_COMMAND)
if (eq(t->t_dcom[0], STRnice))
if (t->t_dcom[1])
if (strchr("+-", t->t_dcom[1][0]))
if (t->t_dcom[2]) {
setname("nice");
t->t_nice =
getn(t->t_dcom[1]);
lshift(t->t_dcom, 2);
t->t_dflg |= F_NICE;
}
else
break;
else {
t->t_nice = 4;
lshift(t->t_dcom, 1);
t->t_dflg |= F_NICE;
}
else
break;
else if (eq(t->t_dcom[0], STRnohup))
if (t->t_dcom[1]) {
t->t_dflg |= F_NOHUP;
lshift(t->t_dcom, 1);
}
else
break;
else if (eq(t->t_dcom[0], STRtime))
if (t->t_dcom[1]) {
t->t_dflg |= F_TIME;
lshift(t->t_dcom, 1);
}
else
break;
else
break;
/* is it a command */
if (t->t_dtyp == NODE_COMMAND) {
/*
* Check if we have a builtin function and remember which one.
*/
bifunc = isbfunc(t);
if (noexec) {
/*
* Continue for builtins that are part of the scripting language
*/
if (bifunc->bfunct != dobreak && bifunc->bfunct != docontin &&
bifunc->bfunct != doelse && bifunc->bfunct != doend &&
bifunc->bfunct != doforeach && bifunc->bfunct != dogoto &&
bifunc->bfunct != doif && bifunc->bfunct != dorepeat &&
bifunc->bfunct != doswbrk && bifunc->bfunct != doswitch &&
bifunc->bfunct != dowhile && bifunc->bfunct != dozip)
break;
}
}
else { /* not a command */
bifunc = NULL;
if (noexec)
break;
}
/*
* We fork only if we are timed, or are not the end of a parenthesized
* list and not a simple builtin function. Simple meaning one that is
* not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
* fork in some of these cases.
*/
/*
* Prevent forking cd, pushd, popd, chdir cause this will cause the
* shell not to change dir!
*/
if (bifunc && (bifunc->bfunct == dochngd ||
bifunc->bfunct == dopushd ||
bifunc->bfunct == dopopd))
t->t_dflg &= ~(F_NICE);
if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
(!bifunc || t->t_dflg &
(F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP)))) ||
/*
* We have to fork for eval too.
*/
(bifunc && (t->t_dflg & (F_PIPEIN | F_PIPEOUT)) != 0 &&
bifunc->bfunct == doeval))
if (t->t_dtyp == NODE_PAREN ||
t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc) {
forked++;
/*
* We need to block SIGCHLD here, so that if the process does
* not die before we can set the process group
*/
if (wanttty >= 0 && !nosigchld) {
csigmask = sigblock(sigmask(SIGCHLD));
nosigchld = 1;
}
pid = pfork(t, wanttty);
if (pid == 0 && nosigchld) {
(void) sigsetmask(csigmask);
nosigchld = 0;
}
else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
backpid = pid;
}
else {
int ochild, osetintr, ohaderr, odidfds;
int oSHIN, oSHOUT, oSHERR, oOLDSTD, otpgrp;
sigset_t omask;
/*
* Prepare for the vfork by saving everything that the child
* corrupts before it exec's. Note that in some signal
* implementations which keep the signal info in user space
* (e.g. Sun's) it will also be necessary to save and restore
* the current sigvec's for the signals the child touches
* before it exec's.
*/
if (wanttty >= 0 && !nosigchld && !noexec) {
csigmask = sigblock(sigmask(SIGCHLD));
nosigchld = 1;
}
omask = sigblock(sigmask(SIGCHLD) | sigmask(SIGINT));
ochild = child;
osetintr = setintr;
ohaderr = haderr;
odidfds = didfds;
oSHIN = SHIN;
oSHOUT = SHOUT;
oSHERR = SHERR;
oOLDSTD = OLDSTD;
otpgrp = tpgrp;
ocsigmask = csigmask;
onosigchld = nosigchld;
Vsav = Vdp = 0;
Vexpath = 0;
Vt = 0;
pid = vfork();
if (pid < 0) {
(void) sigsetmask(omask);
stderror(ERR_NOPROC);
}
forked++;
if (pid) { /* parent */
child = ochild;
setintr = osetintr;
haderr = ohaderr;
didfds = odidfds;
SHIN = oSHIN;
SHOUT = oSHOUT;
SHERR = oSHERR;
OLDSTD = oOLDSTD;
tpgrp = otpgrp;
csigmask = ocsigmask;
nosigchld = onosigchld;
xfree((ptr_t) Vsav);
Vsav = 0;
xfree((ptr_t) Vdp);
Vdp = 0;
xfree((ptr_t) Vexpath);
Vexpath = 0;
blkfree((Char **) Vt);
Vt = 0;
/* this is from pfork() */
palloc(pid, t);
(void) sigsetmask(omask);
}
else { /* child */
/* this is from pfork() */
int pgrp;
bool ignint = 0;
if (nosigchld) {
(void) sigsetmask(csigmask);
nosigchld = 0;
}
if (setintr)
ignint =
(tpgrp == -1 &&
(t->t_dflg & F_NOINTERRUPT))
|| (gointr && eq(gointr, STRminus));
pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
child++;
if (setintr) {
setintr = 0;
if (ignint) {
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGQUIT, SIG_IGN);
}
else {
(void) signal(SIGINT, vffree);
(void) signal(SIGQUIT, SIG_DFL);
}
if (wanttty >= 0) {
(void) signal(SIGTSTP, SIG_DFL);
(void) signal(SIGTTIN, SIG_DFL);
(void) signal(SIGTTOU, SIG_DFL);
}
(void) signal(SIGTERM, parterm);
}
else if (tpgrp == -1 &&
(t->t_dflg & F_NOINTERRUPT)) {
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGQUIT, SIG_IGN);
}
pgetty(wanttty, pgrp);
if (t->t_dflg & F_NOHUP)
(void) signal(SIGHUP, SIG_IGN);
if (t->t_dflg & F_NICE)
(void) setpriority(PRIO_PROCESS, 0, t->t_nice);
}
}
if (pid != 0) {
/*
* It would be better if we could wait for the whole job when we
* knew the last process had been started. Pwait, in fact, does
* wait for the whole job anyway, but this test doesn't really
* express our intentions.
*/
if (didfds == 0 && t->t_dflg & F_PIPEIN) {
(void) close(pipein[0]);
(void) close(pipein[1]);
}
if ((t->t_dflg & F_PIPEOUT) == 0) {
if (nosigchld) {
(void) sigsetmask(csigmask);
nosigchld = 0;
}
if ((t->t_dflg & F_AMPERSAND) == 0)
pwait();
}
break;
}
doio(t, pipein, pipeout);
if (t->t_dflg & F_PIPEOUT) {
(void) close(pipeout[0]);
(void) close(pipeout[1]);
}
/*
* Perform a builtin function. If we are not forked, arrange for
* possible stopping
*/
if (bifunc) {
func(t, bifunc);
if (forked)
exitstat();
break;
}
if (t->t_dtyp != NODE_PAREN) {
doexec(NULL, t);
/* NOTREACHED */
}
/*
* For () commands must put new 0,1,2 in FSH* and recurse
*/
OLDSTD = dcopy(0, FOLDSTD);
SHOUT = dcopy(1, FSHOUT);
SHERR = dcopy(2, FSHERR);
(void) close(SHIN);
SHIN = -1;
didfds = 0;
wanttty = -1;
t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
execute(t->t_dspr, wanttty, NULL, NULL);
exitstat();
case NODE_PIPE:
t->t_dcar->t_dflg |= F_PIPEOUT |
(t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
execute(t->t_dcar, wanttty, pipein, pv);
t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
(F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
if (wanttty > 0)
wanttty = 0; /* got tty already */
execute(t->t_dcdr, wanttty, pv, pipeout);
break;
case NODE_LIST:
if (t->t_dcar) {
t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
execute(t->t_dcar, wanttty, NULL, NULL);
/*
* In strange case of A&B make a new job after A
*/
if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
pendjob();
}
if (t->t_dcdr) {
t->t_dcdr->t_dflg |= t->t_dflg &
(F_NOFORK | F_NOINTERRUPT);
execute(t->t_dcdr, wanttty, NULL, NULL);
}
break;
case NODE_OR:
case NODE_AND:
if (t->t_dcar) {
t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
execute(t->t_dcar, wanttty, NULL, NULL);
if ((getn(value(STRstatus)) == 0) !=
(t->t_dtyp == NODE_AND))
return;
}
if (t->t_dcdr) {
t->t_dcdr->t_dflg |= t->t_dflg &
(F_NOFORK | F_NOINTERRUPT);
execute(t->t_dcdr, wanttty, NULL, NULL);
}
break;
}
/*
* Fall through for all breaks from switch
*
* If there will be no more executions of this command, flush all file
* descriptors. Places that turn on the F_REPEAT bit are responsible for
* doing donefds after the last re-execution
*/
if (didfds && !(t->t_dflg & F_REPEAT))
donefds();
}
static void
vffree(i)
int i;
{
register Char **v;
if ((v = gargv) != NULL) {
gargv = 0;
xfree((ptr_t) v);
}
if ((v = pargv) != NULL) {
pargv = 0;
xfree((ptr_t) v);
}
_exit(i);
}
/*
* Expand and glob the words after an i/o redirection.
* If more than one word is generated, then update the command vector.
*
* This is done differently in all the shells:
* 1. in the bourne shell and ksh globbing is not performed
* 2. Bash/csh say ambiguous
* 3. zsh does i/o to/from all the files
* 4. itcsh concatenates the words.
*
* I don't know what is best to do. I think that Ambiguous is better
* than restructuring the command vector, because the user can get
* unexpected results. In any case, the command vector restructuring
* code is present and the user can choose it by setting noambiguous
*/
static Char *
splicepipe(t, cp)
register struct command *t;
Char *cp; /* word after < or > */
{
Char *blk[2];
if (adrof(STRnoambiguous)) {
Char **pv;
blk[0] = Dfix1(cp); /* expand $ */
blk[1] = NULL;
gflag = 0, tglob(blk);
if (gflag) {
pv = globall(blk);
if (pv == NULL) {
setname(vis_str(blk[0]));
xfree((ptr_t) blk[0]);
stderror(ERR_NAME | ERR_NOMATCH);
}
gargv = NULL;
if (pv[1] != NULL) { /* we need to fix the command vector */
Char **av = blkspl(t->t_dcom, &pv[1]);
xfree((ptr_t) t->t_dcom);
t->t_dcom = av;
}
xfree((ptr_t) blk[0]);
blk[0] = pv[0];
xfree((ptr_t) pv);
}
}
else {
blk[0] = globone(blk[1] = Dfix1(cp), G_ERROR);
xfree((ptr_t) blk[1]);
}
return(blk[0]);
}
/*
* Perform io redirection.
* We may or maynot be forked here.
*/
static void
doio(t, pipein, pipeout)
register struct command *t;
int *pipein, *pipeout;
{
register int fd;
register Char *cp;
register int flags = t->t_dflg;
if (didfds || (flags & F_REPEAT))
return;
if ((flags & F_READ) == 0) {/* F_READ already done */
if (t->t_dlef) {
char tmp[MAXPATHLEN+1];
/*
* so < /dev/std{in,out,err} work
*/
(void) dcopy(SHIN, 0);
(void) dcopy(SHOUT, 1);
(void) dcopy(SHERR, 2);
cp = splicepipe(t, t->t_dlef);
(void) strncpy(tmp, short2str(cp), MAXPATHLEN);
tmp[MAXPATHLEN] = '\0';
xfree((ptr_t) cp);
if ((fd = open(tmp, O_RDONLY)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
(void) dmove(fd, 0);
}
else if (flags & F_PIPEIN) {
(void) close(0);
(void) dup(pipein[0]);
(void) close(pipein[0]);
(void) close(pipein[1]);
}
else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
(void) close(0);
(void) open(_PATH_DEVNULL, O_RDONLY);
}
else {
(void) close(0);
(void) dup(OLDSTD);
(void) ioctl(0, FIONCLEX, NULL);
}
}
if (t->t_drit) {
char tmp[MAXPATHLEN+1];
cp = splicepipe(t, t->t_drit);
(void) strncpy(tmp, short2str(cp), MAXPATHLEN);
tmp[MAXPATHLEN] = '\0';
xfree((ptr_t) cp);
/*
* so > /dev/std{out,err} work
*/
(void) dcopy(SHOUT, 1);
(void) dcopy(SHERR, 2);
if ((flags & F_APPEND) &&
#ifdef O_APPEND
(fd = open(tmp, O_WRONLY | O_APPEND)) >= 0);
#else
(fd = open(tmp, O_WRONLY)) >= 0)
(void) lseek(1, (off_t) 0, L_XTND);
#endif
else {
if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
if (flags & F_APPEND)
stderror(ERR_SYSTEM, tmp, strerror(errno));
chkclob(tmp);
}
if ((fd = creat(tmp, 0666)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
}
(void) dmove(fd, 1);
}
else if (flags & F_PIPEOUT) {
(void) close(1);
(void) dup(pipeout[1]);
}
else {
(void) close(1);
(void) dup(SHOUT);
(void) ioctl(1, FIONCLEX, NULL);
}
(void) close(2);
if (flags & F_STDERR) {
(void) dup(1);
}
else {
(void) dup(SHERR);
(void) ioctl(2, FIONCLEX, NULL);
}
didfds = 1;
}
void
mypipe(pv)
register int *pv;
{
if (pipe(pv) < 0)
goto oops;
pv[0] = dmove(pv[0], -1);
pv[1] = dmove(pv[1], -1);
if (pv[0] >= 0 && pv[1] >= 0)
return;
oops:
stderror(ERR_PIPE);
}
static void
chkclob(cp)
register char *cp;
{
struct stat stb;
if (stat(cp, &stb) < 0)
return;
if (S_ISCHR(stb.st_mode))
return;
stderror(ERR_EXISTS, cp);
}

844
bin/csh/set.c Normal file
View File

@ -0,0 +1,844 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)set.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <stdlib.h>
#ifndef SHORT_STRINGS
#include <string.h>
#endif /* SHORT_STRINGS */
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
static Char *getinx __P((Char *, int *));
static void asx __P((Char *, int, Char *));
static struct varent
*getvx __P((Char *, int));
static Char *xset __P((Char *, Char ***));
static Char *operate __P((int, Char *, Char *));
static void putn1 __P((int));
static struct varent
*madrof __P((Char *, struct varent *));
static void unsetv1 __P((struct varent *));
static void exportpath __P((Char **));
static void balance __P((struct varent *, int, int));
/*
* C Shell
*/
void
/*ARGSUSED*/
doset(v, t)
Char **v;
struct command *t;
{
register Char *p;
Char *vp, op;
Char **vecp;
bool hadsub;
int subscr;
v++;
p = *v++;
if (p == 0) {
prvars();
return;
}
do {
hadsub = 0;
vp = p;
if (letter(*p))
for (; alnum(*p); p++)
continue;
if (vp == p || !letter(*vp))
stderror(ERR_NAME | ERR_VARBEGIN);
if ((p - vp) > MAXVARLEN) {
stderror(ERR_NAME | ERR_VARTOOLONG);
return;
}
if (*p == '[') {
hadsub++;
p = getinx(p, &subscr);
}
if ((op = *p) != '\0') {
*p++ = 0;
if (*p == 0 && *v && **v == '(')
p = *v++;
}
else if (*v && eq(*v, STRequal)) {
op = '=', v++;
if (*v)
p = *v++;
}
if (op && op != '=')
stderror(ERR_NAME | ERR_SYNTAX);
if (eq(p, STRLparen)) {
register Char **e = v;
if (hadsub)
stderror(ERR_NAME | ERR_SYNTAX);
for (;;) {
if (!*e)
stderror(ERR_NAME | ERR_MISSING, ')');
if (**e == ')')
break;
e++;
}
p = *e;
*e = 0;
vecp = saveblk(v);
set1(vp, vecp, &shvhed);
*e = p;
v = e + 1;
}
else if (hadsub)
asx(vp, subscr, Strsave(p));
else
set(vp, Strsave(p));
if (eq(vp, STRpath)) {
exportpath(adrof(STRpath)->vec);
dohash(NULL, NULL);
}
else if (eq(vp, STRhistchars)) {
register Char *pn = value(STRhistchars);
HIST = *pn++;
HISTSUB = *pn;
}
else if (eq(vp, STRuser)) {
Setenv(STRUSER, value(vp));
Setenv(STRLOGNAME, value(vp));
}
else if (eq(vp, STRwordchars)) {
word_chars = value(vp);
}
else if (eq(vp, STRterm))
Setenv(STRTERM, value(vp));
else if (eq(vp, STRhome)) {
register Char *cp;
cp = Strsave(value(vp)); /* get the old value back */
/*
* convert to cononical pathname (possibly resolving symlinks)
*/
cp = dcanon(cp, cp);
set(vp, Strsave(cp)); /* have to save the new val */
/* and now mirror home with HOME */
Setenv(STRHOME, cp);
/* fix directory stack for new tilde home */
dtilde();
xfree((ptr_t) cp);
}
#ifdef FILEC
else if (eq(vp, STRfilec))
filec = 1;
#endif
} while ((p = *v++) != NULL);
}
static Char *
getinx(cp, ip)
register Char *cp;
register int *ip;
{
*ip = 0;
*cp++ = 0;
while (*cp && Isdigit(*cp))
*ip = *ip * 10 + *cp++ - '0';
if (*cp++ != ']')
stderror(ERR_NAME | ERR_SUBSCRIPT);
return (cp);
}
static void
asx(vp, subscr, p)
Char *vp;
int subscr;
Char *p;
{
register struct varent *v = getvx(vp, subscr);
xfree((ptr_t) v->vec[subscr - 1]);
v->vec[subscr - 1] = globone(p, G_APPEND);
}
static struct varent *
getvx(vp, subscr)
Char *vp;
int subscr;
{
register struct varent *v = adrof(vp);
if (v == 0)
udvar(vp);
if (subscr < 1 || subscr > blklen(v->vec))
stderror(ERR_NAME | ERR_RANGE);
return (v);
}
void
/*ARGSUSED*/
dolet(v, t)
Char **v;
struct command *t;
{
register Char *p;
Char *vp, c, op;
bool hadsub;
int subscr;
v++;
p = *v++;
if (p == 0) {
prvars();
return;
}
do {
hadsub = 0;
vp = p;
if (letter(*p))
for (; alnum(*p); p++)
continue;
if (vp == p || !letter(*vp))
stderror(ERR_NAME | ERR_VARBEGIN);
if ((p - vp) > MAXVARLEN)
stderror(ERR_NAME | ERR_VARTOOLONG);
if (*p == '[') {
hadsub++;
p = getinx(p, &subscr);
}
if (*p == 0 && *v)
p = *v++;
if ((op = *p) != '\0')
*p++ = 0;
else
stderror(ERR_NAME | ERR_ASSIGN);
if (*p == '\0' && *v == NULL)
stderror(ERR_NAME | ERR_ASSIGN);
vp = Strsave(vp);
if (op == '=') {
c = '=';
p = xset(p, &v);
}
else {
c = *p++;
if (any("+-", c)) {
if (c != op || *p)
stderror(ERR_NAME | ERR_UNKNOWNOP);
p = Strsave(STR1);
}
else {
if (any("<>", op)) {
if (c != op)
stderror(ERR_NAME | ERR_UNKNOWNOP);
c = *p++;
stderror(ERR_NAME | ERR_SYNTAX);
}
if (c != '=')
stderror(ERR_NAME | ERR_UNKNOWNOP);
p = xset(p, &v);
}
}
if (op == '=')
if (hadsub)
asx(vp, subscr, p);
else
set(vp, p);
else if (hadsub) {
struct varent *gv = getvx(vp, subscr);
asx(vp, subscr, operate(op, gv->vec[subscr - 1], p));
}
else
set(vp, operate(op, value(vp), p));
if (eq(vp, STRpath)) {
exportpath(adrof(STRpath)->vec);
dohash(NULL, NULL);
}
xfree((ptr_t) vp);
if (c != '=')
xfree((ptr_t) p);
} while ((p = *v++) != NULL);
}
static Char *
xset(cp, vp)
Char *cp, ***vp;
{
register Char *dp;
if (*cp) {
dp = Strsave(cp);
--(*vp);
xfree((ptr_t) ** vp);
**vp = dp;
}
return (putn(expr(vp)));
}
static Char *
operate(op, vp, p)
int op;
Char *vp, *p;
{
Char opr[2];
Char *vec[5];
register Char **v = vec;
Char **vecp = v;
register int i;
if (op != '=') {
if (*vp)
*v++ = vp;
opr[0] = op;
opr[1] = 0;
*v++ = opr;
if (op == '<' || op == '>')
*v++ = opr;
}
*v++ = p;
*v++ = 0;
i = expr(&vecp);
if (*vecp)
stderror(ERR_NAME | ERR_EXPRESSION);
return (putn(i));
}
static Char *putp;
Char *
putn(n)
register int n;
{
int num;
static Char number[15];
putp = number;
if (n < 0) {
n = -n;
*putp++ = '-';
}
num = 2; /* confuse lint */
if (sizeof(int) == num && ((unsigned int) n) == 0x8000) {
*putp++ = '3';
n = 2768;
#ifdef pdp11
}
#else
}
else {
num = 4; /* confuse lint */
if (sizeof(int) == num && ((unsigned int) n) == 0x80000000) {
*putp++ = '2';
n = 147483648;
}
}
#endif
putn1(n);
*putp = 0;
return (Strsave(number));
}
static void
putn1(n)
register int n;
{
if (n > 9)
putn1(n / 10);
*putp++ = n % 10 + '0';
}
int
getn(cp)
register Char *cp;
{
register int n;
int sign;
sign = 0;
if (cp[0] == '+' && cp[1])
cp++;
if (*cp == '-') {
sign++;
cp++;
if (!Isdigit(*cp))
stderror(ERR_NAME | ERR_BADNUM);
}
n = 0;
while (Isdigit(*cp))
n = n * 10 + *cp++ - '0';
if (*cp)
stderror(ERR_NAME | ERR_BADNUM);
return (sign ? -n : n);
}
Char *
value1(var, head)
Char *var;
struct varent *head;
{
register struct varent *vp;
vp = adrof1(var, head);
return (vp == 0 || vp->vec[0] == 0 ? STRNULL : vp->vec[0]);
}
static struct varent *
madrof(pat, vp)
Char *pat;
register struct varent *vp;
{
register struct varent *vp1;
for (; vp; vp = vp->v_right) {
if (vp->v_left && (vp1 = madrof(pat, vp->v_left)))
return vp1;
if (Gmatch(vp->v_name, pat))
return vp;
}
return vp;
}
struct varent *
adrof1(name, v)
register Char *name;
register struct varent *v;
{
register cmp;
v = v->v_left;
while (v && ((cmp = *name - *v->v_name) ||
(cmp = Strcmp(name, v->v_name))))
if (cmp < 0)
v = v->v_left;
else
v = v->v_right;
return v;
}
/*
* The caller is responsible for putting value in a safe place
*/
void
set(var, val)
Char *var, *val;
{
register Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
vec[0] = val;
vec[1] = 0;
set1(var, vec, &shvhed);
}
void
set1(var, vec, head)
Char *var, **vec;
struct varent *head;
{
register Char **oldv = vec;
gflag = 0;
tglob(oldv);
if (gflag) {
vec = globall(oldv);
if (vec == 0) {
blkfree(oldv);
stderror(ERR_NAME | ERR_NOMATCH);
return;
}
blkfree(oldv);
gargv = 0;
}
setq(var, vec, head);
}
void
setq(name, vec, p)
Char *name, **vec;
register struct varent *p;
{
register struct varent *c;
register f;
f = 0; /* tree hangs off the header's left link */
while ((c = p->v_link[f]) != NULL) {
if ((f = *name - *c->v_name) == 0 &&
(f = Strcmp(name, c->v_name)) == 0) {
blkfree(c->vec);
goto found;
}
p = c;
f = f > 0;
}
p->v_link[f] = c = (struct varent *) xmalloc((size_t) sizeof(struct varent));
c->v_name = Strsave(name);
c->v_bal = 0;
c->v_left = c->v_right = 0;
c->v_parent = p;
balance(p, f, 0);
found:
trim(c->vec = vec);
}
void
/*ARGSUSED*/
unset(v, t)
Char **v;
struct command *t;
{
unset1(v, &shvhed);
#ifdef FILEC
if (adrof(STRfilec) == 0)
filec = 0;
#endif
if (adrof(STRhistchars) == 0) {
HIST = '!';
HISTSUB = '^';
}
if (adrof(STRwordchars) == 0)
word_chars = STR_WORD_CHARS;
}
void
unset1(v, head)
register Char *v[];
struct varent *head;
{
register struct varent *vp;
register int cnt;
while (*++v) {
cnt = 0;
while ((vp = madrof(*v, head->v_left)) != NULL)
unsetv1(vp), cnt++;
if (cnt == 0)
setname(vis_str(*v));
}
}
void
unsetv(var)
Char *var;
{
register struct varent *vp;
if ((vp = adrof1(var, &shvhed)) == 0)
udvar(var);
unsetv1(vp);
}
static void
unsetv1(p)
register struct varent *p;
{
register struct varent *c, *pp;
register f;
/*
* Free associated memory first to avoid complications.
*/
blkfree(p->vec);
xfree((ptr_t) p->v_name);
/*
* If p is missing one child, then we can move the other into where p is.
* Otherwise, we find the predecessor of p, which is guaranteed to have no
* right child, copy it into p, and move it's left child into it.
*/
if (p->v_right == 0)
c = p->v_left;
else if (p->v_left == 0)
c = p->v_right;
else {
for (c = p->v_left; c->v_right; c = c->v_right)
continue;
p->v_name = c->v_name;
p->vec = c->vec;
p = c;
c = p->v_left;
}
/*
* Move c into where p is.
*/
pp = p->v_parent;
f = pp->v_right == p;
if ((pp->v_link[f] = c) != NULL)
c->v_parent = pp;
/*
* Free the deleted node, and rebalance.
*/
xfree((ptr_t) p);
balance(pp, f, 1);
}
void
setNS(cp)
Char *cp;
{
set(cp, Strsave(STRNULL));
}
void
/*ARGSUSED*/
shift(v, t)
Char **v;
struct command *t;
{
register struct varent *argv;
register Char *name;
v++;
name = *v;
if (name == 0)
name = STRargv;
else
(void) strip(name);
argv = adrof(name);
if (argv == 0)
udvar(name);
if (argv->vec[0] == 0)
stderror(ERR_NAME | ERR_NOMORE);
lshift(argv->vec, 1);
}
static void
exportpath(val)
Char **val;
{
Char exppath[BUFSIZ];
exppath[0] = 0;
if (val)
while (*val) {
if (Strlen(*val) + Strlen(exppath) + 2 > BUFSIZ) {
(void) fprintf(csherr,
"Warning: ridiculously long PATH truncated\n");
break;
}
if ((**val != '/' || **val == '\0') && (euid == 0 || uid == 0))
(void) fprintf(csherr,
"Warning: exported path contains relative components.\n");
(void) Strcat(exppath, *val++);
if (*val == 0 || eq(*val, STRRparen))
break;
(void) Strcat(exppath, STRcolon);
}
Setenv(STRPATH, exppath);
}
#ifndef lint
/*
* Lint thinks these have null effect
*/
/* macros to do single rotations on node p */
#define rright(p) (\
t = (p)->v_left,\
(t)->v_parent = (p)->v_parent,\
((p)->v_left = t->v_right) ? (t->v_right->v_parent = (p)) : 0,\
(t->v_right = (p))->v_parent = t,\
(p) = t)
#define rleft(p) (\
t = (p)->v_right,\
(t)->v_parent = (p)->v_parent,\
((p)->v_right = t->v_left) ? (t->v_left->v_parent = (p)) : 0,\
(t->v_left = (p))->v_parent = t,\
(p) = t)
#else
struct varent *
rleft(p)
struct varent *p;
{
return (p);
}
struct varent *
rright(p)
struct varent *p;
{
return (p);
}
#endif /* ! lint */
/*
* Rebalance a tree, starting at p and up.
* F == 0 means we've come from p's left child.
* D == 1 means we've just done a delete, otherwise an insert.
*/
static void
balance(p, f, d)
register struct varent *p;
register int f, d;
{
register struct varent *pp;
#ifndef lint
register struct varent *t; /* used by the rotate macros */
#endif
register ff;
/*
* Ok, from here on, p is the node we're operating on; pp is it's parent; f
* is the branch of p from which we have come; ff is the branch of pp which
* is p.
*/
for (; (pp = p->v_parent) != NULL; p = pp, f = ff) {
ff = pp->v_right == p;
if (f ^ d) { /* right heavy */
switch (p->v_bal) {
case -1: /* was left heavy */
p->v_bal = 0;
break;
case 0: /* was balanced */
p->v_bal = 1;
break;
case 1: /* was already right heavy */
switch (p->v_right->v_bal) {
case 1: /* sigle rotate */
pp->v_link[ff] = rleft(p);
p->v_left->v_bal = 0;
p->v_bal = 0;
break;
case 0: /* single rotate */
pp->v_link[ff] = rleft(p);
p->v_left->v_bal = 1;
p->v_bal = -1;
break;
case -1: /* double rotate */
(void) rright(p->v_right);
pp->v_link[ff] = rleft(p);
p->v_left->v_bal =
p->v_bal < 1 ? 0 : -1;
p->v_right->v_bal =
p->v_bal > -1 ? 0 : 1;
p->v_bal = 0;
break;
}
break;
}
}
else { /* left heavy */
switch (p->v_bal) {
case 1: /* was right heavy */
p->v_bal = 0;
break;
case 0: /* was balanced */
p->v_bal = -1;
break;
case -1: /* was already left heavy */
switch (p->v_left->v_bal) {
case -1: /* single rotate */
pp->v_link[ff] = rright(p);
p->v_right->v_bal = 0;
p->v_bal = 0;
break;
case 0: /* signle rotate */
pp->v_link[ff] = rright(p);
p->v_right->v_bal = -1;
p->v_bal = 1;
break;
case 1: /* double rotate */
(void) rleft(p->v_left);
pp->v_link[ff] = rright(p);
p->v_left->v_bal =
p->v_bal < 1 ? 0 : -1;
p->v_right->v_bal =
p->v_bal > -1 ? 0 : 1;
p->v_bal = 0;
break;
}
break;
}
}
/*
* If from insert, then we terminate when p is balanced. If from
* delete, then we terminate when p is unbalanced.
*/
if ((p->v_bal == 0) ^ d)
break;
}
}
void
plist(p)
register struct varent *p;
{
register struct varent *c;
register len;
if (setintr)
(void) sigsetmask(sigblock((sigset_t) 0) & ~sigmask(SIGINT));
for (;;) {
while (p->v_left)
p = p->v_left;
x:
if (p->v_parent == 0) /* is it the header? */
return;
len = blklen(p->vec);
(void) fprintf(cshout, "%s\t", short2str(p->v_name));
if (len != 1)
(void) fputc('(', cshout);
blkpr(cshout, p->vec);
if (len != 1)
(void) fputc(')', cshout);
(void) fputc('\n', cshout);
if (p->v_right) {
p = p->v_right;
continue;
}
do {
c = p;
p = p->v_parent;
} while (p->v_right == c);
goto x;
}
}

467
bin/csh/str.c Normal file
View File

@ -0,0 +1,467 @@
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)str.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#define MALLOC_INCR 128
/*
* tc.str.c: Short string package
* This has been a lesson of how to write buggy code!
*/
#include <sys/types.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include <vis.h>
#include "csh.h"
#include "extern.h"
#ifdef SHORT_STRINGS
Char **
blk2short(src)
register char **src;
{
size_t n;
register Char **sdst, **dst;
/*
* Count
*/
for (n = 0; src[n] != NULL; n++)
continue;
sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
for (; *src != NULL; src++)
*dst++ = SAVE(*src);
*dst = NULL;
return (sdst);
}
char **
short2blk(src)
register Char **src;
{
size_t n;
register char **sdst, **dst;
/*
* Count
*/
for (n = 0; src[n] != NULL; n++)
continue;
sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
for (; *src != NULL; src++)
*dst++ = strsave(short2str(*src));
*dst = NULL;
return (sdst);
}
Char *
str2short(src)
register char *src;
{
static Char *sdst;
static size_t dstsize = 0;
register Char *dst, *edst;
if (src == NULL)
return (NULL);
if (sdst == (NULL)) {
dstsize = MALLOC_INCR;
sdst = (Char *) xmalloc((size_t) dstsize * sizeof(Char));
}
dst = sdst;
edst = &dst[dstsize];
while (*src) {
*dst++ = (Char) ((unsigned char) *src++);
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = (Char *) xrealloc((ptr_t) sdst,
(size_t) dstsize * sizeof(Char));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
}
*dst = 0;
return (sdst);
}
char *
short2str(src)
register Char *src;
{
static char *sdst = NULL;
static size_t dstsize = 0;
register char *dst, *edst;
if (src == NULL)
return (NULL);
if (sdst == NULL) {
dstsize = MALLOC_INCR;
sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
}
dst = sdst;
edst = &dst[dstsize];
while (*src) {
*dst++ = (char) *src++;
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = (char *) xrealloc((ptr_t) sdst,
(size_t) dstsize * sizeof(char));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
}
*dst = 0;
return (sdst);
}
Char *
s_strcpy(dst, src)
register Char *dst, *src;
{
register Char *sdst;
sdst = dst;
while ((*dst++ = *src++) != '\0')
continue;
return (sdst);
}
Char *
s_strncpy(dst, src, n)
register Char *dst, *src;
register size_t n;
{
register Char *sdst;
if (n == 0)
return(dst);
sdst = dst;
do
if ((*dst++ = *src++) == '\0') {
while (--n != 0)
*dst++ = '\0';
return(sdst);
}
while (--n != 0);
return (sdst);
}
Char *
s_strcat(dst, src)
register Char *dst, *src;
{
register short *sdst;
sdst = dst;
while (*dst++)
continue;
--dst;
while ((*dst++ = *src++) != '\0')
continue;
return (sdst);
}
#ifdef NOTUSED
Char *
s_strncat(dst, src, n)
register Char *dst, *src;
register size_t n;
{
register Char *sdst;
if (n == 0)
return (dst);
sdst = dst;
while (*dst++)
continue;
--dst;
do
if ((*dst++ = *src++) == '\0')
return(sdst);
while (--n != 0)
continue;
*dst = '\0';
return (sdst);
}
#endif
Char *
s_strchr(str, ch)
register Char *str;
int ch;
{
do
if (*str == ch)
return (str);
while (*str++);
return (NULL);
}
Char *
s_strrchr(str, ch)
register Char *str;
int ch;
{
register Char *rstr;
rstr = NULL;
do
if (*str == ch)
rstr = str;
while (*str++);
return (rstr);
}
size_t
s_strlen(str)
register Char *str;
{
register size_t n;
for (n = 0; *str++; n++)
continue;
return (n);
}
int
s_strcmp(str1, str2)
register Char *str1, *str2;
{
for (; *str1 && *str1 == *str2; str1++, str2++)
continue;
/*
* The following case analysis is necessary so that characters which look
* negative collate low against normal characters but high against the
* end-of-string NUL.
*/
if (*str1 == '\0' && *str2 == '\0')
return (0);
else if (*str1 == '\0')
return (-1);
else if (*str2 == '\0')
return (1);
else
return (*str1 - *str2);
}
int
s_strncmp(str1, str2, n)
register Char *str1, *str2;
register size_t n;
{
if (n == 0)
return (0);
do {
if (*str1 != *str2) {
/*
* The following case analysis is necessary so that characters
* which look negative collate low against normal characters
* but high against the end-of-string NUL.
*/
if (*str1 == '\0')
return (-1);
else if (*str2 == '\0')
return (1);
else
return (*str1 - *str2);
break;
}
if (*str1 == '\0')
return(0);
str1++, str2++;
} while (--n != 0);
return(0);
}
Char *
s_strsave(s)
register Char *s;
{
Char *n;
register Char *p;
if (s == 0)
s = STRNULL;
for (p = s; *p++;)
continue;
n = p = (Char *) xmalloc((size_t) ((p - s) * sizeof(Char)));
while ((*p++ = *s++) != '\0')
continue;
return (n);
}
Char *
s_strspl(cp, dp)
Char *cp, *dp;
{
Char *ep;
register Char *p, *q;
if (!cp)
cp = STRNULL;
if (!dp)
dp = STRNULL;
for (p = cp; *p++;)
continue;
for (q = dp; *q++;)
continue;
ep = (Char *) xmalloc((size_t)
(((p - cp) + (q - dp) - 1) * sizeof(Char)));
for (p = ep, q = cp; (*p++ = *q++) != '\0';)
continue;
for (p--, q = dp; (*p++ = *q++) != '\0';)
continue;
return (ep);
}
Char *
s_strend(cp)
register Char *cp;
{
if (!cp)
return (cp);
while (*cp)
cp++;
return (cp);
}
Char *
s_strstr(s, t)
register Char *s, *t;
{
do {
register Char *ss = s;
register Char *tt = t;
do
if (*tt == '\0')
return (s);
while (*ss++ == *tt++);
} while (*s++ != '\0');
return (NULL);
}
#endif /* SHORT_STRINGS */
char *
short2qstr(src)
register Char *src;
{
static char *sdst = NULL;
static size_t dstsize = 0;
register char *dst, *edst;
if (src == NULL)
return (NULL);
if (sdst == NULL) {
dstsize = MALLOC_INCR;
sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
}
dst = sdst;
edst = &dst[dstsize];
while (*src) {
if (*src & QUOTE) {
*dst++ = '\\';
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = (char *) xrealloc((ptr_t) sdst,
(size_t) dstsize * sizeof(char));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
}
*dst++ = (char) *src++;
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = (char *) xrealloc((ptr_t) sdst,
(size_t) dstsize * sizeof(char));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
}
*dst = 0;
return (sdst);
}
/*
* XXX: Should we worry about QUOTE'd chars?
*/
char *
vis_str(cp)
Char *cp;
{
static char *sdst = NULL;
static size_t dstsize = 0;
size_t n;
Char *dp;
if (cp == NULL)
return (NULL);
for (dp = cp; *dp++;)
continue;
n = ((dp - cp) << 2) + 1; /* 4 times + NULL */
if (dstsize < n) {
sdst = (char *) (dstsize ?
xrealloc(sdst, (size_t) n * sizeof(char)) :
xmalloc((size_t) n * sizeof(char)));
dstsize = n;
}
/*
* XXX: When we are in AsciiOnly we want all characters >= 0200 to
* be encoded, but currently there is no way in vis to do that.
*/
(void) strvis(sdst, short2str(cp), VIS_NOSLASH);
return (sdst);
}

316
bin/csh/time.c Normal file
View File

@ -0,0 +1,316 @@
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
/*
* C Shell - routines handling process timing and niceing
*/
static void pdeltat __P((struct timeval *, struct timeval *));
void
settimes()
{
struct rusage ruch;
(void) gettimeofday(&time0, NULL);
(void) getrusage(RUSAGE_SELF, &ru0);
(void) getrusage(RUSAGE_CHILDREN, &ruch);
ruadd(&ru0, &ruch);
}
/*
* dotime is only called if it is truly a builtin function and not a
* prefix to another command
*/
void
/*ARGSUSED*/
dotime(v, t)
Char **v;
struct command *t;
{
struct timeval timedol;
struct rusage ru1, ruch;
(void) getrusage(RUSAGE_SELF, &ru1);
(void) getrusage(RUSAGE_CHILDREN, &ruch);
ruadd(&ru1, &ruch);
(void) gettimeofday(&timedol, NULL);
prusage(&ru0, &ru1, &timedol, &time0);
}
/*
* donice is only called when it on the line by itself or with a +- value
*/
void
/*ARGSUSED*/
donice(v, t)
Char **v;
struct command *t;
{
register Char *cp;
int nval = 0;
v++, cp = *v++;
if (cp == 0)
nval = 4;
else if (*v == 0 && any("+-", cp[0]))
nval = getn(cp);
(void) setpriority(PRIO_PROCESS, 0, nval);
}
void
ruadd(ru, ru2)
register struct rusage *ru, *ru2;
{
tvadd(&ru->ru_utime, &ru2->ru_utime);
tvadd(&ru->ru_stime, &ru2->ru_stime);
if (ru2->ru_maxrss > ru->ru_maxrss)
ru->ru_maxrss = ru2->ru_maxrss;
ru->ru_ixrss += ru2->ru_ixrss;
ru->ru_idrss += ru2->ru_idrss;
ru->ru_isrss += ru2->ru_isrss;
ru->ru_minflt += ru2->ru_minflt;
ru->ru_majflt += ru2->ru_majflt;
ru->ru_nswap += ru2->ru_nswap;
ru->ru_inblock += ru2->ru_inblock;
ru->ru_oublock += ru2->ru_oublock;
ru->ru_msgsnd += ru2->ru_msgsnd;
ru->ru_msgrcv += ru2->ru_msgrcv;
ru->ru_nsignals += ru2->ru_nsignals;
ru->ru_nvcsw += ru2->ru_nvcsw;
ru->ru_nivcsw += ru2->ru_nivcsw;
}
void
prusage(r0, r1, e, b)
register struct rusage *r0, *r1;
struct timeval *e, *b;
{
register time_t t =
(r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
(r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
(r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
(r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
register char *cp;
register long i;
register struct varent *vp = adrof(STRtime);
int ms =
(e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000;
cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
if (vp && vp->vec[0] && vp->vec[1])
cp = short2str(vp->vec[1]);
for (; *cp; cp++)
if (*cp != '%')
(void) fputc(*cp, cshout);
else if (cp[1])
switch (*++cp) {
case 'U': /* user CPU time used */
pdeltat(&r1->ru_utime, &r0->ru_utime);
break;
case 'S': /* system CPU time used */
pdeltat(&r1->ru_stime, &r0->ru_stime);
break;
case 'E': /* elapsed (wall-clock) time */
pcsecs((long) ms);
break;
case 'P': /* percent time spent running */
/* check if it did not run at all */
i = (ms == 0) ? 0 : (t * 1000 / ms);
/* nn.n% */
(void) fprintf(cshout, "%ld.%01ld%%", i / 10, i % 10);
break;
case 'W': /* number of swaps */
i = r1->ru_nswap - r0->ru_nswap;
(void) fprintf(cshout, "%ld", i);
break;
case 'X': /* (average) shared text size */
(void) fprintf(cshout, "%ld", t == 0 ? 0L :
(r1->ru_ixrss - r0->ru_ixrss) / t);
break;
case 'D': /* (average) unshared data size */
(void) fprintf(cshout, "%ld", t == 0 ? 0L :
(r1->ru_idrss + r1->ru_isrss -
(r0->ru_idrss + r0->ru_isrss)) / t);
break;
case 'K': /* (average) total data memory used */
(void) fprintf(cshout, "%ld", t == 0 ? 0L :
((r1->ru_ixrss + r1->ru_isrss + r1->ru_idrss) -
(r0->ru_ixrss + r0->ru_idrss + r0->ru_isrss)) / t);
break;
case 'M': /* max. Resident Set Size */
(void) fprintf(cshout, "%ld", r1->ru_maxrss / 2L);
break;
case 'F': /* page faults */
(void) fprintf(cshout, "%ld", r1->ru_majflt - r0->ru_majflt);
break;
case 'R': /* page reclaims */
(void) fprintf(cshout, "%ld", r1->ru_minflt - r0->ru_minflt);
break;
case 'I': /* FS blocks in */
(void) fprintf(cshout, "%ld", r1->ru_inblock - r0->ru_inblock);
break;
case 'O': /* FS blocks out */
(void) fprintf(cshout, "%ld", r1->ru_oublock - r0->ru_oublock);
break;
case 'r': /* socket messages recieved */
(void) fprintf(cshout, "%ld", r1->ru_msgrcv - r0->ru_msgrcv);
break;
case 's': /* socket messages sent */
(void) fprintf(cshout, "%ld", r1->ru_msgsnd - r0->ru_msgsnd);
break;
case 'k': /* number of signals recieved */
(void) fprintf(cshout, "%ld", r1->ru_nsignals-r0->ru_nsignals);
break;
case 'w': /* num. voluntary context switches (waits) */
(void) fprintf(cshout, "%ld", r1->ru_nvcsw - r0->ru_nvcsw);
break;
case 'c': /* num. involuntary context switches */
(void) fprintf(cshout, "%ld", r1->ru_nivcsw - r0->ru_nivcsw);
break;
}
(void) fputc('\n', cshout);
}
static void
pdeltat(t1, t0)
struct timeval *t1, *t0;
{
struct timeval td;
tvsub(&td, t1, t0);
(void) fprintf(cshout, "%d.%01d", td.tv_sec, td.tv_usec / 100000);
}
void
tvadd(tsum, t0)
struct timeval *tsum, *t0;
{
tsum->tv_sec += t0->tv_sec;
tsum->tv_usec += t0->tv_usec;
if (tsum->tv_usec > 1000000)
tsum->tv_sec++, tsum->tv_usec -= 1000000;
}
void
tvsub(tdiff, t1, t0)
struct timeval *tdiff, *t1, *t0;
{
tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
if (tdiff->tv_usec < 0)
tdiff->tv_sec--, tdiff->tv_usec += 1000000;
}
#define P2DIG(i) (void) fprintf(cshout, "%d%d", (i) / 10, (i) % 10)
void
psecs(l)
long l;
{
register int i;
i = l / 3600;
if (i) {
(void) fprintf(cshout, "%d:", i);
i = l % 3600;
P2DIG(i / 60);
goto minsec;
}
i = l;
(void) fprintf(cshout, "%d", i / 60);
minsec:
i %= 60;
(void) fputc(':', cshout);
P2DIG(i);
}
void
pcsecs(l) /* PWP: print mm:ss.dd, l is in sec*100 */
long l;
{
register int i;
i = l / 360000;
if (i) {
(void) fprintf(cshout, "%d:", i);
i = (l % 360000) / 100;
P2DIG(i / 60);
goto minsec;
}
i = l / 100;
(void) fprintf(cshout, "%d", i / 60);
minsec:
i %= 60;
(void) fputc(':', cshout);
P2DIG(i);
(void) fputc('.', cshout);
P2DIG((int) (l % 100));
}

8
bin/date/Makefile Normal file
View File

@ -0,0 +1,8 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= date
SRCS= date.c netdate.c
DPADD= ${LIBUTIL}
LDADD= -lutil
.include <bsd.prog.mk>

230
bin/date/date.1 Normal file
View File

@ -0,0 +1,230 @@
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)date.1 8.2 (Berkeley) 11/17/93
.\"
.Dd November 17, 1993
.Dt DATE 1
.Os
.Sh NAME
.Nm date
.Nd display or set date and time
.Sh SYNOPSIS
.Nm date
.Op Fl d Ar dst
.Op Fl r Ar seconds
.Op Fl t Ar minutes_west
.Op Fl nu
.Op Cm + Ns Ar format
.Op [yy[mm[dd[hh]]]]mm[\&.ss]
.Sh DESCRIPTION
.Nm Date
displays the current date and time when invoked without arguments.
Providing arguments will format the date and time in a user-defined
way or set the date.
Only the superuser may set the date.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl d
Set the kernel's value for daylight savings time.
If
.Ar dst
is non-zero, future calls
to
.Xr gettimeofday 2
will return a non-zero
.Ql tz_dsttime .
.It Fl n
The utility
.Xr timed 8
is used to synchronize the clocks on groups of machines.
By default, if
.Xr timed
is running,
.Nm date
will set the time on all of the machines in the local group.
The
.Fl n
option stops
.Nm date
from setting the time for other than the current machine.
.It Fl r
Print out the date and time in
.Ar seconds
from the Epoch.
.It Fl t
Set the kernel's value for minutes west of
.Tn GMT .
.Ar Minutes_west
specifies the number of minutes returned in
.Ql tz_minuteswest
by future calls to
.Xr gettimeofday 2 .
.It Fl u
Display or set the date in
.Tn UCT
(universal) time.
.El
.Pp
An operand with a leading plus (``+'') sign signals a user-defined format
string which specifies the format in which to display the date and time.
The format string may contain any of the conversion specifications described
in the
.Xr strftime 3
manual page, as well as any arbitrary text.
The format string for the default display is:
.Bd -literal -offset indent
``%a %b %e %H:%M:%S %Z n''.
.Ed
.Pp
If an operand does not have a leading plus sign, it is interpreted as
a value for setting the system's notion of the current date and time.
The canonical representation for setting the date and time is:
.Pp
.Bl -tag -width Ds -compact -offset indent
.It Ar yy
Year in abbreviated form (.e.g 89 for 1989).
.It Ar mm
Numeric month.
A number from 1 to 12.
.It Ar dd
Day, a number from 1 to 31.
.It Ar hh
Hour, a number from 0 to 23.
.It Ar mm
Minutes, a number from 0 to 59.
.It Ar .ss
Seconds, a number from 0 to 61 (59 plus a a maximum of two leap seconds).
.El
.Pp
Everything but the minutes is optional.
.Pp
Time changes for Daylight Saving and Standard time and leap seconds
and years are handled automatically.
.Sh EXAMPLES
The command:
.Bd -literal -offset indent
date ``+DATE: %m/%d/%y%nTIME: %H:%M:0n''
.Ed
.Pp
will display:
.Bd -literal -offset indent
DATE: 11/21/87
TIME: 13:36:16
.Ed
.Pp
The command:
.Bd -literal -offset indent
date 8506131627
.Ed
.Pp
sets the date to
.Dq Li "June 13, 1985, 4:27 PM" .
.Pp
The command:
.Bd -literal -offset indent
date 1432
.Ed
.Pp
sets the time to
.Li "2:32 PM" ,
without modifying the date.
.Sh ENVIRONMENTAL VARIABLES
The following environment variables affect the execution of
.Nm date :
.Bl -tag -width TZ
.It Ev TZ
The timezone to use when displaying dates.
See
.Xr environ 7
for more information.
.El
.Sh FILES
.Bl -tag -width /var/log/messages -compact
.It Pa /var/log/wtmp
A record of date resets and time changes.
.It Pa /var/log/messages
A record of the user setting the time.
.El
.Sh SEE ALSO
.Xr gettimeofday 2 ,
.Xr strftime 3 ,
.Xr utmp 5 ,
.Xr timed 8
.Rs
.%T "TSP: The Time Synchronization Protocol for UNIX 4.3BSD"
.%A R. Gusella
.%A S. Zatti
.Re
.Sh DIAGNOSTICS
Exit status is 0 on success, 1 if unable to set the date, and 2
if able to set the local date, but unable to set it globally.
.Pp
Occasionally, when
.Xr timed
synchronizes the time on many hosts, the setting of a new time value may
require more than a few seconds.
On these occasions,
.Nm date
prints:
.Ql Network time being set .
The message
.Ql Communication error with timed
occurs when the communication
between
.Nm date
and
.Xr timed
fails.
.Sh BUGS
The system attempts to keep the date in a format closely compatible
with
.Tn VMS .
.Tn VMS ,
however, uses local time (rather than
.Tn GMT )
and does not understand daylight-savings time.
Thus, if you use both
.Tn UNIX
and
.Tn VMS ,
.Tn VMS
will be running on
.Tn GMT .
.Sh STANDARDS
The
.Nm date
command is expected to be compatible with
.St -p1003.2 .

237
bin/date/date.c Normal file
View File

@ -0,0 +1,237 @@
/*
* Copyright (c) 1985, 1987, 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)date.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "extern.h"
time_t tval;
int retval, nflag;
static void setthetime __P((char *));
static void badformat __P((void));
static void usage __P((void));
int logwtmp __P((char *, char *, char *));
int
main(argc, argv)
int argc;
char **argv;
{
extern int optind;
extern char *optarg;
struct timezone tz;
int ch, rflag;
char *format, buf[1024];
tz.tz_dsttime = tz.tz_minuteswest = 0;
rflag = 0;
while ((ch = getopt(argc, argv, "d:nr:ut:")) != EOF)
switch((char)ch) {
case 'd': /* daylight savings time */
tz.tz_dsttime = atoi(optarg) ? 1 : 0;
break;
case 'n': /* don't set network */
nflag = 1;
break;
case 'r': /* user specified seconds */
rflag = 1;
tval = atol(optarg);
break;
case 'u': /* do everything in GMT */
(void)setenv("TZ", "GMT0", 1);
break;
case 't': /* minutes west of GMT */
/* error check; don't allow "PST" */
if (isdigit(*optarg)) {
tz.tz_minuteswest = atoi(optarg);
break;
}
/* FALLTHROUGH */
default:
usage();
}
argc -= optind;
argv += optind;
/*
* If -d or -t, set the timezone or daylight savings time; this
* doesn't belong here, there kernel should not know about either.
*/
if ((tz.tz_minuteswest || tz.tz_dsttime) &&
settimeofday(NULL, &tz))
err(1, "settimeofday");
if (!rflag && time(&tval) == -1)
err(1, "time");
format = "%a %b %e %H:%M:%S %Z %Y\n";
/* allow the operands in any order */
if (*argv && **argv == '+') {
format = *argv + 1;
++argv;
}
if (*argv) {
setthetime(*argv);
++argv;
}
if (*argv && **argv == '+')
format = *argv + 1;
(void)strftime(buf, sizeof(buf), format, localtime(&tval));
(void)printf("%s", buf);
exit(retval);
}
#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
void
setthetime(p)
register char *p;
{
register struct tm *lt;
struct timeval tv;
char *dot, *t;
for (t = p, dot = NULL; *t; ++t) {
if (isdigit(*t))
continue;
if (*t == '.' && dot == NULL) {
dot = t;
continue;
}
badformat();
}
lt = localtime(&tval);
if (dot != NULL) { /* .ss */
*dot++ = '\0';
if (strlen(dot) != 2)
badformat();
lt->tm_sec = ATOI2(dot);
if (lt->tm_sec > 61)
badformat();
} else
lt->tm_sec = 0;
switch (strlen(p)) {
case 10: /* yy */
lt->tm_year = ATOI2(p);
if (lt->tm_year < 69) /* hack for 2000 ;-} */
lt->tm_year += 100;
/* FALLTHROUGH */
case 8: /* mm */
lt->tm_mon = ATOI2(p);
if (lt->tm_mon > 12)
badformat();
--lt->tm_mon; /* time struct is 0 - 11 */
/* FALLTHROUGH */
case 6: /* dd */
lt->tm_mday = ATOI2(p);
if (lt->tm_mday > 31)
badformat();
/* FALLTHROUGH */
case 4: /* hh */
lt->tm_hour = ATOI2(p);
if (lt->tm_hour > 23)
badformat();
/* FALLTHROUGH */
case 2: /* mm */
lt->tm_min = ATOI2(p);
if (lt->tm_min > 59)
badformat();
break;
default:
badformat();
}
/* convert broken-down time to GMT clock time */
if ((tval = mktime(lt)) == -1)
badformat();
/* set the time */
if (nflag || netsettime(tval)) {
logwtmp("|", "date", "");
tv.tv_sec = tval;
tv.tv_usec = 0;
if (settimeofday(&tv, NULL)) {
perror("date: settimeofday");
exit(1);
}
logwtmp("{", "date", "");
}
if ((p = getlogin()) == NULL)
p = "???";
syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
}
static void
badformat()
{
warnx("illegal time format");
usage();
}
static void
usage()
{
(void)fprintf(stderr,
"usage: date [-nu] [-d dst] [-r seconds] [-t west] [+format]\n");
(void)fprintf(stderr, " [yy[mm[dd[hh]]]]mm[.ss]]\n");
exit(1);
}

36
bin/date/extern.h Normal file
View File

@ -0,0 +1,36 @@
/*-
* Copyright (c) 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)extern.h 8.1 (Berkeley) 5/31/93
*/
int netsettime __P((time_t));

182
bin/date/netdate.c Normal file
View File

@ -0,0 +1,182 @@
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)netdate.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define TSPTYPES
#include <protocols/timed.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
#define WAITACK 2 /* seconds */
#define WAITDATEACK 5 /* seconds */
extern int retval;
/*
* Set the date in the machines controlled by timedaemons by communicating the
* new date to the local timedaemon. If the timedaemon is in the master state,
* it performs the correction on all slaves. If it is in the slave state, it
* notifies the master that a correction is needed.
* Returns 0 on success. Returns > 0 on failure, setting retval to 2;
*/
int
netsettime(tval)
time_t tval;
{
struct timeval tout;
struct servent *sp;
struct tsp msg;
struct sockaddr_in sin, dest, from;
fd_set ready;
long waittime;
int s, length, port, timed_ack, found, err;
char hostname[MAXHOSTNAMELEN];
if ((sp = getservbyname("timed", "udp")) == NULL) {
warnx("udp/timed: unknown service");
return (retval = 2);
}
dest.sin_port = sp->s_port;
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) {
if (errno != EPROTONOSUPPORT)
warn("timed");
return (retval = 2);
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
sin.sin_port = htons((u_short)port);
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
break;
if (errno == EADDRINUSE)
continue;
if (errno != EADDRNOTAVAIL)
warn("bind");
goto bad;
}
if (port == IPPORT_RESERVED / 2) {
warnx("all ports in use");
goto bad;
}
msg.tsp_type = TSP_SETDATE;
msg.tsp_vers = TSPVERSION;
if (gethostname(hostname, sizeof(hostname))) {
warn("gethostname");
goto bad;
}
(void)strncpy(msg.tsp_name, hostname, sizeof(hostname));
msg.tsp_seq = htons((u_short)0);
msg.tsp_time.tv_sec = htonl((u_long)tval);
msg.tsp_time.tv_usec = htonl((u_long)0);
length = sizeof(struct sockaddr_in);
if (connect(s, (struct sockaddr *)&dest, length) < 0) {
warn("connect");
goto bad;
}
if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
if (errno != ECONNREFUSED)
warn("send");
goto bad;
}
timed_ack = -1;
waittime = WAITACK;
loop:
tout.tv_sec = waittime;
tout.tv_usec = 0;
FD_ZERO(&ready);
FD_SET(s, &ready);
found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout);
length = sizeof(err);
if (!getsockopt(s,
SOL_SOCKET, SO_ERROR, (char *)&err, &length) && err) {
if (err != ECONNREFUSED)
warn("send (delayed error)");
goto bad;
}
if (found > 0 && FD_ISSET(s, &ready)) {
length = sizeof(struct sockaddr_in);
if (recvfrom(s, &msg, sizeof(struct tsp), 0,
(struct sockaddr *)&from, &length) < 0) {
if (errno != ECONNREFUSED)
warn("recvfrom");
goto bad;
}
msg.tsp_seq = ntohs(msg.tsp_seq);
msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
switch (msg.tsp_type) {
case TSP_ACK:
timed_ack = TSP_ACK;
waittime = WAITDATEACK;
goto loop;
case TSP_DATEACK:
(void)close(s);
return (0);
default:
warnx("wrong ack received from timed: %s",
tsptype[msg.tsp_type]);
timed_ack = -1;
break;
}
}
if (timed_ack == -1)
warnx("can't reach time daemon, time set locally");
bad:
(void)close(s);
return (retval = 2);
}

6
bin/dd/Makefile Normal file
View File

@ -0,0 +1,6 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= dd
SRCS= args.c conv.c conv_tab.c dd.c misc.c position.c
.include <bsd.prog.mk>

386
bin/dd/args.c Normal file
View File

@ -0,0 +1,386 @@
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
#endif /* not lint */
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dd.h"
#include "extern.h"
static int c_arg __P((const void *, const void *));
static int c_conv __P((const void *, const void *));
static void f_bs __P((char *));
static void f_cbs __P((char *));
static void f_conv __P((char *));
static void f_count __P((char *));
static void f_files __P((char *));
static void f_ibs __P((char *));
static void f_if __P((char *));
static void f_obs __P((char *));
static void f_of __P((char *));
static void f_seek __P((char *));
static void f_skip __P((char *));
static u_long get_bsz __P((char *));
static struct arg {
char *name;
void (*f) __P((char *));
u_int set, noset;
} args[] = {
{ "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
{ "cbs", f_cbs, C_CBS, C_CBS },
{ "conv", f_conv, 0, 0 },
{ "count", f_count, C_COUNT, C_COUNT },
{ "files", f_files, C_FILES, C_FILES },
{ "ibs", f_ibs, C_IBS, C_BS|C_IBS },
{ "if", f_if, C_IF, C_IF },
{ "obs", f_obs, C_OBS, C_BS|C_OBS },
{ "of", f_of, C_OF, C_OF },
{ "seek", f_seek, C_SEEK, C_SEEK },
{ "skip", f_skip, C_SKIP, C_SKIP },
};
static char *oper;
/*
* args -- parse JCL syntax of dd.
*/
void
jcl(argv)
char **argv;
{
struct arg *ap, tmp;
char *arg;
in.dbsz = out.dbsz = 512;
while (oper = *++argv) {
if ((arg = strchr(oper, '=')) == NULL)
errx(1, "unknown operand %s", oper);
*arg++ = '\0';
if (!*arg)
errx(1, "no value specified for %s", oper);
tmp.name = oper;
if (!(ap = (struct arg *)bsearch(&tmp, args,
sizeof(args)/sizeof(struct arg), sizeof(struct arg),
c_arg)))
errx(1, "unknown operand %s", tmp.name);
if (ddflags & ap->noset)
errx(1, "%s: illegal argument combination or already set",
tmp.name);
ddflags |= ap->set;
ap->f(arg);
}
/* Final sanity checks. */
if (ddflags & C_BS) {
/*
* Bs is turned off by any conversion -- we assume the user
* just wanted to set both the input and output block sizes
* and didn't want the bs semantics, so we don't warn.
*/
if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
ddflags &= ~C_BS;
/* Bs supersedes ibs and obs. */
if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
warnx("bs supersedes ibs and obs");
}
/*
* Ascii/ebcdic and cbs implies block/unblock.
* Block/unblock requires cbs and vice-versa.
*/
if (ddflags & (C_BLOCK|C_UNBLOCK)) {
if (!(ddflags & C_CBS))
errx(1, "record operations require cbs");
if (cbsz == 0)
errx(1, "cbs cannot be zero");
cfunc = ddflags & C_BLOCK ? block : unblock;
} else if (ddflags & C_CBS) {
if (ddflags & (C_ASCII|C_EBCDIC)) {
if (ddflags & C_ASCII) {
ddflags |= C_UNBLOCK;
cfunc = unblock;
} else {
ddflags |= C_BLOCK;
cfunc = block;
}
} else
errx(1, "cbs meaningless if not doing record operations");
if (cbsz == 0)
errx(1, "cbs cannot be zero");
} else
cfunc = def;
if (in.dbsz == 0 || out.dbsz == 0)
errx(1, "buffer sizes cannot be zero");
/*
* Read, write and seek calls take ints as arguments. Seek sizes
* could be larger if we wanted to do it in stages or check only
* regular files, but it's probably not worth it.
*/
if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
errx(1, "buffer sizes cannot be greater than %d", INT_MAX);
if (in.offset > INT_MAX / in.dbsz || out.offset > INT_MAX / out.dbsz)
errx(1, "seek offsets cannot be larger than %d", INT_MAX);
}
static int
c_arg(a, b)
const void *a, *b;
{
return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name));
}
static void
f_bs(arg)
char *arg;
{
in.dbsz = out.dbsz = (int)get_bsz(arg);
}
static void
f_cbs(arg)
char *arg;
{
cbsz = (int)get_bsz(arg);
}
static void
f_count(arg)
char *arg;
{
cpy_cnt = (u_int)get_bsz(arg);
if (!cpy_cnt)
terminate(0);
}
static void
f_files(arg)
char *arg;
{
files_cnt = (int)get_bsz(arg);
}
static void
f_ibs(arg)
char *arg;
{
if (!(ddflags & C_BS))
in.dbsz = (int)get_bsz(arg);
}
static void
f_if(arg)
char *arg;
{
in.name = arg;
}
static void
f_obs(arg)
char *arg;
{
if (!(ddflags & C_BS))
out.dbsz = (int)get_bsz(arg);
}
static void
f_of(arg)
char *arg;
{
out.name = arg;
}
static void
f_seek(arg)
char *arg;
{
out.offset = (u_int)get_bsz(arg);
}
static void
f_skip(arg)
char *arg;
{
in.offset = (u_int)get_bsz(arg);
}
static struct conv {
char *name;
u_int set, noset;
u_char *ctab;
} clist[] = {
{ "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
{ "block", C_BLOCK, C_UNBLOCK, NULL },
{ "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
{ "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
{ "lcase", C_LCASE, C_UCASE, NULL },
{ "noerror", C_NOERROR, 0, NULL },
{ "notrunc", C_NOTRUNC, 0, NULL },
{ "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
{ "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
{ "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
{ "osync", C_OSYNC, C_BS, NULL },
{ "swab", C_SWAB, 0, NULL },
{ "sync", C_SYNC, 0, NULL },
{ "ucase", C_UCASE, C_LCASE, NULL },
{ "unblock", C_UNBLOCK, C_BLOCK, NULL },
};
static void
f_conv(arg)
char *arg;
{
struct conv *cp, tmp;
while (arg != NULL) {
tmp.name = strsep(&arg, ",");
if (!(cp = (struct conv *)bsearch(&tmp, clist,
sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
c_conv)))
errx(1, "unknown conversion %s", tmp.name);
if (ddflags & cp->noset)
errx(1, "%s: illegal conversion combination", tmp.name);
ddflags |= cp->set;
if (cp->ctab)
ctab = cp->ctab;
}
}
static int
c_conv(a, b)
const void *a, *b;
{
return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
}
/*
* Convert an expression of the following forms to an unsigned long.
* 1) A positive decimal number.
* 2) A positive decimal number followed by a b (mult by 512).
* 3) A positive decimal number followed by a k (mult by 1024).
* 4) A positive decimal number followed by a m (mult by 512).
* 5) A positive decimal number followed by a w (mult by sizeof int)
* 6) Two or more positive decimal numbers (with/without k,b or w).
* seperated by x (also * for backwards compatibility), specifying
* the product of the indicated values.
*/
static u_long
get_bsz(val)
char *val;
{
u_long num, t;
char *expr;
num = strtoul(val, &expr, 0);
if (num == ULONG_MAX) /* Overflow. */
err(1, "%s", oper);
if (expr == val) /* No digits. */
errx(1, "%s: illegal numeric value", oper);
switch(*expr) {
case 'b':
t = num;
num *= 512;
if (t > num)
goto erange;
++expr;
break;
case 'k':
t = num;
num *= 1024;
if (t > num)
goto erange;
++expr;
break;
case 'm':
t = num;
num *= 1048576;
if (t > num)
goto erange;
++expr;
break;
case 'w':
t = num;
num *= sizeof(int);
if (t > num)
goto erange;
++expr;
break;
}
switch(*expr) {
case '\0':
break;
case '*': /* Backward compatible. */
case 'x':
t = num;
num *= get_bsz(expr + 1);
if (t > num)
erange: errx(1, "%s: %s", oper, strerror(ERANGE));
break;
default:
errx(1, "%s: illegal numeric value", oper);
}
return (num);
}

260
bin/dd/conv.c Normal file
View File

@ -0,0 +1,260 @@
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
#endif /* not lint */
#include <sys/param.h>
#include <err.h>
#include <string.h>
#include "dd.h"
#include "extern.h"
/*
* def --
* Copy input to output. Input is buffered until reaches obs, and then
* output until less than obs remains. Only a single buffer is used.
* Worst case buffer calculation is (ibs + obs - 1).
*/
void
def()
{
int cnt;
u_char *inp, *t;
if (t = ctab)
for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
*inp = t[*inp];
/* Make the output buffer look right. */
out.dbp = in.dbp;
out.dbcnt = in.dbcnt;
if (in.dbcnt >= out.dbsz) {
/* If the output buffer is full, write it. */
dd_out(0);
/*
* Ddout copies the leftover output to the beginning of
* the buffer and resets the output buffer. Reset the
* input buffer to match it.
*/
in.dbp = out.dbp;
in.dbcnt = out.dbcnt;
}
}
void
def_close()
{
/* Just update the count, everything is already in the buffer. */
if (in.dbcnt)
out.dbcnt = in.dbcnt;
}
/*
* Copy variable length newline terminated records with a max size cbsz
* bytes to output. Records less than cbs are padded with spaces.
*
* max in buffer: MAX(ibs, cbsz)
* max out buffer: obs + cbsz
*/
void
block()
{
static int intrunc;
int ch, cnt, maxlen;
u_char *inp, *outp, *t;
/*
* Record truncation can cross block boundaries. If currently in a
* truncation state, keep tossing characters until reach a newline.
* Start at the beginning of the buffer, as the input buffer is always
* left empty.
*/
if (intrunc) {
for (inp = in.db, cnt = in.dbrcnt;
cnt && *inp++ != '\n'; --cnt);
if (!cnt) {
in.dbcnt = 0;
in.dbp = in.db;
return;
}
intrunc = 0;
/* Adjust the input buffer numbers. */
in.dbcnt = cnt - 1;
in.dbp = inp + cnt - 1;
}
/*
* Copy records (max cbsz size chunks) into the output buffer. The
* translation is done as we copy into the output buffer.
*/
for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
maxlen = MIN(cbsz, in.dbcnt);
if (t = ctab)
for (cnt = 0;
cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
*outp++ = t[ch];
else
for (cnt = 0;
cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
*outp++ = ch;
/*
* Check for short record without a newline. Reassemble the
* input block.
*/
if (ch != '\n' && in.dbcnt < cbsz) {
memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
break;
}
/* Adjust the input buffer numbers. */
in.dbcnt -= cnt;
if (ch == '\n')
--in.dbcnt;
/* Pad short records with spaces. */
if (cnt < cbsz)
(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
else {
/*
* If the next character wouldn't have ended the
* block, it's a truncation.
*/
if (!in.dbcnt || *inp != '\n')
++st.trunc;
/* Toss characters to a newline. */
for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
if (!in.dbcnt)
intrunc = 1;
else
--in.dbcnt;
}
/* Adjust output buffer numbers. */
out.dbp += cbsz;
if ((out.dbcnt += cbsz) >= out.dbsz)
dd_out(0);
outp = out.dbp;
}
in.dbp = in.db + in.dbcnt;
}
void
block_close()
{
/*
* Copy any remaining data into the output buffer and pad to a record.
* Don't worry about truncation or translation, the input buffer is
* always empty when truncating, and no characters have been added for
* translation. The bottom line is that anything left in the input
* buffer is a truncated record. Anything left in the output buffer
* just wasn't big enough.
*/
if (in.dbcnt) {
++st.trunc;
memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
(void)memset(out.dbp + in.dbcnt,
ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
out.dbcnt += cbsz;
}
}
/*
* Convert fixed length (cbsz) records to variable length. Deletes any
* trailing blanks and appends a newline.
*
* max in buffer: MAX(ibs, cbsz) + cbsz
* max out buffer: obs + cbsz
*/
void
unblock()
{
int cnt;
u_char *inp, *t;
/* Translation and case conversion. */
if (t = ctab)
for (cnt = in.dbrcnt, inp = in.dbp; cnt--;)
*--inp = t[*inp];
/*
* Copy records (max cbsz size chunks) into the output buffer. The
* translation has to already be done or we might not recognize the
* spaces.
*/
for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
if (t >= inp) {
cnt = t - inp + 1;
memmove(out.dbp, inp, cnt);
out.dbp += cnt;
out.dbcnt += cnt;
}
++out.dbcnt;
*out.dbp++ = '\n';
if (out.dbcnt >= out.dbsz)
dd_out(0);
}
if (in.dbcnt)
memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
in.dbp = in.db + in.dbcnt;
}
void
unblock_close()
{
int cnt;
u_char *t;
if (in.dbcnt) {
warnx("%s: short input record", in.name);
for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
if (t >= in.db) {
cnt = t - in.db + 1;
memmove(out.dbp, in.db, cnt);
out.dbp += cnt;
out.dbcnt += cnt;
}
++out.dbcnt;
*out.dbp++ = '\n';
}
}

357
bin/dd/conv_tab.c Normal file
View File

@ -0,0 +1,357 @@
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)conv_tab.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
/*
* There are currently eight tables:
*
* lower-case -> upper-case conv=upper
* upper-case -> lower-case conv=lower
*
* ebcdic -> ascii 32V conv=oldascii
* ascii -> ebcdic 32V conv=oldebcdic
* ascii -> ibm ebcdic 32V conv=oldibm
*
* ebcdic -> ascii POSIX/S5 conv=ascii
* ascii -> ebcdic POSIX/S5 conv=ebcdic
* ascii -> ibm ebcdic POSIX/S5 conv=ibm
*
* Other tables are built from these if multiple conversions are being
* done.
*
* Tables used for conversions to/from IBM and EBCDIC to support an extension
* to POSIX P1003.2/D11. The tables referencing POSIX contain data extracted
* from tables 4-3 and 4-4 in P1003.2/Draft 11. The historic tables were
* constructed by running against a file with all possible byte values.
*
* More information can be obtained in "Correspondences of 8-Bit and Hollerith
* Codes for Computer Environments-A USASI Tutorial", Communications of the
* ACM, Volume 11, Number 11, November 1968, pp. 783-789.
*/
/* Lower-case to upper-case */
u_char l2u[] = {
0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007, /* 0000 */
0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027, /* 0020 */
0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037, /* 0030 */
0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047, /* 0040 */
0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057, /* 0050 */
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, /* 0060 */
0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077, /* 0070 */
0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107, /* 0100 */
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117, /* 0110 */
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127, /* 0120 */
0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137, /* 0130 */
0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107, /* 0140 */
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117, /* 0150 */
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127, /* 0160 */
0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177, /* 0170 */
0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207, /* 0200 */
0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217, /* 0210 */
0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227, /* 0220 */
0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237, /* 0230 */
0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247, /* 0240 */
0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257, /* 0250 */
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, /* 0260 */
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, /* 0270 */
0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307, /* 0300 */
0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317, /* 0310 */
0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327, /* 0320 */
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, /* 0330 */
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347, /* 0340 */
0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357, /* 0350 */
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, /* 0360 */
0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* Upper-case to lower-case */
u_char u2l[] = {
0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007, /* 0000 */
0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027, /* 0020 */
0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037, /* 0030 */
0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047, /* 0040 */
0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057, /* 0050 */
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, /* 0060 */
0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077, /* 0070 */
0100, 0141, 0142, 0143, 0144, 0145, 0146, 0147, /* 0100 */
0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157, /* 0110 */
0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167, /* 0120 */
0170, 0171, 0172, 0133, 0134, 0135, 0136, 0137, /* 0130 */
0140, 0141, 0142, 0143, 0144, 0145, 0146, 0147, /* 0140 */
0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157, /* 0150 */
0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167, /* 0160 */
0170, 0171, 0172, 0173, 0174, 0175, 0176, 0177, /* 0170 */
0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207, /* 0200 */
0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217, /* 0210 */
0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227, /* 0220 */
0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237, /* 0230 */
0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247, /* 0240 */
0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257, /* 0250 */
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, /* 0260 */
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, /* 0270 */
0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307, /* 0300 */
0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317, /* 0310 */
0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327, /* 0320 */
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, /* 0330 */
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347, /* 0340 */
0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357, /* 0350 */
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, /* 0360 */
0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* EBCDIC to ASCII -- 32V compatible. */
u_char e2a_32V[] = {
0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177, /* 0000 */
0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207, /* 0020 */
0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037, /* 0030 */
0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033, /* 0040 */
0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007, /* 0050 */
0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004, /* 0060 */
0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032, /* 0070 */
0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246, /* 0100 */
0247, 0250, 0133, 0056, 0074, 0050, 0053, 0041, /* 0110 */
0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257, /* 0120 */
0260, 0261, 0135, 0044, 0052, 0051, 0073, 0136, /* 0130 */
0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267, /* 0140 */
0270, 0271, 0174, 0054, 0045, 0137, 0076, 0077, /* 0150 */
0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301, /* 0160 */
0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042, /* 0170 */
0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147, /* 0200 */
0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311, /* 0210 */
0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160, /* 0220 */
0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320, /* 0230 */
0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170, /* 0240 */
0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327, /* 0250 */
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, /* 0260 */
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347, /* 0270 */
0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107, /* 0300 */
0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355, /* 0310 */
0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120, /* 0320 */
0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363, /* 0330 */
0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130, /* 0340 */
0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371, /* 0350 */
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, /* 0360 */
0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* ASCII to EBCDIC -- 32V compatible. */
u_char a2e_32V[] = {
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057, /* 0000 */
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046, /* 0020 */
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037, /* 0030 */
0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175, /* 0040 */
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141, /* 0050 */
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, /* 0060 */
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157, /* 0070 */
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307, /* 0100 */
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326, /* 0110 */
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346, /* 0120 */
0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155, /* 0130 */
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207, /* 0140 */
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226, /* 0150 */
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246, /* 0160 */
0247, 0250, 0251, 0300, 0152, 0320, 0241, 0007, /* 0170 */
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027, /* 0200 */
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033, /* 0210 */
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010, /* 0220 */
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341, /* 0230 */
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, /* 0240 */
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127, /* 0250 */
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147, /* 0260 */
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165, /* 0270 */
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215, /* 0300 */
0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236, /* 0310 */
0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257, /* 0320 */
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, /* 0330 */
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, /* 0340 */
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333, /* 0350 */
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355, /* 0360 */
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* ASCII to IBM EBCDIC -- 32V compatible. */
u_char a2ibm_32V[] = {
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057, /* 0000 */
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046, /* 0020 */
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037, /* 0030 */
0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175, /* 0040 */
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141, /* 0050 */
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, /* 0060 */
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157, /* 0070 */
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307, /* 0100 */
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326, /* 0110 */
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346, /* 0120 */
0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155, /* 0130 */
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207, /* 0140 */
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226, /* 0150 */
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246, /* 0160 */
0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007, /* 0170 */
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027, /* 0200 */
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033, /* 0210 */
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010, /* 0220 */
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341, /* 0230 */
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, /* 0240 */
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127, /* 0250 */
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147, /* 0260 */
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165, /* 0270 */
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215, /* 0300 */
0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236, /* 0310 */
0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257, /* 0320 */
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, /* 0330 */
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, /* 0340 */
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333, /* 0350 */
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355, /* 0360 */
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* EBCDIC to ASCII -- POSIX and System V compatible. */
u_char e2a_POSIX[] = {
0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177, /* 0000 */
0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207, /* 0020 */
0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037, /* 0030 */
0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033, /* 0040 */
0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007, /* 0050 */
0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004, /* 0060 */
0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032, /* 0070 */
0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246, /* 0100 */
0247, 0250, 0325, 0056, 0074, 0050, 0053, 0174, /* 0110 */
0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257, /* 0120 */
0260, 0261, 0041, 0044, 0052, 0051, 0073, 0176, /* 0130 */
0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267, /* 0140 */
0270, 0271, 0313, 0054, 0045, 0137, 0076, 0077, /* 0150 */
0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301, /* 0160 */
0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042, /* 0170 */
0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147, /* 0200 */
0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311, /* 0210 */
0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160, /* 0220 */
0161, 0162, 0136, 0314, 0315, 0316, 0317, 0320, /* 0230 */
0321, 0345, 0163, 0164, 0165, 0166, 0167, 0170, /* 0240 */
0171, 0172, 0322, 0323, 0324, 0133, 0326, 0327, /* 0250 */
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, /* 0260 */
0340, 0341, 0342, 0343, 0344, 0135, 0346, 0347, /* 0270 */
0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107, /* 0300 */
0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355, /* 0310 */
0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120, /* 0320 */
0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363, /* 0330 */
0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130, /* 0340 */
0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371, /* 0350 */
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, /* 0360 */
0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* ASCII to EBCDIC -- POSIX and System V compatible. */
u_char a2e_POSIX[] = {
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057, /* 0000 */
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046, /* 0020 */
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037, /* 0030 */
0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175, /* 0040 */
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141, /* 0050 */
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, /* 0060 */
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157, /* 0070 */
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307, /* 0100 */
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326, /* 0110 */
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346, /* 0120 */
0347, 0350, 0351, 0255, 0340, 0275, 0232, 0155, /* 0130 */
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207, /* 0140 */
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226, /* 0150 */
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246, /* 0160 */
0247, 0250, 0251, 0300, 0117, 0320, 0137, 0007, /* 0170 */
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027, /* 0200 */
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033, /* 0210 */
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010, /* 0220 */
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341, /* 0230 */
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, /* 0240 */
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127, /* 0250 */
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147, /* 0260 */
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165, /* 0270 */
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215, /* 0300 */
0216, 0217, 0220, 0152, 0233, 0234, 0235, 0236, /* 0310 */
0237, 0240, 0252, 0253, 0254, 0112, 0256, 0257, /* 0320 */
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, /* 0330 */
0270, 0271, 0272, 0273, 0274, 0241, 0276, 0277, /* 0340 */
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333, /* 0350 */
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355, /* 0360 */
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* ASCII to IBM EBCDIC -- POSIX and System V compatible. */
u_char a2ibm_POSIX[] = {
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057, /* 0000 */
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046, /* 0020 */
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037, /* 0030 */
0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175, /* 0040 */
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141, /* 0050 */
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, /* 0060 */
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157, /* 0070 */
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307, /* 0100 */
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326, /* 0110 */
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346, /* 0120 */
0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155, /* 0130 */
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207, /* 0140 */
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226, /* 0150 */
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246, /* 0160 */
0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007, /* 0170 */
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027, /* 0200 */
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033, /* 0210 */
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010, /* 0220 */
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341, /* 0230 */
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, /* 0240 */
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127, /* 0250 */
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147, /* 0260 */
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165, /* 0270 */
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215, /* 0300 */
0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236, /* 0310 */
0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257, /* 0320 */
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, /* 0330 */
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, /* 0340 */
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333, /* 0350 */
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355, /* 0360 */
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};

348
bin/dd/dd.1 Normal file
View File

@ -0,0 +1,348 @@
.\" Copyright (c) 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Keith Muller of the University of California, San Diego.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)dd.1 8.2 (Berkeley) 1/13/94
.\"
.Dd January 13, 1994
.Dt DD 1
.Os
.Sh NAME
.Nm dd
.Nd convert and copy a file
.Sh SYNOPSIS
.Nm dd
.Op operands ...
.Sh DESCRIPTION
The
.Nm
utility copies the standard input to the standard output.
Input data is read and written in 512-byte blocks.
If input reads are short, input from multiple reads are aggregated
to form the output block.
When finished,
.Nm dd
displays the number of complete and partial input and output blocks
and truncated input records to the standard error output.
.Pp
The following operands are available:
.Bl -tag -width of=file
.It Cm bs= Ns Ar n
Set both input and output block size, superseding the
.Cm ibs
and
.Cm obs
operands.
If no conversion values other than
.Cm noerror ,
.Cm notrunc
or
.Cm sync
are specified, then each input block is copied to the output as a
single block without any aggregation of short blocks.
.It Cm cbs= Ns Ar n
Set the conversion record size to
.Va n
bytes.
The conversion record size is required by the record oriented conversion
values.
.It Cm count= Ns Ar n
Copy only
.Va n
input blocks.
.It Cm files= Ns Ar n
Copy
.Va n
input files before terminating.
This operand is only applicable when the input device is a tape.
.It Cm ibs= Ns Ar n
Set the input block size to
.Va n
bytes instead of the default 512.
.It Cm if= Ns Ar file
Read input from
.Ar file
instead of the standard input.
.It Cm obs= Ns Ar n
Set the output block size to
.Va n
bytes instead of the default 512.
.It Cm of= Ns Ar file
Write output to
.Ar file
instead of the standard output.
Any regular output file is truncated unless the
.Cm notrunc
conversion value is specified.
If an initial portion of the output file is skipped (see the
.Cm seek
operand)
the output file is truncated at that point.
.It Cm seek= Ns Ar n
Seek
.Va n
blocks from the beginning of the output before copying.
On non-tape devices, a
.Xr lseek 2
operation is used.
Otherwise, existing blocks are read and the data discarded.
If the user does not have read permission for the tape, it is positioned
using the tape
.Xr ioctl 2
function calls.
If the seek operation is past the end of file, space from the current
end of file to the specified offset is filled with blocks of
.Tn NUL
bytes.
.It Cm skip= Ns Ar n
Skip
.Va n
blocks from the beginning of the input before copying.
On input which supports seeks, a
.Xr lseek 2
operation is used.
Otherwise, input data is read and discarded.
For pipes, the correct number of bytes is read.
For all other devices, the correct number of blocks is read without
distinguishing between a partial or complete block being read.
.It Xo
.Cm conv=
.Ns Cm value Ns Op \&, Cm value \&...
.Xc
Where
.Cm value
is one of the symbols from the following list.
.Bl -tag -width unblock
.It Cm ascii , oldascii
The same as the
.Cm unblock
value except that characters are translated from
.Tn ECBDIC
to
.Tn ASCII
before the
records are converted.
(These values imply
.Cm unblock
if the operand
.Cm cbs
is also specified.)
There are two conversion maps for
.Tn ASCII .
The value
.Cm ascii
specifies the recommended one which is compatible with System V.
The value
.Cm oldascii
specifies the one used in historic
.Tn AT&T
and pre-4.3BSD-reno systems.
.It Cm block
Treats the input as a sequence of newline or end-of-file terminated variable
length records independent of input and output block boundaries.
Any trailing newline character is discarded.
Each input record is converted to a fixed length output record where the
length is specified by the
.Cm cbs
operand.
Input records shorter than the conversion record size are padded with spaces.
Input records longer than the conversion record size are truncated.
The number of truncated input records, if any, are reported to the standard
error output at the completion of the copy.
.It Cm ebcdic , ibm , oldebcdic , oldibm
The same as the
.Cm block
value except that characters are translated from
.Tn ASCII
to
.Tn EBCDIC
after the
records are converted.
(These values imply
.Cm block
if the operand
.Cm cbs
is also specified.)
There are four conversion maps for
.Tn EBCDIC .
The value
.Cm ebcdic
specifies the recommended one which is compatible with
.At V .
The value
.Cm ibm
is a slightly different mapping, which is compatible with the
.At V
.Cm ibm
value.
The values
.Cm oldebcdic
and
.Cm oldibm
are maps used in historic
.Tn AT&T
and pre-4.3BSD-reno systems.
.It Cm lcase
Transform uppercase characters into lowercase characters.
.It Cm noerror
Do not stop processing on an input error.
When an input error occurs, a diagnostic message followed by the current
input and output block counts will be written to the standard error output
in the same format as the standard completion message.
If the
.Cm sync
conversion is also specified, any missing input data will be replaced
with
.Tn NUL
bytes (or with spaces if a block oriented conversion value was
specified) and processed as a normal input buffer.
If the
.Cm sync
conversion is not specified, the input block is omitted from the output.
On input files which are not tapes or pipes, the file offset
will be positioned past the block in which the error occurred using
.Xr lseek 2 .
.It Cm notrunc
Do not truncate the output file.
This will preserve any blocks in the output file not explicitly written
by
.Nm dd .
The
.Cm notrunc
value is not supported for tapes.
.It Cm osync
Pad the final output block to the full output block size.
If the input file is not a multiple of the output block size
after conversion, this conversion forces the final output block
to be the same size as preceding blocks for use on devices that require
regularly sized blocks to be written.
This option is incompatible with use of the
.Cm bs= Ns Ar n
block size specification.
.It Cm swab
Swap every pair of input bytes.
If an input buffer has an odd number of bytes, the last byte will be
ignored during swapping.
.It Cm sync
Pad every input block to the input buffer size.
Spaces are used for pad bytes if a block oriented conversion value is
specified, otherwise
.Tn NUL
bytes are used.
.It Cm ucase
Transform lowercase characters into uppercase characters.
.It Cm unblock
Treats the input as a sequence of fixed length records independent of input
and output block boundaries.
The length of the input records is specified by the
.Cm cbs
operand.
Any trailing space characters are discarded and a newline character is
appended.
.El
.El
.Pp
Where sizes are specified, a decimal number of bytes is expected.
If the number ends with a ``b'', ``k'', ``m'' or ``w'', the number
is multiplied by 512, 1024 (1K), 1048576 (1M) or the number of bytes
in an integer, respectively.
Two or more numbers may be separated by an ``x'' to indicate a product.
.Pp
When finished,
.Nm dd
displays the number of complete and partial input and output blocks,
truncated input records and odd-length byte-swapping blocks to the
standard error output.
A partial input block is one where less than the input block size
was read.
A partial output block is one where less than the output block size
was written.
Partial output blocks to tape devices are considered fatal errors.
Otherwise, the rest of the block will be written.
Partial output blocks to character devices will produce a warning message.
A truncated input block is one where a variable length record oriented
conversion value was specified and the input line was too long to
fit in the conversion record or was not newline terminated.
.Pp
Normally, data resulting from input or conversion or both are aggregated
into output blocks of the specified size.
After the end of input is reached, any remaining output is written as
a block.
This means that the final output block may be shorter than the output
block size.
.Pp
If
.Nm dd
receives a
.Dv SIGINFO
(see the ``status'' argument for
.Xr stty 1 )
signal, the current input and output block counts will
be written to the standard error output
in the same format as the standard completion message.
If
.Nm dd
receives a
.Dv SIGINT
signal, the current input and output block counts will
be written to the standard error output
in the same format as the standard completion message and
.Nm dd
will exit.
.Pp
The
.Nm dd
utility exits 0 on success and >0 if an error occurred.
.Sh SEE ALSO
.Xr cp 1 ,
.Xr mt 1 ,
.Xr tr 1
.Sh STANDARDS
The
.Nm dd
utility is expected to be a superset of the
.St -p1003.2
standard.
The
.Cm files
operand and the
.Cm ascii ,
.Cm ebcdic ,
.Cm ibm ,
.Cm oldascii ,
.Cm oldebcdic
and
.Cm oldibm
values are extensions to the
.Tn POSIX
standard.

395
bin/dd/dd.c Normal file
View File

@ -0,0 +1,395 @@
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1991, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mtio.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dd.h"
#include "extern.h"
static void dd_close __P((void));
static void dd_in __P((void));
static void getfdtype __P((IO *));
static void setup __P((void));
IO in, out; /* input/output state */
STAT st; /* statistics */
void (*cfunc) __P((void)); /* conversion function */
u_long cpy_cnt; /* # of blocks to copy */
u_int ddflags; /* conversion options */
u_int cbsz; /* conversion block size */
u_int files_cnt = 1; /* # of files to copy */
u_char *ctab; /* conversion table */
int
main(argc, argv)
int argc;
char *argv[];
{
jcl(argv);
setup();
(void)signal(SIGINFO, summaryx);
(void)signal(SIGINT, terminate);
atexit(summary);
while (files_cnt--)
dd_in();
dd_close();
exit(0);
}
static void
setup()
{
u_int cnt;
if (in.name == NULL) {
in.name = "stdin";
in.fd = STDIN_FILENO;
} else {
in.fd = open(in.name, O_RDONLY, 0);
if (in.fd < 0)
err(1, "%s", in.name);
}
getfdtype(&in);
if (files_cnt > 1 && !(in.flags & ISTAPE))
errx(1, "files is not supported for non-tape devices");
if (out.name == NULL) {
/* No way to check for read access here. */
out.fd = STDOUT_FILENO;
out.name = "stdout";
} else {
#define OFLAGS \
(O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
/*
* May not have read access, so try again with write only.
* Without read we may have a problem if output also does
* not support seeks.
*/
if (out.fd < 0) {
out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
out.flags |= NOREAD;
}
if (out.fd < 0)
err(1, "%s", out.name);
}
getfdtype(&out);
/*
* Allocate space for the input and output buffers. If not doing
* record oriented I/O, only need a single buffer.
*/
if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
err(1, NULL);
out.db = in.db;
} else if ((in.db =
malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
(out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL)
err(1, NULL);
in.dbp = in.db;
out.dbp = out.db;
/* Position the input/output streams. */
if (in.offset)
pos_in();
if (out.offset)
pos_out();
/*
* Truncate the output file; ignore errors because it fails on some
* kinds of output files, tapes, for example.
*/
if (ddflags & (C_OF | C_SEEK | C_NOTRUNC) == (C_OF | C_SEEK))
(void)ftruncate(out.fd, (off_t)out.offset * out.dbsz);
/*
* If converting case at the same time as another conversion, build a
* table that does both at once. If just converting case, use the
* built-in tables.
*/
if (ddflags & (C_LCASE|C_UCASE))
if (ddflags & C_ASCII)
if (ddflags & C_LCASE) {
for (cnt = 0; cnt < 0377; ++cnt)
if (isupper(ctab[cnt]))
ctab[cnt] = tolower(ctab[cnt]);
} else {
for (cnt = 0; cnt < 0377; ++cnt)
if (islower(ctab[cnt]))
ctab[cnt] = toupper(ctab[cnt]);
}
else if (ddflags & C_EBCDIC)
if (ddflags & C_LCASE) {
for (cnt = 0; cnt < 0377; ++cnt)
if (isupper(cnt))
ctab[cnt] = ctab[tolower(cnt)];
} else {
for (cnt = 0; cnt < 0377; ++cnt)
if (islower(cnt))
ctab[cnt] = ctab[toupper(cnt)];
}
else
ctab = ddflags & C_LCASE ? u2l : l2u;
(void)time(&st.start); /* Statistics timestamp. */
}
static void
getfdtype(io)
IO *io;
{
struct mtget mt;
struct stat sb;
if (fstat(io->fd, &sb))
err(1, "%s", io->name);
if (S_ISCHR(sb.st_mode))
io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */
}
static void
dd_in()
{
int flags, n;
for (flags = ddflags;;) {
if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
return;
/*
* Zero the buffer first if trying to recover from errors so
* lose the minimum amount of data. If doing block operations
* use spaces.
*/
if ((flags & (C_NOERROR|C_SYNC)) == (C_NOERROR|C_SYNC))
if (flags & (C_BLOCK|C_UNBLOCK))
memset(in.dbp, ' ', in.dbsz);
else
memset(in.dbp, 0, in.dbsz);
n = read(in.fd, in.dbp, in.dbsz);
if (n == 0) {
in.dbrcnt = 0;
return;
}
/* Read error. */
if (n < 0) {
/*
* If noerror not specified, die. POSIX requires that
* the warning message be followed by an I/O display.
*/
if (!(flags & C_NOERROR))
err(1, "%s", in.name);
warn("%s", in.name);
summary();
/*
* If it's not a tape drive or a pipe, seek past the
* error. If your OS doesn't do the right thing for
* raw disks this section should be modified to re-read
* in sector size chunks.
*/
if (!(in.flags & (ISPIPE|ISTAPE)) &&
lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
warn("%s", in.name);
/* If sync not specified, omit block and continue. */
if (!(ddflags & C_SYNC))
continue;
/* Read errors count as full blocks. */
in.dbcnt += in.dbrcnt = in.dbsz;
++st.in_full;
/* Handle full input blocks. */
} else if (n == in.dbsz) {
in.dbcnt += in.dbrcnt = n;
++st.in_full;
/* Handle partial input blocks. */
} else {
/* If sync, use the entire block. */
if (ddflags & C_SYNC)
in.dbcnt += in.dbrcnt = in.dbsz;
else
in.dbcnt += in.dbrcnt = n;
++st.in_part;
}
/*
* POSIX states that if bs is set and no other conversions
* than noerror, notrunc or sync are specified, the block
* is output without buffering as it is read.
*/
if (ddflags & C_BS) {
out.dbcnt = in.dbcnt;
dd_out(1);
in.dbcnt = 0;
continue;
}
if (ddflags & C_SWAB) {
if ((n = in.dbcnt) & 1) {
++st.swab;
--n;
}
swab(in.dbp, in.dbp, n);
}
in.dbp += in.dbrcnt;
(*cfunc)();
}
}
/*
* Cleanup any remaining I/O and flush output. If necesssary, output file
* is truncated.
*/
static void
dd_close()
{
if (cfunc == def)
def_close();
else if (cfunc == block)
block_close();
else if (cfunc == unblock)
unblock_close();
if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
memset(out.dbp, 0, out.dbsz - out.dbcnt);
out.dbcnt = out.dbsz;
}
if (out.dbcnt)
dd_out(1);
}
void
dd_out(force)
int force;
{
static int warned;
int cnt, n, nw;
u_char *outp;
/*
* Write one or more blocks out. The common case is writing a full
* output block in a single write; increment the full block stats.
* Otherwise, we're into partial block writes. If a partial write,
* and it's a character device, just warn. If a tape device, quit.
*
* The partial writes represent two cases. 1: Where the input block
* was less than expected so the output block was less than expected.
* 2: Where the input block was the right size but we were forced to
* write the block in multiple chunks. The original versions of dd(1)
* never wrote a block in more than a single write, so the latter case
* never happened.
*
* One special case is if we're forced to do the write -- in that case
* we play games with the buffer size, and it's usually a partial write.
*/
outp = out.db;
for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
for (cnt = n;; cnt -= nw) {
nw = write(out.fd, outp, cnt);
if (nw <= 0) {
if (nw == 0)
errx(1, "%s: end of device", out.name);
if (errno != EINTR)
err(1, "%s", out.name);
nw = 0;
}
outp += nw;
st.bytes += nw;
if (nw == n) {
if (n != out.dbsz)
++st.out_part;
else
++st.out_full;
break;
}
++st.out_part;
if (nw == cnt)
break;
if (out.flags & ISCHR && !warned) {
warned = 1;
warnx("%s: short write on character device",
out.name);
}
if (out.flags & ISTAPE)
errx(1, "%s: short write on tape device", out.name);
}
if ((out.dbcnt -= n) < out.dbsz)
break;
}
/* Reassemble the output block. */
if (out.dbcnt)
memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
out.dbp = out.db + out.dbcnt;
}

96
bin/dd/dd.h Normal file
View File

@ -0,0 +1,96 @@
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)dd.h 8.3 (Berkeley) 4/2/94
*/
/* Input/output stream state. */
typedef struct {
u_char *db; /* buffer address */
u_char *dbp; /* current buffer I/O address */
u_long dbcnt; /* current buffer byte count */
int dbrcnt; /* last read byte count */
u_long dbsz; /* buffer size */
#define ISCHR 0x01 /* character device (warn on short) */
#define ISPIPE 0x02 /* pipe (not truncatable) */
#define ISTAPE 0x04 /* tape (not seekable) */
#define NOREAD 0x08 /* not readable */
u_int flags;
char *name; /* name */
int fd; /* file descriptor */
u_long offset; /* # of blocks to skip */
u_long f_stats; /* # of full blocks processed */
u_long p_stats; /* # of partial blocks processed */
u_long s_stats; /* # of odd swab blocks */
u_long t_stats; /* # of truncations */
} IO;
typedef struct {
u_long in_full; /* # of full input blocks */
u_long in_part; /* # of partial input blocks */
u_long out_full; /* # of full output blocks */
u_long out_part; /* # of partial output blocks */
u_long trunc; /* # of truncated records */
u_long swab; /* # of odd-length swab blocks */
u_long bytes; /* # of bytes written */
time_t start; /* start time of dd */
} STAT;
/* Flags (in ddflags). */
#define C_ASCII 0x00001
#define C_BLOCK 0x00002
#define C_BS 0x00004
#define C_CBS 0x00008
#define C_COUNT 0x00010
#define C_EBCDIC 0x00020
#define C_FILES 0x00040
#define C_IBS 0x00080
#define C_IF 0x00100
#define C_LCASE 0x00200
#define C_NOERROR 0x00400
#define C_NOTRUNC 0x00800
#define C_OBS 0x01000
#define C_OF 0x02000
#define C_SEEK 0x04000
#define C_SKIP 0x08000
#define C_SWAB 0x10000
#define C_SYNC 0x20000
#define C_UCASE 0x40000
#define C_UNBLOCK 0x80000
#define C_OSYNC 0x100000

65
bin/dd/extern.h Normal file
View File

@ -0,0 +1,65 @@
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)extern.h 8.3 (Berkeley) 4/2/94
*/
#include <sys/cdefs.h>
void block __P((void));
void block_close __P((void));
void dd_out __P((int));
void def __P((void));
void def_close __P((void));
void jcl __P((char **));
void pos_in __P((void));
void pos_out __P((void));
void summary __P((void));
void summaryx __P((int));
void terminate __P((int));
void unblock __P((void));
void unblock_close __P((void));
extern IO in, out;
extern STAT st;
extern void (*cfunc)();
extern u_long cpy_cnt;
extern u_int cbsz;
extern u_int ddflags;
extern u_int files_cnt;
extern u_char *ctab;
extern u_char a2e_32V[], a2e_POSIX[], a2ibm_32V[], a2ibm_POSIX[], e2a_32V[];
extern u_char e2a_POSIX[], l2u[], u2l[];

100
bin/dd/misc.c Normal file
View File

@ -0,0 +1,100 @@
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94";
#endif /* not lint */
#include <sys/types.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "dd.h"
#include "extern.h"
void
summary()
{
time_t secs;
char buf[100];
(void)time(&secs);
if ((secs -= st.start) == 0)
secs = 1;
/* Use snprintf(3) so that we don't reenter stdio(3). */
(void)snprintf(buf, sizeof(buf),
"%u+%u records in\n%u+%u records out\n",
st.in_full, st.in_part, st.out_full, st.out_part);
(void)write(STDERR_FILENO, buf, strlen(buf));
if (st.swab) {
(void)snprintf(buf, sizeof(buf), "%u odd length swab %s\n",
st.swab, (st.swab == 1) ? "block" : "blocks");
(void)write(STDERR_FILENO, buf, strlen(buf));
}
if (st.trunc) {
(void)snprintf(buf, sizeof(buf), "%u truncated %s\n",
st.trunc, (st.trunc == 1) ? "block" : "blocks");
(void)write(STDERR_FILENO, buf, strlen(buf));
}
(void)snprintf(buf, sizeof(buf),
"%u bytes transferred in %u secs (%u bytes/sec)\n",
st.bytes, secs, st.bytes / secs);
(void)write(STDERR_FILENO, buf, strlen(buf));
}
/* ARGSUSED */
void
summaryx(notused)
int notused;
{
summary();
}
/* ARGSUSED */
void
terminate(notused)
int notused;
{
exit(0);
}

166
bin/dd/position.c Normal file
View File

@ -0,0 +1,166 @@
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mtio.h>
#include <err.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "dd.h"
#include "extern.h"
/*
* Position input/output data streams before starting the copy. Device type
* dependent. Seekable devices use lseek, and the rest position by reading.
* Seeking past the end of file can cause null blocks to be written to the
* output.
*/
void
pos_in()
{
int bcnt, cnt, nr, warned;
/* If not a character, pipe or tape device, try to seek on it. */
if (!(in.flags & (ISCHR|ISPIPE|ISTAPE))) {
if (lseek(in.fd, (off_t)(in.offset * in.dbsz), SEEK_CUR) == -1)
err(1, "%s", in.name);
return;
}
/*
* Read the data. If a pipe, read until satisfy the number of bytes
* being skipped. No differentiation for reading complete and partial
* blocks for other devices.
*/
for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {
if ((nr = read(in.fd, in.db, bcnt)) > 0) {
if (in.flags & ISPIPE) {
if (!(bcnt -= nr)) {
bcnt = in.dbsz;
--cnt;
}
} else
--cnt;
continue;
}
if (nr == 0) {
if (files_cnt > 1) {
--files_cnt;
continue;
}
errx(1, "skip reached end of input");
}
/*
* Input error -- either EOF with no more files, or I/O error.
* If noerror not set die. POSIX requires that the warning
* message be followed by an I/O display.
*/
if (ddflags & C_NOERROR) {
if (!warned) {
warn("%s", in.name);
warned = 1;
summary();
}
continue;
}
err(1, "%s", in.name);
}
}
void
pos_out()
{
struct mtop t_op;
int cnt, n;
/*
* If not a tape, try seeking on the file. Seeking on a pipe is
* going to fail, but don't protect the user -- they shouldn't
* have specified the seek operand.
*/
if (!(out.flags & ISTAPE)) {
if (lseek(out.fd,
(off_t)out.offset * out.dbsz, SEEK_SET) == -1)
err(1, "%s", out.name);
return;
}
/* If no read access, try using mtio. */
if (out.flags & NOREAD) {
t_op.mt_op = MTFSR;
t_op.mt_count = out.offset;
if (ioctl(out.fd, MTIOCTOP, &t_op) < 0)
err(1, "%s", out.name);
return;
}
/* Read it. */
for (cnt = 0; cnt < out.offset; ++cnt) {
if ((n = read(out.fd, out.db, out.dbsz)) > 0)
continue;
if (n < 0)
err(1, "%s", out.name);
/*
* If reach EOF, fill with NUL characters; first, back up over
* the EOF mark. Note, cnt has not yet been incremented, so
* the EOF read does not count as a seek'd block.
*/
t_op.mt_op = MTBSR;
t_op.mt_count = 1;
if (ioctl(out.fd, MTIOCTOP, &t_op) == -1)
err(1, "%s", out.name);
while (cnt++ < out.offset)
if ((n = write(out.fd, out.db, out.dbsz)) != out.dbsz)
err(1, "%s", out.name);
break;
}
}

7
bin/df/Makefile Normal file
View File

@ -0,0 +1,7 @@
# @(#)Makefile 8.2 (Berkeley) 4/1/94
PROG= df
BINGRP= operator
BINMODE=2555
.include <bsd.prog.mk>

116
bin/df/df.1 Normal file
View File

@ -0,0 +1,116 @@
.\" Copyright (c) 1989, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)df.1 8.2 (Berkeley) 1/13/94
.\"
.Dd January 13, 1994
.Dt DF 1
.Os BSD 4
.Sh NAME
.Nm df
.Nd display free disk space
.Sh SYNOPSIS
.Nm df
.Op Fl in
.Op Fl t Ar type
.Op Ar file | Ar filesystem ...
.Sh DESCRIPTION
.Nm Df
displays statistics about the amount of free disk space on the specified
.Ar filesystem
or on the filesystem of which
.Ar file
is a part.
Values are displayed in 512-byte per block block counts.
If neither a file or a filesystem operand is specified,
statistics for all mounted filesystems are displayed
(subject to the
.Fl t
option below).
.Pp
The following options are available:
.Bl -tag -width Ds
.It Fl i
Include statistics on the number of free inodes.
.It Fl n
Print out the previously obtained statistics from the filesystems.
This option should be used if it is possible that one or more
filesystems are in a state such that they will not be able to provide
statistics without a long delay.
When this option is specified,
.Nm df
will not request new statistics from the filesystems, but will respond
with the possibly stale statistics that were previously obtained.
.It Fl t
Only print out statistics for filesystems of the specified types.
The recognized types are:
ufs, nfs, mfs, lfs, msdos, fdesc, portal, kernfs, procfs, afs and isofs.
along with the aggregates:
all (the default),
local (ufs, mfs, lfs, msdos, isofs),
and misc (fdesc, portal, kernfs, procfs).
The string ``no'' may be prepending to a type to get its complement
(e.g. ``nonfs'' to get non-NFS filesystems). The first
.Fl t
option overrides the default, additional such options will add to
(or subtract from) the current set of types; e.g. either
``df -t ufs -t lfs''
or
``df -t local -t nomfs''
will display statistics for UFS and LFS filesystems.
.El
.Sh ENVIRONMENTAL VARIABLES
.Bl -tag -width BLOCKSIZE
.It Ev BLOCKSIZE
If the environmental variable
.Ev BLOCKSIZE
is set, the block counts will be displayed in units of that size block.
.El
.Sh BUGS
The
.Fl n
and
.Fl t
flags are ignored if a file or filesystem is specified.
.Sh SEE ALSO
.Xr quota 1 ,
.Xr statfs 2 ,
.Xr fstatfs 2 ,
.Xr getfsstat 2 ,
.Xr getmntinfo 3 ,
.Xr fstab 5 ,
.Xr mount 8 ,
.Xr quot 8
.Sh HISTORY
A
.Nm df
command appeared in
.At v6 .

420
bin/df/df.c Normal file
View File

@ -0,0 +1,420 @@
/*
* Copyright (c) 1980, 1990, 1993, 1994
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)df.c 8.7 (Berkeley) 4/2/94";
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* XXX assumes MOUNT_MAXTYPE < 32 */
#define MT(m) (1 << (m))
/* fixed values */
#define MT_NONE (0)
#define MT_ALL (MT(MOUNT_MAXTYPE+1)-1)
/* subject to change */
#define MT_LOCAL \
(MT(MOUNT_UFS)|MT(MOUNT_MFS)|MT(MOUNT_LFS)|MT(MOUNT_MSDOS)|MT(MOUNT_CD9660))
#define MT_DEFAULT MT_ALL
struct typetab {
char *str;
long types;
} typetab[] = {
"ufs", MT(MOUNT_UFS),
"local", MT_LOCAL,
"all", MT_ALL,
"nfs", MT(MOUNT_NFS),
"mfs", MT(MOUNT_MFS),
"lfs", MT(MOUNT_LFS),
"msdos", MT(MOUNT_MSDOS),
"fdesc", MT(MOUNT_FDESC),
"portal", MT(MOUNT_PORTAL),
#if 0
/* return fsid of underlying FS */
"lofs", MT(MOUNT_LOFS),
"null", MT(MOUNT_NULL),
"umap", MT(MOUNT_UMAP),
#endif
"kernfs", MT(MOUNT_KERNFS),
"procfs", MT(MOUNT_PROCFS),
"afs", MT(MOUNT_AFS),
"iso9660fs", MT(MOUNT_CD9660),
"cdfs", MT(MOUNT_CD9660),
"misc", MT(MOUNT_LOFS)|MT(MOUNT_FDESC)|MT(MOUNT_PORTAL)|
MT(MOUNT_KERNFS)|MT(MOUNT_PROCFS),
NULL, 0
};
long addtype __P((long, char *));
long regetmntinfo __P((struct statfs **, long, long));
int bread __P((off_t, void *, int));
char *getmntpt __P((char *));
void prtstat __P((struct statfs *, int));
void ufs_df __P((char *, int));
void usage __P((void));
int iflag, nflag, tflag;
struct ufs_args mdev;
int
main(argc, argv)
int argc;
char *argv[];
{
struct stat stbuf;
struct statfs statfsbuf, *mntbuf;
long fsmask, mntsize;
int ch, err, i, maxwidth, width;
char *mntpt;
while ((ch = getopt(argc, argv, "int:")) != EOF)
switch (ch) {
case 'i':
iflag = 1;
break;
case 'n':
nflag = 1;
break;
case 't':
fsmask = addtype(fsmask, optarg);
tflag = 1;
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
maxwidth = 0;
for (i = 0; i < mntsize; i++) {
width = strlen(mntbuf[i].f_mntfromname);
if (width > maxwidth)
maxwidth = width;
}
if (!*argv) {
if (!tflag)
fsmask = MT_DEFAULT;
mntsize = regetmntinfo(&mntbuf, mntsize, fsmask);
if (fsmask != MT_ALL) {
maxwidth = 0;
for (i = 0; i < mntsize; i++) {
width = strlen(mntbuf[i].f_mntfromname);
if (width > maxwidth)
maxwidth = width;
}
}
for (i = 0; i < mntsize; i++)
prtstat(&mntbuf[i], maxwidth);
exit(0);
}
for (; *argv; argv++) {
if (stat(*argv, &stbuf) < 0) {
err = errno;
if ((mntpt = getmntpt(*argv)) == 0) {
warn("%s", *argv);
continue;
}
} else if ((stbuf.st_mode & S_IFMT) == S_IFCHR) {
ufs_df(*argv, maxwidth);
continue;
} else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
if ((mntpt = getmntpt(*argv)) == 0) {
mntpt = mktemp(strdup("/tmp/df.XXXXXX"));
mdev.fspec = *argv;
if (mkdir(mntpt, DEFFILEMODE) != 0) {
warn("%s", mntpt);
continue;
}
if (mount(MOUNT_UFS, mntpt, MNT_RDONLY,
&mdev) != 0) {
ufs_df(*argv, maxwidth);
(void)rmdir(mntpt);
continue;
} else if (statfs(mntpt, &statfsbuf)) {
statfsbuf.f_mntonname[0] = '\0';
prtstat(&statfsbuf, maxwidth);
} else
warn("%s", *argv);
(void)unmount(mntpt, 0);
(void)rmdir(mntpt);
continue;
}
} else
mntpt = *argv;
/*
* Statfs does not take a `wait' flag, so we cannot
* implement nflag here.
*/
if (statfs(mntpt, &statfsbuf) < 0) {
warn("%s", mntpt);
continue;
}
if (argc == 1)
maxwidth = strlen(statfsbuf.f_mntfromname) + 1;
prtstat(&statfsbuf, maxwidth);
}
return (0);
}
char *
getmntpt(name)
char *name;
{
long mntsize, i;
struct statfs *mntbuf;
mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
for (i = 0; i < mntsize; i++) {
if (!strcmp(mntbuf[i].f_mntfromname, name))
return (mntbuf[i].f_mntonname);
}
return (0);
}
long
addtype(omask, str)
long omask;
char *str;
{
struct typetab *tp;
/*
* If it is one of our known types, add it to the current mask
*/
for (tp = typetab; tp->str; tp++)
if (strcmp(str, tp->str) == 0)
return (tp->types | (tflag ? omask : MT_NONE));
/*
* See if it is the negation of one of the known values
*/
if (strlen(str) > 2 && str[0] == 'n' && str[1] == 'o')
for (tp = typetab; tp->str; tp++)
if (strcmp(str+2, tp->str) == 0)
return (~tp->types & (tflag ? omask : MT_ALL));
errx(1, "unknown type `%s'", str);
}
/*
* Make a pass over the filesystem info in ``mntbuf'' filtering out
* filesystem types not in ``fsmask'' and possibly re-stating to get
* current (not cached) info. Returns the new count of valid statfs bufs.
*/
long
regetmntinfo(mntbufp, mntsize, fsmask)
struct statfs **mntbufp;
long mntsize, fsmask;
{
int i, j;
struct statfs *mntbuf;
if (fsmask == MT_ALL)
return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
mntbuf = *mntbufp;
j = 0;
for (i = 0; i < mntsize; i++) {
if (fsmask & MT(mntbuf[i].f_type)) {
if (!nflag)
(void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]);
else if (i != j)
mntbuf[j] = mntbuf[i];
j++;
}
}
return (j);
}
/*
* Convert statfs returned filesystem size into BLOCKSIZE units.
* Attempts to avoid overflow for large filesystems.
*/
#define fsbtoblk(num, fsbs, bs) \
(((fsbs) != 0 && (fsbs) < (bs)) ? \
(num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
/*
* Print out status about a filesystem.
*/
void
prtstat(sfsp, maxwidth)
struct statfs *sfsp;
int maxwidth;
{
static long blocksize;
static int headerlen, timesthrough;
static char *header;
long used, availblks, inodes;
if (maxwidth < 11)
maxwidth = 11;
if (++timesthrough == 1) {
header = getbsize(&headerlen, &blocksize);
(void)printf("%-*.*s %s Used Avail Capacity",
maxwidth, maxwidth, "Filesystem", header);
if (iflag)
(void)printf(" iused ifree %%iused");
(void)printf(" Mounted on\n");
}
(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
used = sfsp->f_blocks - sfsp->f_bfree;
availblks = sfsp->f_bavail + used;
(void)printf(" %*ld %8ld %8ld", headerlen,
fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
fsbtoblk(used, sfsp->f_bsize, blocksize),
fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
(void)printf(" %5.0f%%",
availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
if (iflag) {
inodes = sfsp->f_files;
used = inodes - sfsp->f_ffree;
(void)printf(" %7ld %7ld %5.0f%% ", used, sfsp->f_ffree,
inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
} else
(void)printf(" ");
(void)printf(" %s\n", sfsp->f_mntonname);
}
/*
* This code constitutes the pre-system call Berkeley df code for extracting
* information from filesystem superblocks.
*/
#include <ufs/ffs/fs.h>
#include <errno.h>
#include <fstab.h>
union {
struct fs iu_fs;
char dummy[SBSIZE];
} sb;
#define sblock sb.iu_fs
int rfd;
void
ufs_df(file, maxwidth)
char *file;
int maxwidth;
{
struct statfs statfsbuf;
struct statfs *sfsp;
char *mntpt;
static int synced;
if (synced++ == 0)
sync();
if ((rfd = open(file, O_RDONLY)) < 0) {
warn("%s", file);
return;
}
if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) {
(void)close(rfd);
return;
}
sfsp = &statfsbuf;
sfsp->f_type = MOUNT_UFS;
sfsp->f_flags = 0;
sfsp->f_bsize = sblock.fs_fsize;
sfsp->f_iosize = sblock.fs_bsize;
sfsp->f_blocks = sblock.fs_dsize;
sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
sblock.fs_cstotal.cs_nffree;
sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) -
(sblock.fs_dsize - sfsp->f_bfree);
if (sfsp->f_bavail < 0)
sfsp->f_bavail = 0;
sfsp->f_files = sblock.fs_ncg * sblock.fs_ipg;
sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
sfsp->f_fsid.val[0] = 0;
sfsp->f_fsid.val[1] = 0;
if ((mntpt = getmntpt(file)) == 0)
mntpt = "";
memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
prtstat(sfsp, maxwidth);
(void)close(rfd);
}
int
bread(off, buf, cnt)
off_t off;
void *buf;
int cnt;
{
int nr;
(void)lseek(rfd, off, SEEK_SET);
if ((nr = read(rfd, buf, cnt)) != cnt) {
/* Probably a dismounted disk if errno == EIO. */
if (errno != EIO)
(void)fprintf(stderr, "\ndf: %qd: %s\n",
off, strerror(nr > 0 ? EIO : errno));
return (0);
}
return (1);
}
void
usage()
{
(void)fprintf(stderr, "usage: df [-in] [file | file_system ...]\n");
exit(1);
}

5
bin/echo/Makefile Normal file
View File

@ -0,0 +1,5 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= echo
.include <bsd.prog.mk>

70
bin/echo/echo.1 Normal file
View File

@ -0,0 +1,70 @@
.\" Copyright (c) 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)echo.1 8.1 (Berkeley) 7/22/93
.\"
.Dd July 22, 1993
.Dt ECHO 1
.Os
.Sh NAME
.Nm echo
.Nd write arguments to the standard output
.Sh SYNOPSIS
.Nm echo
.Op Fl n
.Op "string ..."
.Sh DESCRIPTION
The
.Nm echo
utility writes any specified operands, separated by single blank (`` '')
characters and followed by a newline (``\en'') character, to the standard
output.
.Pp
The following option is available:
.Bl -tag -width flag
.It Fl n
Do not print the trailing newline character.
.El
.Pp
The
.Nm echo
utility exits 0 on success, and >0 if an error occurs.
.Sh SEE ALSO
.Xr printf 1
.Sh STANDARDS
The
.Nm echo
utility is expected to be
.St -p1003.2
compatible.

71
bin/echo/echo.c Normal file
View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)echo.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(argc, argv)
int argc;
char *argv[];
{
int nflag;
/* This utility may NOT do getopt(3) option parsing. */
if (*++argv && !strcmp(*argv, "-n")) {
++argv;
nflag = 1;
}
else
nflag = 0;
while (*argv) {
(void)printf("%s", *argv);
if (*++argv)
putchar(' ');
}
if (!nflag)
putchar('\n');
exit(0);
}

5
bin/hostname/Makefile Normal file
View File

@ -0,0 +1,5 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= hostname
.include <bsd.prog.mk>

65
bin/hostname/hostname.1 Normal file
View File

@ -0,0 +1,65 @@
.\" Copyright (c) 1983, 1988, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)hostname.1 8.1 (Berkeley) 5/31/93
.\"
.Dd May 31, 1993
.Dt HOSTNAME 1
.Os BSD 4.2
.Sh NAME
.Nm hostname
.Nd set or print name of current host system
.Sh SYNOPSIS
.Nm hostname
.Op Fl s
.Op Ar nameofhost
.Sh DESCRIPTION
.Nm Hostname
prints the name of the current host. The super-user can
set the hostname by supplying an argument; this is usually done in the
network initialization script
.Pa /etc/netstart ,
normally run at boot
time.
.Pp
Options:
.Bl -tag -width flag
.It Fl s
Trims off any domain information from the printed
name.
.El
.Sh SEE ALSO
.Xr gethostname 2
.Sh HISTORY
The
.Nm hostname
command appeared in
.Bx 4.2 .

87
bin/hostname/hostname.c Normal file
View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1988, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)hostname.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/param.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int
main(argc,argv)
int argc;
char *argv[];
{
extern int optind;
int ch, sflag;
char *p, hostname[MAXHOSTNAMELEN];
sflag = 0;
while ((ch = getopt(argc, argv, "s")) != EOF)
switch (ch) {
case 's':
sflag = 1;
break;
case '?':
default:
(void)fprintf(stderr,
"usage: hostname [-s] [hostname]\n");
exit(1);
}
argc -= optind;
argv += optind;
if (*argv) {
if (sethostname(*argv, strlen(*argv)))
err(1, "sethostname");
} else {
if (gethostname(hostname, sizeof(hostname)))
err(1, "gethostname");
if (sflag && (p = strchr(hostname, '.')))
*p = '\0';
(void)printf("%s\n", hostname);
}
exit(0);
}

5
bin/kill/Makefile Normal file
View File

@ -0,0 +1,5 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= kill
.include <bsd.prog.mk>

120
bin/kill/kill.1 Normal file
View File

@ -0,0 +1,120 @@
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)kill.1 8.1 (Berkeley) 5/31/93
.\"
.Dd May 31, 1993
.Dt KILL 1
.Os
.Sh NAME
.Nm kill
.Nd terminate or signal a process
.Sh SYNOPSIS
.Nm kill
.Op Fl signal_name
.Ar pid
\&...
.Nm kill
.Op Fl signal_number
.Ar pid
\&...
.Nm kill
.Op Fl l
.Sh DESCRIPTION
The kill utility sends the
.Dv TERM
signal to the processes specified
by the pid operand(s).
.Pp
Only the super-user may send signals to other users' processes.
.Pp
The options are as follows:
.Pp
.Bl -tag -width Ds
.It Fl l
List the signal names.
.It Fl signal_name
A symbolic signal name specifying the signal to be sent instead of the
default
.Dv TERM .
The
.Fl l
option displays the signal names.
.It Fl signal_number
A non-negative decimal integer, specifying the signal to be sent instead
of the default
.Dv TERM .
.El
.Pp
Some of the more commonly used signals:
.Bd -ragged -offset indent -compact
.Bl -column XXX TERM
.It -1 -1 (super-user broadcast to all processes, or user broadcast
to user's processes)
.It 0 0 (sh(1) only, signals all members of process group)
.It 2 INT (interrupt)
.It 3 QUIT (quit)
.It 6 ABRT (abort)
.It 9 KILL (non-catchable, non-ignorable kill)
.It 14 ALRM (alarm clock)
.It 15 TERM (software termination signal)
.El
.Ed
.Pp
.Nm Kill
is a built-in to
.Xr csh 1 ;
it allows job specifiers of the form ``%...'' as arguments
so process id's are not as often used as
.Nm kill
arguments.
See
.Xr csh 1
for details.
.Sh SEE ALSO
.Xr csh 1 ,
.Xr ps 1 ,
.Xr kill 2 ,
.Xr sigvec 2
.Sh HISTORY
A
.Nm kill
command appeared in
.At v6 .
.Sh BUGS
A replacement for the command
.Dq Li kill 0
for
.Xr csh 1
users should be provided.

144
bin/kill/kill.c Normal file
View File

@ -0,0 +1,144 @@
/*
* Copyright (c) 1988, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1988, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)kill.c 8.3 (Berkeley) 4/2/94";
#endif /* not lint */
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void nosig __P((char *));
void printsig __P((FILE *));
void usage __P((void));
int
main(argc, argv)
int argc;
char *argv[];
{
const char *const *p;
int errors, numsig, pid;
char *ep;
if (argc < 2)
usage();
if (!strcmp(*++argv, "-l")) {
printsig(stdout);
exit(0);
}
numsig = SIGTERM;
if (**argv == '-') {
++*argv;
if (isalpha(**argv)) {
if (!strncasecmp(*argv, "sig", 3))
*argv += 3;
for (numsig = NSIG, p = sys_signame + 1; --numsig; ++p)
if (!strcasecmp(*p, *argv)) {
numsig = p - sys_signame;
break;
}
if (!numsig)
nosig(*argv);
} else if (isdigit(**argv)) {
numsig = strtol(*argv, &ep, 10);
if (!*argv || *ep)
errx(1, "illegal signal number: %s", *argv);
if (numsig <= 0 || numsig > NSIG)
nosig(*argv);
} else
nosig(*argv);
++argv;
}
if (!*argv)
usage();
for (errors = 0; *argv; ++argv) {
pid = strtol(*argv, &ep, 10);
if (!*argv || *ep) {
warnx("illegal process id: %s", *argv);
errors = 1;
} else if (kill(pid, numsig) == -1) {
warn("%s", *argv);
errors = 1;
}
}
exit(errors);
}
void
nosig(name)
char *name;
{
warnx("unknown signal %s; valid signals:", name);
printsig(stderr);
exit(1);
}
void
printsig(fp)
FILE *fp;
{
const char *const *p;
int cnt;
for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
(void)fprintf(fp, "%s ", *p);
if (cnt == NSIG / 2)
(void)fprintf(fp, "\n");
}
(void)fprintf(fp, "\n");
}
void
usage()
{
(void)fprintf(stderr, "usage: kill [-l] [-sig] pid ...\n");
exit(1);
}

7
bin/ln/Makefile Normal file
View File

@ -0,0 +1,7 @@
# @(#)Makefile 8.2 (Berkeley) 5/31/93
PROG= ln
MAN1= ln.0
MAN7= symlink.0
.include <bsd.prog.mk>

131
bin/ln/ln.1 Normal file
View File

@ -0,0 +1,131 @@
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)ln.1 8.2 (Berkeley) 12/30/93
.\"
.Dd December 30, 1993
.Dt LN 1
.Os BSD 4
.Sh NAME
.Nm ln
.Nd make links
.Sh SYNOPSIS
.Nm ln
.Op Fl fs
.Ar source_file
.Op target_file
.Nm ln
.Op Fl fs
.Ar source_file ...
.Op target_dir
.Sh DESCRIPTION
The
.Nm ln
utility creates a new directory entry (linked file) which has the
same modes as the original file.
It is useful for maintaining multiple copies of a file in many places
at once without using up storage for the
.Dq copies ;
instead, a link
.Dq points
to the original copy.
There are two types of links; hard links and symbolic links.
How a link
.Dq points
to a file is one of the differences between a hard or symbolic link.
.Pp
The options are as follows:
.Bl -tag -width flag
.It Fl f
Unlink any already existing file, permitting the link to occur.
.It Fl s
Create a symbolic link.
.El
.Pp
By default
.Nm ln
makes
.Em hard
links.
A hard link to a file is indistinguishable from the original directory entry;
any changes to a file are effective independent of the name used to reference
the file.
Hard links may not normally refer to directories and may not span file systems.
.Pp
A symbolic link contains the name of the file to
which it is linked. The referenced file is used when an
.Xr open 2
operation is performed on the link.
A
.Xr stat 2
on a symbolic link will return the linked-to file; an
.Xr lstat 2
must be done to obtain information about the link.
The
.Xr readlink 2
call may be used to read the contents of a symbolic link.
Symbolic links may span file systems and may refer to directories.
.Pp
Given one or two arguments,
.Nm ln
creates a link to an existing file
.Ar source_file .
If
.Ar target_file
is given, the link has that name;
.Ar target_file
may also be a directory in which to place the link;
otherwise it is placed in the current directory.
If only the directory is specified, the link will be made
to the last component of
.Ar source_file .
.Pp
Given more than two arguments,
.Nm ln
makes links in
.Ar target_dir
to all the named source files.
The links made will have the same name as the files being linked to.
.Sh SEE ALSO
.Xr link 2 ,
.Xr lstat 2 ,
.Xr readlink 2 ,
.Xr stat 2 ,
.Xr symlink 2 ,
.Xr symlink 7
.Sh HISTORY
A
.Nm ln
command appeared in
.At v6 .

164
bin/ln/ln.c Normal file
View File

@ -0,0 +1,164 @@
/*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1987, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int dirflag; /* Undocumented directory flag. */
int fflag; /* Unlink existing files. */
int sflag; /* Symbolic, not hard, link. */
/* System link call. */
int (*linkf) __P((const char *, const char *));
int linkit __P((char *, char *, int));
void usage __P((void));
int
main(argc, argv)
int argc;
char *argv[];
{
extern int optind;
struct stat sb;
int ch, exitval;
char *sourcedir;
while ((ch = getopt(argc, argv, "Ffs")) != EOF)
switch (ch) {
case 'F':
dirflag = 1; /* XXX: deliberately undocumented. */
break;
case 'f':
fflag = 1;
break;
case 's':
sflag = 1;
break;
case '?':
default:
usage();
}
argv += optind;
argc -= optind;
linkf = sflag ? symlink : link;
switch(argc) {
case 0:
usage();
case 1: /* ln target */
exit(linkit(argv[0], ".", 1));
case 2: /* ln target source */
exit(linkit(argv[0], argv[1], 0));
}
/* ln target1 target2 directory */
sourcedir = argv[argc - 1];
if (stat(sourcedir, &sb))
err(1, "%s", sourcedir);
if (!S_ISDIR(sb.st_mode))
usage();
for (exitval = 0; *argv != sourcedir; ++argv)
exitval |= linkit(*argv, sourcedir, 1);
exit(exitval);
}
int
linkit(target, source, isdir)
char *target, *source;
int isdir;
{
struct stat sb;
int exists;
char *p, path[MAXPATHLEN];
if (!sflag) {
/* If target doesn't exist, quit now. */
if (stat(target, &sb)) {
warn("%s", target);
return (1);
}
/* Only symbolic links to directories, unless -F option used. */
if (!dirflag && (sb.st_mode & S_IFMT) == S_IFDIR) {
warnx("%s: is a directory", target);
return (1);
}
}
/* If the source is a directory, append the target's name. */
if (isdir || (exists = !stat(source, &sb)) && S_ISDIR(sb.st_mode)) {
if ((p = strrchr(target, '/')) == NULL)
p = target;
else
++p;
(void)snprintf(path, sizeof(path), "%s/%s", source, p);
source = path;
exists = !stat(source, &sb);
} else
exists = !stat(source, &sb);
/*
* If the file exists, and -f was specified, unlink it.
* Attempt the link.
*/
if (fflag && exists && unlink(source) || (*linkf)(target, source)) {
warn("%s", source);
return (1);
}
return (0);
}
void
usage()
{
(void)fprintf(stderr,
"usage:\tln [-fs] file1 file2\n\tln [-fs] file ... directory\n");
exit(1);
}

432
bin/ln/symlink.7 Normal file
View File

@ -0,0 +1,432 @@
.\" Copyright (c) 1992, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)symlink.7 8.3 (Berkeley) 3/31/94
.\"
.Dd March 31, 1994
.Dt SYMLINK 7
.Os BSD 4
.Sh NAME
.Nm symlink
.Nd symbolic link handling
.Sh SYMBOLIC LINK HANDLING
Symbolic links are files that act as pointers to other files.
To understand their behavior, you must first understand how hard links
work.
A hard link to a file is indistinguishable from the original file because
it is a reference to the object underlying the original file name.
Changes to a file are independent of the name used to reference the
file.
Hard links may not refer to directories and may not reference files
on different file systems.
A symbolic link contains the name of the file to which it is linked,
i.e. it is a pointer to another name, and not to an underlying object.
For this reason, symbolic links may reference directories and may span
file systems.
.Pp
Because a symbolic link and its referenced object coexist in the filesystem
name space, confusion can arise in distinguishing between the link itself
and the referenced object.
Historically, commands and system calls have adopted their own link
following conventions in a somewhat ad-hoc fashion.
Rules for more a uniform approach, as they are implemented in this system,
are outlined here.
It is important that local applications conform to these rules, too,
so that the user interface can be as consistent as possible.
.Pp
Symbolic links are handled either by operating on the link itself,
or by operating on the object referenced by the link.
In the latter case,
an application or system call is said to
.Dq follow
the link.
Symbolic links may reference other symbolic links,
in which case the links are dereferenced until an object that is
not a symbolic link is found,
a symbolic link which references a file which doesn't exist is found,
or a loop is detected.
(Loop detection is done by placing an upper limit on the number of
links that may be followed, and an error results if this limit is
exceeded.)
.Pp
There are three separate areas that need to be discussed.
They are as follows:
.sp
.Bl -enum -compact -offset indent
.It
Symbolic links used as file name arguments for system calls.
.It
Symbolic links specified as command line arguments to utilities that
are not traversing a file tree.
.It
Symbolic links encountered by utilities that are traversing a file tree
(either specified on the command line or encountered as part of the
file hierarchy walk).
.El
.Ss System calls.
The first area is symbolic links used as file name arguments for
system calls.
.Pp
Except as noted below, all system calls follow symbolic links.
For example, if there were a symbolic link
.Dq Li slink
which pointed to a file named
.Dq Li afile ,
the system call
.Dq Li open("slink" ...)
would return a file descriptor to the file
.Dq afile .
.Pp
There are four system calls that do not follow links, and which operate
on the symbolic link itself.
They are:
.Xr lstat 2 ,
.Xr readlink 2 ,
.Xr rename 2 ,
and
.Xr unlink 2 .
Because
.Xr remove 3
is an alias for
.Xr unlink 2 ,
it also does not follow symbolic links.
.Pp
Unlike other filesystem objects, symbolic links do not have an owner,
group, permissions, access and modification times, etc.
The only attributes returned from an
.Xr lstat 2
that refer to the symbolic link itself are the file type (S_IFLNK),
size, blocks, and link count (always 1).
The other attributes are filled in from the directory that contains
the link.
For portability reasons, you should be aware that other implementations
(including historic implementations of 4BSD), implement symbolic links
such that they have the same attributes as any other file.
.Pp
The
.Bx 4.4
system differs from historical 4BSD systems in that the system call
.Xr chown 2
has been changed to follow symbolic links.
.Ss Commands not traversing a file tree.
The second area is symbolic links, specified as command line file
name arguments, to commands which are not traversing a file tree.
.Pp
Except as noted below, commands follow symbolic links named as command
line arguments.
For example, if there were a symbolic link
.Dq Li slink
which pointed to a file named
.Dq Li afile ,
the command
.Dq Li cat slink
would display the contents of the file
.Dq Li afile .
.Pp
It is important to realize that this rule includes commands which may
optionally traverse file trees, e.g. the command
.Dq Li "chown file"
is included in this rule, while the command
.Dq Li "chown -R file"
is not.
(The latter is described in the third area, below.)
.Pp
If it is explicitly intended that the command operate on the symbolic
link instead of following the symbolic link, e.g., it is desired that
.Dq Li "file slink"
display the type of file that
.Dq Li slink
is, whether it is a symbolic link or not, the
.Fl h
option should be used.
In the above example,
.Dq Li "file slink"
would report the type of the file referenced by
.Dq Li slink ,
while
.Dq Li "file -h slink"
would report that
.Dq Li slink
was a symbolic link.
.Pp
There are three exceptions to this rule.
The
.Xr mv 1
and
.Xr rm 1
commands do not follow symbolic links named as arguments,
but respectively attempt to rename and delete them.
(Note, if the symbolic link references a file via a relative path,
moving it to another directory may very well cause it to stop working,
since the path may no longer be correct.)
.Pp
The
.Xr ls 1
command is also an exception to this rule.
For compatibility with historic systems (when
.Nm ls
is not doing a tree walk, i.e. the
.Fl R
option is not specified),
the
.Nm ls
command follows symbolic links named as arguments if the
.Fl L
option is specified,
or if the
.Fl F ,
.Fl d
or
.Fl l
options are not specified.
(If the
.Fl L
option is specified,
.Nm ls
always follows symbolic links.
.Nm Ls
is the only command where the
.Fl L
option affects its behavior even though it is not doing a walk of
a file tree.)
.Pp
The
.Bx 4.4
system differs from historical 4BSD systems in that the
.Nm chown ,
.Nm chgrp
and
.Nm file
commands follow symbolic links specified on the command line.
.Ss Commands traversing a file tree.
The following commands either optionally or always traverse file trees:
.Xr chflags 1 ,
.Xr chgrp 1 ,
.Xr chmod 1 ,
.Xr cp 1 ,
.Xr du 1 ,
.Xr find 1 ,
.Xr ls 1 ,
.Xr pax 1 ,
.Xr rm 1 ,
.Xr tar 1
and
.Xr chown 8 .
.Pp
It is important to realize that the following rules apply equally to
symbolic links encountered during the file tree traversal and symbolic
links listed as command line arguments.
.Pp
The first rule applies to symbolic links that reference files that are
not of type directory.
Operations that apply to symbolic links are performed on the links
themselves, but otherwise the links are ignored.
.Pp
For example, the command
.Dq Li "chown -R user slink directory"
will ignore
.Dq Li slink ,
because symbolic links in this system do not have owners.
Any symbolic links encountered during the tree traversal will also be
ignored.
The command
.Dq Li "rm -r slink directory"
will remove
.Dq Li slink ,
as well as any symbolic links encountered in the tree traversal of
.Dq Li directory ,
because symbolic links may be removed.
In no case will either
.Nm chown
or
.Nm rm
affect the file which
.Dq Li slink
references in any way.
.Pp
The second rule applies to symbolic links that reference files of type
directory.
Symbolic links which reference files of type directory are never
.Dq followed
by default.
This is often referred to as a
.Dq physical
walk, as opposed to a
.Dq logical
walk (where symbolic links referencing directories are followed).
.Pp
As consistently as possible, you can make commands doing a file tree
walk follow any symbolic links named on the command line, regardless
of the type of file they reference, by specifying the
.Fl H
(for
.Dq half\-logical )
flag.
This flag is intended to make the command line name space look
like the logical name space.
(Note, for commands that do not always do file tree traversals, the
.Fl H
flag will be ignored if the
.Fl R
flag is not also specified.)
.Pp
For example, the command
.Dq Li "chown -HR user slink"
will traverse the file hierarchy rooted in the file pointed to by
.Dq Li slink .
Note, the
.Fl H
is not the same as the previously discussed
.Fl h
flag.
The
.Fl H
flag causes symbolic links specified on the command line to be
dereferenced both for the purposes of the action to be performed
and the tree walk, and it is as if the user had specified the
name of the file to which the symbolic link pointed.
.Pp
As consistently as possible, you can make commands doing a file tree
walk follow any symbolic links named on the command line, as well as
any symbolic links encountered during the traversal, regardless of
the type of file they reference, by specifying the
.Fl L
(for
.Dq logical )
flag.
This flag is intended to make the entire name space look like
the logical name space.
(Note, for commands that do not always do file tree traversals, the
.Fl L
flag will be ignored if the
.Fl R
flag is not also specified.)
.Pp
For example, the command
.Dq Li "chown -LR user slink"
will change the owner of the file referenced by
.Dq Li slink .
If
.Dq Li slink
references a directory,
.Nm chown
will traverse the file hierarchy rooted in the directory that it
references.
In addition, if any symbolic links are encountered in any file tree that
.Nm chown
traverses, they will be treated in the same fashion as
.Dq Li slink .
.Pp
As consistently as possible, you can specify the default behavior by
specifying the
.Fl P
(for
.Dq physical )
flag.
This flag is intended to make the entire name space look like the
physical name space.
.Pp
For commands that do not by default do file tree traversals, the
.Fl H ,
.Fl L
and
.Fl P
flags are ignored if the
.Fl R
flag is not also specified.
In addition, you may specify the
.Fl H ,
.Fl L
and
.Fl P
options more than once; the last one specified determines the
command's behavior.
This is intended to permit you to alias commands to behave one way
or the other, and then override that behavior on the command line.
.Pp
The
.Xr ls 1
and
.Xr rm 1
commands have exceptions to these rules.
The
.Nm rm
command operates on the symbolic link, and not the file it references,
and therefore never follows a symbolic link.
The
.Nm rm
command does not support the
.Fl H ,
.Fl L
or
.Fl P
options.
.Pp
To maintain compatibility with historic systems,
the
.Nm ls
command never follows symbolic links unless the
.Fl L
flag is specified.
If the
.Fl L
flag is specified,
.Nm ls
follows all symbolic links,
regardless of their type,
whether specified on the command line or encountered in the tree walk.
The
.Nm ls
command does not support the
.Fl H
or
.Fl P
options.
.Sh SEE ALSO
.Xr chflags 1 ,
.Xr chgrp 1 ,
.Xr chmod 1 ,
.Xr cp 1 ,
.Xr du 1 ,
.Xr find 1 ,
.Xr ln 1 ,
.Xr ls 1 ,
.Xr mv 1 ,
.Xr pax 1 ,
.Xr rm 1 ,
.Xr tar 1 ,
.Xr lstat 2 ,
.Xr readlink 2 ,
.Xr rename 2 ,
.Xr unlink 2 ,
.Xr fts 3 ,
.Xr remove 3 ,
.Xr chown 8

6
bin/ls/Makefile Normal file
View File

@ -0,0 +1,6 @@
# @(#)Makefile 8.1 (Berkeley) 6/2/93
PROG= ls
SRCS= cmp.c stat_flags.c ls.c print.c util.c
.include <bsd.prog.mk>

104
bin/ls/cmp.c Normal file
View File

@ -0,0 +1,104 @@
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Michael Fischbein.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)cmp.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>
#include <string.h>
#include "ls.h"
#include "extern.h"
int
namecmp(a, b)
const FTSENT *a, *b;
{
return (strcmp(a->fts_name, b->fts_name));
}
int
revnamecmp(a, b)
const FTSENT *a, *b;
{
return (strcmp(b->fts_name, a->fts_name));
}
int
modcmp(a, b)
const FTSENT *a, *b;
{
return (b->fts_statp->st_mtime - a->fts_statp->st_mtime);
}
int
revmodcmp(a, b)
const FTSENT *a, *b;
{
return (a->fts_statp->st_mtime - b->fts_statp->st_mtime);
}
int
acccmp(a, b)
const FTSENT *a, *b;
{
return (b->fts_statp->st_atime - a->fts_statp->st_atime);
}
int
revacccmp(a, b)
const FTSENT *a, *b;
{
return (a->fts_statp->st_atime - b->fts_statp->st_atime);
}
int
statcmp(a, b)
const FTSENT *a, *b;
{
return (b->fts_statp->st_ctime - a->fts_statp->st_ctime);
}
int
revstatcmp(a, b)
const FTSENT *a, *b;
{
return (a->fts_statp->st_ctime - b->fts_statp->st_ctime);
}

50
bin/ls/extern.h Normal file
View File

@ -0,0 +1,50 @@
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)extern.h 8.1 (Berkeley) 5/31/93
*/
int acccmp __P((const FTSENT *, const FTSENT *));
int revacccmp __P((const FTSENT *, const FTSENT *));
int modcmp __P((const FTSENT *, const FTSENT *));
int revmodcmp __P((const FTSENT *, const FTSENT *));
int namecmp __P((const FTSENT *, const FTSENT *));
int revnamecmp __P((const FTSENT *, const FTSENT *));
int statcmp __P((const FTSENT *, const FTSENT *));
int revstatcmp __P((const FTSENT *, const FTSENT *));
char *flags_to_string __P((u_long, char *));
void prcopy __P((char *, char *, int));
void printcol __P((DISPLAY *));
void printlong __P((DISPLAY *));
void printscol __P((DISPLAY *));
void usage __P((void));

333
bin/ls/ls.1 Normal file
View File

@ -0,0 +1,333 @@
.\" Copyright (c) 1980, 1990, 1991, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)ls.1 8.6 (Berkeley) 4/18/94
.\"
.Dd April 18, 1994
.Dt LS 1
.Os
.Sh NAME
.Nm ls
.Nd list directory contents
.Sh SYNOPSIS
.Nm ls
.Op Fl ACFLRTacdfiloqrstu1
.Op Ar file ...
.Sh DESCRIPTION
For each operand that names a
.Ar file
of a type other than
directory,
.Nm ls
displays its name as well as any requested,
associated information.
For each operand that names a
.Ar file
of type directory,
.Nm ls
displays the names of files contained
within that directory, as well as any requested, associated
information.
.Pp
If no operands are given, the contents of the current
directory are displayed.
If more than one operand is given,
non-directory operands are displayed first; directory
and non-directory operands are sorted separately and in
lexicographical order.
.Pp
The following options are available:
.Bl -tag -width indent
.It Fl A
List all entries except for
.Ql \&.
and
.Ql \&.. .
Always set for the super-user.
.It Fl C
Force multi-column output; this is the default when output is to a terminal.
.It Fl F
Display a slash (/) immediately after each pathname
that is a directory, an asterisk (*) after each that is
executable,
and an at sign (@) after each symbolic link.
.\" and a vertical bar (|) after each that is a
.\" .Tn FIFO .
.It Fl L
If argument is a symbolic link, list the file or directory the link references
rather than the link itself.
.It Fl R
Recursively list subdirectories encountered.
.It Fl T
Display complete time information for the file, including
month, day, hour, minute, second, and year.
.It Fl a
Include directory entries whose names begin with a
dot (.).
.It Fl c
Use time when file status was last changed for sorting or printing.
.It Fl d
Directories are listed as plain files (not searched recursively) and
symbolic links in the argument list are not indirected through.
.It Fl f
Output is not sorted.
.It Fl i
For each file, print the file's file serial number (inode number).
.It Fl l
(The lowercase letter ``ell.'') List in long format. (See below.)
If the output is to a terminal, a total sum for all the file
sizes is output on a line before the long listing.
.It Fl o
Include the file flags in a long
.Pq Fl l
output
.It Fl q
Force printing of non-graphic characters in file names as
the character `?'; this is the default when output is to a terminal.
.It Fl r
Reverse the order of the sort to get reverse
lexicographical order or the oldest entries first.
.It Fl s
Display the number of file system blocks actually used by each file, in units
of 512 bytes, where partial units are rounded up to the next integer value.
If the output is to a terminal, a total sum for all the file
sizes is output on a line before the listing.
.It Fl t
Sort by time modified (most recently modified
first) before sorting the operands by lexicographical
order.
.It Fl u
Use time of last access,
instead of last modification
of the file for sorting
.Pq Fl t
or printing
.Pq Fl l .
.It Fl \&1
(The numeric digit ``one.'') Force output to be
one entry per line.
This is the default when
output is not to a terminal.
.El
.Pp
The
.Fl 1 ,
.Fl C ,
and
.Fl l
options all override each other; the last one specified determines
the format used.
.Pp
The
.Fl c ,
and
.Fl u
options override each other; the last one specified determines
the file time used.
.Pp
By default,
.Nm ls
lists one entry per line to standard
output; the exceptions are to terminals or when the
.Fl C
option is specified.
.Pp
File information is displayed with one or more
<blank>s separating the information associated with the
.Fl i ,
.Fl s ,
and
.Fl l
options.
.Ss The Long Format
If the
.Fl l
option is given, the following information
is displayed for each file:
file mode,
number of links, owner name, group name,
number of bytes in the file, abbreviated
month, day-of-month file was last modified,
hour file last modified, minute file last
modified, and the pathname.
In addition, for each directory whose contents are displayed, the total
number of 512-byte blocks used by the files in the directory is displayed
on a line by itself immediately before the information for the files in the
directory.
.Pp
If the owner or group names are not a known user or group name
the numeric ID's are displayed.
.Pp
If the file is a character special or block special file,
the major and minor device numbers for the file are displayed
in the size field. If the file is a symbolic link the pathname of the
linked-to file is preceded by
.Dq \-> .
.Pp
The file mode printed under the -l option consists of the
entry type, owner permissions, and group permissions.
The entry type character describes the type of file, as
follows:
.Pp
.Bl -tag -width 4n -offset indent -compact
.It Sy b
Block special file.
.It Sy c
Character special file.
.It Sy d
Directory.
.It Sy l
Symbolic link.
.It Sy s
Socket link.
.\" .It Sy p
.\" .Tn FIFO .
.It Sy \-
Regular file.
.El
.Pp
The next three fields
are three characters each:
owner permissions,
group permissions, and
other permissions.
Each field has three character positions:
.Bl -enum -offset indent
.It
If
.Sy r ,
the file is readable; if
.Sy \- ,
it is not readable.
.It
If
.Sy w ,
the file is writable; if
.Sy \- ,
it is not writable.
.It
The first of the following that applies:
.Bl -tag -width 4n -offset indent
.It Sy S
If in the owner permissions, the file is not executable and
set-user-ID mode is set.
If in the group permissions, the file is not executable
and set-group-ID mode is set.
.It Sy s
If in the owner permissions, the file is executable
and set-user-ID mode is set.
If in the group permissions, the file is executable
and setgroup-ID mode is set.
.It Sy x
The file is executable or the directory is
searchable.
.It Sy \-
The file is neither readable, writeable, executable,
nor set-user-ID nor set-group-ID mode, nor sticky. (See below.)
.El
.Pp
These next two apply only to the third character in the last group
(other permissions).
.Bl -tag -width 4n -offset indent
.It Sy T
The sticky bit is set
(mode
.Li 1000 ) ,
but not execute or search permission. (See
.Xr chmod 1
or
.Xr sticky 8 . )
.It Sy t
The sticky bit is set (mode
.Li 1000 ) ,
and is searchable or executable.
(See
.Xr chmod 1
or
.Xr sticky 8 . )
.El
.El
.Pp
The
.Nm ls
utility exits 0 on success, and >0 if an error occurs.
.Sh ENVIRONMENTAL VARIABLES
The following environment variables affect the execution of
.Nm ls :
.Bl -tag -width BLOCKSIZE
.It Ev BLOCKSIZE
If the environmental variable
.Ev BLOCKSIZE
is set, the block counts
(see
.Fl s )
will be displayed in units of that size block.
.It COLUMNS
If this variable contains a string representing a
decimal integer, it is used as the
column position width for displaying
multiple-text-column output.
The
.Nm ls
utility calculates how
many pathname text columns to display
based on the width provided.
(See
.Fl C . )
.It Ev TZ
The timezone to use when displaying dates.
See
.Xr environ 7
for more information.
.El
.Sh COMPATIBILITY
The group field is now automatically included in the long listing for
files in order to be compatible with the
.St -p1003.2
specification.
.Sh SEE ALSO
.Xr chmod 1 ,
.Xr symlink 7 ,
.Xr sticky 8
.Sh HISTORY
An
.Nm ls
command appeared in
.At v6 .
.Sh STANDARDS
The
.Nm ls
function is expected to be a superset of the
.St -p1003.2
specification.

506
bin/ls/ls.c Normal file
View File

@ -0,0 +1,506 @@
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Michael Fischbein.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1989, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fts.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ls.h"
#include "extern.h"
static void display __P((FTSENT *, FTSENT *));
static int mastercmp __P((const FTSENT **, const FTSENT **));
static void traverse __P((int, char **, int));
static void (*printfcn) __P((DISPLAY *));
static int (*sortfcn) __P((const FTSENT *, const FTSENT *));
long blocksize; /* block size units */
int termwidth = 80; /* default terminal width */
/* flags */
int f_accesstime; /* use time of last access */
int f_column; /* columnated format */
int f_flags; /* show flags associated with a file */
int f_inode; /* print inode */
int f_listdir; /* list actual directory, not contents */
int f_listdot; /* list files beginning with . */
int f_longform; /* long listing format */
int f_newline; /* if precede with newline */
int f_nonprint; /* show unprintables as ? */
int f_nosort; /* don't sort output */
int f_recursive; /* ls subdirectories also */
int f_reversesort; /* reverse whatever sort is used */
int f_sectime; /* print the real time for all files */
int f_singlecol; /* use single column output */
int f_size; /* list size in short listing */
int f_statustime; /* use time of last mode change */
int f_dirname; /* if precede with directory name */
int f_timesort; /* sort by time vice name */
int f_type; /* add type character for non-regular files */
int
main(argc, argv)
int argc;
char *argv[];
{
static char dot[] = ".", *dotav[] = { dot, NULL };
struct winsize win;
int ch, fts_options, notused;
char *p;
/* Terminal defaults to -Cq, non-terminal defaults to -1. */
if (isatty(STDOUT_FILENO)) {
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
!win.ws_col) {
if ((p = getenv("COLUMNS")) != NULL)
termwidth = atoi(p);
}
else
termwidth = win.ws_col;
f_column = f_nonprint = 1;
} else
f_singlecol = 1;
/* Root is -A automatically. */
if (!getuid())
f_listdot = 1;
fts_options = FTS_PHYSICAL;
while ((ch = getopt(argc, argv, "1ACFLRTacdfgiloqrstu")) != EOF) {
switch (ch) {
/*
* The -1, -C and -l options all override each other so shell
* aliasing works right.
*/
case '1':
f_singlecol = 1;
f_column = f_longform = 0;
break;
case 'C':
f_column = 1;
f_longform = f_singlecol = 0;
break;
case 'l':
f_longform = 1;
f_column = f_singlecol = 0;
break;
/* The -c and -u options override each other. */
case 'c':
f_statustime = 1;
f_accesstime = 0;
break;
case 'u':
f_accesstime = 1;
f_statustime = 0;
break;
case 'F':
f_type = 1;
break;
case 'L':
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
break;
case 'R':
f_recursive = 1;
break;
case 'a':
fts_options |= FTS_SEEDOT;
/* FALLTHROUGH */
case 'A':
f_listdot = 1;
break;
/* The -d option turns off the -R option. */
case 'd':
f_listdir = 1;
f_recursive = 0;
break;
case 'f':
f_nosort = 1;
break;
case 'g': /* Compatibility with 4.3BSD. */
break;
case 'i':
f_inode = 1;
break;
case 'o':
f_flags = 1;
break;
case 'q':
f_nonprint = 1;
break;
case 'r':
f_reversesort = 1;
break;
case 's':
f_size = 1;
break;
case 'T':
f_sectime = 1;
break;
case 't':
f_timesort = 1;
break;
default:
case '?':
usage();
}
}
argc -= optind;
argv += optind;
/*
* If not -F, -i, -l, -s or -t options, don't require stat
* information.
*/
if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type)
fts_options |= FTS_NOSTAT;
/*
* If not -F, -d or -l options, follow any symbolic links listed on
* the command line.
*/
if (!f_longform && !f_listdir && !f_type)
fts_options |= FTS_COMFOLLOW;
/* If -l or -s, figure out block size. */
if (f_longform || f_size) {
(void)getbsize(&notused, &blocksize);
blocksize /= 512;
}
/* Select a sort function. */
if (f_reversesort) {
if (!f_timesort)
sortfcn = revnamecmp;
else if (f_accesstime)
sortfcn = revacccmp;
else if (f_statustime)
sortfcn = revstatcmp;
else /* Use modification time. */
sortfcn = revmodcmp;
} else {
if (!f_timesort)
sortfcn = namecmp;
else if (f_accesstime)
sortfcn = acccmp;
else if (f_statustime)
sortfcn = statcmp;
else /* Use modification time. */
sortfcn = modcmp;
}
/* Select a print function. */
if (f_singlecol)
printfcn = printscol;
else if (f_longform)
printfcn = printlong;
else
printfcn = printcol;
if (argc)
traverse(argc, argv, fts_options);
else
traverse(1, dotav, fts_options);
exit(0);
}
static int output; /* If anything output. */
/*
* Traverse() walks the logical directory structure specified by the argv list
* in the order specified by the mastercmp() comparison function. During the
* traversal it passes linked lists of structures to display() which represent
* a superset (may be exact set) of the files to be displayed.
*/
static void
traverse(argc, argv, options)
int argc, options;
char *argv[];
{
FTS *ftsp;
FTSENT *p, *chp;
int ch_options;
if ((ftsp =
fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
err(1, NULL);
display(NULL, fts_children(ftsp, 0));
if (f_listdir)
return;
/*
* If not recursing down this tree and don't need stat info, just get
* the names.
*/
ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
while ((p = fts_read(ftsp)) != NULL)
switch (p->fts_info) {
case FTS_DC:
warnx("%s: directory causes a cycle", p->fts_name);
break;
case FTS_DNR:
case FTS_ERR:
warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
break;
case FTS_D:
if (p->fts_level != FTS_ROOTLEVEL &&
p->fts_name[0] == '.' && !f_listdot)
break;
/*
* If already output something, put out a newline as
* a separator. If multiple arguments, precede each
* directory with its name.
*/
if (output)
(void)printf("\n%s:\n", p->fts_path);
else if (argc > 1) {
(void)printf("%s:\n", p->fts_path);
output = 1;
}
chp = fts_children(ftsp, ch_options);
display(p, chp);
if (!f_recursive && chp != NULL)
(void)fts_set(ftsp, p, FTS_SKIP);
break;
}
if (errno)
err(1, "fts_read");
}
/*
* Display() takes a linked list of FTSENT structures and passes the list
* along with any other necessary information to the print function. P
* points to the parent directory of the display list.
*/
static void
display(p, list)
FTSENT *p, *list;
{
struct stat *sp;
DISPLAY d;
FTSENT *cur;
NAMES *np;
u_quad_t maxsize;
u_long btotal, maxblock, maxinode, maxlen, maxnlink;
int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser;
int entries, needstats;
char *user, *group, *flags, buf[20]; /* 32 bits == 10 digits */
/*
* If list is NULL there are two possibilities: that the parent
* directory p has no children, or that fts_children() returned an
* error. We ignore the error case since it will be replicated
* on the next call to fts_read() on the post-order visit to the
* directory p, and will be signalled in traverse().
*/
if (list == NULL)
return;
needstats = f_inode || f_longform || f_size;
flen = 0;
btotal = maxblock = maxinode = maxlen = maxnlink = 0;
bcfile = 0;
maxuser = maxgroup = maxflags = 0;
maxsize = 0;
for (cur = list, entries = 0; cur; cur = cur->fts_link) {
if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
warnx("%s: %s",
cur->fts_name, strerror(cur->fts_errno));
cur->fts_number = NO_PRINT;
continue;
}
/*
* P is NULL if list is the argv list, to which different rules
* apply.
*/
if (p == NULL) {
/* Directories will be displayed later. */
if (cur->fts_info == FTS_D && !f_listdir) {
cur->fts_number = NO_PRINT;
continue;
}
} else {
/* Only display dot file if -a/-A set. */
if (cur->fts_name[0] == '.' && !f_listdot) {
cur->fts_number = NO_PRINT;
continue;
}
}
if (f_nonprint)
prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen);
if (cur->fts_namelen > maxlen)
maxlen = cur->fts_namelen;
if (needstats) {
sp = cur->fts_statp;
if (sp->st_blocks > maxblock)
maxblock = sp->st_blocks;
if (sp->st_ino > maxinode)
maxinode = sp->st_ino;
if (sp->st_nlink > maxnlink)
maxnlink = sp->st_nlink;
if (sp->st_size > maxsize)
maxsize = sp->st_size;
btotal += sp->st_blocks;
if (f_longform) {
user = user_from_uid(sp->st_uid, 0);
if ((ulen = strlen(user)) > maxuser)
maxuser = ulen;
group = group_from_gid(sp->st_gid, 0);
if ((glen = strlen(group)) > maxgroup)
maxgroup = glen;
if (f_flags) {
flags =
flags_to_string(sp->st_flags, "-");
if ((flen = strlen(flags)) > maxflags)
maxflags = flen;
} else
flen = 0;
if ((np = malloc(sizeof(NAMES) +
ulen + glen + flen + 3)) == NULL)
err(1, NULL);
np->user = &np->data[0];
(void)strcpy(np->user, user);
np->group = &np->data[ulen + 1];
(void)strcpy(np->group, group);
if (S_ISCHR(sp->st_mode) ||
S_ISBLK(sp->st_mode))
bcfile = 1;
if (f_flags) {
np->flags = &np->data[ulen + glen + 2];
(void)strcpy(np->flags, flags);
}
cur->fts_pointer = np;
}
}
++entries;
}
if (!entries)
return;
d.list = list;
d.entries = entries;
d.maxlen = maxlen;
if (needstats) {
d.bcfile = bcfile;
d.btotal = btotal;
(void)snprintf(buf, sizeof(buf), "%lu", maxblock);
d.s_block = strlen(buf);
d.s_flags = maxflags;
d.s_group = maxgroup;
(void)snprintf(buf, sizeof(buf), "%lu", maxinode);
d.s_inode = strlen(buf);
(void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
d.s_nlink = strlen(buf);
(void)snprintf(buf, sizeof(buf), "%qu", maxsize);
d.s_size = strlen(buf);
d.s_user = maxuser;
}
printfcn(&d);
output = 1;
if (f_longform)
for (cur = list; cur; cur = cur->fts_link)
free(cur->fts_pointer);
}
/*
* Ordering for mastercmp:
* If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
* as larger than directories. Within either group, use the sort function.
* All other levels use the sort function. Error entries remain unsorted.
*/
static int
mastercmp(a, b)
const FTSENT **a, **b;
{
int a_info, b_info;
a_info = (*a)->fts_info;
if (a_info == FTS_ERR)
return (0);
b_info = (*b)->fts_info;
if (b_info == FTS_ERR)
return (0);
if (a_info == FTS_NS || b_info == FTS_NS)
return (namecmp(*a, *b));
if (a_info == b_info)
return (sortfcn(*a, *b));
if ((*a)->fts_level == FTS_ROOTLEVEL)
if (a_info == FTS_D)
return (1);
else if (b_info == FTS_D)
return (-1);
else
return (sortfcn(*a, *b));
else
return (sortfcn(*a, *b));
}

72
bin/ls/ls.h Normal file
View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Michael Fischbein.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ls.h 8.1 (Berkeley) 5/31/93
*/
#define NO_PRINT 1
extern long blocksize; /* block size units */
extern int f_accesstime; /* use time of last access */
extern int f_flags; /* show flags associated with a file */
extern int f_inode; /* print inode */
extern int f_longform; /* long listing format */
extern int f_sectime; /* print the real time for all files */
extern int f_size; /* list size in short listing */
extern int f_statustime; /* use time of last mode change */
extern int f_type; /* add type character for non-regular files */
typedef struct {
FTSENT *list;
u_long btotal;
int bcfile;
int entries;
int maxlen;
int s_block;
int s_flags;
int s_group;
int s_inode;
int s_nlink;
int s_size;
int s_user;
} DISPLAY;
typedef struct {
char *user;
char *group;
char *flags;
char data[1];
} NAMES;

293
bin/ls/print.c Normal file
View File

@ -0,0 +1,293 @@
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Michael Fischbein.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94";
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fts.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <tzfile.h>
#include <unistd.h>
#include <utmp.h>
#include "ls.h"
#include "extern.h"
static int printaname __P((FTSENT *, u_long, u_long));
static void printlink __P((FTSENT *));
static void printtime __P((time_t));
static int printtype __P((u_int));
#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
void
printscol(dp)
DISPLAY *dp;
{
FTSENT *p;
for (p = dp->list; p; p = p->fts_link) {
if (IS_NOPRINT(p))
continue;
(void)printaname(p, dp->s_inode, dp->s_block);
(void)putchar('\n');
}
}
void
printlong(dp)
DISPLAY *dp;
{
struct stat *sp;
FTSENT *p;
NAMES *np;
char buf[20];
if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
for (p = dp->list; p; p = p->fts_link) {
if (IS_NOPRINT(p))
continue;
sp = p->fts_statp;
if (f_inode)
(void)printf("%*lu ", dp->s_inode, sp->st_ino);
if (f_size)
(void)printf("%*qd ",
dp->s_block, howmany(sp->st_blocks, blocksize));
(void)strmode(sp->st_mode, buf);
np = p->fts_pointer;
(void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
sp->st_nlink, dp->s_user, np->user, dp->s_group,
np->group);
if (f_flags)
(void)printf("%-*s ", dp->s_flags, np->flags);
if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
(void)printf("%3d, %3d ",
major(sp->st_rdev), minor(sp->st_rdev));
else if (dp->bcfile)
(void)printf("%*s%*qd ",
8 - dp->s_size, "", dp->s_size, sp->st_size);
else
(void)printf("%*qd ", dp->s_size, sp->st_size);
if (f_accesstime)
printtime(sp->st_atime);
else if (f_statustime)
printtime(sp->st_ctime);
else
printtime(sp->st_mtime);
(void)printf("%s", p->fts_name);
if (f_type)
(void)printtype(sp->st_mode);
if (S_ISLNK(sp->st_mode))
printlink(p);
(void)putchar('\n');
}
}
#define TAB 8
void
printcol(dp)
DISPLAY *dp;
{
extern int termwidth;
static FTSENT **array;
static int lastentries = -1;
FTSENT *p;
int base, chcnt, cnt, col, colwidth, num;
int endcol, numcols, numrows, row;
/*
* Have to do random access in the linked list -- build a table
* of pointers.
*/
if (dp->entries > lastentries) {
lastentries = dp->entries;
if ((array =
realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
warn(NULL);
printscol(dp);
}
}
for (p = dp->list, num = 0; p; p = p->fts_link)
if (p->fts_number != NO_PRINT)
array[num++] = p;
colwidth = dp->maxlen;
if (f_inode)
colwidth += dp->s_inode + 1;
if (f_size)
colwidth += dp->s_block + 1;
if (f_type)
colwidth += 1;
colwidth = (colwidth + TAB) & ~(TAB - 1);
if (termwidth < 2 * colwidth) {
printscol(dp);
return;
}
numcols = termwidth / colwidth;
numrows = num / numcols;
if (num % numcols)
++numrows;
if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
for (row = 0; row < numrows; ++row) {
endcol = colwidth;
for (base = row, chcnt = col = 0; col < numcols; ++col) {
chcnt += printaname(array[base], dp->s_inode,
dp->s_block);
if ((base += numrows) >= num)
break;
while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
(void)putchar('\t');
chcnt = cnt;
}
endcol += colwidth;
}
(void)putchar('\n');
}
}
/*
* print [inode] [size] name
* return # of characters printed, no trailing characters.
*/
static int
printaname(p, inodefield, sizefield)
FTSENT *p;
u_long sizefield, inodefield;
{
struct stat *sp;
int chcnt;
sp = p->fts_statp;
chcnt = 0;
if (f_inode)
chcnt += printf("%*lu ", (int)inodefield, sp->st_ino);
if (f_size)
chcnt += printf("%*qd ",
(int)sizefield, howmany(sp->st_blocks, blocksize));
chcnt += printf("%s", p->fts_name);
if (f_type)
chcnt += printtype(sp->st_mode);
return (chcnt);
}
static void
printtime(ftime)
time_t ftime;
{
int i;
char *longstring;
longstring = ctime(&ftime);
for (i = 4; i < 11; ++i)
(void)putchar(longstring[i]);
#define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
if (f_sectime)
for (i = 11; i < 24; i++)
(void)putchar(longstring[i]);
else if (ftime + SIXMONTHS > time(NULL))
for (i = 11; i < 16; ++i)
(void)putchar(longstring[i]);
else {
(void)putchar(' ');
for (i = 20; i < 24; ++i)
(void)putchar(longstring[i]);
}
(void)putchar(' ');
}
static int
printtype(mode)
u_int mode;
{
switch (mode & S_IFMT) {
case S_IFDIR:
(void)putchar('/');
return (1);
case S_IFIFO:
(void)putchar('|');
return (1);
case S_IFLNK:
(void)putchar('@');
return (1);
case S_IFSOCK:
(void)putchar('=');
return (1);
}
if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
(void)putchar('*');
return (1);
}
return (0);
}
static void
printlink(p)
FTSENT *p;
{
int lnklen;
char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
if (p->fts_level == FTS_ROOTLEVEL)
(void)snprintf(name, sizeof(name), "%s", p->fts_name);
else
(void)snprintf(name, sizeof(name),
"%s/%s", p->fts_parent->fts_accpath, p->fts_name);
if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
return;
}
path[lnklen] = '\0';
(void)printf(" -> %s", path);
}

148
bin/ls/stat_flags.c Normal file
View File

@ -0,0 +1,148 @@
/*-
* Copyright (c) 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)stat_flags.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <stddef.h>
#include <string.h>
#define SAPPEND(s) { \
if (prefix != NULL) \
(void)strcat(string, prefix); \
(void)strcat(string, s); \
prefix = ","; \
}
/*
* flags_to_string --
* Convert stat flags to a comma-separated string. If no flags
* are set, return the default string.
*/
char *
flags_to_string(flags, def)
u_long flags;
char *def;
{
static char string[128];
char *prefix;
string[0] = '\0';
prefix = NULL;
if (flags & UF_APPEND)
SAPPEND("uappnd");
if (flags & UF_IMMUTABLE)
SAPPEND("uchg");
if (flags & UF_NODUMP)
SAPPEND("nodump");
if (flags & SF_APPEND)
SAPPEND("sappnd");
if (flags & SF_ARCHIVED)
SAPPEND("arch");
if (flags & SF_IMMUTABLE)
SAPPEND("schg");
return (prefix == NULL && def != NULL ? def : string);
}
#define TEST(a, b, f) { \
if (!memcmp(a, b, sizeof(b))) { \
if (clear) { \
if (clrp) \
*clrp |= (f); \
} else if (setp) \
*setp |= (f); \
break; \
} \
}
/*
* string_to_flags --
* Take string of arguments and return stat flags. Return 0 on
* success, 1 on failure. On failure, stringp is set to point
* to the offending token.
*/
int
string_to_flags(stringp, setp, clrp)
char **stringp;
u_long *setp, *clrp;
{
int clear;
char *string, *p;
clear = 0;
if (setp)
*setp = 0;
if (clrp)
*clrp = 0;
string = *stringp;
while ((p = strsep(&string, "\t ,")) != NULL) {
*stringp = p;
if (*p == '\0')
continue;
if (p[0] == 'n' && p[1] == 'o') {
clear = 1;
p += 2;
}
switch (p[0]) {
case 'a':
TEST(p, "arch", SF_ARCHIVED);
TEST(p, "archived", SF_ARCHIVED);
return (1);
case 'd':
clear = !clear;
TEST(p, "dump", UF_NODUMP);
return (1);
case 's':
TEST(p, "sappnd", SF_APPEND);
TEST(p, "sappend", SF_APPEND);
TEST(p, "schg", SF_IMMUTABLE);
TEST(p, "schange", SF_IMMUTABLE);
TEST(p, "simmutable", SF_IMMUTABLE);
return (1);
case 'u':
TEST(p, "uappnd", UF_APPEND);
TEST(p, "uappend", UF_APPEND);
TEST(p, "uchg", UF_IMMUTABLE);
TEST(p, "uchange", UF_IMMUTABLE);
TEST(p, "uimmutable", UF_IMMUTABLE);
/* FALLTHROUGH */
default:
return (1);
}
}
return (0);
}

71
bin/ls/util.c Normal file
View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Michael Fischbein.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/2/94";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fts.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ls.h"
#include "extern.h"
void
prcopy(src, dest, len)
char *src, *dest;
int len;
{
int ch;
while (len--) {
ch = *src++;
*dest++ = isprint(ch) ? ch : '?';
}
}
void
usage()
{
(void)fprintf(stderr, "usage: ls [-1ACFLRTacdfiklqrstu] [file ...]\n");
exit(1);
}

5
bin/mkdir/Makefile Normal file
View File

@ -0,0 +1,5 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= mkdir
.include <bsd.prog.mk>

92
bin/mkdir/mkdir.1 Normal file
View File

@ -0,0 +1,92 @@
.\" Copyright (c) 1989, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)mkdir.1 8.2 (Berkeley) 1/25/94
.\"
.Dd January 25, 1994
.Dt MKDIR 1
.Os
.Sh NAME
.Nm mkdir
.Nd make directories
.Sh SYNOPSIS
.Nm mkdir
.Op Fl p
.Op Fl m Ar mode
.Ar directory_name ...
.Sh DESCRIPTION
.Nm Mkdir
creates the directories named as operands, in the order specified,
using mode
.Li rwxrwxrwx (\&0777)
as modified by the current
.Xr umask 2 .
.Pp
The options are as follows:
.Pp
.Bl -tag -width indent
.It Fl m
Set the file permission bits of the final created directory to
the specified mode.
The mode argument can be in any of the formats specified to the
.Xr chmod 1
command.
If a symbolic mode is specified, the operation characters
.Dq +
and
.Dq -
are interpreted relative to an initial mode of
.Dq a=rwx .
.It Fl p
Create intermediate directories as required.
If this option is not specified, the full path prefix of each
operand must already exist.
Intermediate directories are created with permission bits of
.Li rwxrwxrwx (\&0777)
as modified by the current umask, plus write and search
permission for the owner.
.El
.Pp
The user must have write permission in the parent directory.
.Pp
.Nm Mkdir
exits 0 if successful, and >0 if an error occurred.
.Sh SEE ALSO
.Xr rmdir 1
.Sh STANDARDS
The
.Nm mkdir
utility is expected to be
.St -p1003.2
compatible.

169
bin/mkdir/mkdir.c Normal file
View File

@ -0,0 +1,169 @@
/*
* Copyright (c) 1983, 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1983, 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int build __P((char *));
void usage __P((void));
int
main(argc, argv)
int argc;
char *argv[];
{
int ch, exitval, oct, omode, pflag;
mode_t *set;
char *ep, *mode;
pflag = 0;
mode = NULL;
while ((ch = getopt(argc, argv, "m:p")) != EOF)
switch(ch) {
case 'p':
pflag = 1;
break;
case 'm':
mode = optarg;
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argv[0] == NULL)
usage();
if (mode == NULL) {
omode = S_IRWXU | S_IRWXG | S_IRWXO;
oct = 1;
} else if (*mode >= '0' && *mode <= '7') {
omode = (int)strtol(mode, &ep, 8);
if (omode < 0 || *ep)
errx(1, "invalid file mode: %s", mode);
oct = 1;
} else {
if ((set = setmode(mode)) == NULL)
errx(1, "invalid file mode: %s", mode);
oct = 0;
}
for (exitval = 0; *argv != NULL; ++argv) {
if (pflag && build(*argv)) {
exitval = 1;
continue;
}
if (mkdir(*argv, oct ?
omode : getmode(set, S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
warn("%s", *argv);
exitval = 1;
}
}
exit(exitval);
}
int
build(path)
char *path;
{
struct stat sb;
mode_t numask, oumask;
int first;
char *p;
p = path;
if (p[0] == '/') /* Skip leading '/'. */
++p;
for (first = 1;; ++p) {
if (p[0] == '\0' || p[0] == '/' && p[1] == '\0')
break;
if (p[0] != '/')
continue;
*p = '\0';
if (first) {
/*
* POSIX 1003.2:
* For each dir operand that does not name an existing
* directory, effects equivalent to those cased by the
* following command shall occcur:
*
* mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
* mkdir [-m mode] dir
*
* We change the user's umask and then restore it,
* instead of doing chmod's.
*/
oumask = umask(0);
numask = oumask & ~(S_IWUSR | S_IXUSR);
(void)umask(numask);
first = 0;
}
if (stat(path, &sb)) {
if (errno != ENOENT ||
mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
warn("%s", path);
return (1);
}
}
*p = '/';
}
if (!first)
(void)umask(oumask);
return (0);
}
void
usage()
{
(void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n");
exit (1);
}

8
bin/mv/Makefile Normal file
View File

@ -0,0 +1,8 @@
# @(#)Makefile 8.2 (Berkeley) 4/2/94
PROG= mv
DPADD= ${LIBUTIL}
LDADD= -lutil
.include <bsd.prog.mk>

129
bin/mv/mv.1 Normal file
View File

@ -0,0 +1,129 @@
.\" Copyright (c) 1989, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)mv.1 8.1 (Berkeley) 5/31/93
.\"
.Dd May 31, 1993
.Dt MV 1
.Os
.Sh NAME
.Nm mv
.Nd move files
.Sh SYNOPSIS
.Nm mv
.Op Fl f | Fl i
.Ar source target
.Nm mv
.Op Fl f | Fl i
.Ar source ... source directory
.Sh DESCRIPTION
.Pp
In its first form, the
.Nm mv
utility renames the file named by the
.Ar source
operand to the destination path named by the
.Ar target
operand.
This form is assumed when the last operand does not name an already
existing directory.
.Pp
In its second form,
.Nm mv
moves each file named by a
.Ar source
operand to a destination file in the existing directory named by the
.Ar directory
operand.
The destination path for each operand is the pathname produced by the
concatenation of the last operand, a slash, and the final pathname
component of the named file.
.Pp
The following options are available:
.Bl -tag -width flag
.It Fl f
Do not prompt for confirmation before overwriting the destination
path.
(The
.Fl i
option is ignored if the
.Fl f
option is specified.)
.It Fl i
Causes
.Nm mv
to write a prompt to standard error before moving a file that would
overwrite an existing file.
If the response from the standard input begins with the character ``y'',
the move is attempted.
.El
.Pp
It is an error for either the
.Ar source
operand or the destination path to specify a directory unless both do.
.Pp
If the destination path does not have a mode which permits writing,
.Nm mv
prompts the user for confirmation as specified for the
.Fl i
option.
.Pp
As the
.Xr rename 2
call does not work across file systems,
.Nm mv
uses
.Xr cp 1
and
.Xr rm 1
to accomplish the move.
The effect is equivalent to:
.Bd -literal -offset indent
rm -f destination_path && \e
\tcp -pr source_file destination && \e
\trm -rf source_file
.Ed
.Pp
The
.Nm mv
utility exits 0 on success, and >0 if an error occurs.
.Sh SEE ALSO
.Xr cp 1 ,
.Xr symlink 7
.Sh STANDARDS
The
.Nm mv
utility is expected to be
.St -p1003.2
compatible.

300
bin/mv/mv.c Normal file
View File

@ -0,0 +1,300 @@
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Ken Smith of The State University of New York at Buffalo.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1989, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pathnames.h"
int fflg, iflg;
int copy __P((char *, char *));
int do_move __P((char *, char *));
int fastcopy __P((char *, char *, struct stat *));
void usage __P((void));
int
main(argc, argv)
int argc;
char *argv[];
{
register int baselen, len, rval;
register char *p, *endp;
struct stat sb;
int ch;
char path[MAXPATHLEN + 1];
while ((ch = getopt(argc, argv, "-if")) != EOF)
switch (ch) {
case 'i':
iflg = 1;
break;
case 'f':
fflg = 1;
break;
case '-': /* Undocumented; for compatibility. */
goto endarg;
case '?':
default:
usage();
}
endarg: argc -= optind;
argv += optind;
if (argc < 2)
usage();
/*
* If the stat on the target fails or the target isn't a directory,
* try the move. More than 2 arguments is an error in this case.
*/
if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
if (argc > 2)
usage();
exit(do_move(argv[0], argv[1]));
}
/* It's a directory, move each file into it. */
(void)strcpy(path, argv[argc - 1]);
baselen = strlen(path);
endp = &path[baselen];
*endp++ = '/';
++baselen;
for (rval = 0; --argc; ++argv) {
if ((p = strrchr(*argv, '/')) == NULL)
p = *argv;
else
++p;
if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
warnx("%s: destination pathname too long", *argv);
rval = 1;
} else {
memmove(endp, p, len + 1);
if (do_move(*argv, path))
rval = 1;
}
}
exit(rval);
}
int
do_move(from, to)
char *from, *to;
{
struct stat sb;
int ask, ch;
char modep[15];
/*
* Check access. If interactive and file exists, ask user if it
* should be replaced. Otherwise if file exists but isn't writable
* make sure the user wants to clobber it.
*/
if (!fflg && !access(to, F_OK)) {
ask = 0;
if (iflg) {
(void)fprintf(stderr, "overwrite %s? ", to);
ask = 1;
} else if (access(to, W_OK) && !stat(to, &sb)) {
strmode(sb.st_mode, modep);
(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
modep + 1, modep[9] == ' ' ? "" : " ",
user_from_uid(sb.st_uid, 0),
group_from_gid(sb.st_gid, 0), to);
ask = 1;
}
if (ask) {
if ((ch = getchar()) != EOF && ch != '\n')
while (getchar() != '\n');
if (ch != 'y')
return (0);
}
}
if (!rename(from, to))
return (0);
if (errno != EXDEV) {
warn("rename %s to %s", from, to);
return (1);
}
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (stat(from, &sb)) {
warn("%s", from);
return (1);
}
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
}
int
fastcopy(from, to, sbp)
char *from, *to;
struct stat *sbp;
{
struct timeval tval[2];
static u_int blen;
static char *bp;
register int nread, from_fd, to_fd;
if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
warn("%s", from);
return (1);
}
if ((to_fd =
open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
warn("%s", to);
(void)close(from_fd);
return (1);
}
if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
warn(NULL);
return (1);
}
while ((nread = read(from_fd, bp, blen)) > 0)
if (write(to_fd, bp, nread) != nread) {
warn("%s", to);
goto err;
}
if (nread < 0) {
warn("%s", from);
err: if (unlink(to))
warn("%s: remove", to);
(void)close(from_fd);
(void)close(to_fd);
return (1);
}
(void)close(from_fd);
if (fchown(to_fd, sbp->st_uid, sbp->st_gid))
warn("%s: set owner/group", to);
if (fchmod(to_fd, sbp->st_mode))
warn("%s: set mode", to);
tval[0].tv_sec = sbp->st_atime;
tval[1].tv_sec = sbp->st_mtime;
tval[0].tv_usec = tval[1].tv_usec = 0;
if (utimes(to, tval))
warn("%s: set times", to);
if (close(to_fd)) {
warn("%s", to);
return (1);
}
if (unlink(from)) {
warn("%s: remove", from);
return (1);
}
return (0);
}
int
copy(from, to)
char *from, *to;
{
int pid, status;
if ((pid = vfork()) == 0) {
execl(_PATH_CP, "mv", "-PRp", from, to, NULL);
warn("%s", _PATH_CP);
_exit(1);
}
if (waitpid(pid, &status, 0) == -1) {
warn("%s: waitpid", _PATH_CP);
return (1);
}
if (!WIFEXITED(status)) {
warn("%s: did not terminate normally", _PATH_CP);
return (1);
}
if (WEXITSTATUS(status)) {
warn("%s: terminated with %d (non-zero) status",
_PATH_CP, WEXITSTATUS(status));
return (1);
}
if (!(pid = vfork())) {
execl(_PATH_RM, "mv", "-rf", from, NULL);
warn("%s", _PATH_RM);
_exit(1);
}
if (waitpid(pid, &status, 0) == -1) {
warn("%s: waitpid", _PATH_RM);
return (1);
}
if (!WIFEXITED(status)) {
warn("%s: did not terminate normally", _PATH_RM);
return (1);
}
if (WEXITSTATUS(status)) {
warn("%s: terminated with %d (non-zero) status",
_PATH_RM, WEXITSTATUS(status));
return (1);
}
return (0);
}
void
usage()
{
(void)fprintf(stderr,
"usage: mv [-if] src target;\n or: mv [-if] src1 ... srcN directory\n");
exit(1);
}

37
bin/mv/pathnames.h Normal file
View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)pathnames.h 8.1 (Berkeley) 5/31/93
*/
#define _PATH_RM "/bin/rm"
#define _PATH_CP "/bin/cp"

32
bin/pax/Makefile Normal file
View File

@ -0,0 +1,32 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
# To install on versions prior to BSD 4.4 the following may have to be
# defined with CFLAGS +=
#
# -DNET2_STAT Use NET2 or older stat structure. The version of the
# stat structure is easily determined by looking at the
# basic type of an off_t (often defined in the file:
# /usr/include/sys/types.h). If off_t is a long (and is
# NOT A quad) then you must define NET2_STAT.
# This define is important, as if you do have a quad_t
# off_t and define NET2_STAT, pax will compile but will
# NOT RUN PROPERLY.
#
# -DNET2_FTS Use the older NET2 fts. To identify the version,
# examine the file: /usr/include/fts.h. If FTS_COMFOLLOW
# is not defined then you must define NET2_FTS.
# Pax may not compile if this not (un)defined properly.
#
# -DNET2_REGEX Use the older regexp.h not regex.h. The regex version
# is determined by looking at the value returned by
# regexec() (man 3 regexec). If regexec return a 1 for
# success (and NOT a 0 for success) you have the older
# regex routines and must define NET2_REGEX.
# Pax may not compile if this not (un)defined properly.
PROG= pax
SRCS= ar_io.c ar_subs.c buf_subs.c cache.c cpio.c file_subs.c ftree.c\
gen_subs.c options.c pat_rep.c pax.c sel_subs.c tables.c tar.c\
tty_subs.c
.include <bsd.prog.mk>

1288
bin/pax/ar_io.c Normal file

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More