Annotation of 43BSDReno/usr.bin/fold/fold.c, revision 1.1

1.1     ! root        1: /*-
        !             2:  * Copyright (c) 1990 The Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * This code is derived from software contributed to Berkeley by
        !             6:  * Kevin Ruddy.
        !             7:  *
        !             8:  * Redistribution and use in source and binary forms are permitted
        !             9:  * provided that: (1) source distributions retain this entire copyright
        !            10:  * notice and comment, and (2) distributions including binaries display
        !            11:  * the following acknowledgement:  ``This product includes software
        !            12:  * developed by the University of California, Berkeley and its contributors''
        !            13:  * in the documentation or other materials provided with the distribution
        !            14:  * and in all advertising materials mentioning features or use of this
        !            15:  * software. Neither the name of the University nor the names of its
        !            16:  * contributors may be used to endorse or promote products derived
        !            17:  * from this software without specific prior written permission.
        !            18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            19:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            20:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            21:  */
        !            22: 
        !            23: #ifndef lint
        !            24: char copyright[] =
        !            25: "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
        !            26:  All rights reserved.\n";
        !            27: #endif /* not lint */
        !            28: 
        !            29: #ifndef lint
        !            30: static char sccsid[] = "@(#)fold.c     5.5 (Berkeley) 6/1/90";
        !            31: #endif /* not lint */
        !            32: 
        !            33: #include <stdio.h>
        !            34: #include <string.h>
        !            35: 
        !            36: #define        DEFLINEWIDTH    80
        !            37: 
        !            38: main(argc, argv)
        !            39:        int argc;
        !            40:        char **argv;
        !            41: {
        !            42:        extern int errno, optind;
        !            43:        extern char *optarg;
        !            44:        register int ch;
        !            45:        int width;
        !            46:        char *p;
        !            47: 
        !            48:        width = -1;
        !            49:        while ((ch = getopt(argc, argv, "0123456789w:")) != EOF)
        !            50:                switch (ch) {
        !            51:                case 'w':
        !            52:                        if ((width = atoi(optarg)) <= 0) {
        !            53:                                (void)fprintf(stderr,
        !            54:                                    "fold: illegal width value.\n");
        !            55:                                exit(1);
        !            56:                        }
        !            57:                        break;
        !            58:                case '0': case '1': case '2': case '3': case '4':
        !            59:                case '5': case '6': case '7': case '8': case '9':
        !            60:                        if (width == -1) {
        !            61:                                p = argv[optind - 1];
        !            62:                                if (p[0] == '-' && p[1] == ch && !p[2])
        !            63:                                        width = atoi(++p);
        !            64:                                else
        !            65:                                        width = atoi(argv[optind] + 1);
        !            66:                        }
        !            67:                        break;
        !            68:                default:
        !            69:                        (void)fprintf(stderr,
        !            70:                            "usage: fold [-w width] [file ...]\n");
        !            71:                        exit(1);
        !            72:                }
        !            73:        argv += optind;
        !            74:        argc -= optind;
        !            75: 
        !            76:        if (width == -1)
        !            77:                width = DEFLINEWIDTH;
        !            78:        if (!*argv)
        !            79:                fold(width);
        !            80:        else for (; *argv; ++argv)
        !            81:                if (!freopen(*argv, "r", stdin)) {
        !            82:                        (void)fprintf(stderr,
        !            83:                            "fold: %s: %s\n", *argv, strerror(errno));
        !            84:                        exit(1);
        !            85:                } else
        !            86:                        fold(width);
        !            87:        exit(0);
        !            88: }
        !            89: 
        !            90: fold(width)
        !            91:        register int width;
        !            92: {
        !            93:        register int ch, col, new;
        !            94: 
        !            95:        for (col = 0;;) {
        !            96:                switch (ch = getchar()) {
        !            97:                case EOF:
        !            98:                        return;
        !            99:                case '\b':
        !           100:                        new = col ? col - 1 : 0;
        !           101:                        break;
        !           102:                case '\n':
        !           103:                case '\r':
        !           104:                        new = 0;
        !           105:                        break;
        !           106:                case '\t':
        !           107:                        new = (col + 8) & ~7;
        !           108:                        break;
        !           109:                default:
        !           110:                        new = col + 1;
        !           111:                        break;
        !           112:                }
        !           113: 
        !           114:                if (new > width) {
        !           115:                        putchar('\n');
        !           116:                        col = 0;
        !           117:                }
        !           118:                putchar(ch);
        !           119: 
        !           120:                switch (ch) {
        !           121:                case '\b':
        !           122:                        if (col > 0)
        !           123:                                --col;
        !           124:                        break;
        !           125:                case '\n':
        !           126:                case '\r':
        !           127:                        col = 0;
        !           128:                        break;
        !           129:                case '\t':
        !           130:                        col += 8;
        !           131:                        col &= ~7;
        !           132:                        break;
        !           133:                default:
        !           134:                        ++col;
        !           135:                        break;
        !           136:                }
        !           137:        }
        !           138: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.