--- np2/x11/dosio.c 2003/11/18 14:41:52 1.5 +++ np2/x11/dosio.c 2004/07/08 14:21:50 1.12 @@ -1,3 +1,5 @@ +/* $Id: dosio.c,v 1.12 2004/07/08 14:21:50 monaka Exp $ */ + #include "compiler.h" #include @@ -112,28 +114,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; } @@ -163,67 +173,143 @@ file_setcd(const char *exepath) } char * -file_getcd(const char *sjis) +file_getcd(const char *filename) { *curfilep = '\0'; - file_catname(curpath, sjis, sizeof(curpath)); + file_catname(curpath, filename, sizeof(curpath)); return curpath; } FILEH -file_open_c(const char *sjis) +file_open_c(const char *filename) { *curfilep = '\0'; - file_catname(curpath, sjis, sizeof(curpath)); + file_catname(curpath, filename, sizeof(curpath)); return file_open(curpath); } FILEH -file_open_rb_c(const char *sjis) +file_open_rb_c(const char *filename) { *curfilep = '\0'; - file_catname(curpath, sjis, sizeof(curpath)); + file_catname(curpath, filename, sizeof(curpath)); return file_open_rb(curpath); } FILEH -file_create_c(const char *sjis) +file_create_c(const char *filename) { *curfilep = '\0'; - file_catname(curpath, sjis, sizeof(curpath)); + file_catname(curpath, filename, sizeof(curpath)); return file_create(curpath); } short -file_delete_c(const char *sjis) +file_delete_c(const char *filename) { *curfilep = '\0'; - file_catname(curpath, sjis, sizeof(curpath)); + file_catname(curpath, filename, sizeof(curpath)); return file_delete(curpath); } short -file_attr_c(const char *sjis) +file_attr_c(const char *filename) { *curfilep = '\0'; - file_catname(curpath, sjis, sizeof(curpath)); + file_catname(curpath, filename, sizeof(curpath)); return file_attr_c(curpath); } +FLISTH +file_list1st(const char *dir, FLINFO *fli) +{ + FLISTH ret; + + ret = (FLISTH)_MALLOC(sizeof(_FLISTH), "FLISTH"); + if (ret == NULL) { + VERBOSE(("file_list1st: couldn't alloc memory (size = %d)", sizeof(_FLISTH))); + return FLISTH_INVALID; + } + + milstr_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")); + _MFREE(ret); + return FLISTH_INVALID; + } + if (file_listnext((FLISTH)ret, fli) == SUCCESS) { + return (FLISTH)ret; + } + VERBOSE(("file_list1st: file_listnext failure")); + closedir(ret->hdl); + _MFREE(ret); + return FLISTH_INVALID; +} + +BOOL +file_listnext(FLISTH hdl, FLINFO *fli) +{ + char buf[MAX_PATH]; + struct dirent *de; + struct stat sb; + + de = readdir(hdl->hdl); + if (de == NULL) { + VERBOSE(("file_listnext: readdir failure")); + return FAILURE; + } + + 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); + milstr_ncpy(fli->path, de->d_name, sizeof(fli->path)); + VERBOSE(("file_listnext: success")); + return SUCCESS; +} + +void +file_listclose(FLISTH hdl) +{ + + if (hdl) { + closedir(hdl->hdl); + _MFREE(hdl); + } +} + static int euckanji1st(const char *str, int pos) { int ret; + int c; - ret = 0; - while ((pos >= 0) && (((str[pos--] - 0xa1) & 0xff) < 0x5d)) { - ret ^= 1; + for (ret = 0; pos >= 0; ret ^= 1) { + c = (UINT8)str[pos--]; + if (!ISKANJI(c)) + break; } return ret; } @@ -233,7 +319,7 @@ file_cpyname(char *dst, const char *src, { int i; - if (maxlen--) { + if (maxlen-- > 0) { for (i = 0; i < maxlen && src[i] != '\0'; i++) { dst[i] = src[i]; } @@ -247,40 +333,36 @@ file_cpyname(char *dst, const char *src, } void -file_catname(char *path, const char *sjis, int maxlen) +file_catname(char *path, const char *filename, int maxlen) { - while (maxlen) { + for (; maxlen > 0; path++, maxlen--) { if (*path == '\0') { break; } - path++; - maxlen--; } - if (maxlen) { - codecnv_sjis2euc(path, maxlen, sjis, (UINT)-1); - for (; path[0] != '\0'; path++) { - if (!ISKANJI(path[0])) { - if (path[1] == '\0') { + if (maxlen > 0) { + milstr_ncpy(path, filename, maxlen); + for (; *path != '\0'; path++) { + if (!ISKANJI(*path)) { + path++; + if (*path == '\0') { break; } - path++; - } else if ((((path[0]) - 0x41) & 0xff) < 26) { - path[0] |= 0x20; - } else if (path[0] == '\\') { - path[0] = '/'; + } else if (((*path - 0x41) & 0xff) < 26) { + *path |= 0x20; + } else if (*path == '\\') { + *path = '/'; } } } } BOOL -file_cmpname(const char *path, const char *sjis) +file_cmpname(const char *path, const char *path2) { - char euc[MAX_PATH]; - codecnv_sjis2euc(euc, sizeof(euc), sjis, (UINT)-1); - return strcmp(path, euc); + return strcmp(path, path2); } char * @@ -288,13 +370,13 @@ file_getname(char *path) { char *ret; - for (ret = path; path[0] != '\0'; path++) { - if (ISKANJI(path[0])) { - if (path[1] == '\0') { + for (ret = path; *path != '\0'; path++) { + if (ISKANJI(*path)) { + path++; + if (*path == '\0') { break; } - path++; - } else if (path[0] == '/') { + } else if (*path == '/') { ret = path + 1; } } @@ -313,16 +395,12 @@ file_cutname(char *path) char * file_getext(char *path) { - char *p; - char *q; + char *p, *q; - p = file_getname(path); - q = NULL; - while (*p != '\0') { + for (p = file_getname(path), q = NULL; *p != '\0'; p++) { if (*p == '.') { q = p + 1; } - p++; } if (q == NULL) { q = p; @@ -333,16 +411,12 @@ file_getext(char *path) void file_cutext(char *path) { - char *p; - char *q; + char *p, *q; - p = file_getname(path); - q = NULL; - while (*p != '\0') { + for (p = file_getname(path), q = NULL; *p != '\0'; p++) { if (*p == '.') { q = p; } - p++; } if (q != NULL) { *q = '\0';