1 | /* Copyright 1985-1999 by the Massachusetts Institute of Technology. |
---|
2 | * |
---|
3 | * Permission to use, copy, modify, and distribute this |
---|
4 | * software and its documentation for any purpose and without |
---|
5 | * fee is hereby granted, provided that the above copyright |
---|
6 | * notice appear in all copies and that both that copyright |
---|
7 | * notice and this permission notice appear in supporting |
---|
8 | * documentation, and that the name of M.I.T. not be used in |
---|
9 | * advertising or publicity pertaining to distribution of the |
---|
10 | * software without specific, written prior permission. |
---|
11 | * M.I.T. makes no representations about the suitability of |
---|
12 | * this software for any purpose. It is provided "as is" |
---|
13 | * without express or implied warranty. |
---|
14 | */ |
---|
15 | |
---|
16 | /* This is dent, a program to indent the standard input. */ |
---|
17 | |
---|
18 | static const char rcsid[] = "$Id: dent.c,v 1.4 1999-09-16 00:02:55 danw Exp $"; |
---|
19 | |
---|
20 | #include <stdio.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <string.h> |
---|
23 | |
---|
24 | void indent(FILE *fptr, int nspaces); |
---|
25 | void spaceout(int spaces); |
---|
26 | static int read_line(FILE *fp, char **buf, int *bufsize); |
---|
27 | |
---|
28 | int main(int argc, char **argv) |
---|
29 | { |
---|
30 | FILE *fptr = stdin; /* initially read standard input file */ |
---|
31 | int i; |
---|
32 | int nspaces = 8; |
---|
33 | |
---|
34 | for (i = 1; i < argc; i++) |
---|
35 | { |
---|
36 | if (argv[i][0] == '-') |
---|
37 | { |
---|
38 | if (sscanf(&argv[i][1], "%d", &nspaces) == 0) |
---|
39 | { |
---|
40 | fprintf(stderr, "%s: argument must be integer.\n", argv[0]); |
---|
41 | exit(1); |
---|
42 | } |
---|
43 | continue; |
---|
44 | } |
---|
45 | |
---|
46 | fptr = fopen(argv[i],"r"); |
---|
47 | if (!fptr) |
---|
48 | { |
---|
49 | fprintf(stderr,"%s: no such file %s.\n", argv[0], argv[i]); |
---|
50 | exit(1); |
---|
51 | } |
---|
52 | indent(fptr, nspaces); |
---|
53 | fclose(fptr); |
---|
54 | } |
---|
55 | |
---|
56 | if (fptr == stdin) |
---|
57 | indent(fptr, nspaces); |
---|
58 | |
---|
59 | return 0; |
---|
60 | } |
---|
61 | |
---|
62 | void indent(FILE *fptr, int nspaces) |
---|
63 | { |
---|
64 | int j, linesize; |
---|
65 | char *line = NULL; |
---|
66 | |
---|
67 | while (read_line(fptr, &line, &linesize) == 0) |
---|
68 | { |
---|
69 | if (strlen(line) >= 1) |
---|
70 | { |
---|
71 | spaceout(nspaces); |
---|
72 | for (j = 0; j < strlen(line); j++) |
---|
73 | { |
---|
74 | putchar(line[j]); |
---|
75 | if (line[j] == '\r') |
---|
76 | spaceout(nspaces); |
---|
77 | } |
---|
78 | putchar('\n'); |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | void spaceout(int spaces) |
---|
84 | { |
---|
85 | while (spaces-- > 0) |
---|
86 | putchar(' '); |
---|
87 | } |
---|
88 | |
---|
89 | /* This is an internal function. Its contract is to read a line from a |
---|
90 | * file into a dynamically allocated buffer, zeroing the trailing newline |
---|
91 | * if there is one. The calling routine may call read_line multiple |
---|
92 | * times with the same buf and bufsize pointers; *buf will be reallocated |
---|
93 | * and *bufsize adjusted as appropriate. The initial value of *buf |
---|
94 | * should be NULL. After the calling routine is done reading lines, it |
---|
95 | * should free *buf. This function returns 0 if a line was successfully |
---|
96 | * read, 1 if the file ended, and -1 if there was an I/O error or if it |
---|
97 | * ran out of memory. |
---|
98 | */ |
---|
99 | |
---|
100 | static int read_line(FILE *fp, char **buf, int *bufsize) |
---|
101 | { |
---|
102 | char *newbuf; |
---|
103 | int offset = 0, len; |
---|
104 | |
---|
105 | if (*buf == NULL) |
---|
106 | { |
---|
107 | *buf = malloc(128); |
---|
108 | if (!*buf) |
---|
109 | return -1; |
---|
110 | *bufsize = 128; |
---|
111 | } |
---|
112 | |
---|
113 | while (1) |
---|
114 | { |
---|
115 | if (!fgets(*buf + offset, *bufsize - offset, fp)) |
---|
116 | return (offset != 0) ? 0 : (ferror(fp)) ? -1 : 1; |
---|
117 | len = offset + strlen(*buf + offset); |
---|
118 | if ((*buf)[len - 1] == '\n') |
---|
119 | { |
---|
120 | (*buf)[len - 1] = 0; |
---|
121 | return 0; |
---|
122 | } |
---|
123 | offset = len; |
---|
124 | |
---|
125 | /* Allocate more space. */ |
---|
126 | newbuf = realloc(*buf, *bufsize * 2); |
---|
127 | if (!newbuf) |
---|
128 | return -1; |
---|
129 | *buf = newbuf; |
---|
130 | *bufsize *= 2; |
---|
131 | } |
---|
132 | } |
---|