/* * Process a request for including a file, either for configuration or for * ACLs. Called by read_conf_file and acl_check_file. * * Takes the file to include, the current file, the line number, the function * to call for each included file, and a piece of data to pass to that * function. Handles including either files or directories. When used for * processing ACL files named in the configuration file, the current file and * line number will be passed as zero. * * If the function returns a value less than -1, return its return code. If * the file is recursively included or if there is an error in reading a file * or processing an include directory, return CONFIG_ERROR. Otherwise, return * the greatest of all status codes returned by function, or CONFIG_NOMATCH if * the file was empty. */ static enum config_status handle_include(const char *included, const char *file, size_t lineno, enum config_status (*function)(void *, const char *), void *data) { struct stat st; /* Sanity checking. */ if (strcmp(included, file) == 0) { warn("%s:%lu: %s recursively included", file, (unsigned long) lineno, file); return CONFIG_ERROR; } if (stat(included, &st) < 0) { syswarn("%s:%lu: included file %s not found", file, (unsigned long) lineno, included); return CONFIG_ERROR; } /* * If it's a directory, include everything in the directory whose * filenames contain only the allowed characters. Otherwise, just include * the one file. */ if (!S_ISDIR(st.st_mode)) { return (*function)(data, included); } else { DIR *dir; struct dirent *entry; int status = CONFIG_NOMATCH; int last; dir = opendir(included); if (dir == NULL) { syswarn("%s:%lu: included directory %s cannot be opened", file, (unsigned long) lineno, included); return CONFIG_ERROR; } while ((entry = readdir(dir)) != NULL) { char *path; if (!valid_filename(entry->d_name)) continue; xasprintf(&path, "%s/%s", included, entry->d_name); last = (*function)(data, path); free(path); if (last < -1) { closedir(dir); return last; } if (last > status) status = last; } closedir(dir); return status; } }
/* * returns a valid filename */ char* get_filename(char* prompt) { char* filename; printf("%s", prompt); filename = readline(); while (!valid_filename(filename)) { printf("Could not open %s\n", filename); printf("%s", prompt); free(filename); filename = readline(); } return filename; }
int view_file(struct char_data *ch, const char *dir, const char *file) { char fname[MAX_INPUT_LENGTH]; char *filebuf = NULL; if (!valid_filename(file)) return -1; path(fname, dir, file); if (!access(fname, R_OK)) file_to_string_alloc(fname, &filebuf); else return -1; send_to_char(filebuf, ch); FREE(filebuf); return 0; }