/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2009-2021 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#ifdef HAVE_STDBOOL_H23# include <stdbool.h>24#else25# include <compat/stdbool.h>26#endif27#include <string.h>28#include <errno.h>29#include <limits.h>30#include <time.h>3132#include <sudo_compat.h>33#include <sudo_debug.h>34#include <sudo_iolog.h>3536/*37* Like fgets() but for struct iolog_file.38*/39char *40iolog_gets(struct iolog_file *iol, char *buf, int bufsize,41const char **errstr)42{43char *str;44debug_decl(iolog_gets, SUDO_DEBUG_UTIL);4546if (bufsize < 0) {47errno = EINVAL;48if (errstr != NULL)49*errstr = strerror(errno);50debug_return_str(NULL);51}5253#ifdef HAVE_ZLIB_H54if (iol->compressed) {55if ((str = gzgets(iol->fd.g, buf, bufsize)) == NULL) {56if (errstr != NULL) {57int errnum;58*errstr = gzerror(iol->fd.g, &errnum);59if (errnum == Z_ERRNO)60*errstr = strerror(errno);61}62}63} else64#endif65{66if ((str = fgets(buf, bufsize, iol->fd.f)) == NULL) {67if (errstr != NULL)68*errstr = strerror(errno);69}70}71debug_return_str(str);72}737475