Update kill, last, ln, mesg, mkdir, mv, nice, od, pagesize, pr, printenv.

This commit is contained in:
Serge
2022-07-10 18:33:12 -07:00
parent 26e2d7dd5e
commit 1253330adc
11 changed files with 643 additions and 768 deletions

View File

@@ -1,24 +1,24 @@
/* /*
* kill - send signal to process * kill - send signal to process
*/ */
#include <ctype.h>
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <string.h>
#include <ctype.h> #include <errno.h>
char *signm[] = { 0, char *signm[] = {
"HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", "EMT", "FPE", /* 1-8 */ 0, "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", "EMT", "FPE", /* 1-8 */
"KILL", "BUS", "SEGV", "SYS", "PIPE", "ALRM", "TERM", "URG", /* 9-16 */ "KILL", "BUS", "SEGV", "SYS", "PIPE", "ALRM", "TERM", "URG", /* 9-16 */
"STOP", "TSTP", "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 17-24 */ "STOP", "TSTP", "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 17-24 */
"XFSZ", "VTALRM", "PROF", "WINCH", 0, "USR1", "USR2", 0, /* 25-31 */ "XFSZ", "VTALRM", "PROF", "WINCH", 0, "USR1", "USR2", 0, /* 25-31 */
}; };
main(argc, argv) int main(int argc, char **argv)
char **argv;
{ {
register signo, pid, res; int signo, pid, res;
int errlev; int errlev;
extern errno;
errlev = 0; errlev = 0;
if (argc <= 1) { if (argc <= 1) {
@@ -38,22 +38,20 @@ char **argv;
printf("\n"); printf("\n");
exit(0); exit(0);
} else if (isdigit(argv[1][1])) { } else if (isdigit(argv[1][1])) {
signo = atoi(argv[1]+1); signo = atoi(argv[1] + 1);
if (signo < 0 || signo > NSIG) { if (signo < 0 || signo > NSIG) {
printf("kill: %s: number out of range\n", printf("kill: %s: number out of range\n", argv[1]);
argv[1]);
exit(1); exit(1);
} }
} else { } else {
char *name = argv[1]+1; char *name = argv[1] + 1;
for (signo = 0; signo <= NSIG; signo++) for (signo = 0; signo <= NSIG; signo++)
if (signm[signo] && !strcmp(signm[signo], name)) if (signm[signo] && !strcmp(signm[signo], name))
goto foundsig; goto foundsig;
printf("kill: %s: unknown signal; kill -l lists signals\n", name); printf("kill: %s: unknown signal; kill -l lists signals\n", name);
exit(1); exit(1);
foundsig:
;
} }
foundsig:
argc--; argc--;
argv++; argv++;
} else } else
@@ -63,12 +61,12 @@ foundsig:
if (!(isdigit(**argv) || **argv == '-')) if (!(isdigit(**argv) || **argv == '-'))
goto usage; goto usage;
res = kill(pid = atoi(*argv), signo); res = kill(pid = atoi(*argv), signo);
if (res<0) { if (res < 0) {
printf("%u: %s\n", pid, strerror(errno)); printf("%u: %s\n", pid, strerror(errno));
errlev = 1; errlev = 1;
} }
argc--; argc--;
argv++; argv++;
} }
return(errlev); return (errlev);
} }

View File

