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