/* Attempt to read and parse a config file. Return a handle to the config file * structure, or NULL if errors were encountered. */ struct configFile *readConfigFile (char *filename) { /* TODO: Attempt to read from homedir, /etc, etc if file not found in cwd. */ struct stat filestats; struct configFile *cfg; int fileHandle, bytesRead; if (stat (filename, &filestats) != 0) { perror ("ReadConfigFile: stat"); return NULL; } if (filestats.st_size > MAXIMUM_SANE_FILESIZE) return NULL; cfg = malloc (sizeof (struct configFile)); cfg->bbdg = malloc (filestats.st_size); cfg->sections = hashConstructTable (31); /* Some prime, just for laughs */ if ( (fileHandle=open (filename, O_RDONLY)) == -1) { perror ("ReadConfigFile: open"); return NULL; } if ( (bytesRead=read (fileHandle, cfg->bbdg, filestats.st_size)) != filestats.st_size) { perror ("ReadConfigFile: read"); return NULL; } close (fileHandle); cfg->bbdgSize=filestats.st_size; return _cfgParseConfigFile (cfg); }
struct configFile *readPlayList(char *filename) { /* TODO: Attempt to read from homedir, /etc, etc if file not found in cwd. */ struct stat filestats; struct configFile *cfg; int /* fileHandle ,*/ bytesRead; FILE *fin; if (stat(filename, &filestats) != 0) { /*perror ("ReadConfigFile: stat"); */ return NULL; } if (filestats.st_size > MAXIMUM_SANE_FILESIZE) return NULL; #ifdef _GLOBAL_CFG if(gplist == NULL) gplist = malloc(sizeof(struct configFile)); else return gplist; cfg = gplist; #else cfg = (struct configFile *)malloc(sizeof(struct configFile)); #endif cfg->bbdg = (unsigned char*)malloc(filestats.st_size); cfg->sections = hashConstructTable(31); if ((fin = fopen(filename, "r")) == NULL) { printf("can not open %s\n", filename); return NULL; } if ((bytesRead = fread(cfg->bbdg, 1, filestats.st_size, fin)) != filestats.st_size) { printf("read %s error\n", filename); return NULL; } fclose(fin); cfg->bbdgSize = filestats.st_size; return _cfgParseConfigFile(cfg); }