void get_raid_setting(struct ncfs_state *ncfs_data) { FILE *fp = fopen("raid_setting", "r"); char buf[80]; while (fgets(buf, sizeof(buf), fp)) { if (parse_setting_line(ncfs_data, buf) == disk_total_num) { ncfs_data->free_offset = (int*)malloc(sizeof(int)*ncfs_data->disk_total_num); ncfs_data->free_size = (int*)malloc(sizeof(int)*ncfs_data->disk_total_num); ncfs_data->dev_name = (char **)calloc(ncfs_data->disk_total_num, sizeof(char *)); } } fclose(fp); }
int config_read(char *name, struct config *cfg) { enum config_section current_section = UNKNOWN_SECTION; enum parser_result parser_res; FILE *fp; char buf[1024], *line, *c, *option, *value; int current_port, line_num; fp = 0 == strncmp(name, "-", 2) ? stdin : fopen(name, "r"); if (!fp) { fprintf(stderr, "failed to open configuration file %s: %m\n", name); return -1; } for (line_num = 1; fgets(buf, sizeof(buf), fp); line_num++) { c = buf; /* skip whitespace characters */ while (isspace(*c)) c++; /* ignore empty lines and comments */ if (*c == '#' || *c == '\n' || *c == '\0') continue; line = c; /* remove trailing whitespace characters and \n */ c += strlen(line) - 1; while (c > line && (*c == '\n' || isspace(*c))) *c-- = '\0'; if (parse_section_line(line, ¤t_section) == PARSED_OK) { if (current_section == PORT_SECTION) { char port[17]; if (1 != sscanf(line, " %16s", port)) { fprintf(stderr, "could not parse port name on line %d\n", line_num); goto parse_error; } current_port = config_create_interface(port, cfg); if (current_port < 0) goto parse_error; } continue; } switch (current_section) { case GLOBAL_SECTION: case PORT_SECTION: if (parse_setting_line(line, &option, &value)) { fprintf(stderr, "could not parse line %d in %s section\n", line_num, current_section == GLOBAL_SECTION ? "global" : cfg->iface[current_port].name); goto parse_error; } if (current_section == GLOBAL_SECTION) parser_res = parse_global_setting(option, value, cfg); else parser_res = parse_port_setting(option, value, cfg, current_port); switch (parser_res) { case PARSED_OK: break; case NOT_PARSED: fprintf(stderr, "unknown option %s at line %d in %s section\n", option, line_num, current_section == GLOBAL_SECTION ? "global" : cfg->iface[current_port].name); goto parse_error; case BAD_VALUE: fprintf(stderr, "%s is a bad value for option %s at line %d\n", value, option, line_num); goto parse_error; } break; case UNKNOWN_SECTION: fprintf(stderr, "line %d is not in a section\n", line_num); goto parse_error; default: continue; } } fclose(fp); return 0; parse_error: fprintf(stderr, "failed to parse configuration file %s\n", name); fclose(fp); return -2; }
/* read .conf file and store info in driver */ static int read_conf_file(char *conf_file_name, struct load_driver *driver) { char setting_line[SETTING_LEN]; struct stat statbuf; FILE *config; char setting_name[MAX_NDIS_SETTING_NAME_LEN]; char setting_value[MAX_NDIS_SETTING_VALUE_LEN]; int ret, nr_settings; int i, vendor, device, subvendor, subdevice, dev_bustype, conf_bustype; if (lstat(conf_file_name, &statbuf)) { error("unable to open config file: %s", strerror(errno)); return -EINVAL; } if (strlen(conf_file_name) == 16) { i = sscanf(conf_file_name, "%04X:%04X.%d.conf", &vendor, &device, &dev_bustype); if (i != 3) { error("unable to parse conf file name %s (%d)", conf_file_name, i); return -EINVAL; } } else { i = sscanf(conf_file_name, "%04X:%04X:%04X:%04X.%d.conf", &vendor, &device, &subvendor, &subdevice, &dev_bustype); if (i != 5) { error("unable to parse conf file name %s (%d)", conf_file_name, i); return -EINVAL; } } nr_settings = 0; driver->nr_settings = 0; if ((config = fopen(conf_file_name, "r")) == NULL) { error("unable to open config file: %s", strerror(errno)); return -EINVAL; } while (fgets(setting_line, SETTING_LEN-1, config)) { struct load_device_setting *setting; setting_line[SETTING_LEN-1] = 0; ret = parse_setting_line(setting_line, setting_name, setting_value); if (ret == 0) continue; if (ret < 0) return -EINVAL; if (strcmp(setting_name, "BusType") == 0) { conf_bustype = strtol(setting_value, NULL, 10); if (dev_bustype != conf_bustype) { error("invalid bustype: %d(%d)", dev_bustype, conf_bustype); return -EINVAL; } } setting = &driver->settings[nr_settings]; strncpy(setting->name, setting_name, MAX_NDIS_SETTING_NAME_LEN); strncpy(setting->value, setting_value, MAX_NDIS_SETTING_VALUE_LEN); nr_settings++; if (nr_settings >= MAX_NDIS_SETTINGS) { error("too many settings"); return -EINVAL; } } fclose(config); if (conf_bustype == -1) { error("coudn't find device type in settings"); return -EINVAL; } driver->nr_settings = nr_settings; return 0; }