/* * andyp@osdl.org * Tue Nov 19 09:26:22 PST 2002 * * Compose a kernel command line on stdout from the contents * of /proc/iomem and /proc/cmndline. */ #include #include #include struct memregion { unsigned long first; unsigned long last; struct memregion *next; }; int memopt(char *iomem, char *out, int outlen) { FILE *f; struct memregion *list, *tmp; char *pattern; regex_t preg; int cc, kb; char line[256]; if ((f = fopen(iomem, "r")) == NULL) return -1; pattern = "^[0-9a-fA-F].*-[0-9a-fA-F].* : System RAM"; if (regcomp(&preg, pattern, 0)) { (void) fclose(f); return -1; } list = (struct memregion *) 0; while (fgets(line, sizeof(line), f) != NULL) { if (regexec(&preg, line, 0, 0, 0) == REG_NOMATCH) continue; tmp = (struct memregion *) malloc(sizeof(struct memregion)); if (tmp == (struct memregion *) 0) goto out; cc = sscanf(line, "%x-%x", &tmp->first, &tmp->last); if (cc != 2) { free(tmp); goto out; } tmp->next = list; list = tmp; } out[0] = 0; tmp = list; while (tmp) { strcat(out, "mem="); kb = (tmp->last - tmp->first + 1) >> 10; sprintf(line, "%dK@0x%08x", kb, tmp->first); strcat(out, line); if (tmp->next) strcat(out, " "); tmp = tmp->next; } out: while (list) { tmp = list->next; free(list); list = tmp; } regfree(&preg); (void) fclose(f); return 0; } static int lastcmd(char *cmndline, char *out, int outlen) { FILE *f; char line[256]; if ((f = fopen(cmndline, "r")) == NULL) return -1; memset(out, 0, outlen); if (fgets(line, sizeof(line), f) != NULL) strncpy(out, line, strlen(line) - 1); fclose(f); return 0; } int main(int argc, char **argv) { int cc; char *name; char memline[256]; char curline[256]; name = "/proc/iomem"; cc = memopt(name, memline, sizeof(memline)); if (cc < 0) { perror(name); exit(1); } name = "/proc/cmdline"; cc = lastcmd(name, curline, sizeof(curline)); if (cc < 0) { perror(name); exit(1); } printf("%s %s\n", curline, memline); exit(0); }