20 static const char *ego = "<unset>";
22 static const char *bkp = 0;
24 static unsigned flags = 0;
25 #define F_MIDLINETABS 1u
29 #define F_UNTABIFY 16u
33 static void usage(FILE *fp)
34 { fprintf(fp, "Usage: %s [-cmtuv] [-i[BKP]] [FILE...]\n\n", ego); }
36 static char *augment(const char *name, const char *suffix)
38 size_t n = strlen(name), nn = strlen(suffix);
39 char *p = malloc(n + nn + 1);
42 fprintf(stderr, "%s: Out of memory!\n", ego);
46 memcpy(p + n, suffix, nn + 1);
50 static FILE *freshname(const char *name, char **newname, mode_t mode)
58 for (i = 0; i < 32767; i++) {
59 sprintf(buf, ".new%d", i);
60 if ((n = augment(name, buf)) == 0)
62 if ((fd = open(n, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
63 if (errno == EEXIST) {
67 fprintf(stderr, "%s: Can't create new file for `%s': %s\n",
68 ego, name, strerror(errno));
73 fprintf(stderr, "%s: Can't find new file to update `%s'\n", ego, name);
78 fprintf(stderr, "%s: Can't set permissions on `%s': %s\n",
79 ego, n, strerror(errno));
82 if ((fp = fdopen(fd, "w")) == 0) {
83 fprintf(stderr, "%s: fdopen on `%s' failed: %s\n",
84 ego, n, strerror(errno));
103 #define BUF_INIT { 0, 0, 0 }
105 static void reset(buf *b) { b->n = 0; }
107 static int put(buf *b, int ch)
117 b->b = realloc(b->b, w);
120 fprintf(stderr, "%s: Not enough memory for buffer!\n", ego);
129 #define TABSTOP(n) (((n) + 8u) & ~7u)
131 static int space(const char *name)
133 static buf b = BUF_INIT;
134 FILE *fin, *fout = stdout;
135 char *newname = 0, *oldname = 0;
136 int rc = TROUBLE, status = OK;
138 unsigned nsp = 0, nwsp = 0, hpos = 0, ohpos = 0, nhpos = 0, nl = 1;
141 #define f_warnspacetab 2u
143 #define f_warntabs 8u
144 #define f_warnspaces 16u
147 #define f_forced 128u
148 unsigned f = f_newline | (flags & F_TABIFY ? f_tabify : 0);
151 if (strcmp(name, "-") == 0) {
152 if (flags & F_INPLACE) {
153 fprintf(stderr, "%s: Can't modify stdin in-place.\n", ego);
158 if ((fin = fopen(name, "r")) == 0) {
159 fprintf(stderr, "%s: Failed to open file `%s': %s.\n",
160 ego, name, strerror(errno));
163 else if (flags & F_INPLACE) {
165 if (stat(name, &st)) {
166 fprintf(stderr, "%s: Can't stat `%s': %s.\n",
167 ego, name, strerror(errno));
170 if ((fout = freshname(name, &newname, st.st_mode)) == 0)
181 nsp++; nwsp++; hpos++;
182 if (put(&b, ' ')) goto done_2;
185 if (flags & F_UNTABIFY) {
186 if ((flags & F_CHECK) && !(f & f_warntabs)) {
187 fprintf(stderr, "%s:%u: found tab\n", name, nl);
191 } else if (((flags & F_MIDLINETABS) || (f & f_newline)) && nsp) {
192 if ((flags & F_VERBOSE) && !(f & f_warnspacetab)) {
193 fprintf(stderr, "%s:%u: space followed by tab\n", name, nl);
197 f |= f_tabify | f_forced;
200 nsp = 0; nwsp++; hpos = TABSTOP(hpos);
201 if (put(&b, '\t')) goto done_2;
204 if (nwsp || !(f & f_newline)) {
205 if (flags & F_VERBOSE)
206 fprintf(stderr, "%s:%u: file ends in mid-line\n", name, nl);
208 if (fout) putc('\n', fout);
213 if (nwsp && (flags & F_VERBOSE)) {
214 fprintf(stderr, "%s:%u: trailing whitespace\n", name, nl);
217 if (fout) putc('\n', fout);
219 nsp = nwsp = hpos = ohpos = 0; nl++;
221 f &= ~(f_tab | f_warnspacetab | f_warntabs | f_warnspaces);
222 if (flags & F_TABIFY)
230 if (flags & F_UNTABIFY) {
231 if (fout) for (; ohpos < hpos; ohpos++) putc(' ', fout);
232 } else if ((f & f_tabify) &&
233 ((hpos - ohpos >= (last == '.' || last == ':' ?
235 (f & (f_tab | f_newline)))) {
238 nhpos = TABSTOP(ohpos);
239 if (nhpos > hpos) break;
240 if (fout) putc('\t', fout);
241 if ((flags & F_VERBOSE) && (flags & F_TABIFY) &&
242 i < b.n && b.b[i] != '\t' &&
243 !(f & (f_warnspaces | f_forced))) {
244 fprintf(stderr, "%s:%u: spaces could be turned into tabs\n",
252 for (; ohpos < hpos; ohpos++) putc(' ', fout);
254 for (i = 0; i < b.n; i++) putc(b.b[i], fout);
257 f &= ~(f_newline | f_tab | f_forced);
258 if (!(flags & F_TABIFY) || !(flags & F_MIDLINETABS)) f &= ~f_tabify;
260 hpos++; ohpos = hpos;
261 if (fout) putc(ch, fout);
262 if (ch != '"' && ch != '\'')
270 fprintf(stderr, "%s: Error reading `%s': %s\n",
271 ego, name, strerror(errno));
276 if (fflush(fout) || ferror(fout)) f |= f_bad;
277 if (fout != stdout && fclose(fout)) f |= f_bad;
280 fprintf(stderr, "%s: Error writing `%s': %s\n",
281 ego, newname, strerror(errno));
286 if (flags & F_INPLACE) {
288 if ((oldname = augment(name, bkp)) == 0)
290 if (rename(name, oldname)) {
291 fprintf(stderr, "%s: Failed to back up `%s' as `%s': %s\n",
292 ego, name, oldname, strerror(errno));
296 if (rename(newname, name)) {
297 if (oldname) rename(oldname, name);
298 fprintf(stderr, "%s: Failed to install `%s' as `%s': %s\n",
299 ego, newname, name, strerror(errno));
307 if (oldname) free(oldname);
313 if (fout && fout != stdout) fclose(fout);
319 static int manysetp(unsigned f) { return (!!(f & (f - 1))); }
321 int main(int argc, char *argv[])
326 if ((ego = strrchr(argv[0], '/')) == 0)
332 if ((i = getopt(argc, argv, "h" "cmtuv" "i::")) < 0)
336 printf("%s -- remove extraneous spaces from files\n\n", ego);
339 -h Print this help text\n\
340 -c Check files for badness, but don't produce other output\n\
341 -m Fix spaces followed by tabs in mid-line\n\
342 -t Tabify file completely\n\
343 -u Untabify file completely\n\
344 -i[BKP] Modify files in place; leave FILEBKP as copy of old FILE\n\
352 flags |= F_MIDLINETABS;
371 if (flags & F_BOGUS) {
375 if (manysetp(flags & (F_CHECK | F_INPLACE))) {
376 fprintf(stderr, "%s: Options -c and -i are mutually exclusive.\n", ego);
379 if (manysetp(flags & (F_TABIFY | F_UNTABIFY))) {
380 fprintf(stderr, "%s: Options -t and -u are mutually exclusive.\n", ego);
384 if (optind == argc) {
386 fprintf(stderr, "%s: No options given and stdin is a terminal.\n",
391 } else for (i = optind; i < argc; i++) {
393 if (st > rc) rc = st;
395 if (rc == BADNESS && !(flags & F_CHECK))