/* * system.c - system interface * * Copyright (c) 2000-2024 Shiro Kawai * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * 3. Neither the name of the authors nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #define LIBGAUCHE_BODY #include "gauche.h" #include "gauche/priv/configP.h" #include "gauche/priv/bignumP.h" #include "gauche/priv/builtin-syms.h" #include "gauche/priv/fastlockP.h" #include #include #include #include #include #include #include #include #include #if !defined(GAUCHE_WINDOWS) #include #include #include #include # if !defined(HAVE_CRT_EXTERNS_H) /* POSIX defines environ, and ISO C defines __environ. Modern C seems to have the latter declared in unistd.h */ extern char **environ; # else /* HAVE_CRT_EXTERNS_H */ /* On newer OSX, we can't directly access global 'environ' variable. We need to use _NSGetEnviron(), and this header defines it. */ #include # endif /* HAVE_CRT_EXTERNS_H */ #else /* GAUCHE_WINDOWS */ #include #include /* For windows redirection; win_prepare_handles creats and returns win_redirects[3]. Each entry contains an inheritable handle for the child process' stdin, stdout and stderr, respectively, and the flag duped indicates whether the parent process must close the handle. */ typedef struct win_redirects_rec { HANDLE *h; int duped; } win_redirects; static win_redirects *win_prepare_handles(int *fds); static int win_wait_for_handles(HANDLE *handles, int nhandles, int options, int *status /*out*/); #endif /* GAUCHE_WINDOWS */ #ifdef HAVE_GLOB_H #include #endif #ifdef HAVE_SCHED_H #include #endif /* * Auxiliary system interface functions. See libsys.scm for * Scheme binding. */ /*=============================================================== * Windows specific - conversion between mbs and wcs. */ #if defined(GAUCHE_WINDOWS) && defined(UNICODE) #include "win-compat.c" WCHAR *Scm_MBS2WCS(const char *s) { return mbs2wcs(s, TRUE, Scm_Error); } const char *Scm_WCS2MBS(const WCHAR *s) { return wcs2mbs(s, TRUE, Scm_Error); } #endif /* defined(GAUCHE_WINDOWS) && defined(UNICODE) */ /*=============================================================== * OBSOLETED: Wrapper to the system call to handle signals. * Use SCM_SYSCALL_{I|P} macro instead. */ int Scm_SysCall(int r) { Scm_Warn("Obsoleted API Scm_SysCall is called."); if (r < 0 && errno == EINTR) { ScmVM *vm = Scm_VM(); errno = 0; SCM_SIGCHECK(vm); } return r; } void *Scm_PtrSysCall(void *r) { Scm_Warn("Obsoleted API Scm_PtrSysCall is called."); if (r == NULL && errno == EINTR) { ScmVM *vm = Scm_VM(); errno = 0; SCM_SIGCHECK(vm); } return r; } /* * A utility function for the procedures that accepts either port or * integer file descriptor. Returns the file descriptor. If port_or_fd * is a port that is not associated with the system file, and needfd is * true, signals error. Otherwise it returns -1. */ int Scm_GetPortFd(ScmObj port_or_fd, int needfd) { int fd = -1; if (SCM_INTP(port_or_fd)) { fd = SCM_INT_VALUE(port_or_fd); } else if (SCM_PORTP(port_or_fd)) { fd = Scm_PortFileNo(SCM_PORT(port_or_fd)); if (fd < 0 && needfd) { Scm_Error("the port is not associated with a system file descriptor: %S", port_or_fd); } } else { Scm_Error("port or small integer required, but got %S", port_or_fd); } return fd; } /*=============================================================== * Directory primitives (dirent.h) * We don't provide the iterator primitives, but a function which * reads entire directory. */ /* Returns a list of directory entries. If pathname is not a directory, or can't be opened by some reason, an error is signalled. */ ScmObj Scm_ReadDirectory(ScmString *pathname) { ScmObj head = SCM_NIL, tail = SCM_NIL; #if !defined(GAUCHE_WINDOWS) ScmVM *vm = Scm_VM(); struct dirent *dire; DIR *dirp = opendir(Scm_GetStringConst(pathname)); if (dirp == NULL) { SCM_SIGCHECK(vm); Scm_SysError("couldn't open directory %S", pathname); } while ((dire = readdir(dirp)) != NULL) { ScmObj ent = SCM_MAKE_STR_COPYING(dire->d_name); SCM_APPEND1(head, tail, ent); } SCM_SIGCHECK(vm); closedir(dirp); return head; #else /* GAUCHE_WINDOWS */ WIN32_FIND_DATA fdata; DWORD winerrno; ScmObj pattern; int pathlen = SCM_STRING_LENGTH(pathname); if (pathlen == 0) { Scm_Error("Couldn't open directory \"\""); } ScmChar lastchar = Scm_StringRef(pathname, pathlen-1, FALSE); if (lastchar == SCM_CHAR('/') || lastchar == SCM_CHAR('\\')) { pattern = Scm_StringAppendC(pathname, "*", 1, 1); } else { pattern = Scm_StringAppendC(pathname, "\\*", 2, 2); } const char *path = Scm_GetStringConst(SCM_STRING(pattern)); HANDLE dirp = FindFirstFile(SCM_MBS2WCS(path), &fdata); if (dirp == INVALID_HANDLE_VALUE) { if ((winerrno = GetLastError()) != ERROR_FILE_NOT_FOUND) goto err; return head; } const char *tpath = SCM_WCS2MBS(fdata.cFileName); SCM_APPEND1(head, tail, SCM_MAKE_STR_COPYING(tpath)); while (FindNextFile(dirp, &fdata) != 0) { tpath = SCM_WCS2MBS(fdata.cFileName); SCM_APPEND1(head, tail, SCM_MAKE_STR_COPYING(tpath)); } winerrno = GetLastError(); FindClose(dirp); if (winerrno != ERROR_NO_MORE_FILES) goto err; return head; err: Scm_Error("Searching directory failed by windows error %d", winerrno); return SCM_UNDEFINED; /* dummy */ #endif } /* getcwd compatibility layer. Some implementations of getcwd accepts NULL as buffer to allocate enough buffer memory in it, but that's not standardized and we avoid relying on it. */ ScmObj Scm_GetCwd(void) { #if defined(GAUCHE_WINDOWS)&&defined(UNICODE) # define CHAR_T wchar_t # define GETCWD _wgetcwd #else /*!(defined(GAUCHE_WINDOWS)&&defined(UNICODE))*/ # define CHAR_T char # define GETCWD getcwd #endif /*!(defined(GAUCHE_WINDOWS)&&defined(UNICODE))*/ #define GETCWD_INITIAL_BUFFER_SIZE 1024 int bufsiz = GETCWD_INITIAL_BUFFER_SIZE; CHAR_T sbuf[GETCWD_INITIAL_BUFFER_SIZE]; CHAR_T *buf = sbuf; CHAR_T *r; for (;;) { SCM_SYSCALL3(r, GETCWD(buf, bufsiz), r == NULL); if (r != NULL) break; if (errno == ERANGE) { bufsiz *= 2; buf = SCM_NEW_ATOMIC_ARRAY(CHAR_T, bufsiz); } else { Scm_SysError("getcwd failed"); } } #if defined(GAUCHE_WINDOWS) && defined(UNICODE) return Scm_MakeString(Scm_WCS2MBS(buf), -1, -1, 0); #else /*!(defined(GAUCHE_WINDOWS) && defined(UNICODE))*/ return Scm_MakeString(buf, -1, -1, SCM_STRING_COPYING); #endif /*!(defined(GAUCHE_WINDOWS) && defined(UNICODE))*/ #undef CHAR_T } /*=============================================================== * Pathname manipulation * * It gets complicated since the byte '/' and '\\' can appear in * the trailing octets of a multibyte character. * Assuming these operations won't be a bottleneck, we use simple and * straightforward code rather than tricky and fast one. */ /* Returns the system's native pathname delimiter. */ const char *Scm_PathDelimiter(void) { #if !defined(GAUCHE_WINDOWS) return "/"; #else /* GAUCHE_WINDOWS */ return "\\"; #endif /* GAUCHE_WINDOWS */ } /* On Windows, '/' is *allowed* to be an alternative separator. */ #if defined(GAUCHE_WINDOWS) #define SEPARATOR '\\' #define ROOTDIR "\\" #define SEPARATOR_P(c) ((c) == SEPARATOR || (c) == '/') #else #define SEPARATOR '/' #define ROOTDIR "/" #define SEPARATOR_P(c) ((c) == SEPARATOR) #endif /* Returns the pointer to the first path separator character, or NULL if no separator is found. */ static const char *get_first_separator(const char *path, const char *end) { const char *p = path; while (p < end) { if (SEPARATOR_P(*p)) return p; p += SCM_CHAR_NFOLLOWS(*p)+1; } return NULL; } /* Returns the pointer to the last path separator character, or NULL if no separator is found. */ static const char *get_last_separator(const char *path, const char *end) { const char *p = path, *last = NULL; while (p < end) { if (SEPARATOR_P(*p)) last = p; p += SCM_CHAR_NFOLLOWS(*p)+1; } return last; } static const char *skip_separators(const char *p, const char *end) { while (p < end) { if (!SEPARATOR_P(*p)) break; p += SCM_CHAR_NFOLLOWS(*p)+1; } return p; } /* Returns the end pointer sans trailing separators. */ static const char *truncate_trailing_separators(const char *path, const char *end) { const char *p = get_first_separator(path, end); if (p == NULL) return end; for (;;) { const char *q = skip_separators(p, end); if (q == end) return p; p = get_first_separator(q, end); if (p == NULL) return end; } } /* for keyword arguments */ static ScmObj key_absolute = SCM_FALSE; static ScmObj key_expand = SCM_FALSE; static ScmObj key_canonicalize = SCM_FALSE; ScmObj Scm_NormalizePathname(ScmString *pathname, int flags) { static ScmObj proc = SCM_UNDEFINED; SCM_BIND_PROC(proc, "sys-normalize-pathname", Scm_GaucheModule()); ScmObj h = SCM_NIL, t = SCM_NIL; SCM_APPEND1(h, t, SCM_OBJ(pathname)); if (flags & SCM_PATH_ABSOLUTE) { SCM_APPEND1(h, t, key_absolute); SCM_APPEND1(h, t, SCM_TRUE); } if (flags & SCM_PATH_CANONICALIZE) { SCM_APPEND1(h, t, key_canonicalize); SCM_APPEND1(h, t, SCM_TRUE); } if (flags & SCM_PATH_EXPAND) { SCM_APPEND1(h, t, key_expand); SCM_APPEND1(h, t, SCM_TRUE); } return Scm_ApplyRec(proc, h); } /* Returns system's temporary directory. */ ScmObj Scm_TmpDir(void) { #if defined(GAUCHE_WINDOWS) # define TMP_PATH_MAX 1024 TCHAR buf[TMP_PATH_MAX+1], *tbuf = buf; /* According to the windows document, this API checks environment variables TMP, TEMP, and USERPROFILE. Fallback is the Windows directory. */ DWORD r = GetTempPath(TMP_PATH_MAX, buf); if (r == 0) Scm_SysError("GetTempPath failed"); if (r > TMP_PATH_MAX) { tbuf = SCM_NEW_ATOMIC_ARRAY(TCHAR, r+1); DWORD r2 = GetTempPath(r, tbuf); if (r2 != r) Scm_SysError("GetTempPath failed"); } return SCM_MAKE_STR_COPYING(SCM_WCS2MBS(tbuf)); #else /*!GAUCHE_WINDOWS*/ const char *s; if ((s = Scm_GetEnv("TMPDIR")) != NULL) return SCM_MAKE_STR_COPYING(s); if ((s = Scm_GetEnv("TMP")) != NULL) return SCM_MAKE_STR_COPYING(s); else return SCM_MAKE_STR("/tmp"); /* fallback */ #endif /*!GAUCHE_WINDOWS*/ } /* Basename and dirname. On Win32, we need to treat drive names specially, e.g.: (sys-dirname "C:/a") == (sys-dirname "C:/") == (sys-dirname "C:") == "C:\\" (sys-basename "C:/") == (sys-basename "C:) == "" */ ScmObj Scm_BaseName(ScmString *filename) { ScmSmallInt size; const char *path = Scm_GetStringContent(filename, &size, NULL, NULL); #if defined(GAUCHE_WINDOWS) /* Ignore drive letter, for it can never be a part of basename. */ if (size >= 2 && path[1] == ':' && isalpha(path[0])) { path += 2; size -= 2; } #endif /* GAUCHE_WINDOWS) */ if (size == 0) return SCM_MAKE_STR(""); const char *endp = truncate_trailing_separators(path, path+size); const char *last = get_last_separator(path, endp); if (last == NULL) { return Scm_MakeString(path, (int)(endp-path), -1, 0); } else { return Scm_MakeString(last+1, (int)(endp-last-1), -1, 0); } } ScmObj Scm_DirName(ScmString *filename) { ScmSmallInt size; const char *path = Scm_GetStringContent(filename, &size, NULL, NULL); #if defined(GAUCHE_WINDOWS) int drive_letter = -1; if (size >= 2 && path[1] == ':' && isalpha(path[0])) { drive_letter = path[0]; path += 2; size -= 2; } #endif /* GAUCHE_WINDOWS */ if (size == 0) { path = NULL; goto finale; } const char *endp = truncate_trailing_separators(path, path+size); if (endp == path) { path = ROOTDIR, size = 1; goto finale; } const char *last = get_last_separator(path, endp); if (last == NULL) { path = ".", size = 1; goto finale; } /* we have "something/", and 'last' points to the last separator. */ last = truncate_trailing_separators(path, last); if (last == path) { path = ROOTDIR, size = 1; } else { size = (int)(last - path); } finale: #if defined(GAUCHE_WINDOWS) if (drive_letter > 0) { ScmObj z; char p[3] = "x:"; p[0] = (char)drive_letter; z = Scm_MakeString(p, 2, 2, SCM_MAKSTR_COPYING); if (path) { return Scm_StringAppendC(SCM_STRING(z), path, size, -1); } else { return Scm_StringAppendC(SCM_STRING(z), ROOTDIR, 1, -1); } } #endif /* GAUCHE_WINDOWS */ if (path) return Scm_MakeString(path, size, -1, 0); else return Scm_MakeString(".", 1, 1, 0); } #undef ROOTDIR #undef SEPARATOR #if !defined(HAVE_MKSTEMP) || !defined(HAVE_MKDTEMP) /* * Helper function to emulate mkstemp or mkdtemp. FUNC returns 0 on * success and non-zero otherwize. NAME is a name of operation * performed by FUNC. ARG is caller supplied data passed to FUNC. */ static void emulate_mkxtemp(char *name, char *templat, int (*func)(char *, void *), void *arg) { /* Emulate mkxtemp. */ int siz = (int)strlen(templat); if (siz < 6) { Scm_Error("%s - invalid template: %s", name, templat); } #define MKXTEMP_MAX_TRIALS 65535 /* avoid infinite loop */ { u_long seed = (u_long)time(NULL); int numtry, rv; char suffix[7]; for (numtry=0; numtry>8)&0xffffff); memcpy(templat+siz-6, suffix, 7); rv = (*func)(templat, arg); if (rv == 0) break; seed *= 2654435761UL; } if (numtry == MKXTEMP_MAX_TRIALS) { Scm_Error("%s failed", name); } } } #endif /* !defined(HAVE_MKSTEMP) || !defined(HAVE_MKDTEMP) */ #define MKXTEMP_PATH_MAX 1025 /* Geez, remove me */ static void build_template(ScmString *templat, char *name) { ScmSmallInt siz; const char *t = Scm_GetStringContent(templat, &siz, NULL, NULL); if (siz >= MKXTEMP_PATH_MAX-6) { Scm_Error("pathname too long: %S", templat); } memcpy(name, t, siz); memcpy(name + siz, "XXXXXX", 6); name[siz+6] = '\0'; } #if !defined(HAVE_MKSTEMP) static int create_tmpfile(char *templat, void *arg) { int *fdp = (int *)arg; int flags; #if defined(GAUCHE_WINDOWS) flags = O_CREAT|O_EXCL|O_WRONLY|O_BINARY; #else /* !GAUCHE_WINDOWS */ flags = O_CREAT|O_EXCL|O_WRONLY; #endif /* !GAUCHE_WINDOWS */ SCM_SYSCALL(*fdp, open(templat, flags, 0600)); return *fdp < 0; } #endif /* Make mkstemp() work even if the system doesn't have one. */ int Scm_Mkstemp(char *templat) { int fd = -1; #if defined(HAVE_MKSTEMP) SCM_SYSCALL(fd, mkstemp(templat)); if (fd < 0) Scm_SysError("mkstemp failed"); return fd; #else /*!defined(HAVE_MKSTEMP)*/ emulate_mkxtemp("mkstemp", templat, create_tmpfile, &fd); return fd; #endif /*!defined(HAVE_MKSTEMP)*/ } ScmObj Scm_SysMkstemp(ScmString *templat) { char name[MKXTEMP_PATH_MAX]; build_template(templat, name); int fd = Scm_Mkstemp(name); ScmObj sname = SCM_MAKE_STR_COPYING(name); SCM_RETURN(Scm_Values2(Scm_MakePortWithFd(sname, SCM_PORT_OUTPUT, fd, SCM_PORT_BUFFER_FULL, TRUE), sname)); } #if !defined(HAVE_MKDTEMP) static int create_tmpdir(char *templat, void *arg SCM_UNUSED) { int r; #if defined(GAUCHE_WINDOWS) SCM_SYSCALL(r, mkdir(templat)); #else /* !GAUCHE_WINDOWS */ SCM_SYSCALL(r, mkdir(templat, 0700)); #endif /* !GAUCHE_WINDOWS */ return r < 0; } #endif ScmObj Scm_SysMkdtemp(ScmString *templat) { char name[MKXTEMP_PATH_MAX]; build_template(templat, name); #if defined(HAVE_MKDTEMP) { char *p = NULL; SCM_SYSCALL3(p, mkdtemp(name), (p == NULL)); if (p == NULL) Scm_SysError("mkdtemp failed"); } #else /*!defined(HAVE_MKDTEMP)*/ emulate_mkxtemp("mkdtemp", name, create_tmpdir, NULL); #endif /*!defined(HAVE_MKDTEMP)*/ return SCM_MAKE_STR_COPYING(name); } /*=============================================================== * Stat (sys/stat.h) */ static ScmObj stat_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED) { return SCM_OBJ(SCM_NEW_INSTANCE(ScmSysStat, klass)); } static ScmSmallInt stat_hash(ScmObj obj, ScmSmallInt salt, u_long flags) { ScmStat *s = SCM_SYS_STAT_STAT(obj); ScmSmallInt h = salt; #define STAT_HASH_UI(name) \ h = Scm_CombineHashValue(Scm_SmallIntHash((ScmSmallInt)s->SCM_CPP_CAT(st_, name), \ salt, flags), h) #define STAT_HASH_TIME(name) \ h = Scm_CombineHashValue(Scm_Int64Hash((int64_t)s->SCM_CPP_CAT(st_, name), \ salt, flags), h) #define STAT_HASH_TIMESPEC(name) \ h = Scm_CombineHashValue(Scm_Int64Hash((int64_t)s->SCM_CPP_CAT(st_, name).tv_sec, \ salt, flags), \ Scm_CombineHashValue(Scm_Int64Hash((int64_t)s->SCM_CPP_CAT(st_, name).tv_nsec, \ salt, flags),h)) STAT_HASH_UI(mode); STAT_HASH_UI(ino); STAT_HASH_UI(dev); STAT_HASH_UI(rdev); STAT_HASH_UI(nlink); STAT_HASH_UI(uid); STAT_HASH_UI(gid); #if HAVE_STRUCT_STAT_ST_ATIM STAT_HASH_TIMESPEC(atim); #else STAT_HASH_TIME(atime); #endif #if HAVE_STRUCT_STAT_ST_MTIM STAT_HASH_TIMESPEC(mtim); #else STAT_HASH_TIME(mtime); #endif #if HAVE_STRUCT_STAT_ST_CTIM STAT_HASH_TIMESPEC(ctim); #else STAT_HASH_TIME(ctime); #endif return h; } SCM_DEFINE_BUILTIN_CLASS(Scm_SysStatClass, NULL, NULL, stat_hash, stat_allocate, SCM_CLASS_DEFAULT_CPL); ScmObj Scm_MakeSysStat(void) { return stat_allocate(&Scm_SysStatClass, SCM_NIL); } static ScmObj stat_type_get(ScmSysStat *stat) { if (S_ISDIR(SCM_SYS_STAT_STAT(stat)->st_mode)) return (SCM_SYM_DIRECTORY); if (S_ISREG(SCM_SYS_STAT_STAT(stat)->st_mode)) return (SCM_SYM_REGULAR); if (S_ISCHR(SCM_SYS_STAT_STAT(stat)->st_mode)) return (SCM_SYM_CHARACTER); if (S_ISBLK(SCM_SYS_STAT_STAT(stat)->st_mode)) return (SCM_SYM_BLOCK); if (S_ISFIFO(SCM_SYS_STAT_STAT(stat)->st_mode)) return (SCM_SYM_FIFO); #ifdef S_ISLNK if (S_ISLNK(SCM_SYS_STAT_STAT(stat)->st_mode)) return (SCM_SYM_SYMLINK); #endif #ifdef S_ISSOCK if (S_ISSOCK(SCM_SYS_STAT_STAT(stat)->st_mode)) return (SCM_SYM_SOCKET); #endif return (SCM_FALSE); } static ScmObj stat_perm_get(ScmSysStat *stat) { return Scm_MakeIntegerFromUI(SCM_SYS_STAT_STAT(stat)->st_mode & 0777); } static ScmObj stat_size_get(ScmSysStat *stat) { return Scm_OffsetToInteger(SCM_SYS_STAT_STAT(stat)->st_size); } #define STAT_GETTER_UI(name) \ static ScmObj SCM_CPP_CAT3(stat_, name, _get)(ScmSysStat *s) \ { return Scm_MakeIntegerFromUI((u_long)(SCM_SYS_STAT_STAT(s)->SCM_CPP_CAT(st_, name))); } #define STAT_GETTER_TIME(name) \ static ScmObj SCM_CPP_CAT3(stat_, name, _get)(ScmSysStat *s) \ { return Scm_MakeSysTime(SCM_SYS_STAT_STAT(s)->SCM_CPP_CAT(st_, name)); } STAT_GETTER_UI(mode) STAT_GETTER_UI(ino) STAT_GETTER_UI(dev) STAT_GETTER_UI(rdev) STAT_GETTER_UI(nlink) STAT_GETTER_UI(uid) STAT_GETTER_UI(gid) STAT_GETTER_TIME(atime) STAT_GETTER_TIME(mtime) STAT_GETTER_TIME(ctime) static ScmObj stat_atim_get(ScmSysStat *s) { #if HAVE_STRUCT_STAT_ST_ATIM return Scm_MakeTime64(SCM_SYM_TIME_UTC, (int64_t)s->statrec.st_atim.tv_sec, s->statrec.st_atim.tv_nsec); #else return Scm_MakeTime64(SCM_SYM_TIME_UTC, (int64_t)s->statrec.st_atime, 0); #endif } static ScmObj stat_mtim_get(ScmSysStat *s) { #if HAVE_STRUCT_STAT_ST_MTIM return Scm_MakeTime64(SCM_SYM_TIME_UTC, (int64_t)s->statrec.st_mtim.tv_sec, s->statrec.st_mtim.tv_nsec); #else return Scm_MakeTime64(SCM_SYM_TIME_UTC, (int64_t)s->statrec.st_mtime, 0); #endif } static ScmObj stat_ctim_get(ScmSysStat *s) { #if HAVE_STRUCT_STAT_ST_CTIM return Scm_MakeTime64(SCM_SYM_TIME_UTC, (int64_t)s->statrec.st_ctim.tv_sec, s->statrec.st_ctim.tv_nsec); #else return Scm_MakeTime64(SCM_SYM_TIME_UTC, (int64_t)s->statrec.st_ctime, 0); #endif } static ScmClassStaticSlotSpec stat_slots[] = { SCM_CLASS_SLOT_SPEC("type", stat_type_get, NULL), SCM_CLASS_SLOT_SPEC("perm", stat_perm_get, NULL), SCM_CLASS_SLOT_SPEC("mode", stat_mode_get, NULL), SCM_CLASS_SLOT_SPEC("ino", stat_ino_get, NULL), SCM_CLASS_SLOT_SPEC("dev", stat_dev_get, NULL), SCM_CLASS_SLOT_SPEC("rdev", stat_rdev_get, NULL), SCM_CLASS_SLOT_SPEC("nlink", stat_nlink_get, NULL), SCM_CLASS_SLOT_SPEC("uid", stat_uid_get, NULL), SCM_CLASS_SLOT_SPEC("gid", stat_gid_get, NULL), SCM_CLASS_SLOT_SPEC("size", stat_size_get, NULL), SCM_CLASS_SLOT_SPEC("atime", stat_atime_get, NULL), SCM_CLASS_SLOT_SPEC("mtime", stat_mtime_get, NULL), SCM_CLASS_SLOT_SPEC("ctime", stat_ctime_get, NULL), SCM_CLASS_SLOT_SPEC("atim", stat_atim_get, NULL), SCM_CLASS_SLOT_SPEC("mtim", stat_mtim_get, NULL), SCM_CLASS_SLOT_SPEC("ctim", stat_ctim_get, NULL), SCM_CLASS_SLOT_SPEC_END() }; /*=============================================================== * Time (sys/time.h and time.h) */ /* Gauche has two notion of time. A simple number is used by the low-level * system interface (sys-time, sys-gettimeofday). An object of