The following commit has been merged in the master branch:
commit b95907e6e0f3f25136fb2ebcc8d3489efb208dea
Author: Ian Jackson <[EMAIL PROTECTED]>
Date:   Tue Mar 11 05:10:53 2008 +0200

    Refactor fgets checking code into fgets_must and fgets_checked functions

diff --git a/ChangeLog b/ChangeLog
index 8a2263f..c5656bf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2008-03-11  Ian Jackson  <[EMAIL PROTECTED]>
+
+       * 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  <[EMAIL PROTECTED]>
 
        * dselect/pkgdisplay.cc (relatestrings): Change 'breaks with' to
diff --git a/lib/dpkg.h b/lib/dpkg.h
index d0e15fe..edb4009 100644
--- a/lib/dpkg.h
+++ b/lib/dpkg.h
@@ -371,6 +371,9 @@ void showcopyright(const struct cmdinfo*, const char*);
 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 {
diff --git a/lib/utils.c b/lib/utils.c
index f2a6a55..632df1e 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -20,6 +20,7 @@
 
 #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
@@ -32,3 +33,35 @@ int cisdigit(int 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;
+}
+
diff --git a/src/filesdb.c b/src/filesdb.c
index 425e766..bc5973b 100644
--- a/src/filesdb.c
+++ b/src/filesdb.c
@@ -470,37 +470,18 @@ void ensure_diversions(void) {
   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;
 

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to