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

Annotation of src/usr.bin/fold/fold.c, Revision 1.7

1.7     ! millert     1: /*     $OpenBSD: fold.c,v 1.6 2002/06/17 07:06:12 deraadt Exp $        */
1.1       deraadt     2: /*     $NetBSD: fold.c,v 1.6 1995/09/01 01:42:44 jtc Exp $     */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Kevin Ruddy.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
1.7     ! millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: static char copyright[] =
                     38: "@(#) Copyright (c) 1990, 1993\n\
                     39:        The Regents of the University of California.  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: #if 0
                     44: static char sccsid[] = "@(#)fold.c     8.1 (Berkeley) 6/6/93";
                     45: #endif
1.7     ! millert    46: static char rcsid[] = "$OpenBSD: fold.c,v 1.6 2002/06/17 07:06:12 deraadt Exp $";
1.1       deraadt    47: #endif /* not lint */
                     48:
                     49: #include <stdio.h>
                     50: #include <stdlib.h>
                     51: #include <string.h>
                     52: #include <unistd.h>
                     53: #include <err.h>
                     54:
                     55: #define        DEFLINEWIDTH    80
                     56:
                     57: static void fold ();
                     58: static int new_column_position ();
                     59: int count_bytes = 0;
                     60: int split_words = 0;
                     61:
                     62: int
                     63: main(argc, argv)
                     64:        int argc;
                     65:        char **argv;
                     66: {
1.5       mpech      67:        int ch;
1.1       deraadt    68:        int width;
                     69:        char *p;
                     70:
                     71:        width = -1;
                     72:        while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1)
                     73:                switch (ch) {
                     74:                case 'b':
                     75:                        count_bytes = 1;
                     76:                        break;
                     77:                case 's':
                     78:                        split_words = 1;
                     79:                        break;
                     80:                case 'w':
1.4       pjanzen    81:                        if ((width = atoi(optarg)) <= 0)
                     82:                                errx(1, "illegal width value.");
1.1       deraadt    83:                        break;
                     84:                case '0': case '1': case '2': case '3': case '4':
                     85:                case '5': case '6': case '7': case '8': case '9':
                     86:                        if (width == -1) {
                     87:                                p = argv[optind - 1];
                     88:                                if (p[0] == '-' && p[1] == ch && !p[2])
                     89:                                        width = atoi(++p);
                     90:                                else
                     91:                                        width = atoi(argv[optind] + 1);
                     92:                        }
                     93:                        break;
                     94:                default:
                     95:                        (void)fprintf(stderr,
                     96:                            "usage: fold [-bs] [-w width] [file ...]\n");
                     97:                        exit(1);
                     98:                }
                     99:        argv += optind;
                    100:        argc -= optind;
                    101:
                    102:        if (width == -1)
                    103:                width = DEFLINEWIDTH;
                    104:
                    105:        if (!*argv)
                    106:                fold(width);
                    107:        else for (; *argv; ++argv)
                    108:                if (!freopen(*argv, "r", stdin)) {
1.6       deraadt   109:                        err(1, "%s", *argv);
1.1       deraadt   110:                        /* NOTREACHED */
                    111:                } else
                    112:                        fold(width);
                    113:        exit(0);
                    114: }
                    115:
                    116: /*
                    117:  * Fold the contents of standard input to fit within WIDTH columns
                    118:  * (or bytes) and write to standard output.
                    119:  *
                    120:  * If split_words is set, split the line at the last space character
                    121:  * on the line.  This flag necessitates storing the line in a buffer
                    122:  * until the current column > width, or a newline or EOF is read.
                    123:  *
                    124:  * The buffer can grow larger than WIDTH due to backspaces and carriage
                    125:  * returns embedded in the input stream.
                    126:  */
                    127: static void
                    128: fold(width)
1.5       mpech     129:        int width;
1.1       deraadt   130: {
                    131:        static char *buf = NULL;
                    132:        static int   buf_max = 0;
1.5       mpech     133:        int ch, col;
                    134:        int indx;
1.1       deraadt   135:
                    136:        col = indx = 0;
                    137:        while ((ch = getchar()) != EOF) {
                    138:                if (ch == '\n') {
                    139:                        if (indx != 0)
1.6       deraadt   140:                                fwrite(buf, 1, indx, stdout);
1.1       deraadt   141:                        putchar('\n');
                    142:                        col = indx = 0;
                    143:                        continue;
                    144:                }
                    145:
1.6       deraadt   146:                col = new_column_position(col, ch);
1.1       deraadt   147:                if (col > width) {
                    148:                        int i, last_space;
                    149:
                    150:                        if (split_words) {
                    151:                                for (i = 0, last_space = -1; i < indx; i++)
1.6       deraadt   152:                                        if(buf[i] == ' ')
                    153:                                                last_space = i;
1.1       deraadt   154:                        }
                    155:
                    156:                        if (split_words && last_space != -1) {
                    157:                                last_space++;
                    158:
1.6       deraadt   159:                                fwrite(buf, 1, last_space, stdout);
                    160:                                memmove(buf, buf+last_space, indx-last_space);
1.1       deraadt   161:
                    162:                                indx -= last_space;
                    163:                                col = 0;
                    164:                                for (i = 0; i < indx; i++) {
1.6       deraadt   165:                                        col = new_column_position(col, buf[i]);
1.1       deraadt   166:                                }
                    167:                        } else {
1.6       deraadt   168:                                fwrite(buf, 1, indx, stdout);
1.1       deraadt   169:                                col = indx = 0;
                    170:                        }
                    171:                        putchar('\n');
                    172:
                    173:                        /* calculate the column position for the next line. */
1.6       deraadt   174:                        col = new_column_position(col, ch);
1.1       deraadt   175:                }
                    176:
                    177:                if (indx + 1 > buf_max) {
                    178:                        /* Allocate buffer in LINE_MAX increments */
                    179:                        buf_max += 2048;
1.6       deraadt   180:                        if((buf = realloc(buf, buf_max)) == NULL) {
                    181:                                err(1, NULL);
1.1       deraadt   182:                                /* NOTREACHED */
                    183:                        }
                    184:                }
                    185:                buf[indx++] = ch;
                    186:        }
                    187:
                    188:        if (indx != 0)
1.6       deraadt   189:                fwrite(buf, 1, indx, stdout);
1.1       deraadt   190: }
                    191:
                    192: /*
                    193:  * calculate the column position
                    194:  */
                    195: static int
1.6       deraadt   196: new_column_position(col, ch)
1.1       deraadt   197:        int col;
                    198:        int ch;
                    199: {
                    200:        if (!count_bytes) {
                    201:                switch (ch) {
                    202:                case '\b':
                    203:                        if (col > 0)
                    204:                                --col;
                    205:                        break;
                    206:                case '\r':
                    207:                        col = 0;
                    208:                        break;
                    209:                case '\t':
                    210:                        col = (col + 8) & ~7;
                    211:                        break;
                    212:                default:
                    213:                        ++col;
                    214:                        break;
                    215:                }
                    216:        } else {
                    217:                ++col;
                    218:        }
                    219:
                    220:        return col;
                    221: }