+2008-03-11 Ian Jackson <ian@davenant.greenend.org.uk>
+
+ * lib/dpkg.h (fgets_checked, fgets_must): New function declarations.
+ * lib/utils.c: Include <string.h>.
+ (fgets_checked, fgets_must): New function.
+ * src/filesdb.c (ensure_diversions): Use new fgets_checked and
+ fgets_must instead of duped code.
+
2008-03-09 Ian Jackson <ian@davenant.greenend.org.uk>
* dselect/pkgdisplay.cc (relatestrings): Change 'breaks with' to
int cisdigit(int c);
int cisalpha(int c);
+int fgets_checked(char *buf, size_t bufsz, FILE *f, const char *fn);
+int fgets_must(char *buf, size_t bufsz, FILE *f, const char *fn);
+
/*** from compression.c ***/
enum compress_type {
#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
int cisalpha(int c) {
return ((c>='a') && (c<='z')) || ((c>='A') && (c<='Z'));
}
+
+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;
+}
+
diversions = NULL;
if (!file) { onerr_abort--; return; }
- while (fgets(linebuf,sizeof(linebuf),file)) {
+ while ((l = fgets_checked(linebuf, sizeof(linebuf), file, vb.buf)) >= 0) {
oicontest= nfmalloc(sizeof(struct diversion));
oialtname= nfmalloc(sizeof(struct diversion));
- l= strlen(linebuf);
- if (l == 0) ohshit(_("fgets gave an empty string from diversions [i]"));
- if (linebuf[--l] != '\n') ohshit(_("diversions file has too-long line or EOF [i]"));
- linebuf[l]= 0;
oialtname->camefrom= findnamenode(linebuf, 0);
oialtname->useinstead = NULL;
- if (!fgets(linebuf,sizeof(linebuf),file)) {
- if (ferror(file)) ohshite(_("read error in diversions [ii]"));
- else ohshit(_("unexpected EOF in diversions [ii]"));
- }
- l= strlen(linebuf);
- if (l == 0) ohshit(_("fgets gave an empty string from diversions [ii]"));
- if (linebuf[--l] != '\n') ohshit(_("diversions file has too-long line or EOF [ii]"));
- linebuf[l]= 0;
+ fgets_must(linebuf, sizeof(linebuf), file, vb.buf);
oicontest->useinstead= findnamenode(linebuf, 0);
oicontest->camefrom = NULL;
-
- if (!fgets(linebuf,sizeof(linebuf),file)) {
- if (ferror(file)) ohshite(_("read error in diversions [iii]"));
- else ohshit(_("unexpected EOF in diversions [iii]"));
- }
- l= strlen(linebuf);
- if (l == 0) ohshit(_("fgets gave an empty string from diversions [iii]"));
- if (linebuf[--l] != '\n') ohshit(_("diversions file has too-long line or EOF [ii]"));
- linebuf[l]= 0;
+ fgets_must(linebuf, sizeof(linebuf), file, vb.buf);
oicontest->pkg= oialtname->pkg=
strcmp(linebuf, ":") ? findpackage(linebuf) : NULL;