static void ioutil_filldir(const char *path, ioutil_name_table_t *dirs, ioutil_name_table_t *files) { DIR *dirp = NULL; struct dirent *dp = NULL; int dir_count=0; int file_count=0; unsigned int len, isdir; char *filename; int retval; dirp = opendir(path); dp = readdir(dirp); while (dp != NULL) { filename = util_concat(path, FSDEV_DIR_SEP_STR, dp->d_name, NULL); retval = ioutil_stat(filename, &len, &isdir); if (retval == 0) { if (isdir) { dirs[dir_count].name = lib_stralloc(dp->d_name); dir_count++; } else { files[file_count].name = lib_stralloc(dp->d_name); file_count++; } } dp = readdir(dirp); lib_free(filename); } closedir(dirp); }
/* NOTE: even when _DIRENT_HAVE_D_TYPE is defined, d_type may still be returned as DT_UNKNOWN - in that case we must fall back to using stat instead. */ static int ioutil_count_dir_items(const char *path) { DIR *dirp; struct dirent *dp; /* #ifndef _DIRENT_HAVE_D_TYPE */ unsigned int len, isdir; char *filename; int retval; /* #endif */ dirs_amount = 0; files_amount = 0; dirp = opendir(path); if (dirp == NULL) { return -1; } dp = readdir(dirp); while (dp != NULL) { #ifdef _DIRENT_HAVE_D_TYPE if (dp->d_type != DT_UNKNOWN) { if (dp->d_type == DT_DIR) { dirs_amount++; } else { files_amount++; } dp = readdir(dirp); } else { #endif filename = util_concat(path, FSDEV_DIR_SEP_STR, dp->d_name, NULL); retval = ioutil_stat(filename, &len, &isdir); if (retval == 0) { if (isdir) { dirs_amount++; } else { files_amount++; } } dp = readdir(dirp); lib_free(filename); #ifdef _DIRENT_HAVE_D_TYPE } #endif } closedir(dirp); return 0; }
static void ioutil_filldir(const char *path, ioutil_name_table_t *dirs, ioutil_name_table_t *files) { DIR *dirp = NULL; struct dirent *dp = NULL; int dir_count = 0; int file_count = 0; /* #ifndef _DIRENT_HAVE_D_TYPE */ unsigned int len, isdir; char *filename; int retval; /* #endif */ dirp = opendir(path); dp = readdir(dirp); while (dp != NULL) { #ifdef _DIRENT_HAVE_D_TYPE if (dp->d_type != DT_UNKNOWN) { if (dp->d_type == DT_DIR) { dirs[dir_count].name = lib_stralloc(dp->d_name); dir_count++; } else { files[file_count].name = lib_stralloc(dp->d_name); file_count++; } dp = readdir(dirp); } else { #endif filename = util_concat(path, FSDEV_DIR_SEP_STR, dp->d_name, NULL); retval = ioutil_stat(filename, &len, &isdir); if (retval == 0) { if (isdir) { dirs[dir_count].name = lib_stralloc(dp->d_name); dir_count++; } else { files[file_count].name = lib_stralloc(dp->d_name); file_count++; } } dp = readdir(dirp); lib_free(filename); #ifdef _DIRENT_HAVE_D_TYPE } #endif } closedir(dirp); }
static int ioutil_count_dir_items(const char *path) { DIR *dirp; struct dirent *dp; unsigned int len, isdir; char *filename; int retval; dirs_amount = 0; files_amount = 0; dirp = opendir(path); if (dirp == NULL) { return -1; } dp = readdir(dirp); while (dp != NULL) { filename = util_concat(path, FSDEV_DIR_SEP_STR, dp->d_name, NULL); retval = ioutil_stat(filename, &len, &isdir); if (retval == 0) { if (isdir) { dirs_amount++; } else { files_amount++; } } dp = readdir(dirp); lib_free(filename); } closedir(dirp); return 0; }
static UI_CALLBACK(events_select_dir) { char *wd; unsigned int i, is_dir; int len; len = ioutil_maxpathlen(); wd = lib_malloc(len); ioutil_getcwd(wd, len); vsync_suspend_speed_eval(); if (ui_input_string(_("VICE setting"), _("Select history directory"), wd, len) == UI_BUTTON_OK) { ioutil_stat(wd, &i, &is_dir); if (!is_dir) ui_error(_("Directory not found")); else resources_set_string("EventSnapshotDir", wd); } lib_free(wd); }
static void command_directory_get(vdrive_t *vdrive, bufinfo_t *bufinfo, BYTE *data, unsigned int secondary) { int i, l, f, statrc; unsigned int blocks; char *direntry; unsigned int filelen, isdir; fileio_info_t *finfo = NULL; unsigned int format = 0; char *buf; buf = (char *)lib_malloc(ioutil_maxpathlen()); bufinfo->bufp = bufinfo->name; if (fsdevice_convert_p00_enabled[(vdrive->unit) - 8]) format |= FILEIO_FORMAT_P00; if (!fsdevice_hide_cbm_files_enabled[vdrive->unit - 8]) format |= FILEIO_FORMAT_RAW; /* * Find the next directory entry and return it as a CBM * directory line. */ /* first test if dirmask is needed - maybe this should be replaced by some regex functions... */ f = 1; do { BYTE *p; finfo = NULL; /* iAN CooG 31/08 */ direntry = ioutil_readdir(bufinfo->ioutil_dir); if (direntry == NULL) break; finfo = fileio_open(direntry, bufinfo->dir, format, FILEIO_COMMAND_READ | FILEIO_COMMAND_FSNAME, FILEIO_TYPE_PRG); if (finfo == NULL) continue; bufinfo->type = finfo->type; if (bufinfo->dirmask[0] == '\0') break; l = strlen(bufinfo->dirmask); for (p = finfo->name, i = 0; *p && bufinfo->dirmask[i] && i < l; i++) { if (bufinfo->dirmask[i] == '?') { p++; } else if (bufinfo->dirmask[i] == '*') { if (!(bufinfo->dirmask[i + 1])) { f = 0; break; } /* end mask */ while (*p && (*p != bufinfo->dirmask[i + 1])) p++; } else { if (*p != bufinfo->dirmask[i]) break; p++; } if ((!*p) && (!(bufinfo->dirmask[i + 1]))) { f = 0; break; } } if (f > 0) fileio_close(finfo); } while (f); if (direntry != NULL) { BYTE *p = bufinfo->name; strcpy(buf, bufinfo->dir); strcat(buf, FSDEV_DIR_SEP_STR); strcat(buf, direntry); /* Line link, Length and spaces */ *p++ = 1; *p++ = 1; statrc = ioutil_stat(buf, &filelen, &isdir); if (statrc == 0) blocks = (filelen + 253) / 254; else blocks = 0; /* this file can't be opened */ if (blocks > 0xffff) blocks = 0xffff; /* Limit file size to 16 bits. */ SET_LO_HI(p, blocks); if (blocks < 10) *p++ = ' '; if (blocks < 100) *p++ = ' '; if (blocks < 1000) *p++ = ' '; /* * Filename */ *p++ = '"'; for (i = 0; finfo->name[i] && (*p = finfo->name[i]); ++i, ++p); *p++ = '"'; for (; i < 16; i++) *p++ = ' '; if (isdir != 0) { *p++ = ' '; /* normal file */ *p++ = 'D'; *p++ = 'I'; *p++ = 'R'; } else { if (blocks) *p++ = ' '; /* normal file */ else *p++ = '*'; /* splat file */ switch(bufinfo->type) { case CBMDOS_FT_DEL: *p++ = 'D'; *p++ = 'E'; *p++ = 'L'; break; case CBMDOS_FT_SEQ: *p++ = 'S'; *p++ = 'E'; *p++ = 'Q'; break; case CBMDOS_FT_PRG: *p++ = 'P'; *p++ = 'R'; *p++ = 'G'; break; case CBMDOS_FT_USR: *p++ = 'U'; *p++ = 'S'; *p++ = 'R'; break; case CBMDOS_FT_REL: *p++ = 'R'; *p++ = 'E'; *p++ = 'L'; break; } } if (ioutil_access(buf, IOUTIL_ACCESS_W_OK)) *p++ = '<'; /* read-only file */ *p = '\0'; /* to allow strlen */ /* some (really very) old programs rely on the directory entry to be 32 Bytes in total (incl. nullbyte) */ l = strlen((char *)(bufinfo->name + 4)) + 4; while (l < 31) { *p++ = ' '; l++; } *p++ = '\0'; bufinfo->buflen = (int)(p - bufinfo->name); } else { BYTE *p = bufinfo->name; /* EOF => End file */ *p++ = 1; *p++ = 1; *p++ = 0; *p++ = 0; memcpy(p, "BLOCKS FREE.", 12); p += 12; memset(p, ' ', 13); p += 13; memset(p, 0, 3); bufinfo->buflen = 32; bufinfo->eof++; } if (finfo != NULL) /* iAN CooG 31/08 */ fileio_close(finfo); lib_free(buf); }
rawfile_info_t *rawfile_open(const char *file_name, const char *path, unsigned int command) { rawfile_info_t *info; char *complete; FILE *fd; const char *mode = NULL; unsigned int isdir, len; if (path == NULL) { complete = lib_stralloc(file_name); } else { complete = util_concat(path, FSDEV_DIR_SEP_STR, file_name, NULL); } switch (command) { case FILEIO_COMMAND_STAT: case FILEIO_COMMAND_READ: mode = MODE_READ; break; case FILEIO_COMMAND_WRITE: mode = MODE_WRITE; break; case FILEIO_COMMAND_APPEND: mode = MODE_APPEND; break; case FILEIO_COMMAND_APPEND_READ: mode = MODE_APPEND_READ_WRITE; break; default: return NULL; } if (ioutil_stat(complete, &len, &isdir) != 0) { /* if stat failed exit early, except in write mode (since opening a non existing file creates a new file) */ if (command != FILEIO_COMMAND_WRITE) { lib_free(complete); return NULL; } } info = lib_malloc(sizeof(rawfile_info_t)); if ((isdir) && (command == FILEIO_COMMAND_STAT)) { info->fd = NULL; info->read_only = 1; } else { fd = fopen(complete, mode); if (fd == NULL) { lib_free(complete); lib_free(info); return NULL; } info->fd = fd; info->read_only = 0; } util_fname_split(complete, &(info->path), &(info->name)); lib_free(complete); return info; }