|
|
1.1 root 1: /* number.c
2: * 11/28/83
3: * Usage: number [ -tn ] [ file ... ]
4: * Reads file ... or standard input,
5: * prints line numbered listing on standard output.
6: * Option -tn expands tabs to every n spaces (default: 8).
7: */
8:
9: #define VERSION "V1.4" /* version number */
10: #ifndef UDI
11: #define VCHAR 'V'
12: #else
13: #define VCHAR 'v' /* upper case mapped to lower under UDI */
14: #endif
15:
16: #include <stdio.h>
17:
18: int tab = 8; /* tab stop spacing */
19: int line = 0; /* line number */
20: int pos = 0; /* line position */
21: int last = '\n'; /* last character read */
22:
23: main (argc, argv)
24: int argc;
25: register char **argv;
26: {
27: register int c;
28:
29: /* process flags */
30: while (*++argv != NULL && **argv == '-') {
31: switch (*++*argv) {
32: case 't': tab = atoi (++*argv);
33: break;
34: case VCHAR: fprintf (stderr, "number: %s\n", VERSION);
35: break;
36: default: fprintf (stderr, "Usage: number [ -tn ] [ file ... ]\n");
37: exit (1);
38: break;
39: }
40: }
41:
42: /* process files */
43: do {
44: if (*argv != NULL) {
45: if (freopen (*argv++, "r", stdin) == NULL ) {
46: fprintf (stderr, "number: cannot open %s\n", *--argv);
47: exit (1);
48: }
49: }
50: while ((c = getchar ()) != EOF) put1 (c);
51: } while (*argv != NULL);
52: exit (0);
53: }
54:
55: /* print character c, expanding tabs and numbering after newlines */
56: put1 (c)
57: register c;
58: {
59: if (last == '\n') printf ("%4d: ", ++line);
60: last = c;
61: switch (c) {
62: case '\t': if (tab != 0) { /* expand tab */
63: do putchar (' ');
64: while ( ++pos % tab != 0);
65: return;
66: }
67: break;
68: case '\n': pos = 0;
69: break;
70: case '\b': --pos;
71: break;
72: default: ++pos;
73: break;
74: }
75: putchar (c);
76: }
77:
78: /* end of number.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.