/src/sudo/lib/iolog/iolog_gets.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2009-2021 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <stdio.h> |
27 | | #include <stdlib.h> |
28 | | #ifdef HAVE_STDBOOL_H |
29 | | # include <stdbool.h> |
30 | | #else |
31 | | # include "compat/stdbool.h" |
32 | | #endif |
33 | | #include <string.h> |
34 | | #include <errno.h> |
35 | | #include <limits.h> |
36 | | #include <time.h> |
37 | | |
38 | | #include "sudo_compat.h" |
39 | | #include "sudo_debug.h" |
40 | | #include "sudo_iolog.h" |
41 | | |
42 | | /* |
43 | | * Like fgets() but for struct iolog_file. |
44 | | */ |
45 | | char * |
46 | | iolog_gets(struct iolog_file *iol, char *buf, int bufsize, |
47 | | const char **errstr) |
48 | 4.42k | { |
49 | 4.42k | char *str; |
50 | 4.42k | debug_decl(iolog_gets, SUDO_DEBUG_UTIL); |
51 | | |
52 | 4.42k | if (bufsize < 0) { |
53 | 0 | errno = EINVAL; |
54 | 0 | if (errstr != NULL) |
55 | 0 | *errstr = strerror(errno); |
56 | 0 | debug_return_str(NULL); |
57 | 0 | } |
58 | | |
59 | | #ifdef HAVE_ZLIB_H |
60 | | if (iol->compressed) { |
61 | | if ((str = gzgets(iol->fd.g, buf, bufsize)) == NULL) { |
62 | | if (errstr != NULL) { |
63 | | int errnum; |
64 | | *errstr = gzerror(iol->fd.g, &errnum); |
65 | | if (errnum == Z_ERRNO) |
66 | | *errstr = strerror(errno); |
67 | | } |
68 | | } |
69 | | } else |
70 | | #endif |
71 | 4.42k | { |
72 | 4.42k | if ((str = fgets(buf, bufsize, iol->fd.f)) == NULL) { |
73 | 87 | if (errstr != NULL) |
74 | 87 | *errstr = strerror(errno); |
75 | 87 | } |
76 | 4.42k | } |
77 | 4.42k | debug_return_str(str); |
78 | 4.42k | } |