home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
CMDS
/
uustat.t.Z
/
uustat.t
/
uustat.c
< prev
next >
Wrap
Text File
|
2009-11-06
|
13KB
|
577 lines
/*
* This program tries to emulate the Unix (tm) System V program 'uustat' under
* the OS-9 operating system. Uustat can display the status of previously
* issued uucp commands.
*
* This code is Copyright (C) 1989 by Helge Oldach, Hamburg, West Germany.
* It is not derived from licensed software.
*
* Permission is granted to anyone to use this software for any purpose on any
* computer system, and to redistribute it freely, subject to the following
* restrictions:
*
* 1. The author is not responsible for the consequences of use of this
* software, no matter how awful, even if they arise from defects in it.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 4. Incorporation of this software into commercial products is permitted
* only with the written permission of the author.
*
* If changes are made to the original software, the author would appreciate
* to receive a notice about the changes made. Please send a note to:
*
* Address: Electronic mail:
* Helge Oldach he@sephh.uucp (Sub-Net)
* Maretstrasse 53 ..!unido!stollco!sup!he
* D-2100 Hamburg 90
*
* Version history:
*
* 890822 initial release
* 900102 time->tm_mon is based to 0 (0 == January)
* 900115 system named does not contain processing order in X. file
*/
char copyright[] = "Copyright (C) 1989 by Helge Oldach, Hamburg, West Germany";
#include <stdio.h>
#include <ctype.h>
#include <dir.h>
#include <info.h>
#include <pwd.h>
#include <time.h>
#include <stat.h>
#ifdef DEBUG
#define DBG(level, routine, command) { \
if (debug >= level) { \
fprintf(stderr, "--- %s: ", routine); \
command; \
fprintf(stderr, "\n"); \
} \
}
#else
#define DBG(level, routine, command)
#endif
int debug = 0, myuid, verbose = 0;
usage(str)
char *str;
{
fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
"Syntax: ",
_prgname(),
" [<opts>]\n",
"Function: uucp status inquiry and job control\n",
"Options:\n",
" -k kill all uucp requests\n",
" -k[=]<jobid> kill uucp request <jobid>\n",
" -r rejuvenate all uucp requests\n",
" -r[=]<jobid> rejuvenate uucp request <jobid>\n",
" -s[=]<system> display status for <system>\n",
" -u[=]<user> report requests issued by <user>\n",
" -v verbose mode (with -k or -r)\n",
#ifdef DEBUG
" -x[=]<num> debug level <num>\n",
#else
"",
#endif
" -? display this help\n");
if (str != NULL)
fprintf(stderr, "%s: %s\n", _prgname(), str);
exit(0);
}
getstring(to, from)
char **to, **from;
{
if (**from == '=')
++*from;
if (*to != NULL)
usage("duplicate option");
else
*to = *from;
while (**from)
++*from;
}
getint(str)
char **str;
{
int num;
if (isdigit(**str))
;
else if ((**str == '+' || **str == '-' ) && isdigit((*str)[1]))
;
else
usage("invalid integer");
num = atoi(*str);
if (**str == '+' || **str == '-')
++*str;
while (isdigit(**str))
++*str;
return num;
}
checkuid(user, mode, jobid)
char *user, *mode, *jobid;
{
static char prev[MAXNAMLEN] = "";
static int stat = 0;
struct passwd *pw;
if (myuid == 0)
return !0;
if (user == NULL || strcmp(prev, user) == 0) {
if (user == NULL && stat == 0)
fprintf(stderr, "%s: wrong user ID to %s %s\n",
_prgname(), mode, jobid);
} else if (*user == '\0')
stat = 0;
else {
DBG(2, "checkuid", fprintf(stderr, "\"%s\"", user))
strcpy(prev, user);
if (setpwent() != 0 || (pw = getpwnam(user)) == NULL) {
fprintf(stderr, "%s: cannot find user ID %s\n",
_prgname(), user);
stat = 0;
} else
stat = myuid == (pw->pw_gid << 16) + pw->pw_uid;
endpwent();
}
DBG(2, "checkuid", fprintf(stderr, "%d", stat))
return stat;
}
touch(file, cmd, xmit, rcv, who)
char *file, *cmd, *xmit, *rcv, *who;
{
FILE *fp;
DBG(2, "touch", fprintf(stderr, "\"%s\"", xmit))
if (!checkuid(who, "rejuvenate", &xmit[2]))
return;
if ((fp = fopen(xmit, "a+")) == NULL) {
fprintf(stderr, "%s: cannot touch %s\n", _prgname(), xmit);
return;
}
fclose(fp);
if (verbose && who == NULL)
fprintf(stderr, "%s: %s rejuvenated\n", _prgname(), &xmit[2]);
}
delete(file, cmd, xmit, rcv, who)
char *file, *cmd, *xmit, *rcv, *who;
{
DBG(2, "delete", fprintf(stderr, "\"%s\"", xmit))
if (!checkuid(who, "kill", &xmit[2]))
return;
if (unlink(xmit) != 0)
fprintf(stderr, "%s: cannot delete %s\n", _prgname(), xmit);
if (verbose && who == NULL)
fprintf(stderr, "%s: %s killed\n", _prgname(), &xmit[2]);
}
scantime(file)
char *file;
{
struct stat sbuf;
struct tm *time;
if (stat(file, &sbuf) == -1) {
fprintf(stderr, "%s: cannot stat %s\n", _prgname(), file);
sbuf.st_atime = 0;
}
time = localtime(&sbuf.st_atime);
++time->tm_mon;
DBG(1, "scantime", fprintf(stderr, "%02d/%02d/%02d %02d:%02d",
time->tm_year, time->tm_mon, time->tm_mday,
time->tm_hour, time->tm_min))
printf("%02d/%02d/%02d-%02d:%02d ", time->tm_year, time->tm_mon,
time->tm_mday, time->tm_hour, time->tm_min);
}
scansize(file)
char *file;
{
struct stat sbuf;
if (stat(file, &sbuf) == -1)
fprintf(stderr, "%s: cannot stat %s\n", _prgname(), file);
else
printf("%ld %s", sbuf.st_size, file);
}
char *scanuser(file)
char *file;
{
static char user[MAXNAMLEN] = "";
FILE *fp;
char line[BUFSIZ];
int first = 0;
strcpy(user, "<unknown>");
if ((fp = fopen(file, "r")) == NULL) {
fprintf(stderr, "%s: cannot open %s\n", _prgname(), file);
return user;
}
DBG(1, "scanuser", fprintf(stderr, "\"%s\"", file))
while (fgets(line, sizeof line, fp) != NULL) {
register char *p;
for (p = line; *p != '\n'; ++p)
;
*p = '\0';
if (line[0] != 'U' || line[1] != ' ')
continue;
for (p = &line[2]; isascii(*p) && isprint(*p) && *p != ' '; ++p)
;
*p = '\0';
DBG(3, "scanuser", fprintf(stderr, "user: |%s|", &line[2]))
strcpy(user, &line[2]);
++first;
break;
}
DBG(2, "scanuser", fprintf(stderr, "EOF"))
fclose(fp);
if (!first)
fprintf(stderr, "%s: no remote user in %s\n", _prgname(),
file);
return user;
}
scancmd(file)
char *file;
{
FILE *fp;
char line[BUFSIZ];
int first = 0;
if ((fp = fopen(file, "r")) == NULL) {
fprintf(stderr, "%s: cannot open %s\n", _prgname(), file);
return;
}
DBG(1, "scancmd", fprintf(stderr, "\"%s\"", file))
while (fgets(line, sizeof line, fp) != NULL) {
register char *p;
for (p = line; *p != '\n'; ++p)
;
*p = '\0';
if (line[0] == 'C' && line[1] == ' ') {
if (first++)
printf("; ");
printf(&line[2]);
}
}
if (!first)
fprintf(stderr, "%s: no remote command in %s\n", _prgname(),
file);
DBG(2, "scancmd", fprintf(stderr, "EOF"))
fclose(fp);
}
display(file, cmd, xmit, rcv, who, user, system)
char *file, *cmd, *xmit, *rcv, *who, *user, *system;
{
static char save[MAXNAMLEN];
register char *p;
DBG(3, "display", fprintf(stderr,
"file=|%s| cmd=|%s| xmit=|%s| rcv=|%s| who=|%s| user=|%s| system=|%s|",
file == NULL ? "<NULL>" : file,
cmd == NULL ? "<NULL>" : cmd,
xmit == NULL ? "<NULL>" : xmit,
rcv == NULL ? "<NULL>" : rcv,
who == NULL ? "<NULL>" : who,
user == NULL ? "<NULL>" : user,
system == NULL ? "<NULL>" : system))
if (cmd == NULL)
return;
if (*cmd == 'X')
who = scanuser(xmit);
if (user != NULL && strcmp(who, user) != 0)
return;
if (system != NULL && strncmp(&file[2], system, strlen(system)) != 0)
return;
if (strcmp(save, file) == 0) {
for (p = save; *p != '\0'; ++p)
*p = ' ';
printf("%s ", &save[2]);
} else
printf("%s ", &file[2]);
scantime(xmit);
printf("%s ", cmd);
strcpy(save, &file[2]);
save[strlen(save) - (*cmd == 'S' ? 4 : 5)] = '\0';
printf("%s ", save);
printf("%s ", who);
switch (*rcv) {
case 'D':
scansize(xmit);
break;
case 'X':
scancmd(xmit);
break;
default:
fprintf(stderr,
"%s: unknown remote file type %s in %s\n",
_prgname(), *xmit, file);
break;
}
printf("\n");
strcpy(save, file);
DBG(2, "display", fprintf(stderr, "return"))
}
scanline(file, line, funct, user, system)
char *file, *line;
int (*funct)();
char *user, *system;
{
char *p, *t, *cmd = NULL, *xmit = NULL, *rcv = NULL, *who = NULL;
DBG(2, "scanline", fprintf(stderr, "|%s|", line))
p = line;
if (p[0] == 'S' && p[1] == ' ') {
cmd = p;
*++p= '\0';
++p;
}
DBG(3, "scanline", fprintf(stderr, "cmd: |%s|", cmd))
for (t = p; isascii(*t) && (isalnum(*t) || *t == '.'); ++t)
;
if (t > p && *t == ' ') {
xmit = p;
p = t;
*p++ = '\0';
}
DBG(3, "scanline", fprintf(stderr, "xmit: |%s|", xmit))
for (t = p; isascii(*t) && (isalnum(*t) || *t == '.'); ++t)
;
if (t > p && *t == ' ') {
rcv = p;
p = t;
*p++ = '\0';
}
DBG(3, "scanline", fprintf(stderr, "rcv: |%s|", rcv))
for (t = p; isascii(*t) && (isalnum(*t) || *t == '.'); ++t)
;
if (t > p && *t == ' ') {
who = p;
p = t;
*p++ = '\0';
}
DBG(3, "scanline", fprintf(stderr, "who |%s|", who))
if (cmd == NULL || xmit == NULL || rcv == NULL || who == NULL)
fprintf(stderr, "%s: unrecognized syntax in %s\n", file);
else
(*funct)(file, cmd, xmit, rcv, who, user, system);
DBG(2, "scanline", fprintf(stderr, "return"))
}
scanxline(file, line, funct, user, system)
char *file, *line;
int (*funct)();
char *user, *system;
{
char *p, *t;
DBG(2, "scanxline", fprintf(stderr, "|%s|", line))
if (*line != 'F')
return;
p = line;
if (p[0] == 'F' && p[1] == ' ') {
*++p = '\0';
++p;
}
DBG(3, "scanxline", fprintf(stderr, "cmd: |%s|", line))
for (t = p; isascii(*t) && (isalnum(*t) || *t == '.'); ++t)
;
if (t > p)
*t++ = '\0';
DBG(3, "scanxline", fprintf(stderr, "xmit: |%s|", p))
if (t > p)
(*funct)(NULL, NULL, p, NULL, "", user, system);
else
fprintf(stderr, "%s: unrecognized syntax in %s\n", file);
}
scanfile(file, funct, user, system)
char *file;
int (*funct)();
char *user, *system;
{
FILE *fp;
char line[BUFSIZ];
register char *p;
if ((fp = fopen(file, "r")) == NULL) {
fprintf(stderr, "%s: cannot open %s\n", _prgname(), file);
return;
}
DBG(1, "scanfile", fprintf(stderr, "\"%s\"", file))
while (fgets(line, sizeof line, fp) != NULL) {
for (p = line; *p != '\n'; ++p)
;
*p = '\0';
if (*file == 'C')
scanline(file, line, funct, user, system);
else
scanxline(file, line, funct, user, system);
DBG(2, "scanfile", fprintf(stderr, "next line"))
}
DBG(2, "scanfile", fprintf(stderr, "EOF"))
fclose(fp);
if (*file == 'C')
(*funct)(NULL, NULL, file, NULL, NULL, user, system);
else
(*funct)(file, "X", file, file, NULL, user, system);
}
scandir(jobid, funct, user, system)
char *jobid;
int (*funct)();
char *user, *system;
{
DIR *dp;
struct direct *dir;
int done = 0;
if (info_is_locked("lck.uucp"))
fprintf(stderr, "%s: warning\007: uucp is possibly active\n",
_prgname());
if ((dp = opendir(".")) == NULL) {
fprintf(stderr, "%s: cannot open uucp spool directory\n",
_prgname());
return;
}
while ((dir = readdir(dp)) != NULL) {
DBG(1, "scandir", fprintf(stderr, "\"%s\"", dir->d_name))
if ((dir->d_name[0] == 'C' || dir->d_name[0] == 'X') &&
dir->d_name[1] == '.' &&
(jobid == NULL ||
*jobid == '\0' ||
strcmp(jobid, &dir->d_name[2]) == 0)) {
scanfile(dir->d_name, funct, user, system);
++done;
}
}
closedir(dp);
if (done == 0 && jobid != NULL)
fprintf(stderr, "%s: cannot access job ID %s\n", _prgname(),
jobid);
}
main(argc, argv)
int argc;
char *argv[];
{
register i;
char *kill = NULL, *rejuv = NULL, *system = NULL, *user = NULL;
struct passwd *pw;
static char uucpdir[BUFSIZ] = "";
myuid = getuid();
if (setpwent() != 0 ||
(pw = getpwnam("uucp")) == NULL ||
setuid((pw->pw_gid << 16) + pw->pw_uid) == -1)
fprintf(stderr, "%s: cannot set user ID to uucp\n",
_prgname());
endpwent();
if (info_str("UUCP.SPOOL", uucpdir, sizeof uucpdir) == NULL)
strcpy(uucpdir, "/h0/SPOOL/UUCP");
if (chdir(uucpdir) != 0) {
fprintf(stderr, "%s: cannot chdir to uucp spool directory\n",
_prgname());
exit(0);
}
for (i = 1; i < argc; ++i)
if (*argv[i] == '-') {
++argv[i];
do {
switch (tolower(*argv[i])) {
case 'k':
++argv[i];
getstring(&kill, &argv[i]);
break;
case 'r':
++argv[i];
getstring(&rejuv, &argv[i]);
break;
case 'u':
++argv[i];
getstring(&user, &argv[i]);
break;
case 's':
++argv[i];
getstring(&system, &argv[i]);
break;
case 'v':
++argv[i];
++verbose;
break;
case 'x':
if (*++argv[i] == '=')
++argv[i];
debug = getint(&argv[i]);
break;
default:
usage(NULL);
break;
}
} while (*argv[i]);
} else
usage("no arguments allowed");
if (kill != NULL && rejuv != NULL)
fprintf(stderr, "%s: conflicting options\n", _prgname());
else if (kill == NULL && rejuv == NULL)
scandir(NULL, display, user, system);
else {
if (system != NULL || user != NULL)
fprintf(stderr, "%s: %s%s%s option%s ignored\n",
_prgname(), user != NULL ? "-u " : "",
user != NULL && system != NULL ? "and " : "",
system != NULL ? "-s " : "",
user != NULL && system != NULL ? "s" : "");
if (kill != NULL)
scandir(kill, delete);
if (rejuv != NULL)
scandir(rejuv, touch);
}
}