Merge commit 'upstream/1.8.3'
[debian/sudo] / plugins / sudoers / regress / logging / check_wrap.c
1 /*
2  * Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com>
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 */
38
39 #include "missing.h"
40 #include "error.h"
41
42 extern void writeln_wrap(FILE *fp, char *line, size_t len, size_t maxlen);
43
44 static void
45 usage(void)
46 {
47     fprintf(stderr, "usage: check_wrap inputfile\n");
48     exit(1);
49 }
50
51 int
52 main(int argc, char *argv[])
53 {
54     size_t len;
55     FILE *fp;
56     char *cp, *dash, *line, lines[2][2048];
57     int which = 0;
58
59 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
60     setprogname(argc > 0 ? argv[0] : "check_wrap");
61 #endif
62
63     if (argc != 2)
64         usage();
65
66     fp = fopen(argv[1], "r");
67     if (fp == NULL)
68         errorx(1, "unable to open %s", argv[1]);
69
70     /*
71      * Each test record consists of a log entry on one line and a list of
72      * line lengths to test it with on the next.  E.g.
73      *
74      * 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
75      * 60-80,40
76      */
77     while ((line = fgets(lines[which], sizeof(lines[which]), fp)) != NULL) {
78         len = strcspn(line, "\n");
79         line[len] = '\0';
80
81         /* If we read the 2nd line, parse list of line lengths and check. */
82         if (which) {
83             for (cp = strtok(lines[1], ","); cp != NULL; cp = strtok(NULL, ",")) {
84                 size_t maxlen;
85                 /* May be either a number or a range. */
86                 len = maxlen = atoi(cp);
87                 dash = strchr(cp, '-');
88                 if (dash)
89                     maxlen = atoi(dash + 1);
90                 while (len <= maxlen) {
91                     printf("# word wrap at %d characters\n", (int)len);
92                     writeln_wrap(stdout, lines[0], strlen(lines[0]), len);
93                     len++;
94                 }
95             }
96         }
97         which = !which;
98     }
99
100     exit(0);
101 }
102
103 void
104 cleanup(int gotsig)
105 {
106     return;
107 }