|
|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T i
Length: 4632 (0x1218)
Types: TextFile
Names: »io.c«
└─⟦87ddcff64⟧ Bits:30001253 CPHDIST85 Tape, 1985 Autumn Conference Copenhagen
└─⟦this⟧ »cph85dist/stat/src/io.c«
#include "unixstat.h"
PGM(io,Control and Monitor Input and Output,5.1,5/15/85)
/* Copyright (c) 1982 Gary Perlman (see Copyright file) */
#define MORE "/usr/ucb/more"
#include <sys/types.h>
#include <sys/stat.h>
#include <sgtty.h>
int Pipein; /* is input being piped/redirected in? */
int Pipeout; /* is output being piped/redirected out? */
#define Ttyin (!Pipein)
#define Ttyout (!Pipeout)
/* OPTIONS */
int Flowmeter; /* should the data flow through io be monitored? */
int Forceio; /* force the overwriting of existing files? */
int Appendio; /* should the data be appended to files? */
initial (argc, argv) char **argv;
{
int C;
int opterr = 0;
struct sgttyb tty;
extern int optind; /* set by getopt, is index of first file */
ARGV0;
Pipein = gtty (fileno (stdin), &tty);
Pipeout = gtty (fileno (stdout), &tty);
while ((C = getopt (argc, argv, "amf")) != EOF)
switch (C)
{
case 'a': Appendio = 1; break;
case 'm': Flowmeter = 1; break;
case 'f': Forceio = 1; break;
default: opterr++; break;
}
if (opterr)
USAGE ([-amf] files)
return (optind);
}
main (argc, argv) char **argv;
{
int optind = initial (argc, argv);
int nfiles = argc - optind;
if (Pipein && Pipeout) /* in the middle of a pipeline */
{
if (nfiles == 0)
{
Flowmeter = 1; /* automatically use as flowmeter, else useless */
foutput (stdin, stdout); /* copy stdin to stdout */
exit (0);
}
else if (nfiles == 1)
output (argc, argv, optind);
else
ERRMSG0 (Can only copy output to one file)
}
else if (Pipein && Ttyout) /* at the end of the pipeline */
{
if (nfiles == 0)
ERRMSG0 (No file to output to)
output (argc, argv, optind);
}
else if (Ttyin) /* at beginning of pipeline */
{
if (nfiles == 0)
ERRDATA
if (Ttyout && Flowmeter)
{
execv (MORE, argv);
WARNING (No flowmeter for tty output)
Flowmeter = 0;
}
input (argc, argv, optind);
}
exit (0);
}
foutput (in, out)
FILE *in, *out;
{
char *flowchrs = "=*#@$&+";
char flowchar = flowchrs[getpid () % 7];
char buffer[BUFSIZ];
int nbytes;
while ((nbytes = fread (buffer, 1, BUFSIZ, in)) == BUFSIZ)
{
VOID fwrite (buffer, 1, BUFSIZ, out);
if (Pipeout) VOID fwrite (buffer, 1, BUFSIZ, stdout);
if (Flowmeter) putc (flowchar, stderr);
}
VOID fwrite (buffer, 1, nbytes, out);
if (Pipeout) VOID fwrite (buffer, 1, nbytes, stdout);
if (Flowmeter) putc ('\n', stderr);
}
char *
directory (file)
char *file;
{
static char dir[BUFSIZ];
strcpy (dir, file);
for (file = dir; *file; file++);
while (file > dir && *(file-1) != '/') file--;
if (file == dir) strcpy (dir, ".");
else *file = '\0';
return (dir);
}
output (argc, argv, optind)
int argc;
char **argv;
int optind;
{
char tmp[50];
char *file; /* full name of file to be processed */
char *filename;
FILE *ioptr;
int nfiles = argc - optind;
if (nfiles == 0)
{
foutput (stdin, stdout);
exit (0);
}
if (nfiles >= 2)
ERRMSG0 (Can only output to one file)
file = argv[optind];
if (access (file, 4) == 0) /* must create tmp file */
{
if (access (file, 2) || access (directory (file), 2))
/* must be able to overwrite file and directory */
ERRMSG1 (Cannot overwrite %s, file)
if (Forceio == 0)
if (!confirm ("io: delayed safe overwrite of %s?", file))
ERRMSG1 (Exiting to avoid overwriting %s, file)
filename = file + strlen (file);
while (filename > file && *(filename-1) != '/') filename--;
VOID sprintf (tmp, "/tmp/%d%s", getpid (), filename);
filename = tmp;
if ((ioptr = fopen (filename, "w")) == NULL)
ERROPEN (filename)
}
else
{
filename = file;
if ((ioptr = fopen (filename, "w")) == NULL)
ERROPEN (filename)
}
foutput (stdin, ioptr);
VOID fclose (ioptr);
if (filename == tmp)
{
char copycomm[100];
VOID sprintf (copycomm, "cat %s %s %s", tmp, Appendio?">>":">", file);
system (copycomm);
unlink (tmp);
}
exit (0);
}
input (argc, argv, optind)
int argc;
char **argv;
int optind;
{
int i;
struct stat statbuf;
FILE *ioptr;
register int C;
long size = 0, sofar = 0;
for (i = optind; i < argc; i++)
{
if (stat (argv[i], &statbuf))
ERROPEN (argv[i])
else size += statbuf.st_size;
}
size /= 10;
for (i = optind; i < argc; i++)
{
if ((ioptr = fopen (argv[i], "r")) == NULL)
ERROPEN (argv[i])
while ((C = getc (ioptr)) != EOF)
{
putchar (C);
if (Flowmeter && (++sofar%size == 0))
fprintf (stderr, "%4D%%",10*sofar/size);
}
VOID fclose (ioptr);
}
if (Flowmeter) putc ('\n', stderr);
exit (0);
}