void client_tick(int delta) { if (is_file_source) { file_loop(delta); } else { event_loop(EVLOOP_NONBLOCK); } }
void main(int argc, char **argv) { if (argc < 2) return; //Not enough arguments. // -r recusive // -l lazy // -s strict int r = 0, s = 0; for (int i = 1; i < argc; i++) { if (strlen(argv[i]) == 2 && argv[i][0] == '-') { switch (argv[i][1]) { case 'r': r = 1; break; case 'l': s = 0; break; case 's': s = 1; } } } for (int i = 1; i < argc; i++) { file_loop(argv[i], r, s); } }
void file_loop(const char *filename, int r, int s) { if (filename[0] == '-' || filename[0] == '.') { return; } struct stat buf; stat(filename, &buf); if (r && S_ISDIR(buf.st_mode)) { DIR *dir; struct dirent *ent; int path_size = strlen(filename) + 2; int size = path_size + 20; char *path = malloc(size); //Already checked length of filename. strcpy(path, filename); path[path_size - 2] = '/'; path[path_size - 1] = '\0'; if ((dir = opendir(path))) { //For each file ent. while ((ent = readdir(dir)) != NULL) { if (ent->d_name[0] == '.') continue; if (strlen(ent->d_name) + path_size >= size) { size = strlen(ent->d_name) + path_size; path = realloc(path, size); } //Already checked length of ent->d_name. memcpy(path+path_size - 1, ent->d_name, strlen(ent->d_name) + 1); file_loop(path, r, s); } } else { printf("Could not open dir %s\n", filename); } free(path); closedir(dir); } else if(S_ISREG(buf.st_mode)) { process_file(filename, s); } }
/* Initialize the databases */ int readdata(void) { register FILE *f; if ((f = fopen(arpfile, "r")) == NULL) { syslog(LOG_ERR, "fopen(%s): %m", arpfile); return(0); } if (!file_loop(f, ent_add, arpfile)) { (void)fclose(f); return(0); } (void)fclose(f); /* It's not fatal if we can't open the ethercodes file */ if ((f = fopen(ethercodes, "r")) != NULL) { (void)ec_loop(f, ec_add, ethercodes); (void)fclose(f); } return(1); }