summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-08-30 10:10:48 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-08-30 10:10:48 -0700
commite4ef51fb143fbd971d94b2f2f92683627dbabc60 (patch)
treec8302b4abf5610a3cd273ebae3233165647f2f67
parent8dbbc8c2f56e84e9cff97188dc5ad832660d3cc8 (diff)
downloadtxr-e4ef51fb143fbd971d94b2f2f92683627dbabc60.tar.gz
txr-e4ef51fb143fbd971d94b2f2f92683627dbabc60.tar.bz2
txr-e4ef51fb143fbd971d94b2f2f92683627dbabc60.zip
Move stat functions to use a struct.
* lib.c (init): Move hash_init and struct init before sysif_init. * share/txr/stdlib/path-test.tl (sys:path-test-mode, path-mine-p, path-my-group-p, sys:path-access, path-newer, path-examine): Convert to struct return of stat functions. * stream.c (dev_k, ino_k, mode_k, nlink_k, uid_k, gid_k, rdev_k, size_k, blksize_k, blocks_k, atime_k, mtime_k, ctime_k): Global variable definitions removed from here. (stream_init): Initializations of moved global variables removed from here. * stream.h (dev_k, ino_k, mode_k, nlink_k, uid_k, gid_k, rdev_k, size_k, blksize_k, blocks_k, atime_k, mtime_k, ctime_k): Declarations removed from here. * sysif.c (stat_s, dev_s, ino_s, mode_s, nlink_s, uid_s, gid_s, rdev_s, size_s, blksize_s, blocks_s, atime_s, mtime_s, ctime_s): New global variables. (dev_k, ino_k, mode_k, nlink_k, uid_k, gid_k, rdev_k, size_k, blksize_k, blocks_k, atime_k, mtime_k, ctime_k): Existing variables moved here. (stat_to_struct): New static function. (stat_impl): Use stat_to_struct, except under 113 compatibility. (sysif_init): Initialize new symbol variables. Make stat struct type. * sysif.h (stat_s, dev_s, ino_s, mode_s, nlink_s, uid_s, gid_s, rdev_s, size_s, blksize_s, blocks_s, atime_s, mtime_s, ctime_s): Declared.
-rw-r--r--lib.c4
-rw-r--r--share/txr/stdlib/path-test.tl24
-rw-r--r--stream.c16
-rw-r--r--stream.h3
-rw-r--r--sysif.c77
-rw-r--r--sysif.h8
6 files changed, 95 insertions, 37 deletions
diff --git a/lib.c b/lib.c
index 8b41696f..fde73899 100644
--- a/lib.c
+++ b/lib.c
@@ -7643,6 +7643,8 @@ void init(const wchar_t *pn, mem_t *(*oom)(mem_t *, size_t),
obj_init();
uw_init();
eval_init();
+ hash_init();
+ struct_init();
sysif_init();
arith_init();
rand_init();
@@ -7651,7 +7653,6 @@ void init(const wchar_t *pn, mem_t *(*oom)(mem_t *, size_t),
sig_init();
#endif
filter_init();
- hash_init();
regex_init();
gc_late_init();
parse_init();
@@ -7664,7 +7665,6 @@ void init(const wchar_t *pn, mem_t *(*oom)(mem_t *, size_t),
glob_init();
#endif
cadr_init();
- struct_init();
gc_state(gc_save);
}
diff --git a/share/txr/stdlib/path-test.tl b/share/txr/stdlib/path-test.tl
index 1ca8d940..c7e825ed 100644
--- a/share/txr/stdlib/path-test.tl
+++ b/share/txr/stdlib/path-test.tl
@@ -7,8 +7,7 @@
(defun sys:path-test-mode (statfun path mask)
(sys:path-test (s statfun path)
- (let ((m (prop s :mode)))
- (if (plusp (logand m mask)) t))))
+ (plusp (logand s.mode mask))))
(defun path-exists-p (path)
(sys:path-test (s stat path) t))
@@ -45,24 +44,23 @@
(defun path-mine-p (path)
(sys:path-test (s stat path)
- (let ((u (prop s :uid)))
- (= u (geteuid)))))
+ (= s.uid (geteuid))))
(defun path-my-group-p (path)
(sys:path-test (s stat path)
- (let ((g (prop s :gid)))
+ (let ((g s.gid))
(or (= g (getegid))
(find g (getgroups))))))
(defun sys:path-access (path umask gmask omask)
(sys:path-test (s stat path)
- (let ((m (prop s :mode))
+ (let ((m s.mode)
(euid (geteuid)))
(cond
((zerop euid) (or (zerop (logior umask s-ixusr))
(plusp (logand m (logior umask gmask omask)))))
- ((= euid (prop s :uid)) (plusp (logand m umask)))
- ((let ((g (prop s :gid)))
+ ((= euid s.uid) (plusp (logand m umask)))
+ ((let ((g s.gid))
(or (= g (getegid))
(find g (getgroups))))
(plusp (logand m gmask)))
@@ -81,9 +79,7 @@
(defun path-newer (path-0 path-1)
(sys:path-examine (s0 stat path-0)
(sys:path-examine (s1 stat path-1)
- (and s0 (or (not s1)
- (> (prop s0 :mtime)
- (prop s1 :mtime)))))))
+ (and s0 (or (not s1) (> s0.mtime s1.mtime))))))
(defun path-older (path-0 path-1)
(path-newer path-1 path-0))
@@ -92,7 +88,5 @@
(sys:path-examine (s0 stat path-0)
(sys:path-examine (s1 stat path-1)
(and s0 s1
- (eql (prop s0 :dev)
- (prop s1 :dev))
- (eql (prop s0 :ino)
- (prop s1 :ino))))))
+ (eql s0.dev s1.dev)
+ (eql s0.ino s1.ino)))))
diff --git a/stream.c b/stream.c
index fe250f70..85b356b2 100644
--- a/stream.c
+++ b/stream.c
@@ -64,9 +64,6 @@
val stdin_s, stdout_s, stddebug_s, stderr_s, stdnull_s;
-val dev_k, ino_k, mode_k, nlink_k, uid_k;
-val gid_k, rdev_k, size_k, blksize_k, blocks_k;
-val atime_k, mtime_k, ctime_k;
val from_start_k, from_current_k, from_end_k;
val real_time_k, name_k, fd_k;
val format_s;
@@ -3213,19 +3210,6 @@ void stream_init(void)
detect_format_string();
- dev_k = intern(lit("dev"), keyword_package);
- ino_k = intern(lit("ino"), keyword_package);
- mode_k = intern(lit("mode"), keyword_package);
- nlink_k = intern(lit("nlink"), keyword_package);
- uid_k = intern(lit("uid"), keyword_package);
- gid_k = intern(lit("gid"), keyword_package);
- rdev_k = intern(lit("rdev"), keyword_package);
- size_k = intern(lit("size"), keyword_package);
- blksize_k = intern(lit("blksize"), keyword_package);
- blocks_k = intern(lit("blocks"), keyword_package);
- atime_k = intern(lit("atime"), keyword_package);
- mtime_k = intern(lit("mtime"), keyword_package);
- ctime_k = intern(lit("ctime"), keyword_package);
from_start_k = intern(lit("from-start"), keyword_package);
from_current_k = intern(lit("from-current"), keyword_package);
from_end_k = intern(lit("from-end"), keyword_package);
diff --git a/stream.h b/stream.h
index 6dcbdd05..e5d58d26 100644
--- a/stream.h
+++ b/stream.h
@@ -83,9 +83,6 @@ struct strm_ops {
#define std_null (deref(lookup_var_l(nil, stdnull_s)))
loc lookup_var_l(val env, val sym);
-extern val dev_k, ino_k, mode_k, nlink_k, uid_k;
-extern val gid_k, rdev_k, size_k, blksize_k, blocks_k;
-extern val atime_k, mtime_k, ctime_k;
extern val from_start_k, from_current_k, from_end_k;
extern val real_time_k, name_k, fd_k;
extern val format_s;
diff --git a/sysif.c b/sysif.c
index 355eecd6..4abf5ac5 100644
--- a/sysif.c
+++ b/sysif.c
@@ -24,6 +24,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -55,6 +56,10 @@
#if HAVE_POLL
#include <poll.h>
#endif
+#if HAVE_PWUID
+#include <pwd.h>
+#endif
+#include ALLOCA_H
#include "lib.h"
#include "stream.h"
#include "hash.h"
@@ -63,8 +68,19 @@
#include "unwind.h"
#include "gc.h"
#include "eval.h"
+#include "args.h"
+#include "struct.h"
+#include "txr.h"
#include "sysif.h"
+val stat_s;
+val dev_k, ino_k, mode_k, nlink_k, uid_k;
+val gid_k, rdev_k, size_k, blksize_k, blocks_k;
+val atime_k, mtime_k, ctime_k;
+val dev_s, ino_s, mode_s, nlink_s, uid_s;
+val gid_s, rdev_s, size_s, blksize_s, blocks_s;
+val atime_s, mtime_s, ctime_s;
+
static val errno_wrap(val newval)
{
val oldval = num(errno);
@@ -578,6 +594,31 @@ static val stat_to_list(struct stat st)
nao);
}
+static val stat_to_struct(struct stat st)
+{
+ args_decl(args, ARGS_MIN);
+ val strct = make_struct(stat_s, nil, args);
+ slotset(strct, dev_s, num(st.st_dev));
+ slotset(strct, ino_s, num(st.st_ino));
+ slotset(strct, mode_s, num(st.st_mode));
+ slotset(strct, nlink_s, num(st.st_nlink));
+ slotset(strct, uid_s, num(st.st_uid));
+ slotset(strct, gid_s, num(st.st_gid));
+ slotset(strct, rdev_s, num(st.st_rdev));
+ slotset(strct, size_s, num(st.st_size));
+#if !HAVE_WINDOWS_H
+ slotset(strct, blksize_s, num(st.st_blksize));
+ slotset(strct, blocks_s, num(st.st_blocks));
+#else
+ slotset(strct, blksize_s, zero);
+ slotset(strct, blocks_s, zero);
+#endif
+ slotset(strct, atime_s, num(st.st_atime));
+ slotset(strct, mtime_s, num(st.st_mtime));
+ slotset(strct, ctime_s, num(st.st_ctime));
+
+ return strct;
+}
#endif
static val stat_impl(val obj, int (*statfn)(val, struct stat *),
@@ -591,7 +632,8 @@ static val stat_impl(val obj, int (*statfn)(val, struct stat *),
uw_throwf(file_error_s, lit("unable to ~a ~a: ~a/~s"),
name, obj, num(errno), string_utf8(strerror(errno)), nao);
- return stat_to_list(st);
+ return if3(opt_compat && opt_compat <= 113,
+ stat_to_list(st), stat_to_struct(st));
#else
uw_throwf(file_error_s, lit("~a is not implemented"), name, nao);
#endif
@@ -810,6 +852,39 @@ static val setegid_wrap(val nval)
void sysif_init(void)
{
+ stat_s = intern(lit("stat"), user_package);
+ dev_k = intern(lit("dev"), keyword_package);
+ ino_k = intern(lit("ino"), keyword_package);
+ mode_k = intern(lit("mode"), keyword_package);
+ nlink_k = intern(lit("nlink"), keyword_package);
+ uid_k = intern(lit("uid"), keyword_package);
+ gid_k = intern(lit("gid"), keyword_package);
+ rdev_k = intern(lit("rdev"), keyword_package);
+ size_k = intern(lit("size"), keyword_package);
+ blksize_k = intern(lit("blksize"), keyword_package);
+ blocks_k = intern(lit("blocks"), keyword_package);
+ atime_k = intern(lit("atime"), keyword_package);
+ mtime_k = intern(lit("mtime"), keyword_package);
+ ctime_k = intern(lit("ctime"), keyword_package);
+ dev_s = intern(lit("dev"), user_package);
+ ino_s = intern(lit("ino"), user_package);
+ mode_s = intern(lit("mode"), user_package);
+ nlink_s = intern(lit("nlink"), user_package);
+ uid_s = intern(lit("uid"), user_package);
+ gid_s = intern(lit("gid"), user_package);
+ rdev_s = intern(lit("rdev"), user_package);
+ size_s = intern(lit("size"), user_package);
+ blksize_s = intern(lit("blksize"), user_package);
+ blocks_s = intern(lit("blocks"), user_package);
+ atime_s = intern(lit("atime"), user_package);
+ mtime_s = intern(lit("mtime"), user_package);
+ ctime_s = intern(lit("ctime"), user_package);
+
+ make_struct_type(stat_s, nil,
+ list(dev_s, ino_s, mode_s, nlink_s, uid_s, gid_s,
+ rdev_s, size_s, blksize_s, blocks_s, atime_s,
+ mtime_s, ctime_s, nao), nil, nil);
+
reg_fun(intern(lit("errno"), user_package), func_n1o(errno_wrap, 0));
reg_fun(intern(lit("exit"), user_package), func_n1(exit_wrap));
reg_fun(intern(lit("abort"), user_package), func_n0(abort_wrap));
diff --git a/sysif.h b/sysif.h
index 034d3911..c54ddbad 100644
--- a/sysif.h
+++ b/sysif.h
@@ -24,4 +24,12 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+extern val stat_s;
+extern val dev_k, ino_k, mode_k, nlink_k, uid_k;
+extern val gid_k, rdev_k, size_k, blksize_k, blocks_k;
+extern val atime_k, mtime_k, ctime_k;
+extern val dev_s, ino_s, mode_s, nlink_s, uid_s;
+extern val gid_s, rdev_s, size_s, blksize_s, blocks_s;
+extern val atime_s, mtime_s, ctime_s;
+
void sysif_init(void);