/* reads the name of the ac-adapter directory and fills the adapter_t * structure with the name and the state-file. Return 0 on success, negative values on errors */ int init_acpi_acadapt(global_t *globals){ list_t *lst = NULL; adapter_t *ac = &globals->adapt; globals->sysstyle = 0; if((lst = dir_list(PROC_ACPI "ac_adapter")) == NULL || !lst->top) { if((lst = dir_list(SYS_POWER "/AC")) == NULL || !lst->top) return NOT_SUPPORTED; else globals->sysstyle = 1; } if((!lst->top->name || ((ac->name = strdup(lst->top->name)) == NULL))){ delete_list(lst); return ALLOC_ERR; } if(globals->sysstyle) snprintf(ac->state_file, MAX_NAME, SYS_POWER "/AC/online"); else snprintf(ac->state_file, MAX_NAME, PROC_ACPI "ac_adapter/%s/state", ac->name); delete_list(lst); read_acpi_acstate(globals); return SUCCESS; }
static void file_browser_reload_directory_content(struct file_browser *browser, const char *path) { strncpy(browser->directory, path, MAX_PATH_LEN); dir_free_list(browser->files, browser->file_count); dir_free_list(browser->directories, browser->dir_count); browser->files = dir_list(path, 0, &browser->file_count); browser->directories = dir_list(path, 1, &browser->dir_count); }
int dir_print_body(char *arg, unsigned long *dircount) { int rv; unsigned long filecount, bytecount; char *pattern, *cachedPattern; char *p; /* Modified to pre-allocate path to 270 bytes so that we don't have to realloc() it later. That was causing "DIR /S" not to work properly. The path variable cannot be reallocated once dir_list() is called, because dir_list() is recursive. This will also help to reduce memory fragmentation. */ if((p = dfnfullpath(arg)) == NULL) { error_out_of_memory(); return E_NoMem; } if((path = realloc(p, 270*sizeof(char))) == NULL) { free(p); error_out_of_memory(); return E_NoMem; } filecount = bytecount = 0; /* print the header */ if((rv = dir_print_header(toupper(path[0]) - 'A')) == 0) { /* There are some directory specs that are not detected by dfnstat() as they are no part of the filesystem in DOS */ pattern = dfnfilename(path); assert(p); if(!*pattern || (dfnstat(path) & DFN_DIRECTORY) != 0) { pattern = strchr(pattern, '\0'); if(pattern[-1] != '\\') ++pattern; rv = dir_list(pattern - path, "*.*", dircount, &filecount , &bytecount ); } else { if((cachedPattern = strdup(pattern)) == NULL) { error_out_of_memory(); rv = E_NoMem; } else { rv = dir_list(pattern - path, cachedPattern, dircount , &filecount, &bytecount ); free(cachedPattern); } } } free(path); return rv || (optS? print_total(filecount, bytecount): 0); }
//The basic premise here is that we keep trying to list each //directory in the path. If one can't be listed, then that //directory doesn't exist. If they can all be listed, then //the path is valid char dir_exists(unsigned char* dir) { int i, len; char listBuf[256]; if(!dir[0]) return 0; //The root folder always exists if(strcmp(dir, ":")) return 1; for(len = 0; dir[len]; len++); for(i = 1; i < len; i++) if(dir[i] == ':') dir[i] = 0; i = 0; while(i < len) { //Get the directory listing for the ith folder dir_list(dir, listBuf); if(!listBuf[0]) return 0; while(i < len) { if(!dir[i]) dir[i] = ':'; i++; } } return 1; }
/* reads the names of the fan directories, fills fan_t, * return 0 on success, negative values on errors */ int init_acpi_fan(global_t *globals){ char *names[MAX_ITEMS]; list_t *lst = NULL; node_t *node = NULL; int i = 0; fan_t *finfo = NULL; globals->fan_count = 0; if((lst = dir_list(PROC_ACPI "fan")) == NULL || !lst->top) return NOT_SUPPORTED; for(node = lst->top; node; node = node->next){ if((names[globals->fan_count] = strdup(node->name)) == NULL){ delete_list(lst); return ALLOC_ERR; } globals->fan_count++; } if(globals->fan_count > MAX_ITEMS) return ITEM_EXCEED; for (; i < globals->fan_count && i < MAX_ITEMS; i++){ finfo = &fans[i]; snprintf(finfo->name, MAX_NAME, "%s", names[i]); snprintf(finfo->state_file, MAX_NAME, PROC_ACPI "fan/%s/state", names[i]); free(names[i]); } delete_list(lst); read_acpi_fans(globals); return SUCCESS; }
/* reads the name of the thermal-zone directory and fills the adapter_t * structure with the name and the state-file. Return 0 on success, negative values on errors */ int init_acpi_thermal(global_t *globals){ char *names[MAX_ITEMS]; list_t *lst = NULL; node_t *node = NULL; thermal_t *tinfo = NULL; int i = 0; globals->thermal_count = 0; if((lst = dir_list(PROC_ACPI "thermal_zone")) == NULL) return NOT_SUPPORTED; for(node = lst->top; node; node = node->next){ if((names[globals->thermal_count] = strdup(node->name)) == NULL){ delete_list(lst); return ALLOC_ERR; } globals->thermal_count++; } if(globals->thermal_count > MAX_ITEMS) return ITEM_EXCEED; for (; i < globals->thermal_count && i < MAX_ITEMS; i++){ tinfo = &thermals[i]; snprintf(tinfo->name, MAX_NAME, "%s", names[i]); snprintf(tinfo->state_file, MAX_NAME, PROC_ACPI "thermal_zone/%s/state", names[i]); snprintf(tinfo->temp_file, MAX_NAME, PROC_ACPI "thermal_zone/%s/temperature", names[i]); snprintf(tinfo->cooling_file, MAX_NAME, PROC_ACPI "thermal_zone/%s/cooling_mode", names[i]); snprintf(tinfo->freq_file, MAX_NAME, PROC_ACPI "thermal_zone/%s/polling_frequency", names[i]); snprintf(tinfo->trips_file, MAX_NAME, PROC_ACPI "thermal_zone/%s/trip_points", names[i]); free(names[i]); } delete_list(lst); read_acpi_thermalzones(globals); return SUCCESS; }
int dir_print_body(char *arg) { int rv; unsigned long dircount, filecount, bytecount; char *pattern, *cachedPattern; dprintf( ("[DIR: path=\"%s\"]\n", arg) ); if((path = dfnexpand(arg, NULL)) == NULL) { error_out_of_memory(); return E_NoMem; } dircount = filecount = bytecount = 0; pattern = strchr(path, '\0'); if(pattern[-1] == '\\') { /* trailing backslash means that this has to be a directory */ if(!StrAppChr(path, '.')) { error_out_of_memory(); return E_NoMem; } } dprintf( ("[DIR: absolute path=\"%s\"]\n", path) ); /* print the header */ if ((rv = dir_print_header(toupper(path[0]) - 'A')) == 0) { if(dfnstat(path) & DFN_DIRECTORY) { pattern = strchr(path, '\0'); if(pattern[-1] != '\\') ++pattern; rv = dir_list(pattern - path, "*.*", &dircount, &filecount , &bytecount); } else { if((cachedPattern = strdup(pattern = dfnfilename(path))) == NULL) { error_out_of_memory(); rv = E_NoMem; } else { rv = dir_list(pattern - path, cachedPattern, &dircount, &filecount , &bytecount); free(cachedPattern); } } } free(path); return rv; }
void IconChooser::load(const char* dir) { /* copy directory name to input box and internal String so it can be reused later */ path->value(dir); start = dir; list<String> lst; if(!dir_list(dir, lst, true)) return; load_from_list(lst); }
static void file_browser_init(struct file_browser *browser, NVGcontext *vg, struct zr_user_font *font, int width, int height) { memset(browser, 0, sizeof(*browser)); media_init(&browser->media, vg); { /* gui */ browser->font = *font; browser->memory = calloc(1, MAX_COMMAND_MEMORY); memset(&browser->input, 0, sizeof(browser->input)); zr_command_queue_init_fixed(&browser->queue, browser->memory, MAX_COMMAND_MEMORY); zr_style_default(&browser->config, ZR_DEFAULT_ALL, &browser->font); zr_window_init(&browser->window, zr_rect(0,0,width,height), 0, &browser->queue, &browser->config, &browser->input); browser->ratio_dir = 0.75; browser->ratio_sel = 0.25f; } { /* load files and sub-directory list */ const char *home = getenv("HOME"); if (!home) home = getpwuid(getuid())->pw_dir; { size_t l; strncpy(browser->home, home, MAX_PATH_LEN); l = strlen(browser->home); strcpy(browser->home + l, "/"); strcpy(browser->directory, browser->home); } { size_t l; strcpy(browser->desktop, browser->home); l = strlen(browser->desktop); strcpy(browser->desktop + l, "desktop/"); } browser->files = dir_list(browser->directory, 0, &browser->file_count); browser->directories = dir_list(browser->directory, 1, &browser->dir_count); } }
void gendep(const deque<wstring>& params) { list<wstring> source_dirs, include_dirs; parse_cmd_line(params, source_dirs, include_dirs); wstring output; set<wstring> file_set; for (list<wstring>::const_iterator src_dir = source_dirs.begin(); src_dir != source_dirs.end(); src_dir++) { DirList dir_list(get_full_path_name(src_dir->empty() ? L"." : *src_dir)); while (dir_list.next()) { if (!dir_list.data().is_dir() && is_valid_ext(dir_list.data().cFileName)) { process_file(output, file_set, add_trailing_slash(*src_dir) + dir_list.data().cFileName, include_dirs); } } } cout << unicode_to_ansi(output, CP_ACP); }
void logs_enumerate(struct dir *log_dir, uint64_t log_number, uint64_t **logno_list_ptr, int *logno_list_sz_ptr) { int pos = 0; int sz = 1024; uint64_t *logno_list = malloc(sizeof(uint64_t)*sz); char **files_org = dir_list(log_dir, _filter, &log_number); char **files; for (files = files_org; *files != NULL; files++) { logno_list[pos++] = _filename_log_number(*files); free(*files); if (pos == sz) { sz *= 2; logno_list = realloc(logno_list, sizeof(uint64_t)*sz); } } free(files_org); *logno_list_ptr = logno_list; *logno_list_sz_ptr = pos; }
void FileListbox::fill_listbox( GLUI_Listbox *listbox, str_list &save_files, Cstr_ptr &full_path, Cstr_ptr &save_path, const char *extension ) { int j = 0; str_list in_files = dir_list(full_path); for (int i = 0; i < in_files.num(); i++) { int len = in_files[i].len(); if ( (extension) && (len>3) && (strcmp(&(**in_files[i])[len-4],extension) == 0)) { save_files += save_path + in_files[i]; listbox->add_item(j++, **in_files[i]); } } }
void FileListbox::fill_listbox( GLUI_Listbox *listbox, vector<string> &save_files, const string &full_path, const string &save_path, const char *extension ) { int j = 0; vector<string> in_files = dir_list(full_path); for (auto & in_file : in_files) { string::size_type len = in_file.length(); if ( (extension) && (len>3) && (in_file.substr(len-4) == extension)) { save_files.push_back(save_path + in_file); listbox->add_item(j++, in_file.c_str()); } } }
UT_VERIFY( dir_exists("/we/dont/have/this/directory") == false ); UT_VERIFY( dir_readable("/we/dont/have/this/directory") == false ); UT_VERIFY( dir_writeable("/we/dont/have/this/directory") == false ); UT_VERIFY( dir_exists("Jamfile") == false ); UT_VERIFY( dir_exists("../Jamfile") == false ); UT_VERIFY( dir_exists("../../src/File.cpp") == false ); UT_VERIFY( dir_writeable("../../src/File.cpp") == false ); UT_VERIFY( dir_exists("../edelib") ); #endif UT_VERIFY( dir_home() != "" ); UT_VERIFY( dir_current() != "" ); list<String> dlist; UT_VERIFY( dir_list("/this/directory/should/not/exist", dlist) == false ); UT_VERIFY( dir_list(".", dlist) == true ); UT_VERIFY( dlist.size() != 0 ); } UT_FUNC(DirectoryOperations, "Test Directory operations") { UT_VERIFY( dir_create("FooDir") == true ); #if 0 UT_VERIFY( dir_exists("FooDir") == true ); #endif UT_VERIFY( dir_rename("FooDir", "BazDir") == true ); #if 0 UT_VERIFY( dir_exists("BazDir") == true ); UT_VERIFY( dir_exists("FooDir") != true );
/* * dir_list * * list the files in the directory */ int dir_list(int pathlen , char *pattern , unsigned long *dcnt , unsigned long *fcnt , unsigned long *bcnt ) { struct ffblk file; unsigned long bytecount = 0; unsigned long filecount = 0; unsigned long dircount = 0; int time; int count; unsigned mode = FA_RDONLY | FA_ARCH | FA_DIREC; int rv = E_None; assert(path); assert(pattern); assert(pathlen >= 2); /* at least root */ /* if the user wants all files listed */ if (optA) mode |= FA_HIDDEN | FA_SYSTEM; /* Search for matching entries */ path[pathlen - 1] = '\\'; strcpy(&path[pathlen], pattern); if (FINDFIRST(path, &file, mode) == 0) { /* moved down here because if we are recursively searching and * don't find any files, we don't want just to print * Directory of C:\SOMEDIR * with nothing else */ if (!optB) { rv = flush_nl(); if(rv == E_None) { /* path without superflous '\' at its end */ if(pathlen == 3) /* root directory */ path[pathlen] = '\0'; /* path := path w/o filename */ else path[pathlen - 1] = '\0'; /* /// Changed to exactly match DOS's formatting. - Ron Cemer */ printf("%sDirectory of %s\n", (optS ? "" : " "), path); if((rv = incline()) == E_None) { putchar('\n'); rv = incline(); } } } /* For counting columns of output */ count = WIDE_COLUMNS; /* if optB && optS the path with trailing backslash is needed, also for optS below do {} while */ strcpy(&path[pathlen - 1], "\\"); if(rv == E_None) do { assert(strlen(file.ff_name) < 13); if (cbreak) rv = E_CBreak; else { if (optL) strlwr(file.ff_name); if (optW) { char buffer[sizeof(file.ff_name) + 3]; if (file.ff_attrib & FA_DIREC) { sprintf(buffer, "[%s]", file.ff_name); dircount++; } else { strcpy(buffer, file.ff_name); filecount++; bytecount += file.ff_fsize; } printf("%-15s", buffer); if (!--count) { /* outputted 5 columns */ putchar('\n'); rv = incline(); count = WIDE_COLUMNS; } } else if (optB) { if (strcmp(file.ff_name, ".") == 0 || strcmp(file.ff_name, "..") == 0) continue; if (optS) fputs(path, stdout); printf("%-13s\n", file.ff_name); if (file.ff_attrib & FA_DIREC) dircount++; else { filecount++; bytecount += file.ff_fsize; } rv = incline(); } else { char buffer[sizeof(long) * 4 + 2], *ext; if (file.ff_name[0] == '.') printf("%-13s", file.ff_name); else { ext = strrchr(file.ff_name, '.'); if (!ext) ext = ""; else *ext++ = '\0'; printf("%-8s %-3s ", file.ff_name, ext); } if (file.ff_attrib & FA_DIREC) { printf("%-14s", " <DIR>"); dircount++; } else { convert(file.ff_fsize, buffer); printf(" %10s ", buffer); bytecount += file.ff_fsize; filecount++; } printf(" %.2d-%.2d-%02d", ((file.ff_fdate >> 5) & 0x000f), (file.ff_fdate & 0x001f), ((file.ff_fdate >> 9) + 80) % 100); time = file.ff_ftime >> 5 >> 6; printf(" %2d:%.2u%c\n", (time == 0 ? 12 : (time <= 12 ? time : time - 12)), ((file.ff_ftime >> 5) & 0x003f), (time <= 11 ? 'a' : 'p')); rv = incline(); } } } while (rv == E_None && FINDNEXT(&file) == 0); } if (rv == E_None && optW && (count != 0)) { putchar('\n'); rv = incline(); } if (rv == E_None) if(filecount || dircount) { /* The code that was here is now in print_summary */ rv = print_summary(filecount, bytecount); } else if(!optS) { error_file_not_found(); rv = E_Other; } if(rv == E_None /* no error */ && optS) { /* do recursively */ /* already set for optB && optS before do {} while above path[pathlen - 1] = '\\'; */ strcpy(&path[pathlen], "*.*"); if (FINDFIRST(path, &file, mode) == 0) do { if((file.ff_attrib & FA_DIREC) != 0 /* is directory */ && strcmp(file.ff_name, ".") != 0 /* not cur dir */ && strcmp(file.ff_name, "..") != 0) { /* not parent dir */ if (optL) strlwr(file.ff_name); strcpy(&path[pathlen], file.ff_name); rv = dir_list(pathlen + strlen(file.ff_name) + 1, pattern , &dircount, &filecount, &bytecount ); } } while (rv == E_None && FINDNEXT(&file) == 0); } *dcnt += dircount; *fcnt += filecount; *bcnt += bytecount; return rv; }
int main(int argc, char *argv[], char **envp) { // Declare major variables char buffer[BUFFER_LEN] = { 0 }; char command[BUFFER_LEN] = { 0 }; char arg[BUFFER_LEN] = { 0 }; char shell_dir[BUFFER_LEN] = { 0 }; char cwd[1024] = { 0 }; // batchfile condition; call batch function when there are two arguments (1st is the program name, 2nd is batchfile) if (argc == 2) { //passes the batchfile name and environment variables to batch, then quit. batch(argv[1], envp); return EXIT_SUCCESS; } // get current working directory, store this in shell_dir, and display getcwd(shell_dir, sizeof(shell_dir)); printf("Shell = %s\nCopyright (c) 2015\n\n", shell_dir); getcwd(cwd, sizeof(cwd)); // Prompt user printf("%s > ", cwd); // Loop through user commands indefinitely while (fgets(buffer, BUFFER_LEN, stdin) != NULL) { // Echo input back to user printf("%s\n", buffer); // Perform string tokenization to get command and arguments. strcpy(command, strtok(buffer, " ")); // Execute the command entered by the user // Display environment variables if(strcmp(command, "environ") == 0) { environVariable(envp); } // Change directory else if (strcmp(command, "cd") == 0) { strcpy(arg, strtok(NULL, " ")); sh_cd(arg); } // List current directory else if(strcmp(command, "dir") == 0) { dir_list(); } // Pause execution else if(strcmp(command, "pause") == 0) { pause(); } // Display help file. else if(strcmp(command, "help") == 0) { strcpy(arg, strtok(NULL, " ")); help(arg); } // Exit else if (strcmp(command, "quit") == 0) { exit(0); return EXIT_SUCCESS; } // echo input else if (strcmp(command, "echo") == 0) { for(int i = 5; i < sizeof(buffer); i++) { arg[i-5] = buffer[i]; } printf("%s\n", arg); } // n.b.: Add additional batch functions to utility.c // Unsupported command else { fputs("Unsupported command, use help to display the manual\n", stderr); } // Finished running command, update current working directory and prompt user. getcwd(cwd, sizeof(cwd)); printf("%s > ", cwd); } return EXIT_SUCCESS; }
/* reads existent battery directories and starts to fill the battery * structure. Returns 0 on success, negative values on error */ int init_acpi_batt(global_t *globals){ char *names[MAX_ITEMS]; battery_t *binfo; list_t *lst = NULL; node_t *node = NULL; int dir_count, num_bat, i = 0; globals->batt_count = 0; globals->sysstyle = 0; if((lst = dir_list(PROC_ACPI "battery")) == NULL || !lst->top) { if (lst) delete_list(lst); /* check for new Linux 2.6.24+ layout */ if((lst = dir_list(SYS_POWER)) == NULL || !lst->top) return NOT_SUPPORTED; else globals->sysstyle = 1; } for(node = lst->top; node; node=node->next){ if((names[globals->batt_count] = strdup(node->name)) == NULL){ delete_list(lst); return ALLOC_ERR; } globals->batt_count++; } if(globals->batt_count > MAX_ITEMS) return ITEM_EXCEED; /* A quick insertion sort, to sort battery names */ { char *tmp1, *tmp2; int x,y; for (x = 1; x < globals->batt_count; x++) { tmp1 = names[x]; y = x - 1; while ((y >= 0) && ((strcmp (tmp1, names[y])) < 0)) { tmp2 = names[y + 1]; names[y + 1] = names[y]; names[y] = tmp2; } } } dir_count = globals->batt_count; for (num_bat=i=0; i < dir_count && i < MAX_ITEMS; i++){ if (strncmp(names[i], "BAT", 3) == 0) { binfo = &batteries[num_bat]; snprintf(binfo->name, MAX_NAME, "%s", names[i]); if(globals->sysstyle) { snprintf(binfo->state_file, MAX_NAME, SYS_POWER "/%s/status", names[i]); snprintf(binfo->info_file, MAX_NAME, SYS_POWER "/%s", names[i]); snprintf(binfo->alarm_file, MAX_NAME, SYS_POWER "/%s/alarm", names[i]); } else { snprintf(binfo->state_file, MAX_NAME, PROC_ACPI "battery/%s/state", names[i]); snprintf(binfo->info_file, MAX_NAME, PROC_ACPI "battery/%s/info", names[i]); snprintf(binfo->alarm_file, MAX_NAME, PROC_ACPI "battery/%s/alarm", names[i]); } read_acpi_battinfo(num_bat, globals->sysstyle); read_acpi_battalarm(num_bat, globals->sysstyle); num_bat++; } else { globals->batt_count--; } free(names[i]); } delete_list(lst); return SUCCESS; }