make m_getfld2() called with an error state a noop
authorPhilipp Takacs <philipp@bureaucracy.de>
Sat, 21 Nov 2015 11:16:01 +0000 (12:16 +0100)
committerPhilipp Takacs <philipp@bureaucracy.de>
Sat, 21 Nov 2015 11:53:47 +0000 (12:53 +0100)
This probduce some code dublication, but the will be
reduced, if we make m_getfld2() more transparent on errors.

sbr/m_getfld2.c

index de38435..970a3f0 100644 (file)
@@ -48,29 +48,29 @@ m_getfld2(enum state s, struct field *f, FILE *msg)
        ssize_t nchars;
        enum threestate falted;
 
-       nchars = getline(&tmpline, &len, msg);
-       if (nchars < 1) {
-               if (feof(msg)) {
-                       return FILEEOF2;
-               } else {
-                       return IOERR2;
+       switch (s) {
+       case FLD2:
+               nchars = getline(&tmpline, &len, msg);
+               if (nchars < 1) {
+                       if (feof(msg)) {
+                               return FILEEOF2;
+                       } else {
+                               return IOERR2;
+                       }
                }
-       }
 
-       *f->name = '\0';
-       f->namelen = 0;
+               *f->name = '\0';
+               f->namelen = 0;
 
-       if (nchars >= NAMESZ) {
-               if (f->value) {
-                       free(f->value);
+               if (nchars >= NAMESZ) {
+                       if (f->value) {
+                               free(f->value);
+                       }
+                       f->value = tmpline;
+                       f->valuelen = nchars;
+                       return LENERR2;
                }
-               f->value = tmpline;
-               f->valuelen = nchars;
-               return LENERR2;
-       }
 
-       switch (s) {
-       case FLD2:
                if (*(tmpline + nchars - 1) != '\n') {
                        if (f->value) {
                                free(f->value);
@@ -151,6 +151,26 @@ m_getfld2(enum state s, struct field *f, FILE *msg)
                return FLD2;
 
        case BODY2:
+               nchars = getline(&tmpline, &len, msg);
+               if (nchars < 1) {
+                       if (feof(msg)) {
+                               return FILEEOF2;
+                       } else {
+                               return IOERR2;
+                       }
+               }
+
+               *f->name = '\0';
+               f->namelen = 0;
+
+               if (nchars >= NAMESZ) {
+                       if (f->value) {
+                               free(f->value);
+                       }
+                       f->value = tmpline;
+                       f->valuelen = nchars;
+                       return LENERR2;
+               }
                if (f->value) {
                        free(f->value);
                }