Path: blob/main/lib/eventlog/regress/logwrap/check_wrap.c
1532 views
/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2011-2013 Todd C. Miller <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718#include <config.h>1920#include <stdio.h>21#include <stdlib.h>22#include <string.h>23#include <limits.h>24#include <unistd.h>2526#define SUDO_ERROR_WRAP 02728#include <sudo_compat.h>29#include <sudo_eventlog.h>30#include <sudo_fatal.h>31#include <sudo_plugin.h>32#include <sudo_util.h>3334sudo_dso_public int main(int argc, char *argv[]);3536sudo_noreturn static void37usage(void)38{39fprintf(stderr, "usage: %s [-v] inputfile\n", getprogname());40exit(EXIT_FAILURE);41}4243int44main(int argc, char *argv[])45{46int ch, lineno = 0, which = 0;47char *line, lines[2][2048];48const char *infile;49unsigned int len;50FILE *fp;5152initprogname(argc > 0 ? argv[0] : "check_wrap");5354while ((ch = getopt(argc, argv, "v")) != -1) {55switch (ch) {56case 'v':57/* ignored */58break;59default:60usage();61/* NOTREACHED */62}63}64argc -= optind;65argv += optind;6667if (argc != 1)68usage();69infile = argv[0];7071fp = fopen(infile, "r");72if (fp == NULL)73sudo_fatalx("unable to open %s", infile);7475/*76* Each test record consists of a log entry on one line and a list of77* 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/visudo80* 60-80,4081*/82while ((line = fgets(lines[which], sizeof(lines[which]), fp)) != NULL) {83char *cp, *last;8485line[strcspn(line, "\n")] = '\0';8687/* If we read the 2nd line, parse list of line lengths and check. */88if (which) {89lineno++;90for (cp = strtok_r(lines[1], ",", &last); cp != NULL; cp = strtok_r(NULL, ",", &last)) {91unsigned int maxlen;92const char *errstr;93char *dash;9495/* May be either a number or a range. */96dash = strchr(cp, '-');97if (dash != NULL) {98*dash = '\0';99len = (unsigned int)sudo_strtonum(cp, 0, INT_MAX, &errstr);100if (errstr == NULL)101maxlen = (unsigned int)sudo_strtonum(dash + 1, 0, INT_MAX, &errstr);102} else {103len = maxlen = (unsigned int)sudo_strtonum(cp, 0, INT_MAX, &errstr);104}105if (errstr != NULL) {106sudo_fatalx("%s: invalid length on line %d", infile, lineno);107}108while (len <= maxlen) {109if (len == 0) {110puts("# word wrap disabled");111} else {112printf("# word wrap at %u characters\n", len);113}114eventlog_writeln(stdout, lines[0], strlen(lines[0]), len);115len++;116}117}118}119which = !which;120}121122return EXIT_SUCCESS;123}124125126