--- np2/x11/dosio.c 2003/11/17 13:17:16 1.3 +++ np2/x11/dosio.c 2004/01/13 16:30:21 1.7 @@ -2,6 +2,7 @@ #include #include +#include #include "codecnv.h" #include "dosio.h" @@ -31,15 +32,19 @@ dosio_term(void) FILEH file_open(const char *path) { + FILEH fh; - return fopen(path, "rb+"); + fh = fopen(path, "rb+"); + if (fh) + return fh; + return fopen(path, "rb"); } FILEH file_open_rb(const char *path) { - return fopen(path, "rb+"); + return fopen(path, "rb"); } FILEH @@ -108,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; } @@ -212,6 +225,60 @@ file_attr_c(const char *sjis) return file_attr_c(curpath); } +FLISTH +file_list1st(const char *dir, FLINFO *fli) +{ + char eucpath[MAX_PATH]; + DIR *ret; + + mileuc_ncpy(eucpath, dir, sizeof(eucpath)); + file_setseparator(eucpath, sizeof(eucpath)); + ret = opendir(eucpath); + if (ret == NULL) { + return FLISTH_INVALID; + } + if (file_listnext((FLISTH)ret, fli) == SUCCESS) { + return (FLISTH)ret; + } + closedir(ret); + return FLISTH_INVALID; +} + +BOOL +file_listnext(FLISTH hdl, FLINFO *fli) +{ + struct dirent *de; + struct stat sb; + + de = readdir((DIR *)hdl); + if (de == NULL) { + return FAILURE; + } + if (stat(de->d_name, &sb) != 0) { + return FAILURE; + } + + fli->caps = FLICAPS_SIZE | FLICAPS_ATTR | FLICAPS_DATE | FLICAPS_TIME; + fli->size = sb.st_size; + 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)); + + return SUCCESS; +} + +void +file_listclose(FLISTH hdl) +{ + + closedir((DIR *)hdl); +} + static int euckanji1st(const char *str, int pos) { @@ -233,7 +300,7 @@ file_cpyname(char *dst, const char *src, for (i = 0; i < maxlen && src[i] != '\0'; i++) { dst[i] = src[i]; } - if (i != 0) { + if (i > 0) { if (euckanji1st(src, i-1)) { i--; }