- while(fgets(var, sizeof(var), f)) {
- p1 = strchr(var, ' ');
- p2 = p3 = NULL;
- if (!p1)
- goto procstr;
-
- *p1++ = '\0';
- p2 = strchr(p1, ' ');
- if (!p2) {
- tmp = strchr(p1, '\n');
- if (tmp)
- *tmp = '\0';
- goto procstr;
- }
-
- *p2++ = '\0';
- p3 = strchr(p2, ' ');
- if (!p3) {
- tmp = strchr(p2, '\n');
- if (tmp)
- *tmp = '\0';
- goto procstr;
- }
-
- *p3++ = '\0';
- tmp = strchr(p3, '\n');
- if (tmp)
- *tmp = '\0';
-
-procstr:
- if (!p1)
- continue;
-
- if (!strcmp(var, "Package:")) {
- strncpy(pkg, p1, sizeof(pkg) - 1);
- continue;
- }
-
- /* If there is ABIVersion, remove that suffix */
- if (!strcmp(var, "ABIVersion:")) {
- if (strlen(pkg) <= strlen(p1))
- continue;
- tmp = &pkg[strlen(pkg) - strlen(p1)];
- if (strncmp(p1, tmp, strlen(p1)))
- continue;
-
- *tmp = '\0';
- continue;
- }
-
- if (!strcmp(var, "Version:")) {
- strncpy(ver, p1, sizeof(ver) - 1);
- continue;
- }
-
- if (p2 && p3 &&
- !strcmp(var, "Status:") &&
- !strcmp(p1, "install") &&
- (all || strstr(p2, "user")) &&
- !strcmp(p3, "installed") && pkg[0] && ver[0]) {
- blobmsg_add_string(&buf, pkg, ver);
- pkg[0] = ver[0] = '\0';
+ while (fgets(line, sizeof(line), f)) {
+ switch (line[0]) {
+ case 'A':
+ if (is_field("ABIVersion", line)) {
+ /* if there is ABIVersion, remove that suffix */
+ if (sscanf(line, "ABIVersion: %127s", tmp) == 1
+ && strlen(tmp) < strlen(pkg)) {
+ pkg_abi = pkg + (strlen(pkg) - strlen(tmp));
+ if (strncmp(pkg_abi, tmp, strlen(tmp)) == 0)
+ pkg_abi[0] = '\0';
+ }
+ } else if (is_field("Auto-Installed", line))
+ if (sscanf(line, "Auto-Installed: %63s", tmp) == 1)
+ auto_installed = (strcmp(tmp, "yes") == 0);
+ break;
+ case 'P':
+ if (is_field("Package", line))
+ if (sscanf(line, "Package: %127s", pkg) != 1)
+ pkg[0] = '\0';
+ break;
+ case 'V':
+ if (is_field("Version", line))
+ if (sscanf(line, "Version: %127s", ver) != 1)
+ ver[0] = '\0';
+ break;
+ case 'S':
+ if (is_field("Status", line))
+ if (sscanf(line, "Status: install %63s installed", tmp) == 1)
+ installed = true;
+ break;
+ default:
+ if (is_blank(line)) {
+ if (installed && (all || !auto_installed) && pkg[0] && ver[0])
+ blobmsg_add_string(&buf, pkg, ver);
+ pkg[0] = ver[0] = '\0';
+ installed = auto_installed = false;
+ }
+ break;