// Read a name/value file and write data entries for each line. // Returns the number of entries written (always <= stats_count). // // delimiter: used to split each line into name and value // terminator: if non-NULL, processing stops after this string // skip_words: skip this many words at the start of each line static int read_lines( const char *filename, char delimiter, const char *terminator, int skip_words, struct data *stats, int stats_count) { char buf[8192]; int fd = open(filename, O_RDONLY); if (fd < 0) return 0; int len = read(fd, buf, sizeof(buf) - 1); if (len < 0) { perror(filename); close(fd); return 0; } buf[len] = '\0'; close(fd); if (terminator != NULL) { char *end = strstr(buf, terminator); if (end != NULL) *end = '\0'; } int filename_len = strlen(filename); int num = 0; char *line; for (line = strtok(buf, "\n"); line != NULL && num < stats_count; line = strtok(NULL, "\n")) { // Line format: <sp>name<delim><sp>value int i; while (isspace(*line)) ++line; for (i = 0; i < skip_words; ++i) { while (isgraph(*line)) ++line; while (isspace(*line)) ++line; } char *name_end = strchr(line, delimiter); if (name_end == NULL) continue; // Key format: <filename>:<name> struct data *data = &stats[num++]; data->name = malloc(filename_len + 1 + (name_end - line) + 1); unspace(data->name, filename, filename_len); data->name[filename_len] = ':'; unspace(data->name + filename_len + 1, line, name_end - line); data->name[filename_len + 1 + (name_end - line)] = '\0'; char *value = name_end + 1; while (isspace(*value)) ++value; data->value = strdup(value); } return num; }
// Read /proc/yaffs and write data entries for each line. // Returns the number of entries written (always <= stats_count). static int read_proc_yaffs(struct data *stats, int stats_count) { char buf[8192]; int fd = open("/proc/yaffs", O_RDONLY); if (fd < 0) return 0; int len = read(fd, buf, sizeof(buf) - 1); if (len < 0) { perror("/proc/yaffs"); close(fd); return 0; } buf[len] = '\0'; close(fd); int num = 0, device_len = 0; char *line, *device = NULL; for (line = strtok(buf, "\n"); line != NULL && num < stats_count; line = strtok(NULL, "\n")) { if (strncmp(line, "Device ", 7) == 0) { device = strchr(line, '"'); if (device != NULL) { char *end = strchr(++device, '"'); if (end != NULL) *end = '\0'; device_len = strlen(device); } continue; } if (device == NULL) continue; char *name_end = line + strcspn(line, " ."); if (name_end == line || *name_end == '\0') continue; struct data *data = &stats[num++]; data->name = malloc(12 + device_len + 1 + (name_end - line) + 1); memcpy(data->name, "/proc/yaffs:", 12); unspace(data->name + 12, device, device_len); data->name[12 + device_len] = ':'; unspace(data->name + 12 + device_len + 1, line, name_end - line); data->name[12 + device_len + 1 + (name_end - line)] = '\0'; char *value = name_end; while (*value == '.' || isspace(*value)) ++value; data->value = strdup(value); } return num; }
void rad2mgf( /* convert a Radiance file to MGF */ char *inp ) { char buf[512]; char mod[128], typ[32], id[128], alias[128]; FUNARGS fa; register FILE *fp; register int c; if (inp == NULL) { inp = "standard input"; fp = stdin; } else if (inp[0] == '!') { if ((fp = popen(inp+1, "r")) == NULL) { fputs(inp, stderr); fputs(": cannot execute\n", stderr); exit(1); } } else if ((fp = fopen(inp, "r")) == NULL) { fputs(inp, stderr); fputs(": cannot open\n", stderr); exit(1); } printf("# Begin conversion from: %s\n", inp); while ((c = getc(fp)) != EOF) switch (c) { case ' ': /* white space */ case '\t': case '\n': case '\r': case '\f': break; case '#': /* comment */ if (fgets(buf, sizeof(buf), fp) != NULL) printf("# %s", buf); break; case '!': /* inline command */ ungetc(c, fp); fgetline(buf, sizeof(buf), fp); rad2mgf(buf); break; default: /* Radiance primitive */ ungetc(c, fp); if (fgetword(mod, sizeof(mod), fp) == NULL || fgetword(typ, sizeof(typ), fp) == NULL || fgetword(id, sizeof(id), fp) == NULL) { fputs(inp, stderr); fputs(": unexpected EOF\n", stderr); exit(1); } unspace(mod); unspace(id); if (!strcmp(typ, "alias")) { strcpy(alias, "EOF"); fgetword(alias, sizeof(alias), fp); unspace(alias); newmat(id, alias); } else { if (!readfargs(&fa, fp)) { fprintf(stderr, "%s: bad argument syntax for %s \"%s\"\n", inp, typ, id); exit(1); } cvtprim(inp, mod, typ, id, &fa); freefargs(&fa); } break; } printf("# End conversion from: %s\n", inp); if (inp[0] == '!') pclose(fp); else fclose(fp); }