[BACK]Return to unexpand.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / unexpand

Annotation of src/usr.bin/unexpand/unexpand.c, Revision 1.11

1.11    ! deraadt     1: /*     $OpenBSD: unexpand.c,v 1.10 2009/10/27 23:59:46 deraadt Exp $   */
1.1       deraadt     2: /*     $NetBSD: unexpand.c,v 1.5 1994/12/24 17:08:05 cgd Exp $ */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.7       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: /*
                     34:  * unexpand - put tabs into a file replacing blanks
                     35:  */
                     36: #include <stdio.h>
1.9       david      37: #include <stdlib.h>
1.1       deraadt    38: #include <string.h>
1.11    ! deraadt    39: #include <unistd.h>
1.1       deraadt    40:
                     41: char   genbuf[BUFSIZ];
                     42: char   linebuf[BUFSIZ];
                     43: int    all;
                     44:
1.5       millert    45: void tabify(char);
1.1       deraadt    46:
                     47: int
1.8       deraadt    48: main(int argc, char *argv[])
1.1       deraadt    49: {
1.4       mpech      50:        char *cp;
1.11    ! deraadt    51:
        !            52:        if (pledge("stdio rpath", NULL) == -1)
        !            53:                perror("pledge");
1.1       deraadt    54:
                     55:        argc--, argv++;
                     56:        if (argc > 0 && argv[0][0] == '-') {
                     57:                if (strcmp(argv[0], "-a") != 0) {
1.3       deraadt    58:                        fprintf(stderr, "usage: unexpand [-a] [file ...]\n");
1.1       deraadt    59:                        exit(1);
                     60:                }
                     61:                all++;
                     62:                argc--, argv++;
                     63:        }
                     64:        do {
                     65:                if (argc > 0) {
                     66:                        if (freopen(argv[0], "r", stdin) == NULL) {
                     67:                                perror(argv[0]);
                     68:                                exit(1);
                     69:                        }
                     70:                        argc--, argv++;
                     71:                }
                     72:                while (fgets(genbuf, BUFSIZ, stdin) != NULL) {
                     73:                        for (cp = linebuf; *cp; cp++)
                     74:                                continue;
                     75:                        if (cp > linebuf)
                     76:                                cp[-1] = 0;
                     77:                        tabify(all);
                     78:                        printf("%s", linebuf);
                     79:                }
                     80:        } while (argc > 0);
                     81:        exit(0);
                     82: }
                     83:
                     84: void
1.8       deraadt    85: tabify(char c)
1.1       deraadt    86: {
1.4       mpech      87:        char *cp, *dp;
                     88:        int dcol;
1.1       deraadt    89:        int ocol;
1.6       deraadt    90:        size_t len;
1.1       deraadt    91:
                     92:        ocol = 0;
                     93:        dcol = 0;
1.6       deraadt    94:        cp = genbuf;
                     95:        dp = linebuf;
                     96:        len = sizeof linebuf;
                     97:
1.1       deraadt    98:        for (;;) {
                     99:                switch (*cp) {
                    100:
                    101:                case ' ':
                    102:                        dcol++;
                    103:                        break;
                    104:
                    105:                case '\t':
                    106:                        dcol += 8;
                    107:                        dcol &= ~07;
                    108:                        break;
                    109:
                    110:                default:
                    111:                        while (((ocol + 8) &~ 07) <= dcol) {
                    112:                                if (ocol + 1 == dcol)
                    113:                                        break;
1.6       deraadt   114:                                if (len > 1) {
                    115:                                        *dp++ = '\t';
                    116:                                        len--;
                    117:                                }
1.1       deraadt   118:                                ocol += 8;
                    119:                                ocol &= ~07;
                    120:                        }
                    121:                        while (ocol < dcol) {
1.6       deraadt   122:                                if (len > 1) {
                    123:                                        *dp++ = ' ';
                    124:                                        len--;
                    125:                                }
1.1       deraadt   126:                                ocol++;
                    127:                        }
                    128:                        if (*cp == 0 || c == 0) {
1.6       deraadt   129:                                strlcpy(dp, cp, len);
1.1       deraadt   130:                                return;
                    131:                        }
                    132:                        *dp++ = *cp;
1.6       deraadt   133:                        len--;
                    134:                        ocol++;
                    135:                        dcol++;
1.1       deraadt   136:                }
                    137:                cp++;
                    138:        }
                    139: }