Commit | Line | Data |
---|---|---|
1479465f GJ |
1 | /* |
2 | * libdpkg - Debian packaging suite library routines | |
3 | * utils.c - helper functions for dpkg | |
4 | * | |
5 | * Copyright © 1995, 2008 Ian Jackson <ijackson@chiark.greenend.org.uk> | |
6 | * | |
7 | * This is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <compat.h> | |
23 | ||
24 | #include <string.h> | |
25 | ||
26 | #include <dpkg/i18n.h> | |
27 | #include <dpkg/dpkg.h> | |
28 | ||
29 | int | |
30 | fgets_checked(char *buf, size_t bufsz, FILE *f, const char *fn) | |
31 | { | |
32 | int l; | |
33 | ||
34 | if (!fgets(buf, bufsz, f)) { | |
35 | if (ferror(f)) | |
36 | ohshite(_("read error in '%.250s'"), fn); | |
37 | return -1; | |
38 | } | |
39 | l = strlen(buf); | |
40 | if (l == 0) | |
41 | ohshit(_("fgets gave an empty string from '%.250s'"), fn); | |
42 | if (buf[--l] != '\n') | |
43 | ohshit(_("too-long line or missing newline in '%.250s'"), fn); | |
44 | buf[l] = '\0'; | |
45 | ||
46 | return l; | |
47 | } | |
48 | ||
49 | int | |
50 | fgets_must(char *buf, size_t bufsz, FILE *f, const char *fn) | |
51 | { | |
52 | int l = fgets_checked(buf, bufsz, f, fn); | |
53 | ||
54 | if (l < 0) | |
55 | ohshit(_("unexpected end of file reading '%.250s'"), fn); | |
56 | ||
57 | return l; | |
58 | } |