@@ -5,38 +5,41 @@
* All rights reserved. The Berkeley software License Agreement * All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution. * specifies the terms and conditions for redistribution.
*/ */
#include <sys/types.h> #include <paths.h>
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h>
#include <utmp.h> #include <utmp.h>
#include <paths.h> #include <pwd.h>
#include <fcntl.h>
#define NMAX sizeof(buf[0].ut_name) #define NMAX sizeof(buf[0].ut_name)
#define LMAX sizeof(buf[0].ut_line) #define LMAX sizeof(buf[0].ut_line)
#define HMAX sizeof(buf[0].ut_host) #define HMAX sizeof(buf[0].ut_host)
#define SECDAY ((long)24*60*60) #define SECDAY ((long)24 * 60 * 60)
#define lineq(a,b) (!strncmp(a,b,LMAX)) #define lineq(a, b) (!strncmp(a, b, LMAX))
#define nameq(a,b) (!strncmp(a,b,NMAX)) #define nameq(a, b) (!strncmp(a, b, NMAX))
#define hosteq(a,b) (!strncmp(a,b,HMAX)) #define hosteq(a, b) (!strncmp(a, b, HMAX))
#define MAXTTYS 256 #define MAXTTYS 256
char **argv; char **argv;
int argc; int argc;
int nameargs; int nameargs;
struct utmp buf[128]; struct utmp buf[128];
char ttnames[MAXTTYS][LMAX+1]; char ttnames[MAXTTYS][LMAX + 1];
long logouts[MAXTTYS]; long logouts[MAXTTYS];
char *ctime(), *strspl(); static char *strspl(char *left, char *right);
static int want(struct utmp *bp);
void onintr(signo) void onintr(int signo)
int signo;
{ {
char *ct; char *ct;
@@ -49,52 +52,48 @@ void onintr(signo)
exit(1); exit(1);
} }
void void usage(char *progname)
usage(progname)
char *progname;
{ {
printf("Usage: %s [ -f filename ] [-number] [name...] [tty...]\n", printf("Usage: %s [ -f filename ] [-number] [name...] [tty...]\n", progname);
progname);
exit(1); exit(1);
} }
main(ac, av) int main(int ac, char **av)
char **av;
{ {
register int i, k; int i, k;
int wtmp; int wtmp;
off_t bl; off_t bl;
char *ct; char *ct;
char wtmpfile[256]; char wtmpfile[256];
char progname[256]; char progname[256];
register struct utmp *bp; struct utmp *bp;
long otime; long otime;
struct stat stb; struct stat stb;
int print; int print;
int sinput = 0; int sinput = 0;
char * crmsg = (char *)0; char *crmsg = (char *)0;
long crtime; long crtime;
long outrec = 0; long outrec = 0;
long maxrec = 0x7fffffffL; long maxrec = 0x7fffffffL;
time(&buf[0].ut_time); time(&buf[0].ut_time);
strcpy(wtmpfile, _PATH_WTMP); strcpy(wtmpfile, _PATH_WTMP);
strcpy(progname,av[0]); strcpy(progname, av[0]);
ac--, av++; ac--, av++;
nameargs = argc = ac; nameargs = argc = ac;
argv = av; argv = av;
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if (argv[i][0] == '-' ) { if (argv[i][0] == '-') {
if ( argv[i][1] >= '0' && argv[i][1] <= '9') { if (argv[i][1] >= '0' && argv[i][1] <= '9') {
maxrec = atoi(argv[i]+1); maxrec = atoi(argv[i] + 1);
nameargs--; nameargs--;
continue; continue;
} else { } else {
if (argv[i][1] == 'f') { if (argv[i][1] == 'f') {
i++; i++;
if ( i < argc) { if (i < argc) {
strcpy(wtmpfile,argv[i]); strcpy(wtmpfile, argv[i]);
nameargs = nameargs -2; nameargs = nameargs - 2;
continue; continue;
} else { } else {
usage(progname); usage(progname);
@@ -104,7 +103,7 @@ main(ac, av)
} }
} }
} }
if (strlen(argv[i])>2) if (strlen(argv[i]) > 2)
continue; continue;
if (!strcmp(argv[i], "~")) if (!strcmp(argv[i], "~"))
continue; continue;
@@ -122,28 +121,24 @@ main(ac, av)
exit(1); exit(1);
} }
fstat(wtmp, &stb); fstat(wtmp, &stb);
bl = (stb.st_size + sizeof (buf)-1) / sizeof (buf); bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
if (signal(SIGINT, SIG_IGN) != SIG_IGN) { if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
signal(SIGINT, onintr); signal(SIGINT, onintr);
signal(SIGQUIT, onintr); signal(SIGQUIT, onintr);
} }
for (bl--; bl >= 0; bl--) { for (bl--; bl >= 0; bl--) {
lseek(wtmp, bl * sizeof (buf), 0); lseek(wtmp, bl * sizeof(buf), 0);
bp = &buf[read(wtmp, buf, sizeof (buf)) / sizeof(buf[0]) - 1]; bp = &buf[read(wtmp, buf, sizeof(buf)) / sizeof(buf[0]) - 1];
for ( ; bp >= buf; bp--) { for (; bp >= buf; bp--) {
print = want(bp); print = want(bp);
if (print) { if (print) {
ct = ctime(&bp->ut_time); ct = ctime(&bp->ut_time);
printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s ", printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s ", NMAX, NMAX, bp->ut_name, LMAX, LMAX,
NMAX, NMAX, bp->ut_name, bp->ut_line, HMAX, HMAX, bp->ut_host, ct, 11 + ct);
LMAX, LMAX, bp->ut_line,
HMAX, HMAX, bp->ut_host,
ct, 11+ct);
} }
for (i = 0; i < MAXTTYS; i++) { for (i = 0; i < MAXTTYS; i++) {
if (ttnames[i][0] == 0) { if (ttnames[i][0] == 0) {
strncpy(ttnames[i], bp->ut_line, strncpy(ttnames[i], bp->ut_line, sizeof(bp->ut_line));
sizeof(bp->ut_line));
otime = logouts[i]; otime = logouts[i];
logouts[i] = bp->ut_time; logouts[i] = bp->ut_time;
break; break;
@@ -165,16 +160,12 @@ main(ac, av)
otime = -otime; otime = -otime;
printf("- %s", crmsg); printf("- %s", crmsg);
} else } else
printf("- %5.5s", printf("- %5.5s", ctime(&otime) + 11);
ctime(&otime)+11);
delta = otime - bp->ut_time; delta = otime - bp->ut_time;
if (delta < SECDAY) if (delta < SECDAY)
printf(" (%5.5s)\n", printf(" (%5.5s)\n", asctime(gmtime(&delta)) + 11);
asctime(gmtime(&delta))+11);
else else
printf(" (%ld+%5.5s)\n", printf(" (%ld+%5.5s)\n", delta / SECDAY, asctime(gmtime(&delta)) + 11);
delta / SECDAY,
asctime(gmtime(&delta))+11);
} }
fflush(stdout); fflush(stdout);
if (++outrec >= maxrec) if (++outrec >= maxrec)
@@ -195,14 +186,13 @@ main(ac, av)
exit(0); exit(0);
} }
want(bp) int want(struct utmp *bp)
struct utmp *bp;
{ {
register char **av; char **av;
register int ac; int ac;
if (bp->ut_line[0] == '~' && bp->ut_name[0] == '\0') if (bp->ut_line[0] == '~' && bp->ut_name[0] == '\0')
strcpy(bp->ut_name, "reboot"); /* bandaid */ strcpy(bp->ut_name, "reboot"); /* bandaid */
if (strncmp(bp->ut_line, "ftp", 3) == 0) if (strncmp(bp->ut_line, "ftp", 3) == 0)
bp->ut_line[3] = '\0'; bp->ut_line[3] = '\0';
if (strncmp(bp->ut_line, "uucp", 4) == 0) if (strncmp(bp->ut_line, "uucp", 4) == 0)
@@ -221,11 +211,9 @@ want(bp)
return (0); return (0);
} }
char * char *strspl(char *left, char *right)
strspl(left, right)
char *left, *right;
{ {
char *res = (char *)malloc(strlen(left)+strlen(right)+1); char *res = (char *)malloc(strlen(left) + strlen(right) + 1);
strcpy(res, left); strcpy(res, left);
strcat(res, right); strcat(res, right);

View File

@@ -1,24 +1,25 @@
/* /*
* ln * ln
*/ */
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <sys/types.h>
struct stat stb; struct stat stb;
int fflag; /* force flag set? */ int fflag; /* force flag set? */
int sflag; int sflag;
char name[BUFSIZ]; char name[BUFSIZ];
char *rindex();
extern int errno;
main(argc, argv) static int linkit(char *from, char *to);
int argc;
register char **argv; int main(int argc, char **argv)
{ {
register int i, r; int i, r;
argc--, argv++; argc--, argv++;
again: again:
@@ -39,35 +40,31 @@ again:
argc++; argc++;
} }
if (sflag == 0 && argc > 2) { if (sflag == 0 && argc > 2) {
if (stat(argv[argc-1], &stb) < 0) if (stat(argv[argc - 1], &stb) < 0)
goto usage; goto usage;
if ((stb.st_mode&S_IFMT) != S_IFDIR) if ((stb.st_mode & S_IFMT) != S_IFDIR)
goto usage; goto usage;
} }
r = 0; r = 0;
for(i = 0; i < argc-1; i++) for (i = 0; i < argc - 1; i++)
r |= linkit(argv[i], argv[argc-1]); r |= linkit(argv[i], argv[argc - 1]);
exit(r); exit(r);
usage: usage:
fprintf(stderr, "Usage: ln [ -s ] f1\nor: ln [ -s ] f1 f2\nln [ -s ] f1 ... fn d2\n"); fprintf(stderr, "Usage: ln [ -s ] f1\nor: ln [ -s ] f1 f2\nln [ -s ] f1 ... fn d2\n");
exit(1); exit(1);
} }
int link(), symlink(); int linkit(char *from, char *to)
linkit(from, to)
char *from, *to;
{ {
char *tail; char *tail;
int (*linkf)() = sflag ? symlink : link; int (*linkf)(const char *target, const char *linkpath) = sflag ? symlink : link;
/* is target a directory? */ /* is target a directory? */
if (sflag == 0 && fflag == 0 && stat(from, &stb) >= 0 if (sflag == 0 && fflag == 0 && stat(from, &stb) >= 0 && (stb.st_mode & S_IFMT) == S_IFDIR) {
&& (stb.st_mode&S_IFMT) == S_IFDIR) {
printf("%s is a directory\n", from); printf("%s is a directory\n", from);
return (1); return (1);
} }
if (stat(to, &stb) >= 0 && (stb.st_mode&S_IFMT) == S_IFDIR) { if (stat(to, &stb) >= 0 && (stb.st_mode & S_IFMT) == S_IFDIR) {
tail = rindex(from, '/'); tail = rindex(from, '/');
if (tail == 0) if (tail == 0)
tail = from; tail = from;

View File

@@ -8,50 +8,54 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h>
struct stat sbuf; struct stat sbuf;
char *tty; char *tty;
char *ttyname();
main(argc, argv) void error(char *s)
char *argv[];
{ {
int r=0; fprintf(stderr, "mesg: %s\n", s);
exit(-1);
}
void newmode(m)
{
if (chmod(tty, m) < 0)
error("cannot change mode");
}
int main(int argc, char *argv[])
{
int r = 0;
tty = ttyname(2); tty = ttyname(2);
if (tty == 0) if (tty == 0)
exit(13); exit(13);
if(stat(tty, &sbuf) < 0) error("cannot stat"); if (stat(tty, &sbuf) < 0)
if(argc < 2) { error("cannot stat");
if(sbuf.st_mode & 020) if (argc < 2) {
fprintf(stderr,"is y\n"); if (sbuf.st_mode & 020)
else { r=1; fprintf(stderr, "is y\n");
fprintf(stderr,"is n\n"); else {
r = 1;
fprintf(stderr, "is n\n");
} }
} else switch(*argv[1]) { } else
switch (*argv[1]) {
case 'y': case 'y':
newmode(sbuf.st_mode|020); break; newmode(sbuf.st_mode | 020);
break;
case 'n': case 'n':
newmode(sbuf.st_mode&~020); r=1; break; newmode(sbuf.st_mode & ~020);
r = 1;
break;
default: default:
error("usage: mesg [y] [n]"); error("usage: mesg [y] [n]");
} }
exit(r); exit(r);
} }
error(s)
char *s;
{
fprintf(stderr,"mesg: %s\n",s);
exit(-1);
}
newmode(m)
{
if(chmod(tty,m)<0)
error("cannot change mode");
}

View File

@@ -30,25 +30,24 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
extern int errno; static void usage(void);
static int build(char *path);
main(argc, argv) int main(int argc, char **argv)
int argc;
char **argv;
{ {
extern int optind;
int ch, exitval, pflag; int ch, exitval, pflag;
pflag = 0; pflag = 0;
while ((ch = getopt(argc, argv, "p")) != EOF) while ((ch = getopt(argc, argv, "p")) != EOF)
switch(ch) { switch (ch) {
case 'p': case 'p':
pflag = 1; pflag = 1;
break; break;
@@ -64,29 +63,26 @@ main(argc, argv)
if (pflag) if (pflag)
exitval |= build(*argv); exitval |= build(*argv);
else if (mkdir(*argv, 0777) < 0) { else if (mkdir(*argv, 0777) < 0) {
(void)fprintf(stderr, "mkdir: %s: %s\n", (void)fprintf(stderr, "mkdir: %s: %s\n", *argv, strerror(errno));
*argv, strerror(errno));
exitval = 1; exitval = 1;
} }
exit(exitval); exit(exitval);
} }
build(path) int build(char *path)
char *path;
{ {
register char *p; register char *p;
struct stat sb; struct stat sb;
int create, ch; int create, ch;
for (create = 0, p = path;; ++p) for (create = 0, p = path;; ++p)
if (!*p || *p == '/') { if (!*p || *p == '/') {
ch = *p; ch = *p;
*p = '\0'; *p = '\0';
if (stat(path, &sb)) { if (stat(path, &sb)) {
if (errno != ENOENT || mkdir(path, 0777) < 0) { if (errno != ENOENT || mkdir(path, 0777) < 0) {
(void)fprintf(stderr, "mkdir: %s: %s\n", (void)fprintf(stderr, "mkdir: %s: %s\n", path, strerror(errno));
path, strerror(errno)); return (1);
return(1);
} }
create = 1; create = 1;
} }
@@ -94,14 +90,13 @@ build(path)
break; break;
} }
if (!create) { if (!create) {
(void)fprintf(stderr, "mkdir: %s: %s\n", path, (void)fprintf(stderr, "mkdir: %s: %s\n", path, strerror(EEXIST));
strerror(EEXIST)); return (1);
return(1);
} }
return(0); return (0);
} }
usage() void usage()
{ {
(void)fprintf(stderr, "usage: mkdir [-p] dirname ...\n"); (void)fprintf(stderr, "usage: mkdir [-p] dirname ...\n");
exit(1); exit(1);

View File

@@ -7,37 +7,42 @@
/* /*
* mv file1 file2 * mv file1 file2
*/ */
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/dir.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <stdio.h> #define DELIM '/'
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/dir.h>
#include <errno.h>
#include <signal.h>
#define DELIM '/'
#define MODEBITS 07777 #define MODEBITS 07777
#define ISDIR(st) (((st).st_mode&S_IFMT) == S_IFDIR) #define ISDIR(st) (((st).st_mode & S_IFMT) == S_IFDIR)
#define ISLNK(st) (((st).st_mode&S_IFMT) == S_IFLNK) #define ISLNK(st) (((st).st_mode & S_IFMT) == S_IFLNK)
#define ISREG(st) (((st).st_mode&S_IFMT) == S_IFREG) #define ISREG(st) (((st).st_mode & S_IFMT) == S_IFREG)
#define ISDEV(st) \ #define ISDEV(st) (((st).st_mode & S_IFMT) == S_IFCHR || ((st).st_mode & S_IFMT) == S_IFBLK)
(((st).st_mode&S_IFMT) == S_IFCHR || ((st).st_mode&S_IFMT) == S_IFBLK)
char *dname(); struct stat s1, s2;
struct stat s1, s2; int iflag = 0; /* interactive mode */
int iflag = 0; /* interactive mode */ int fflag = 0; /* force overwriting */
int fflag = 0; /* force overwriting */
main(argc, argv) static int movewithshortname(char *src, char *dest);
register char *argv[]; static int move(char *source, char *target);
static char *dname(char *name);
static void error(char *fmt, ...);
static void Perror(char *s);
static void Perror2(char *s1, char *s2);
int main(int argc, char *argv[])
{ {
register i, r; int i, r;
register char *arg; char *arg;
char *dest; char *dest;
if (argc < 2) if (argc < 2)
@@ -50,28 +55,28 @@ main(argc, argv)
* all files following a null option * all files following a null option
* are considered file names * are considered file names
*/ */
if (*(arg+1) == '\0') if (*(arg + 1) == '\0')
break; break;
while (*++arg != '\0') switch (*arg) { while (*++arg != '\0')
switch (*arg) {
case 'i':
iflag++;
break;
case 'i': case 'f':
iflag++; fflag++;
break; break;
case 'f': default:
fflag++; goto usage;
break; }
default:
goto usage;
}
} }
if (argc < 3) if (argc < 3)
goto usage; goto usage;
dest = argv[argc-1]; dest = argv[argc - 1];
if (stat(dest, &s2) >= 0 && ISDIR(s2)) { if (stat(dest, &s2) >= 0 && ISDIR(s2)) {
r = 0; r = 0;
for (i = 1; i < argc-1; i++) for (i = 1; i < argc - 1; i++)
r |= movewithshortname(argv[i], dest); r |= movewithshortname(argv[i], dest);
exit(r); exit(r);
} }
@@ -82,43 +87,39 @@ main(argc, argv)
/*NOTREACHED*/ /*NOTREACHED*/
usage: usage:
fprintf(stderr, fprintf(stderr,
"usage: mv [-if] f1 f2 or mv [-if] f1 ... fn d1 (`fn' is a file or directory)\n"); "usage: mv [-if] f1 f2 or mv [-if] f1 ... fn d1 (`fn' is a file or directory)\n");
return (1); return (1);
} }
movewithshortname(src, dest) int movewithshortname(char *src, char *dest)
char *src, *dest;
{ {
register char *shortname; char *shortname;
char target[MAXPATHLEN + 1]; char target[MAXPATHLEN + 1];
shortname = dname(src); shortname = dname(src);
if (strlen(dest) + strlen(shortname) > MAXPATHLEN - 1) { if (strlen(dest) + strlen(shortname) > MAXPATHLEN - 1) {
error("%s/%s: pathname too long", dest, error("%s/%s: pathname too long", dest, shortname);
shortname);
return (1); return (1);
} }
sprintf(target, "%s/%s", dest, shortname); sprintf(target, "%s/%s", dest, shortname);
return (move(src, target)); return (move(src, target));
} }
int int query(char *prompt, ...)
query (char *prompt, ...)
{ {
va_list args; va_list args;
register int i, c; int i, c;
va_start (args, prompt); va_start(args, prompt);
vfprintf(stderr, prompt, args); vfprintf(stderr, prompt, args);
va_end (args); va_end(args);
i = c = getchar(); i = c = getchar();
while (c != '\n' && c != EOF) while (c != '\n' && c != EOF)
c = getchar(); c = getchar();
return (i == 'y'); return (i == 'y');
} }
move(source, target) int move(char *source, char *target)
char *source, *target;
{ {
int targetexists; int targetexists;
@@ -138,20 +139,17 @@ move(source, target)
error("%s and %s are identical", source, target); error("%s and %s are identical", source, target);
return (1); return (1);
} }
if (iflag && !fflag && isatty(fileno(stdin)) && if (iflag && !fflag && isatty(fileno(stdin)) && query("remove %s? ", target) == 0)
query("remove %s? ", target) == 0)
return (1); return (1);
if (access(target, 2) < 0 && !fflag && isatty(fileno(stdin))) { if (access(target, 2) < 0 && !fflag && isatty(fileno(stdin))) {
if (query("override protection %o for %s? ", if (query("override protection %o for %s? ", s2.st_mode & MODEBITS, target) == 0)
s2.st_mode & MODEBITS, target) == 0)
return (1); return (1);
} }
} }
if (rename(source, target) >= 0) if (rename(source, target) >= 0)
return (0); return (0);
if (errno != EXDEV) { if (errno != EXDEV) {
Perror2(errno == ENOENT && targetexists == 0 ? target : source, Perror2(errno == ENOENT && targetexists == 0 ? target : source, "rename");
"rename");
return (1); return (1);
} }
if (ISDIR(s1)) { if (ISDIR(s1)) {
@@ -168,10 +166,10 @@ move(source, target)
* between file systems. * between file systems.
*/ */
if (ISLNK(s1)) { if (ISLNK(s1)) {
register m; int m;
char symln[MAXPATHLEN + 1]; char symln[MAXPATHLEN + 1];
m = readlink(source, symln, sizeof (symln) - 1); m = readlink(source, symln, sizeof(symln) - 1);
if (m < 0) { if (m < 0) {
Perror(source); Perror(source);
return (1); return (1);
@@ -183,7 +181,7 @@ move(source, target)
Perror(target); Perror(target);
return (1); return (1);
} }
(void) umask(m); (void)umask(m);
goto cleanup; goto cleanup;
} }
if (ISDEV(s1)) { if (ISDEV(s1)) {
@@ -198,11 +196,11 @@ move(source, target)
tv[0].tv_usec = 0; tv[0].tv_usec = 0;
tv[1].tv_sec = s1.st_mtime; tv[1].tv_sec = s1.st_mtime;
tv[1].tv_usec = 0; tv[1].tv_usec = 0;
(void) utimes(target, tv); (void)utimes(target, tv);
goto cleanup; goto cleanup;
} }
if (ISREG(s1)) { if (ISREG(s1)) {
register int fi, fo, n; int fi, fo, n;
struct timeval tv[2]; struct timeval tv[2];
char buf[MAXBSIZE]; char buf[MAXBSIZE];
@@ -243,7 +241,7 @@ move(source, target)
tv[0].tv_usec = 0; tv[0].tv_usec = 0;
tv[1].tv_sec = s1.st_mtime; tv[1].tv_sec = s1.st_mtime;
tv[1].tv_usec = 0; tv[1].tv_usec = 0;
(void) utimes(target, tv); (void)utimes(target, tv);
goto cleanup; goto cleanup;
} }
error("%s: unknown file type %o", source, s1.st_mode); error("%s: unknown file type %o", source, s1.st_mode);
@@ -257,11 +255,9 @@ cleanup:
return (0); return (0);
} }
char * char *dname(char *name)
dname(name)
register char *name;
{ {
register char *p; char *p;
p = name; p = name;
while (*p) while (*p)
@@ -270,18 +266,17 @@ dname(name)
return name; return name;
} }
/*VARARGS*/ void error(char *fmt, ...)
error(fmt, a1, a2)
char *fmt;
{ {
va_list args;
va_start(args, fmt);
fprintf(stderr, "mv: "); fprintf(stderr, "mv: ");
fprintf(stderr, fmt, a1, a2); vfprintf(stderr, fmt, args);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
va_end(args);
} }
Perror(s) void Perror(char *s)
char *s;
{ {
char buf[MAXPATHLEN + 10]; char buf[MAXPATHLEN + 10];
@@ -289,8 +284,7 @@ Perror(s)
perror(buf); perror(buf);
} }
Perror2(s1, s2) void Perror2(char *s1, char *s2)
char *s1, *s2;
{ {
char buf[MAXPATHLEN + 20]; char buf[MAXPATHLEN + 20];

View File

@@ -5,13 +5,11 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
main(argc, argv) int main(int argc, char *argv[])
int argc;
char *argv[];
{ {
int nicarg = 10; int nicarg = 10;
@@ -23,8 +21,7 @@ main(argc, argv)
fputs("usage: nice [ -n ] command\n", stderr); fputs("usage: nice [ -n ] command\n", stderr);
exit(1); exit(1);
} }
if (setpriority(PRIO_PROCESS, 0, if (setpriority(PRIO_PROCESS, 0, getpriority(PRIO_PROCESS, 0) + nicarg) < 0) {
getpriority(PRIO_PROCESS, 0) + nicarg) < 0) {
perror("setpriority"); perror("setpriority");
exit(1); exit(1);
} }

File diff suppressed because it is too large Load Diff

View File

@@ -4,6 +4,7 @@
* specifies the terms and conditions for redistribution. * specifies the terms and conditions for redistribution.
*/ */
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
int main() int main()
{ {

View File

@@ -2,110 +2,118 @@
* print file with headings * print file with headings
* 2+head+2+page[56]+5 * 2+head+2+page[56]+5
*/ */
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
/* Making putcp a macro sped things up by 14%. */ /* Making putcp a macro sped things up by 14%. */
#define putcp(c) if (page >= fpage) putchar(c) #define putcp(c) \
if (page >= fpage) \
putchar(c)
int ncol = 1; int ncol = 1;
char *header; char *header;
int col; int col;
int icol; int icol;
FILE *file; FILE *file;
char *bufp; char *bufp;
#define BUFS 9000 /* at least 66 * 132 */ #define BUFS 9000 /* at least 66 * 132 */
char buffer[BUFS]; /* for multi-column output */ char buffer[BUFS]; /* for multi-column output */
char obuf[BUFSIZ]; char obuf[BUFSIZ];
#define FF 014 #define FF 014
int line; int line;
char *colp[72]; char *colp[72];
int nofile; int nofile;
char isclosed[10]; char isclosed[10];
FILE *ifile[10]; FILE *ifile[10];
char **lastarg; char **lastarg;
int peekc; int peekc;
int fpage; int fpage;
int page; int page;
int colw; int colw;
int nspace; int nspace;
int width = 72; int width = 72;
int length = 66; int length = 66;
int plength = 61; int plength = 61;
int margin = 10; int margin = 10;
int ntflg; int ntflg;
int fflg; int fflg;
int mflg; int mflg;
int tabc; int tabc;
char *tty; char *tty;
int mode; int mode;
char *ttyname();
char *ctime();
void static void fixtty(void);
onintr (sig) static int numeric(char *str);
int sig; static void print(char *fp, char **argp);
static void done(void);
static void mopen(char **ap);
static void nexbuf(void);
static int tpgetc(int ai);
static void put(int ac);
static void putpage(void);
static int pgetc(int i);
void onintr(int sig)
{ {
if (tty) if (tty)
chmod(tty, mode); chmod(tty, mode);
_exit(1); _exit(1);
} }
main(argc, argv) int main(int argc, char **argv)
char **argv;
{ {
int nfdone; int nfdone;
setbuf(stdout, obuf); setbuf(stdout, obuf);
if (signal(SIGINT, SIG_IGN) != SIG_IGN) if (signal(SIGINT, SIG_IGN) != SIG_IGN)
signal(SIGINT, onintr); signal(SIGINT, onintr);
lastarg = &argv[argc-1]; lastarg = &argv[argc - 1];
fixtty(); fixtty();
for (nfdone=0; argc>1; argc--) { for (nfdone = 0; argc > 1; argc--) {
argv++; argv++;
if (**argv == '-') { if (**argv == '-') {
switch (*++*argv) { switch (*++*argv) {
case 'h': /* define page header */ case 'h': /* define page header */
if (argc>=2) { if (argc >= 2) {
header = *++argv; header = *++argv;
argc--; argc--;
} }
continue; continue;
case 't': /* don't print page headers */ case 't': /* don't print page headers */
ntflg++; ntflg++;
continue; continue;
case 'f': /* use form feeds */ case 'f': /* use form feeds */
fflg++; fflg++;
plength = 60; plength = 60;
continue; continue;
case 'l': /* length of page */ case 'l': /* length of page */
length = atoi(++*argv); length = atoi(++*argv);
continue; continue;
case 'w': /* width of page */ case 'w': /* width of page */
width = atoi(++*argv); width = atoi(++*argv);
continue; continue;
case 's': /* col separator */ case 's': /* col separator */
if (*++*argv) if (*++*argv)
tabc = **argv; tabc = **argv;
else else
tabc = '\t'; tabc = '\t';
continue; continue;
case 'm': /* all files at once */ case 'm': /* all files at once */
mflg++; mflg++;
continue; continue;
default: default:
if (numeric(*argv)) { /* # of cols */ if (numeric(*argv)) { /* # of cols */
if ((ncol = atoi(*argv)) == 0) { if ((ncol = atoi(*argv)) == 0) {
fprintf(stderr, "can't print 0 cols, using 1 instead.\n"); fprintf(stderr, "can't print 0 cols, using 1 instead.\n");
ncol = 1; ncol = 1;
@@ -125,32 +133,30 @@ char **argv;
break; break;
} }
} }
if (nfdone==0) if (nfdone == 0)
print((char *)0, (char **)0); print((char *)0, (char **)0);
done(); done();
} }
done() void done()
{ {
if (tty) if (tty)
chmod(tty, mode); chmod(tty, mode);
exit(0); exit(0);
} }
/* numeric -- returns 1 if str is numeric, elsewise 0 */ /* numeric -- returns 1 if str is numeric, elsewise 0 */
numeric(str) int numeric(char *str)
char *str;
{ {
for (; *str ; str++) { for (; *str; str++) {
if (*str > '9' || *str < '0') { if (*str > '9' || *str < '0') {
return(0); return (0);
} }
} }
return(1); return (1);
} }
fixtty() void fixtty()
{ {
struct stat sbuf; struct stat sbuf;
@@ -158,19 +164,17 @@ fixtty()
if (tty == 0) if (tty == 0)
return; return;
stat(tty, &sbuf); stat(tty, &sbuf);
mode = sbuf.st_mode&0777; mode = sbuf.st_mode & 0777;
chmod(tty, 0600); chmod(tty, 0600);
} }
/* print -- print file */ /* print -- print file */
print(fp, argp) void print(char *fp, char **argp)
char *fp;
char **argp;
{ {
struct stat sbuf; struct stat sbuf;
register sncol; int sncol;
register char *sheader; char *sheader;
register char *cbuf; char *cbuf;
char linebuf[150], *cp; char linebuf[150], *cp;
if (ntflg) if (ntflg)
@@ -181,7 +185,7 @@ char **argp;
length = 66; length = 66;
if (width <= 0) if (width <= 0)
width = 72; width = 72;
if (ncol>72 || ncol>width) { if (ncol > 72 || ncol > width) {
fprintf(stderr, "pr: No room for columns.\n"); fprintf(stderr, "pr: No room for columns.\n");
done(); done();
} }
@@ -189,19 +193,19 @@ char **argp;
mopen(argp); mopen(argp);
ncol = nofile; ncol = nofile;
} }
colw = width/(ncol==0? 1 : ncol); colw = width / (ncol == 0 ? 1 : ncol);
sncol = ncol; sncol = ncol;
sheader = header; sheader = header;
plength = length-5; plength = length - 5;
if (ntflg) if (ntflg)
plength = length; plength = length;
if (--ncol<0) if (--ncol < 0)
ncol = 0; ncol = 0;
if (mflg) if (mflg)
fp = 0; fp = 0;
if (fp) { if (fp) {
if((file=fopen(fp, "r"))==NULL) { if ((file = fopen(fp, "r")) == NULL) {
if (tty==NULL) if (tty == NULL)
perror(fp); perror(fp);
ncol = sncol; ncol = sncol;
header = sheader; header = sheader;
@@ -213,40 +217,39 @@ char **argp;
time(&sbuf.st_mtime); time(&sbuf.st_mtime);
} }
if (header == 0) if (header == 0)
header = fp?fp:""; header = fp ? fp : "";
cbuf = ctime(&sbuf.st_mtime); cbuf = ctime(&sbuf.st_mtime);
cbuf[16] = '\0'; cbuf[16] = '\0';
cbuf[24] = '\0'; cbuf[24] = '\0';
page = 1; page = 1;
icol = 0; icol = 0;
colp[ncol] = bufp = buffer; colp[ncol] = bufp = buffer;
if (mflg==0) if (mflg == 0)
nexbuf(); nexbuf();
while (mflg&&nofile || (!mflg)&&tpgetc(ncol)>0) { while (mflg && nofile || (!mflg) && tpgetc(ncol) > 0) {
if (mflg==0) { if (mflg == 0) {
colp[ncol]--; colp[ncol]--;
if (colp[ncol] < buffer) if (colp[ncol] < buffer)
colp[ncol] = &buffer[BUFS]; colp[ncol] = &buffer[BUFS];
} }
line = 0; line = 0;
if (ntflg==0) { if (ntflg == 0) {
if (fflg) { if (fflg) {
/* Assume a ff takes two blank lines at the /* Assume a ff takes two blank lines at the
top of the page. */ top of the page. */
line = 2; line = 2;
sprintf(linebuf, "%s %s %s Page %d\n\n\n", sprintf(linebuf, "%s %s %s Page %d\n\n\n", cbuf + 4, cbuf + 20, header, page);
cbuf+4, cbuf+20, header, page);
} else } else
sprintf(linebuf, "\n\n%s %s %s Page %d\n\n\n", sprintf(linebuf, "\n\n%s %s %s Page %d\n\n\n", cbuf + 4, cbuf + 20, header, page);
cbuf+4, cbuf+20, header, page); for (cp = linebuf; *cp;)
for(cp=linebuf;*cp;) put(*cp++); put(*cp++);
} }
putpage(); putpage();
if (ntflg==0) { if (ntflg == 0) {
if (fflg) if (fflg)
put('\f'); put('\f');
else else
while(line<length) while (line < length)
put('\n'); put('\n');
} }
page++; page++;
@@ -256,85 +259,85 @@ char **argp;
header = sheader; header = sheader;
} }
mopen(ap) void mopen(char **ap)
char **ap;
{ {
register char **p, *p1; char **p, *p1;
p = ap; p = ap;
while((p1 = *p) && p++ <= lastarg) { while ((p1 = *p) && p++ <= lastarg) {
if((ifile[nofile]=fopen(p1, "r")) == NULL){ if ((ifile[nofile] = fopen(p1, "r")) == NULL) {
isclosed[nofile] = 1; isclosed[nofile] = 1;
nofile--; nofile--;
} } else
else
isclosed[nofile] = 0; isclosed[nofile] = 0;
if(++nofile>=10) { if (++nofile >= 10) {
fprintf(stderr, "pr: Too many args\n"); fprintf(stderr, "pr: Too many args\n");
done(); done();
} }
} }
} }
putpage() void putpage()
{ {
register int lastcol, i, c; int lastcol, i, c;
int j; int j;
if (ncol==0) { if (ncol == 0) {
while (line<plength) { while (line < plength) {
while((c = tpgetc(0)) && c!='\n' && c!=FF) while ((c = tpgetc(0)) && c != '\n' && c != FF)
putcp(c); putcp(c);
if (c==0) break; if (c == 0)
break;
putcp('\n'); putcp('\n');
line++; line++;
if (c==FF) if (c == FF)
break; break;
} }
return; return;
} }
colp[0] = colp[ncol]; colp[0] = colp[ncol];
if (mflg==0) for (i=1; i<=ncol; i++) { if (mflg == 0)
colp[i] = colp[i-1]; for (i = 1; i <= ncol; i++) {
for (j = margin; j<length; j++) colp[i] = colp[i - 1];
while((c=tpgetc(i))!='\n') for (j = margin; j < length; j++)
if (c==0) while ((c = tpgetc(i)) != '\n')
break; if (c == 0)
} break;
while (line<plength) { }
while (line < plength) {
lastcol = colw; lastcol = colw;
for (i=0; i<ncol; i++) { for (i = 0; i < ncol; i++) {
while ((c=pgetc(i)) && c!='\n') while ((c = pgetc(i)) && c != '\n')
if (col<lastcol || tabc!=0) if (col < lastcol || tabc != 0)
put(c); put(c);
if (c==0) if (c == 0)
continue; continue;
if (tabc) if (tabc)
put(tabc); put(tabc);
else while (col<lastcol) else
put(' '); while (col < lastcol)
put(' ');
lastcol += colw; lastcol += colw;
} }
while ((c = pgetc(ncol)) && c!='\n') while ((c = pgetc(ncol)) && c != '\n')
put(c); put(c);
put('\n'); put('\n');
} }
} }
nexbuf() void nexbuf()
{ {
register int n; int n;
register char *rbufp; char *rbufp;
rbufp = bufp; rbufp = bufp;
n = &buffer[BUFS] - rbufp; n = &buffer[BUFS] - rbufp;
if (n>512) if (n > 512)
n = 512; n = 512;
if((n=fread(rbufp,1,n,file)) <= 0){ if ((n = fread(rbufp, 1, n, file)) <= 0) {
fclose(file); fclose(file);
*rbufp = 0376; *rbufp = 0376;
} } else {
else {
rbufp += n; rbufp += n;
if (rbufp >= &buffer[BUFS]) if (rbufp >= &buffer[BUFS])
rbufp = buffer; rbufp = buffer;
@@ -343,24 +346,24 @@ nexbuf()
bufp = rbufp; bufp = rbufp;
} }
tpgetc(ai) int tpgetc(int ai)
{ {
register char **p; char **p;
register int c, i; int c, i;
i = ai; i = ai;
if (mflg) { if (mflg) {
if((c=getc(ifile[i])) == EOF) { if ((c = getc(ifile[i])) == EOF) {
if (isclosed[i]==0) { if (isclosed[i] == 0) {
isclosed[i] = 1; isclosed[i] = 1;
if (--nofile <= 0) if (--nofile <= 0)
return(0); return (0);
} }
return('\n'); return ('\n');
} }
if (c==FF && ncol>0) if (c == FF && ncol > 0)
c = '\n'; c = '\n';
return(c); return (c);
} }
loop: loop:
c = **(p = &colp[i]) & 0377; c = **(p = &colp[i]) & 0377;
@@ -369,18 +372,18 @@ loop:
c = **p & 0377; c = **p & 0377;
} }
if (c == 0376) if (c == 0376)
return(0); return (0);
(*p)++; (*p)++;
if (*p >= &buffer[BUFS]) if (*p >= &buffer[BUFS])
*p = buffer; *p = buffer;
if (c==0) if (c == 0)
goto loop; goto loop;
return(c); return (c);
} }
pgetc(i) int pgetc(int i)
{ {
register int c; int c;
if (peekc) { if (peekc) {
c = peekc; c = peekc;
@@ -388,14 +391,13 @@ pgetc(i)
} else } else
c = tpgetc(i); c = tpgetc(i);
if (tabc) if (tabc)
return(c); return (c);
switch (c) { switch (c) {
case '\t': case '\t':
icol++; icol++;
if ((icol&07) != 0) if ((icol & 07) != 0)
peekc = '\t'; peekc = '\t';
return(' '); return (' ');
case '\n': case '\n':
icol = 0; icol = 0;
@@ -408,21 +410,21 @@ pgetc(i)
} }
if (c >= ' ') if (c >= ' ')
icol++; icol++;
return(c); return (c);
} }
put(ac)
void put(int ac)
{ {
register int ns, c; int ns, c;
c = ac; c = ac;
if (tabc) { if (tabc) {
putcp(c); putcp(c);
if (c=='\n') if (c == '\n')
line++; line++;
return; return;
} }
switch (c) { switch (c) {
case ' ': case ' ':
nspace++; nspace++;
col++; col++;
@@ -436,15 +438,14 @@ put(ac)
case 010: case 010:
case 033: case 033:
if (--col<0) if (--col < 0)
col = 0; col = 0;
if (--nspace<0) if (--nspace < 0)
nspace = 0; nspace = 0;
} }
while(nspace) { while (nspace) {
if (nspace>2 && col > (ns=((col-nspace)|07))) { if (nspace > 2 && col > (ns = ((col - nspace) | 07))) {
nspace = col-ns-1; nspace = col - ns - 1;
putcp('\t'); putcp('\t');
} else { } else {
nspace--; nspace--;

View File

@@ -12,8 +12,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
int prefix(cp, dp) int prefix(char *cp, char *dp)
char *cp, *dp;
{ {
while (*cp && *dp && *cp == *dp) while (*cp && *dp && *cp == *dp)
cp++, dp++; cp++, dp++;
@@ -22,20 +21,18 @@ int prefix(cp, dp)
return 0; return 0;
} }
int main(argc, argv) int main(int argc, char *argv[])
int argc;
char *argv[];
{ {
register char **ep; char **ep;
int found = 0; int found = 0;
if (! environ) if (!environ)
return 1; return 1;
argc--, argv++; argc--, argv++;
for (ep = environ; *ep; ep++) { for (ep = environ; *ep; ep++) {
if (argc == 0 || prefix(argv[0], *ep)) { if (argc == 0 || prefix(argv[0], *ep)) {
register char *cp = *ep; char *cp = *ep;
found++; found++;
if (argc) { if (argc) {