[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.12

1.12    ! deraadt     1: /*     $OpenBSD: fold.c,v 1.11 2007/04/07 23:20:18 tedu 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: #include <stdio.h>
                     37: #include <stdlib.h>
                     38: #include <string.h>
                     39: #include <unistd.h>
                     40: #include <err.h>
1.11      tedu       41: #include <limits.h>
1.1       deraadt    42:
                     43: #define        DEFLINEWIDTH    80
                     44:
1.9       deraadt    45: static void fold(int);
                     46: static int new_column_position(int, int);
1.1       deraadt    47: int count_bytes = 0;
                     48: int split_words = 0;
                     49:
                     50: int
1.8       deraadt    51: main(int argc, char *argv[])
1.1       deraadt    52: {
1.5       mpech      53:        int ch;
1.1       deraadt    54:        int width;
                     55:        char *p;
1.11      tedu       56:        char *w;
                     57:        const char *errstr;
1.1       deraadt    58:
                     59:        width = -1;
                     60:        while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1)
                     61:                switch (ch) {
                     62:                case 'b':
                     63:                        count_bytes = 1;
                     64:                        break;
                     65:                case 's':
                     66:                        split_words = 1;
                     67:                        break;
                     68:                case 'w':
1.11      tedu       69:                        width = strtonum(optarg, 1, INT_MAX, &errstr);
                     70:                        if (errstr != NULL)
                     71:                                errx(1, "illegal width value, %s: %s", errstr,
                     72:                                        optarg);
1.1       deraadt    73:                        break;
                     74:                case '0': case '1': case '2': case '3': case '4':
                     75:                case '5': case '6': case '7': case '8': case '9':
                     76:                        if (width == -1) {
                     77:                                p = argv[optind - 1];
                     78:                                if (p[0] == '-' && p[1] == ch && !p[2])
1.11      tedu       79:                                        w = ++p;
1.1       deraadt    80:                                else
1.11      tedu       81:                                        w = argv[optind] + 1;
                     82:
                     83:                                width = strtonum(w, 1, INT_MAX, &errstr);
                     84:                                if (errstr != NULL)
                     85:                                        errx(1, "illegal width value, %s: %s",
                     86:                                                errstr, optarg);
1.1       deraadt    87:                        }
                     88:                        break;
                     89:                default:
                     90:                        (void)fprintf(stderr,
                     91:                            "usage: fold [-bs] [-w width] [file ...]\n");
                     92:                        exit(1);
                     93:                }
                     94:        argv += optind;
                     95:        argc -= optind;
                     96:
                     97:        if (width == -1)
                     98:                width = DEFLINEWIDTH;
                     99:
                    100:        if (!*argv)
                    101:                fold(width);
                    102:        else for (; *argv; ++argv)
                    103:                if (!freopen(*argv, "r", stdin)) {
1.6       deraadt   104:                        err(1, "%s", *argv);
1.1       deraadt   105:                        /* NOTREACHED */
                    106:                } else
                    107:                        fold(width);
                    108:        exit(0);
                    109: }
                    110:
                    111: /*
                    112:  * Fold the contents of standard input to fit within WIDTH columns
                    113:  * (or bytes) and write to standard output.
                    114:  *
                    115:  * If split_words is set, split the line at the last space character
                    116:  * on the line.  This flag necessitates storing the line in a buffer
                    117:  * until the current column > width, or a newline or EOF is read.
                    118:  *
                    119:  * The buffer can grow larger than WIDTH due to backspaces and carriage
                    120:  * returns embedded in the input stream.
                    121:  */
                    122: static void
1.8       deraadt   123: fold(int width)
1.1       deraadt   124: {
                    125:        static char *buf = NULL;
                    126:        static int   buf_max = 0;
1.5       mpech     127:        int ch, col;
                    128:        int indx;
1.1       deraadt   129:
                    130:        col = indx = 0;
                    131:        while ((ch = getchar()) != EOF) {
                    132:                if (ch == '\n') {
                    133:                        if (indx != 0)
1.6       deraadt   134:                                fwrite(buf, 1, indx, stdout);
1.1       deraadt   135:                        putchar('\n');
                    136:                        col = indx = 0;
                    137:                        continue;
                    138:                }
                    139:
1.6       deraadt   140:                col = new_column_position(col, ch);
1.1       deraadt   141:                if (col > width) {
                    142:                        int i, last_space;
                    143:
                    144:                        if (split_words) {
                    145:                                for (i = 0, last_space = -1; i < indx; i++)
1.6       deraadt   146:                                        if(buf[i] == ' ')
                    147:                                                last_space = i;
1.1       deraadt   148:                        }
                    149:
                    150:                        if (split_words && last_space != -1) {
                    151:                                last_space++;
                    152:
1.6       deraadt   153:                                fwrite(buf, 1, last_space, stdout);
                    154:                                memmove(buf, buf+last_space, indx-last_space);
1.1       deraadt   155:
                    156:                                indx -= last_space;
                    157:                                col = 0;
                    158:                                for (i = 0; i < indx; i++) {
1.6       deraadt   159:                                        col = new_column_position(col, buf[i]);
1.1       deraadt   160:                                }
                    161:                        } else {
1.6       deraadt   162:                                fwrite(buf, 1, indx, stdout);
1.1       deraadt   163:                                col = indx = 0;
                    164:                        }
                    165:                        putchar('\n');
                    166:
                    167:                        /* calculate the column position for the next line. */
1.6       deraadt   168:                        col = new_column_position(col, ch);
1.1       deraadt   169:                }
                    170:
                    171:                if (indx + 1 > buf_max) {
1.10      tedu      172:                        int newmax = buf_max + 2048;
                    173:                        char *newbuf;
                    174:
1.1       deraadt   175:                        /* Allocate buffer in LINE_MAX increments */
1.10      tedu      176:                        if ((newbuf = realloc(buf, newmax)) == NULL) {
1.6       deraadt   177:                                err(1, NULL);
1.1       deraadt   178:                                /* NOTREACHED */
                    179:                        }
1.10      tedu      180:                        buf = newbuf;
                    181:                        buf_max = newmax;
1.1       deraadt   182:                }
                    183:                buf[indx++] = ch;
                    184:        }
                    185:
                    186:        if (indx != 0)
1.6       deraadt   187:                fwrite(buf, 1, indx, stdout);
1.1       deraadt   188: }
                    189:
                    190: /*
                    191:  * calculate the column position
                    192:  */
                    193: static int
1.8       deraadt   194: new_column_position(int col, int ch)
1.1       deraadt   195: {
                    196:        if (!count_bytes) {
                    197:                switch (ch) {
                    198:                case '\b':
                    199:                        if (col > 0)
                    200:                                --col;
                    201:                        break;
                    202:                case '\r':
                    203:                        col = 0;
                    204:                        break;
                    205:                case '\t':
                    206:                        col = (col + 8) & ~7;
                    207:                        break;
                    208:                default:
                    209:                        ++col;
                    210:                        break;
                    211:                }
                    212:        } else {
                    213:                ++col;
                    214:        }
                    215:
                    216:        return col;
                    217: }