--- np2/x11/dosio.c 2003/12/19 16:08:01 1.6 +++ np2/x11/dosio.c 2004/02/05 13:45:06 1.9 @@ -1,8 +1,8 @@ +/* $Id: dosio.c,v 1.9 2004/02/05 13:45:06 monaka Exp $ */ + #include "compiler.h" -#include #include -#include #include "codecnv.h" #include "dosio.h" @@ -113,28 +113,36 @@ file_attr(const char *path) return -1; } -short -file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime) +static BOOL +cnvdatetime(struct stat *sb, DOSDATE *dosdate, DOSTIME *dostime) { - struct stat sb; struct tm *ftime; - if (fstat(fileno(handle), &sb) == 0) { - ftime = localtime(&sb.st_mtime); - if (ftime) { - if (dosdate) { - dosdate->year = ftime->tm_year + 1900; - dosdate->month = ftime->tm_mon + 1; - dosdate->day = ftime->tm_mday; - } - if (dostime) { - dostime->hour = ftime->tm_hour; - dostime->minute = ftime->tm_min; - dostime->second = ftime->tm_sec; - } - return 0; + ftime = localtime(&sb->st_mtime); + if (ftime) { + if (dosdate) { + dosdate->year = ftime->tm_year + 1900; + dosdate->month = ftime->tm_mon + 1; + dosdate->day = ftime->tm_mday; + } + if (dostime) { + dostime->hour = ftime->tm_hour; + dostime->minute = ftime->tm_min; + dostime->second = ftime->tm_sec; } + return SUCCESS; } + return FAILURE; +} + +short +file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime) +{ + struct stat sb; + + if ((fstat(fileno(handle), &sb) == 0) + && (cnvdatetime(&sb, dosdate, dostime))) + return 0; return -1; } @@ -217,58 +225,78 @@ file_attr_c(const char *sjis) return file_attr_c(curpath); } -FILEFINDH -file_find1st(const char *dir, FILEFINDT *fft) +FLISTH +file_list1st(const char *dir, FLINFO *fli) { - DIR *ret; + FLISTH ret; - ret = opendir(dir); + ret = (FLISTH)malloc(sizeof(_FLISTH)); if (ret == NULL) { - return FILEFINDH_INVALID; - } - if (file_findnext((FILEFINDH)ret, fft) == SUCCESS) { - return (FILEFINDH)ret; + VERBOSE(("file_list1st: couldn't alloc memory (size = %d)", sizeof(_FLISTH))); + return FLISTH_INVALID; } - closedir(ret); - return FILEFINDH_INVALID; + + mileuc_ncpy(ret->path, dir, sizeof(ret->path)); + file_setseparator(ret->path, sizeof(ret->path)); + ret->hdl = opendir(ret->path); + VERBOSE(("file_list1st: opendir(%s)", ret->path)); + if (ret->hdl == NULL) { + VERBOSE(("file_list1st: opendir failure")); + free(ret); + return FLISTH_INVALID; + } + if (file_listnext((FLISTH)ret, fli) == SUCCESS) { + return (FLISTH)ret; + } + VERBOSE(("file_list1st: file_listnext failure")); + closedir(ret->hdl); + free(ret); + return FLISTH_INVALID; } BOOL -file_findnext(FILEFINDH hdl, FILEFINDT *fft) +file_listnext(FLISTH hdl, FLINFO *fli) { + char buf[MAX_PATH]; struct dirent *de; struct stat sb; - UINT32 attr; - UINT32 size; - de = readdir((DIR *)hdl); + de = readdir(hdl->hdl); if (de == NULL) { + VERBOSE(("file_listnext: readdir failure")); return FAILURE; } - if (fft) { - mileuc_ncpy(fft->path, de->d_name, sizeof(fft->path)); - size = 0; - attr = 0; - if (stat(de->d_name, &sb) == 0) { - size = sb.st_size; - if (S_ISDIR(sb.st_mode)) { - attr = FILEATTR_DIRECTORY; - } - else if (!(sb.st_mode & S_IWUSR)) { - attr = FILEATTR_READONLY; - } - } - fft->size = size; - fft->attr = attr; + + milstr_ncpy(buf, hdl->path, sizeof(buf)); + milstr_ncat(buf, de->d_name, sizeof(buf)); + if (stat(buf, &sb) != 0) { + VERBOSE(("file_listnext: stat failure. (path = %s)", buf)); + return FAILURE; } + + fli->caps = FLICAPS_SIZE | FLICAPS_ATTR | FLICAPS_DATE | FLICAPS_TIME; + fli->size = sb.st_size; + fli->attr = 0; + if (S_ISDIR(sb.st_mode)) { + fli->attr |= FILEATTR_DIRECTORY; + } + if (!(sb.st_mode & S_IWUSR)) { + fli->attr |= FILEATTR_READONLY; + } + cnvdatetime(&sb, &fli->date, &fli->time); + mileuc_ncpy(fli->path, de->d_name, sizeof(fli->path)); + VERBOSE(("file_listnext: success")); return SUCCESS; } void -file_findclose(FILEFINDH hdl) +file_listclose(FLISTH hdl) { - closedir((DIR *)hdl); + if (hdl) { + closedir(hdl->hdl); + free(hdl); + } } static int