From 201d9dba0a1ae222140e243cadec22640a67772e Mon Sep 17 00:00:00 2001 From: Nishi Date: Mon, 22 Jun 2026 05:10:51 +0900 Subject: [PATCH] Make SJ4 more portable --- include/sj4common/sj4compat.h | 8 +++ include/sj4core/Struct.h | 2 - include/sj4core/sj_kanakan.h | 8 --- include/sj4lib/sj4lib.h | 8 +++ lib/sj4common/ftruncate.c | 9 ++++ lib/sj4common/unlink.c | 5 ++ lib/sj4core/sdepend.c | 94 ++++++++--------------------------- 7 files changed, 50 insertions(+), 84 deletions(-) create mode 100644 lib/sj4common/ftruncate.c create mode 100644 lib/sj4common/unlink.c diff --git a/include/sj4common/sj4compat.h b/include/sj4common/sj4compat.h index 4f8d05d..a078b6e 100644 --- a/include/sj4common/sj4compat.h +++ b/include/sj4common/sj4compat.h @@ -5,7 +5,15 @@ #include #include +#ifdef _WIN32 +#include +#else +#include +#endif + int sj4_snprintf(char*, size_t, const char*, ...); size_t sj4_strlcpy(char*, const char*, size_t); +int sj4_ftruncate(FILE*, size_t); +int sj4_unlink(const char*); #endif diff --git a/include/sj4core/Struct.h b/include/sj4core/Struct.h index 136a77c..baad14b 100644 --- a/include/sj4core/Struct.h +++ b/include/sj4core/Struct.h @@ -126,7 +126,6 @@ typedef struct dictfile { #endif FILE* fp; VFile vf; - int fd; u_char* buffer; long bufsiz; @@ -144,7 +143,6 @@ typedef struct stdyfile { int refcnt; ino_t inode; FILE* fp; - int fd; u_char* header; diff --git a/include/sj4core/sj_kanakan.h b/include/sj4core/sj_kanakan.h index 28de558..e45e62d 100644 --- a/include/sj4core/sj_kanakan.h +++ b/include/sj4core/sj_kanakan.h @@ -22,11 +22,6 @@ #include #include #include -#ifdef _WIN32 -#include -#else -#include -#endif #include "sj_kcnv.h" #include "sj_struct.h" @@ -235,9 +230,6 @@ extern u_char kigou[]; /* sdepend.c */ DictFile* opendict(SJ4_CONTEXT char*, char*); int closedict(SJ4_CONTEXT DictFile*); -void lock_dict(DictFile*, int); -void unlock_dict(DictFile*, int); -int is_dict_locked(DictFile*); StdyFile* openstdy(SJ4_CONTEXT char*, char*); int closestdy(SJ4_CONTEXT StdyFile*); int putstydic(SJ4_CONTEXT2); diff --git a/include/sj4lib/sj4lib.h b/include/sj4lib/sj4lib.h index 70e1a59..3ae3169 100644 --- a/include/sj4lib/sj4lib.h +++ b/include/sj4lib/sj4lib.h @@ -6,6 +6,10 @@ /* increase this as needed... */ #define SJ4BUFSZ (4 * 1024) +#ifdef __cplusplus +extern "C" { +#endif + typedef union sj4kouhobuffer { char sjis[SJ4BUFSZ]; char eucjp[SJ4BUFSZ]; @@ -34,4 +38,8 @@ int sj4_getkan(Sj4Lib*, const void*, int len, Sj4Kouho*); int sj4_nextkan(Sj4Lib*); void sj4_close(Sj4Lib*); +#ifdef __cplusplus +} +#endif + #endif diff --git a/lib/sj4common/ftruncate.c b/lib/sj4common/ftruncate.c new file mode 100644 index 0000000..7b756da --- /dev/null +++ b/lib/sj4common/ftruncate.c @@ -0,0 +1,9 @@ +#include "sj4compat.h" + +int sj4_ftruncate(FILE* fp, size_t sz) { +#ifdef _WIN32 + return chsize(fileno(fp), sz); +#else + return ftruncate(fileno(fp), sz); +#endif +} diff --git a/lib/sj4common/unlink.c b/lib/sj4common/unlink.c new file mode 100644 index 0000000..c589012 --- /dev/null +++ b/lib/sj4common/unlink.c @@ -0,0 +1,5 @@ +#include "sj4compat.h" + +int sj4_unlink(const char* path) { + return unlink(path); +} diff --git a/lib/sj4core/sdepend.c b/lib/sj4core/sdepend.c index e06cb93..63a2bc6 100644 --- a/lib/sj4core/sdepend.c +++ b/lib/sj4core/sdepend.c @@ -56,10 +56,6 @@ #define L_END SEEK_END #endif -#ifdef _WIN32 -#define ftruncate chsize -#endif - #ifdef SJ4_GLOBAL DictFile* dictlink = NULL; StdyFile* stdylink = NULL; @@ -129,30 +125,6 @@ fputfile(FILE* fp, long pos, int len, u_char* p) { } return SJ4_NormalEnd; } -static int -getfile(int fd, int pos, int len, u_char* p) { - if(lseek(fd, pos, L_SET) == ERROR) { - serv_errno = SJ4_FileSeekError; - return ERROR; - } - if(read(fd, p, len) != len) { - serv_errno = SJ4_FileReadError; - return ERROR; - } - return SJ4_NormalEnd; -} -static int -putfile(int fd, int pos, int len, u_char* p) { - if(lseek(fd, pos, L_SET) == ERROR) { - serv_errno = SJ4_FileSeekError; - return ERROR; - } - if(write(fd, p, len) != len) { - serv_errno = SJ4_FileWriteError; - return ERROR; - } - return SJ4_NormalEnd; -} static int check_passwd(u_char* buf, char* passwd) { @@ -197,7 +169,7 @@ getdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) { } static int putidx(SJ4_CONTEXT DictFile* dp, short dummy) { - return putfile(dp->fd, dp->idxstrt, dp->dict.idxlen, idxbuf); + return fputfile(dp->fp, dp->idxstrt, dp->dict.idxlen, idxbuf); } static int putdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) { @@ -224,7 +196,7 @@ putdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) { i = dp->segstrt + dp->dict.seglen * seg; if((p = dp->buffer + i) != dicbuf) memcpy(p, dicbuf, dp->dict.seglen); - return putfile(dp->fd, i, dp->dict.seglen, p); + return fputfile(dp->fp, i, dp->dict.seglen, p); } static int rszdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) { @@ -245,10 +217,10 @@ rszdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) { free((char*)(dp->buffer)); dp->buffer = p; dp->bufsiz = i; - if(ftruncate(dp->fd, i) == -1) return -1; + if(sj4_ftruncate(dp->fp, i) == -1) return -1; } put4byte(dp->buffer + DICTSEGNUM, seg); - return putfile(dp->fd, 0, HEADERLENGTH, dp->buffer); + return fputfile(dp->fp, 0, HEADERLENGTH, dp->buffer); } DictFile* @@ -335,7 +307,6 @@ opendict(SJ4_CONTEXT char* name, char* passwd) { dfp->refcnt = 1; dfp->fp = fp; dfp->vf = vf; - dfp->fd = vf.buffer == NULL ? fileno(fp) : -1; dfp->buffer = dp; dfp->bufsiz = sbuf.st_size; dfp->idxstrt = get4byte(dp + DICTIDXPOS); @@ -389,30 +360,6 @@ int closedict(SJ4_CONTEXT DictFile* dfp) { return 0; } -#ifdef SJ4_LOCK -static fd_set zero_fd_set; /* XXX */ -#endif - -void lock_dict(DictFile* p, int fd) { -#ifdef SJ4_LOCK - FD_SET(fd, &(p->lock)); -#endif -} - -void unlock_dict(DictFile* p, int fd) { -#ifdef SJ4_LOCK - FD_CLR(fd, &(p->lock)); -#endif -} - -int is_dict_locked(DictFile* p) { -#ifdef SJ4_LOCK - return memcmp(&(p->lock), &zero_fd_set, sizeof(zero_fd_set)); -#else - return 0; -#endif -} - static int check_stdyfile(u_char* buf) { return (STDYVERSION != get4byte(buf + VERSIONPOS)) ? FALSE : TRUE; @@ -521,7 +468,6 @@ openstdy(SJ4_CONTEXT char* name, char* passwd) { sfp->refcnt = 1; sfp->inode = sbuf.st_ino; sfp->fp = fp; - sfp->fd = fileno(fp); sfp->header = hd; sfp->link = stdylink; @@ -572,13 +518,13 @@ int closestdy(SJ4_CONTEXT StdyFile* sfp) { } int putstydic(SJ4_CONTEXT2) { - int fd; + FILE* fp; u_char* hd; StdyFile* sf; long len; sf = (StdyFile*)stdy_base; - fd = sf->fd; + fp = sf->fp; hd = sf->header; put4byte(hd + STDYNORMCNT, sf->stdy.stdycnt); @@ -586,25 +532,25 @@ int putstydic(SJ4_CONTEXT2) { len = sizeof(STDYIN) * sf->stdy.stdymax; put4byte(hd + STDYNORMLEN, len); - if(putfile(fd, 0, HEADERLENGTH + COMMENTLENGTH, hd)) return ERROR; + if(fputfile(fp, 0, HEADERLENGTH + COMMENTLENGTH, hd)) return ERROR; - return putfile(fd, get4byte(hd + STDYNORMPOS), len, (u_char*)sf->stdy.stdydic); + return fputfile(fp, get4byte(hd + STDYNORMPOS), len, (u_char*)sf->stdy.stdydic); } int putcldic(SJ4_CONTEXT2) { - int fd; + FILE* fp; u_char* hd; StdyFile* sf; sf = (StdyFile*)stdy_base; - fd = sf->fd; + fp = sf->fp; hd = sf->header; - if(putfile(fd, get4byte(hd + STDYCLIDXPOS), - get4byte(hd + STDYCLIDXLEN), (u_char*)sf->stdy.clstdyidx)) /* XXX */ + if(fputfile(fp, get4byte(hd + STDYCLIDXPOS), + get4byte(hd + STDYCLIDXLEN), (u_char*)sf->stdy.clstdyidx)) /* XXX */ return ERROR; - return putfile(fd, get4byte(hd + STDYCLSEGPOS), - get4byte(hd + STDYCLSEGLEN), sf->stdy.clstdydic); + return fputfile(fp, get4byte(hd + STDYCLSEGPOS), + get4byte(hd + STDYCLSEGLEN), sf->stdy.clstdydic); } int makedict(char* path, int idxlen, int seglen, int segnum) { @@ -661,7 +607,7 @@ int makedict(char* path, int idxlen, int seglen, int segnum) { ret = SJ4_NormalEnd; error: fclose(fp); - if(ret != SJ4_NormalEnd) unlink(path); + if(ret != SJ4_NormalEnd) sj4_unlink(path); return ret; } @@ -741,7 +687,7 @@ int makestdy(char* path, int stynum, int clstep, int cllen) { ret = SJ4_NormalEnd; error: fclose(fp); - if(ret != SJ4_NormalEnd) unlink(path); + if(ret != SJ4_NormalEnd) sj4_unlink(path); return ret; } @@ -752,7 +698,7 @@ void sj_closeall(SJ4_CONTEXT2) { int set_dictpass(DictFile* dp, char* pass) { set_passwd(dp->buffer, pass); - return putfile(dp->fd, 0, HEADERLENGTH + COMMENTLENGTH, dp->buffer); + return fputfile(dp->fp, 0, HEADERLENGTH + COMMENTLENGTH, dp->buffer); } int set_stdypass(SJ4_CONTEXT char* pass) { @@ -760,7 +706,7 @@ int set_stdypass(SJ4_CONTEXT char* pass) { sp = (StdyFile*)stdy_base; set_passwd(sp->header, pass); - return putfile(sp->fd, 0, HEADERLENGTH + COMMENTLENGTH, sp->header); + return fputfile(sp->fp, 0, HEADERLENGTH + COMMENTLENGTH, sp->header); } static void @@ -770,7 +716,7 @@ set_comment(u_char* buf, char* comment) { int set_dictcmnt(DictFile* dp, char* cmnt) { set_comment(dp->buffer, cmnt); - return putfile(dp->fd, 0, HEADERLENGTH + COMMENTLENGTH, dp->buffer); + return fputfile(dp->fp, 0, HEADERLENGTH + COMMENTLENGTH, dp->buffer); } int set_stdycmnt(SJ4_CONTEXT char* cmnt) { @@ -778,7 +724,7 @@ int set_stdycmnt(SJ4_CONTEXT char* cmnt) { sp = (StdyFile*)stdy_base; set_comment(sp->header, cmnt); - return putfile(sp->fd, 0, HEADERLENGTH + COMMENTLENGTH, sp->header); + return fputfile(sp->fp, 0, HEADERLENGTH + COMMENTLENGTH, sp->header); } int get_stdysize(SJ4_CONTEXT int* stynum, int* clstep, int* cllen) {