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# include <string.h> 31#endif /* HAVE_STRING_H */ 32#ifdef HAVE_STRINGS_H 33# include <strings.h> 34#endif /* HAVE_STRINGS_H */ 35 36#include "sudo.h" 37 38void 39writeln_wrap(fp, line, len, maxlen) 40 FILE *fp; 41 char *line; 42 size_t len; 43 size_t maxlen; 44{ 45 char *indent = ""; 46 char *beg = line; 47 char *end; 48 49 /* 50 * Print out line with word wrap around maxlen characters. 51 */ 52 beg = line; 53 while (len > maxlen) { 54 end = beg + maxlen; 55 while (end != beg && *end != ' ') 56 end--; 57 if (beg == end) { 58 /* Unable to find word break within maxlen, look beyond. */ 59 end = strchr(beg + maxlen, ' '); 60 if (end == NULL) 61 break; /* no word break */ 62 } 63 fprintf(fp, "%s%.*s\n", indent, (int)(end - beg), beg); 64 while (*end == ' ') 65 end++; 66 len -= (end - beg); 67 beg = end; 68 if (indent[0] == '\0') { 69 indent = LOG_INDENT; 70 maxlen -= sizeof(LOG_INDENT) - 1; 71 } 72 } 73 /* Print remainder, if any. */ 74 if (len) 75 fprintf(fp, "%s%s\n", indent, beg); 76} 77