summaryrefslogtreecommitdiff
path: root/lib/utils.c
blob: 87f6b7c759d6ab30f827c16b1ecd929325f6a676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * libdpkg - Debian packaging suite library routines
 * utils.c - Helper functions for dpkg
 *
 * Copyright (C) 2001 Wichert Akkerman <wakkerma@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * This is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with dpkg; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <config.h>
#include <dpkg.h>
#include <string.h>

/* Reimplementation of the standard ctype.h is* functions. Since gettext
 * has overloaded the meaning of LC_CTYPE we can't use that to force C
 * locale, so use these cis* functions instead.
 */
int cisdigit(int c) {
	return (c>='0') && (c<='9');
}

int cisalpha(int c) {
	return ((c>='a') && (c<='z')) || ((c>='A') && (c<='Z'));
}

int
cisspace(int c)
{
	return (c == '\n' || c == '\t' || c == ' ');
}

int
fgets_checked(char *buf, size_t bufsz, FILE *f, const char *fn)
{
	int l;

	if (!fgets(buf, bufsz, f)) {
		if (ferror(f))
			ohshite(_("read error in `%.250s'"), fn);
		return -1;
	}
	l = strlen(buf);
	if (l == 0)
		ohshit(_("fgets gave an empty string from `%.250s'"), fn);
	if (buf[--l] != '\n')
		ohshit(_("too-long line or missing newline in `%.250s'"), fn);
	buf[l] = 0;

	return l;
}

int
fgets_must(char *buf, size_t bufsz, FILE *f, const char *fn)
{
	int l = fgets_checked(buf, bufsz, f, fn);

	if (l < 0)
		ohshit(_("unexpected eof reading `%.250s'"), fn);

	return l;
}