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

Annotation of src/usr.bin/asa/asa.c, Revision 1.10

1.10    ! lum         1: /*     $OpenBSD: asa.c,v 1.9 2009/10/27 23:59:35 deraadt Exp $ */
1.1       deraadt     2: /*     $NetBSD: asa.c,v 1.10 1995/04/21 03:01:41 cgd Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1993,94 Winning Strategies, Inc.
                      6:  * 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.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *      This product includes software developed by Winning Strategies, Inc.
                     19:  * 4. The name of the author may not be used to endorse or promote products
                     20:  *    derived from this software without specific prior written permission
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     23:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     24:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     25:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     26:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     27:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     28:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     29:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     30:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     31:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     32:  */
                     33:
1.8       moritz     34: #include <err.h>
1.1       deraadt    35: #include <stdio.h>
                     36: #include <stdlib.h>
1.7       moritz     37: #include <string.h>
1.6       robert     38: #include <unistd.h>
1.1       deraadt    39:
1.8       moritz     40: void asa(FILE *);
                     41: __dead void usage(void);
1.1       deraadt    42:
                     43: int
1.4       deraadt    44: main(int argc, char *argv[])
1.1       deraadt    45: {
1.10    ! lum        46:        int ch, exval = 0;
1.1       deraadt    47:        FILE *fp;
                     48:
1.6       robert     49:        while ((ch = getopt(argc, argv, "")) != -1) {
                     50:                switch(ch) {
                     51:                default:
                     52:                        usage();
                     53:                        /* NOTREACHED */
                     54:                }
                     55:        }
                     56:        argc -= optind;
                     57:        argv += optind;
1.1       deraadt    58:
1.8       moritz     59:        if (!argc)
                     60:                asa(stdin);
                     61:        else
                     62:                for (; *argv != NULL; argv++) {
                     63:                        if ((fp = fopen(*argv, "r")) == NULL) {
                     64:                                warn("%s", *argv);
1.10    ! lum        65:                                exval = 1;
1.1       deraadt    66:                                continue;
1.3       deraadt    67:                        }
1.8       moritz     68:                        asa(fp);
                     69:                        fclose(fp);
1.3       deraadt    70:                }
1.1       deraadt    71:
1.10    ! lum        72:        exit(exval);
1.1       deraadt    73: }
                     74:
1.8       moritz     75: void
1.4       deraadt    76: asa(FILE *f)
1.1       deraadt    77: {
1.7       moritz     78:        char *buf, *lbuf = NULL;
1.1       deraadt    79:        size_t len;
1.8       moritz     80:        int firstline = 1;
1.1       deraadt    81:
1.8       moritz     82:        while ((buf = fgetln(f, &len)) != NULL) {
1.7       moritz     83:                if (buf[len - 1] == '\n')
                     84:                        buf[len - 1] = '\0';
                     85:                else {
                     86:                        if ((lbuf = malloc(len + 1)) == NULL)
                     87:                                err(1, NULL);
                     88:                        memcpy(lbuf, buf, len);
                     89:                        lbuf[len] = '\0';
                     90:                        buf = lbuf;
                     91:                }
1.1       deraadt    92:                /* special case the first line  */
1.8       moritz     93:                if (firstline) {
                     94:                        firstline = 0;
                     95:                        switch (buf[0]) {
                     96:                        case '0':
                     97:                                putchar ('\n');
                     98:                                break;
                     99:                        case '1':
                    100:                                putchar ('\f');
                    101:                                break;
1.7       moritz    102:                        }
1.8       moritz    103:                } else {
1.1       deraadt   104:                        switch (buf[0]) {
                    105:                        default:
                    106:                        case ' ':
                    107:                                putchar ('\n');
                    108:                                break;
                    109:                        case '0':
                    110:                                putchar ('\n');
                    111:                                putchar ('\n');
                    112:                                break;
                    113:                        case '1':
                    114:                                putchar ('\f');
                    115:                                break;
                    116:                        case '+':
                    117:                                putchar ('\r');
                    118:                                break;
                    119:                        }
                    120:                }
1.8       moritz    121:                if (buf[0] && buf[1]) {
                    122:                        fputs (&buf[1], stdout);
                    123:                }
                    124:        }
                    125:        free(lbuf);
1.1       deraadt   126:
1.8       moritz    127:        if (!firstline)
1.1       deraadt   128:                putchar ('\n');
1.6       robert    129: }
                    130:
1.8       moritz    131: __dead void
1.6       robert    132: usage(void)
                    133: {
                    134:        extern char *__progname;
                    135:        fprintf(stderr, "usage: %s [file ...]\n", __progname);
                    136:        exit(1);
1.1       deraadt   137: }