- char ** lines;
-
- for (lines = *raw; *lines; lines++) {
- if(isGenericFieldType("Essential:", *lines)) {
- char *essential_value;
- essential_value = parseGenericFieldType("Essential", *lines);
- if (strcmp(essential_value, "yes") == 0) {
- pkg->essential = 1;
- }
- free(essential_value);
+ int ret, lineno;
+ char *buf, *nl;
+ size_t buflen;
+
+ lineno = 1;
+ ret = 0;
+
+ buflen = buf0len;
+ buf = *buf0;
+ buf[0] = '\0';
+
+ while (1) {
+ if (fgets(buf, buflen, fp) == NULL) {
+ if (ferror(fp)) {
+ fprintf(stderr, "%s: fgets: %s\n",
+ __FUNCTION__, strerror(errno));
+ ret = -1;
+ } else if (strlen(*buf0) == buf0len-1) {
+ fprintf(stderr, "%s: missing new line character"
+ " at end of file!\n",
+ __FUNCTION__);
+ pkg_parse_line(pkg, *buf0, mask);
+ }
+ break;
+ }
+
+ nl = strchr(buf, '\n');
+ if (nl == NULL) {
+ if (strlen(buf) < buflen-1) {
+ /*
+ * Line could be exactly buflen-1 long and
+ * missing a newline, but we won't know until
+ * fgets fails to read more data.
+ */
+ fprintf(stderr, "%s: missing new line character"
+ " at end of file!\n",
+ __FUNCTION__);
+ pkg_parse_line(pkg, *buf0, mask);
+ break;
+ }
+ if (buf0len >= EXCESSIVE_LINE_LEN) {
+ fprintf(stderr, "%s: excessively long line at "
+ "%d. Corrupt file?\n",
+ __FUNCTION__, lineno);
+ ret = -1;
+ break;
+ }
+
+ /*
+ * Realloc and move buf past the data already read.
+ * |<--------------- buf0len ----------------->|
+ * | |<------- buflen ---->|
+ * |---------------------|---------------------|
+ * buf0 buf
+ */
+ buflen = buf0len;
+ buf0len *= 2;
+ *buf0 = xrealloc(*buf0, buf0len);
+ buf = *buf0 + buflen -1;
+
+ continue;
+ }
+
+ *nl = '\0';
+
+ lineno++;
+
+ if (pkg_parse_line(pkg, *buf0, mask))
+ break;
+
+ buf = *buf0;
+ buflen = buf0len;
+ buf[0] = '\0';
+ };
+
+ if (pkg->name == NULL) {
+ /* probably just a blank line */
+ ret = EINVAL;