1 /* $NetBSD: expand.c,v 1.12 2008/07/21 14:19:22 lukem Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)expand.c 8.1 (Berkeley) 6/9/93";
42 __RCSID("$NetBSD: expand.c,v 1.12 2008/07/21 14:19:22 lukem Exp $");
52 * expand - expand tabs to equivalent spaces
57 static void getstops(const char *);
58 int main(int, char **);
59 static void usage(void) __dead
;
62 main(int argc
, char *argv
[])
69 /* handle obsolete syntax */
71 argv
[1][0] == '-' && isdigit((unsigned char)argv
[1][1])) {
72 getstops(&argv
[1][1]);
76 while ((c
= getopt (argc
, argv
, "t:")) != -1) {
92 if (freopen(argv
[0], "r", stdin
) == NULL
)
93 err(EXIT_FAILURE
, "Cannot open `%s'", argv
[0]);
97 while ((c
= getchar()) != EOF
) {
104 } while (column
& 07);
111 } while (((column
- 1) % tabstops
[0])
112 != (tabstops
[0] - 1));
115 for (n
= 0; n
< nstops
; n
++)
116 if (tabstops
[n
] > column
)
123 while (column
< tabstops
[n
]) {
151 getstops(const char *spec
)
154 const char *cp
= spec
;
159 while (*cp
>= '0' && *cp
<= '9')
160 i
= i
* 10 + *cp
++ - '0';
161 if (i
<= 0 || i
> 256)
162 errx(EXIT_FAILURE
, "Too large tab stop spec `%d'", i
);
163 if (nstops
> 0 && (size_t)i
<= tabstops
[nstops
-1])
164 errx(EXIT_FAILURE
, "Out of order tabstop spec `%d'", i
);
165 if (nstops
== sizeof(tabstops
) / sizeof(tabstops
[0]) - 1)
166 errx(EXIT_FAILURE
, "Too many tabstops");
167 tabstops
[nstops
++] = i
;
170 if (*cp
!= ',' && *cp
!= ' ')
171 errx(EXIT_FAILURE
, "Illegal tab stop spec `%s'", spec
);
180 (void)fprintf(stderr
, "Usage: %s [-t tablist] [file ...]\n",