UNIX — универсальная среда программирования - Брайан Керниган
Шрифт:
Интервал:
Закладка:
#include <stdio.h>
#define PAGESIZE 22
char *progname; /* program name for error message */
main(argc, argv)
int argc;
char *argv[];
{
int i;
FILE *fp, *efopen();
progname = argv[0];
if (argc == 1)
print(stdin, PAGESIZE);
else
for (i = 1; i < argc; i++) {
fp = efopen(argv[i], "r");
print(fp, PAGESIZE);
fclose(fp);
}
exit(0);
}
print(fp, pagesize) /* print fp in pagesize chunks */
FILE *fp;
int pagesize;
{
static int lines = 0; /* number of lines so far */
char buf[BUFSIZ];
while (fgets(buf, sizeof buf, fp) != NULL)
if (++lines < pagesize)
fputs(buf, stdout);
else {
buf[strlen(buf)-1] = ' ';
fputs(buf, stdout);
fflush(stdout);
ttyin();
lines = 0;
}
}
#include "ttyin1.c"
#include "efopen.c"
3.8.39 p2.c
/* p: print input in chunks (version 2) */
#include <stdio.h>
#define PAGESIZE 22
char *progname; /* program name for error message */
main(argc, argv)
int argc;
char *argv[];
{
FILE *fp, *efopen();
int i, pagesize = PAGESIZE;
progname = argv[0];
if (argc > 1 && argv[1][0] == '-') {
pagesize = atoi(&argv[1][1]);
argc--;
argv++;
}
if (argc == 1)
print(stdin, pagesize);
for (i = 1; i < argc; i++) {
fp = efopen(argv[i], "r");
print(fp, pagesize);
fclose(fp);
}
exit(0);
}
print(fp, pagesize) /* print fp in pagesize chunks */
FILE *fp;
int pagesize;
{
static int lines = 0; /* number of lines so far */
char buf[BUFSIZ];
while (fgets(buf, sizeof buf, fp) != NULL)
if (++lines < pagesize)
fputs(buf, stdout);
else {
buf[strlen(buf)-1] = ' ';
fputs(buf, stdout);
fflush(stdout);
ttyin();
lines = 0;
}
}
#include "ttyin2.c"
#include "efopen.c"
3.8.40 p3.c
/* p: print input in chunks (version 3) */
#include <stdio.h>
#define PAGESIZE 22
char *progname; /* program name for error message */
main(argc, argv)
int argc;
char *argv[];
{
FILE *fp, *efopen();
int i, pagesize = PAGESIZE;
char *p, *getenv();
progname = argv[0];
if ((p=getenv("PAGESIZE")) != NULL)
pagesize = atoi(p);
if (argc > 1 && argv[1][0] == '-') {
pagesize = atoi(&argv[1][1]);
argc--;
argv++;
}
if (argc == 1)
print(stdin, pagesize);
else
for (i = 1; i < argc; i++) {
fp = efopen(argv[i], "r");
print(fp, pagesize);
fclose(fp);
}
exit(0);
}
print(fp, pagesize) /* print fp in pagesize chunks */
FILE *fp;
int pagesize;
{
static int lines = 0; /* number of lines so far */
char buf[BUFSIZ];
while (fgets(buf, sizeof buf, fp) != NULL)
if (++lines < pagesize)
fputs(buf, stdout);
else {
buf[strlen(buf)-1] = ' ';
fputs(buf, stdout);
fflush(stdout);
ttyin();
lines = 0;
}
}
#include "ttyin2.c"
#include "efopen.c"
3.8.41 p4.c
/* p: print input in chunks (version 4) */
#include <stdio.h>
#define PAGESIZE 22
char *progname; /* program name for error message */
main(argc, argv)
int argc;
char *argv[];
{
FILE *fp, *efopen();
int i, pagesize = PAGESIZE;
char *p, *getenv(), buf[BUFSIZ];
progname = argv[0];
if ((p=getenv("PAGESIZE")) != NULL)
pagesize = atoi(p);
if (argc > 1 && argv[1][0] == '-') {
pagesize = atoi(&argv[1][1]);
argc--;
argv++;
}
if (argc == 1)
print(stdin, pagesize);
else
for (i = 1; i < argc; i++)
switch (spname(argv[i], buf)) {
case -1: /* no match possible */
fp = efopen(argv[i], "r");
break;
case 1: /* corrected */
fprintf (stderr, ""%s"? ", buf);
if (ttyin() == 'n')
break;
argv[i] = buf;
/* fall through... */
case 0: /* exact match */
fp = efopen(argv[i], "r");
print(fp, pagesize);
fclose(fp);
}
exit(0);
}
print(fp, pagesize) /* print fp in pagesize chunks */
FILE *fp;
int pagesize;
{
static int lines = 0; /* number of lines so far */
char buf[BUFSIZ];
while (fgets(buf, sizeof buf, fp) != NULL)
if (++lines < pagesize) fputs(buf, stdout);
else {
buf[strlen(buf)-1] = ' ';
fputs(buf, stdout);
fflush(stdout);
ttyin();
lines = 0;
}
}
#include "ttyin2.c"
#include "efopen.c"
#include "spname.c"
3.8.42 pick1
# pick: select arguments
PATH=/bin:/usr/bin
for i # for each argument
do
echo -n "$i? " >/dev/tty
read response
case $response in
y*) echo $i ;;
q*) break
esac
done </dev/tty
3.8.43 pick.c
/* pick: offer choice on each argument */
#include <stdio.h>
char *progname; /* program name for error message */
main(argc, argv)
int argc;
char *argv[];
{
int i;
char buf[BUFSIZ];
progname = argv[0];
if (argc == 2 && strcmp(argv[1], "-") == 0) /* pick - */
while (fgets(buf, sizeof buf, stdin) != NULL) {
buf[strlen(buf)-1] = ' '; /* drop newline */
pick(buf);
}
for (i = 1; i < argc; i++)
pick(argv[i]);
exit(0);
}
pick(s) /* offer choice of s */
char *s;
{
fprintf(stderr, "%s? ", s);
if (ttyin() == 'y')
printf("%sn", s);
}
#include "ttyin2.c"
#include "efopen.c"
3.8.44 prpages
# prpages: compute number of pages that pr will print
wc $* |
awk '!/ total$/ { n += int(($1+55) / 56) }
END { print n }'
3.8.45 put
# put: install file into history
PATH=/bin:/usr/bin
case $# in
1) HIST=$1.H ;;
*) echo 'Usage: put file' 1>&2; exit 1 ;;
esac
if test ! -r $1
then
echo "put: can't open $1" 1>&2
exit 1
fi
trap 'rm -f /tmp/put.[ab]$$; exit 1 12 15
echo -n 'Summary: '
read Summary
if get -o /tmp/put.a$$ $1 # previous version
then # merge pieces
cp $1 /tmp/put.b$$ # current version
echo `getname` `date` $Summary" >>/tmp/put.b$$
diff -e $1 /tmp/put.a$$ >>/tmp/put.b$$ # latest diffs
sed -n '/^@@@/,$р' <$HIST >>/tmp/put.b$$ # old diffs
overwrite $HIST cat /tmp/put.b$$ # put it back
else # make a new one
echo "put: creating $HIST"
cp $1 $HIST
echo "@@@ `getname` `date` $Summary" >>$HIST
fi
rm -f /tmp/put.[ab]$$
3.8.46 readslow.c
/* readslow: keep reading, waiting for more */
#define SIZE 512 /* arbitrary */
main() {
char buf[SIZE];
int n;
for (;;) {
while ((n = read(0, buf, sizeof buf)) > 0)
write(1, buf, n);
sleep(10);
}
}
3.8.47 replace
# replace: replace str1 in files with str2, in place
PATH=/bin:/usr/bin
case $# in
0|1|2) echo 'Usage: replace str1 str2 files' 1>&2; exit 1
esac
left="$1"; right="$2"; shift; shift
for i
do
overwrite $i sed "[email protected][email protected][email protected]" $i
done
3.8.48 signaltest.c
#include <stdio.h>
#include <signal.h>
#include <errno.h>
extern int errno;
main() {
int с, n;
char buf[100];
int onintr();
signal(SIGINT, onintr);
for (;;) {
n = read(0, buf, 100);
if (n > 0)
printf(buf);
else {