Annotation of embedaddon/sudo/plugins/sudoers/regress/logging/check_wrap.c, revision 1.1.1.5

1.1       misho       1: /*
1.1.1.3   misho       2:  * Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       misho       3:  *
                      4:  * Permission to use, copy, modify, and distribute this software for any
                      5:  * purpose with or without fee is hereby granted, provided that the above
                      6:  * copyright notice and this permission notice appear in all copies.
                      7:  *
                      8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                      9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     15:  */
                     16: 
                     17: #include <config.h>
                     18: 
                     19: #include <sys/types.h>
                     20: #include <stdio.h>
                     21: #ifdef STDC_HEADERS
                     22: # include <stdlib.h>
                     23: # include <stddef.h>
                     24: #else
                     25: # ifdef HAVE_STDLIB_H
                     26: #  include <stdlib.h>
                     27: # endif
                     28: #endif /* STDC_HEADERS */
                     29: #ifdef HAVE_STRING_H
                     30: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
                     31: #  include <memory.h>
                     32: # endif
                     33: # include <string.h>
                     34: #endif /* HAVE_STRING_H */
                     35: #ifdef HAVE_STRINGS_H
                     36: # include <strings.h>
                     37: #endif /* HAVE_STRINGS_H */
1.1.1.5 ! misho      38: #include <limits.h>
1.1       misho      39: 
1.1.1.2   misho      40: #define SUDO_ERROR_WRAP 0
                     41: 
1.1       misho      42: #include "missing.h"
1.1.1.4   misho      43: #include "fatal.h"
1.1.1.2   misho      44: #include "sudo_plugin.h"
1.1.1.5 ! misho      45: #include "sudo_util.h"
1.1.1.2   misho      46: 
1.1       misho      47: extern void writeln_wrap(FILE *fp, char *line, size_t len, size_t maxlen);
                     48: 
1.1.1.3   misho      49: __dso_public int main(int argc, char *argv[]);
                     50: 
1.1       misho      51: static void
                     52: usage(void)
                     53: {
1.1.1.5 ! misho      54:     fprintf(stderr, "usage: %s inputfile\n", getprogname());
1.1       misho      55:     exit(1);
                     56: }
                     57: 
                     58: int
                     59: main(int argc, char *argv[])
                     60: {
                     61:     size_t len;
                     62:     FILE *fp;
                     63:     char *cp, *dash, *line, lines[2][2048];
1.1.1.5 ! misho      64:     int lineno = 0;
1.1       misho      65:     int which = 0;
                     66: 
1.1.1.5 ! misho      67:     initprogname(argc > 0 ? argv[0] : "check_wrap");
1.1       misho      68: 
                     69:     if (argc != 2)
                     70:        usage();
                     71: 
                     72:     fp = fopen(argv[1], "r");
                     73:     if (fp == NULL)
1.1.1.3   misho      74:        fatalx("unable to open %s", argv[1]);
1.1       misho      75: 
                     76:     /*
                     77:      * Each test record consists of a log entry on one line and a list of
                     78:      * line lengths to test it with on the next.  E.g.
                     79:      *
                     80:      * Jun 30 14:49:51 : millert : TTY=ttypn ; PWD=/usr/src/local/millert/hg/sudo/trunk/plugins/sudoers ; USER=root ; TSID=0004LD ; COMMAND=/usr/local/sbin/visudo
                     81:      * 60-80,40
                     82:      */
                     83:     while ((line = fgets(lines[which], sizeof(lines[which]), fp)) != NULL) {
                     84:        len = strcspn(line, "\n");
                     85:        line[len] = '\0';
                     86: 
                     87:        /* If we read the 2nd line, parse list of line lengths and check. */
                     88:        if (which) {
1.1.1.5 ! misho      89:            lineno++;
1.1       misho      90:            for (cp = strtok(lines[1], ","); cp != NULL; cp = strtok(NULL, ",")) {
                     91:                size_t maxlen;
                     92:                /* May be either a number or a range. */
                     93:                dash = strchr(cp, '-');
1.1.1.5 ! misho      94:                if (dash != NULL) {
        !            95:                    *dash = '\0';
        !            96:                    len = strtonum(cp, 1, INT_MAX, NULL);
        !            97:                    maxlen = strtonum(dash + 1, 1, INT_MAX, NULL);
        !            98:                } else {
        !            99:                    len = maxlen = strtonum(cp, 1, INT_MAX, NULL);
        !           100:                }
        !           101:                if (len == 0 || maxlen == 0)
        !           102:                    fatalx("%s: invalid length on line %d\n", argv[1], lineno);
1.1       misho     103:                while (len <= maxlen) {
                    104:                    printf("# word wrap at %d characters\n", (int)len);
                    105:                    writeln_wrap(stdout, lines[0], strlen(lines[0]), len);
                    106:                    len++;
                    107:                }
                    108:            }
                    109:        }
                    110:        which = !which;
                    111:     }
                    112: 
                    113:     exit(0);
                    114: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>