Skip to content

Commit 99c8dda

Browse files
author
llmII
committed
Fixes to sys, csys, drop chdir, add fileno
Adding fileno, so that dup2 is actually usable. Dropping chdir, exists as `os/cd`. Fixed all things needing an integer fd to use a helper that'll do the work associated with validation that an abstract is indeed a file. FossilOrigin-Name: 0f46f00d4cecaa414b737e3bcc2ed6dc4241fb22dd6d5cff2ad2b6e728e4736f
1 parent a402ffb commit 99c8dda

File tree

3 files changed

+98
-94
lines changed

3 files changed

+98
-94
lines changed

README.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,36 @@
22

33
#### Purpose
44

5-
Provide exported definitions of system level functions for multiple operating systems.
5+
Provide exported definitions of system level functions for multiple operating
6+
systems.
67

7-
Where sensible provide a unified interface to facilitate ease of use, but allow for
8-
direct usage of system level functions for when a user's needs lay outside the bounds
9-
of predetermination.
8+
Where sensible provide a unified interface to facilitate ease of use, but
9+
allow for direct usage of system level functions for when a user's needs lay
10+
outside the bounds of predetermination.
1011

1112
#### Installation
12-
With `jpm` you can now install this with `jpm install https://github.com/llmII/jsys`
13+
With `jpm` you can now install this with:
14+
15+
`jpm install https://github.com/llmII/jsys`
16+
1317
and import the module. Nothing's been tested so though it loads, and shows
1418
documentation for each export, expect that things may not work as specified!
1519

1620
**NOTE:**
1721
Nothing has been tested at this time.
1822

19-
If there are issues, file a Issue [here](https://github.com/llmII/jsys/issues) or a
20-
Ticket [here](https://code.amlegion.org/jsys/ticket).
23+
If there are issues, file a Issue [here](https://github.com/llmII/jsys/issues)
24+
or a Ticket [here](https://code.amlegion.org/jsys/ticket).
2125

22-
Want another system specific function? File a Ticket/Issue and/or submit a pull request
23-
or email the author a patch. Author may consider implementing a system specific
24-
function on behalf of the submitter of a ticket under a few conditions.
26+
Want another system specific function? File a Ticket/Issue and/or submit a
27+
pull request or email the author a patch. Author may consider implementing a
28+
system specific function on behalf of the submitter of a ticket under a few
29+
conditions.
2530

26-
One condition to make note of is the `flock` family of functions. Those won't be
27-
implemented for *nix systems due to the greater capability of `fnctl`.
31+
One condition to make note of is the `flock` family of functions. Those won't
32+
be implemented for *nix systems due to the greater capability of `fnctl`.
2833

2934
To sum the conditions up:
30-
1. No additional function that serves the same purpose as an already existing function
31-
except to replace the former wherein the function provides greater capability except
32-
where doing so enhances portability to certain systems.
35+
1. No additional function that serves the same purpose as an already existing
36+
function except to replace the former wherein the function provides greater
37+
capability except where doing so enhances portability to certain systems.

src/csys.c

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@
5050
* great in the sys/unified module */
5151
#ifndef JANET_WINDOWS
5252
#include <errno.h> /* errno */
53-
#include <unistd.h> /* chdir(2) chown(2) chroot(2) dup2(2) fork(2)
53+
#include <unistd.h> /* chown(2) chroot(2) dup2(2) fork(2)
5454
* setegid(2) seteuid(2) setgid(2)
5555
* setuid(2) setsid(2) */
5656
#include <fcntl.h> /* F_GETLK F_SETLK - fcntl(2) open(2) */
5757
#include <pwd.h> /* struct passwd - getpwnam_r(3) */
5858
#include <grp.h> /* struct group - getgrnam(3) */
59+
#include <stdio.h> /* fileno(3) */
5960
#endif
6061

6162
/*============================================================================
@@ -111,8 +112,6 @@
111112
#define GETGRNAM_R_NETBSD GETGRGID_R_NETBSD
112113
#endif
113114

114-
/* TODO: Trim down, we only care about *BSD, MacOS, Linux (glibc, musl), and
115-
* Windows! */
116115
#define UCLIBC_PREREQ(M, m, p) \
117116
(__UCLIBC__ && \
118117
(__UCLIBC_MAJOR__ > M || \
@@ -155,7 +154,6 @@
155154
* Forward declarations *
156155
===========================================================================*/
157156
/* *nix: unistd.h, *: ? */
158-
JANET_CFUN(cfun_chdir); /* TODO: remove, os/cd already exists! */
159157
JANET_CFUN(cfun_chown);
160158
JANET_CFUN(cfun_chroot);
161159
JANET_CFUN(cfun_dup2);
@@ -175,39 +173,15 @@ JANET_CFUN(cfun_getpwnam);
175173
/* *nix: grp.h, *: ? */
176174
JANET_CFUN(cfun_getgrnam);
177175

178-
/* *nix: $this */
179-
/* JANET_CFUN(cfun_file_handle); */ /* See later TODO */
180-
/* int file_from_handle(Janet *argv, int idx, FILE **handle) */
176+
/* *nix: stdio.h */
177+
JANET_CFUN(cfun_file_handle); /* See later TODO */
178+
int file_to_fd(Janet *, int);
181179

182180
/*============================================================================
183181
* Function definitions
184182
===========================================================================*/
185183
#ifndef JANET_WINDOWS /* *nix */
186184
/* *nix: unistd.h, *: ? */
187-
JANET_FN(cfun_chdir, SYS_FUSAGE("chdir", " path-or-file"),
188-
"-> _true|throws error_\n\n"
189-
"\t`path-or-file` **:string|:core/file**\n\n"
190-
"Changes the current working directory to `path-or-file`.") {
191-
janet_fixarity(argc, 1);
192-
193-
if(janet_checktype(argv[0], JANET_ABSTRACT)) {
194-
FILE *f = ((JanetFile *)
195-
janet_getabstract(argv, 0, &janet_file_type))->file;
196-
if (0 != fchdir(fileno(f))) {
197-
sys_errno("Failed to chdir with file handle");
198-
return janet_wrap_boolean(0);
199-
}
200-
} else {
201-
const char *where = (char *)janet_getstring(argv, 0);
202-
if (0 != chdir(where)) {
203-
sys_errnof("Failed to chdir with path: %s", where);
204-
return janet_wrap_boolean(0);
205-
}
206-
}
207-
208-
return janet_wrap_boolean(1);
209-
}
210-
211185
JANET_FN(cfun_chown, SYS_FUSAGE("chown", " uid gid path-or-file"),
212186
"-> _true|throws error_\n\n"
213187
"\t`uid` **:number**\n\n"
@@ -222,16 +196,19 @@ JANET_FN(cfun_chown, SYS_FUSAGE("chown", " uid gid path-or-file"),
222196
gid_t gid = janet_getinteger(argv, 1);
223197

224198
if(janet_checktype(argv[0], JANET_ABSTRACT)) {
225-
FILE *f = ((JanetFile *)
226-
janet_getabstract(argv, 0, &janet_file_type))->file;
227-
if (0 != fchown(fileno(f), uid, gid)) {
228-
sys_errno("Failed to chdir with file handle");
229-
return janet_wrap_boolean(0);
199+
int fd;
200+
if (-1 != (fd = file_to_fd(argv, 0))) {
201+
if (0 != fchown(fd, uid, gid)) {
202+
sys_errno("Failed to chown with file handle");
203+
return janet_wrap_boolean(0);
204+
}
205+
} else {
206+
janet_panic("Invalid file handle");
230207
}
231208
} else {
232209
const char *where = (char *)janet_getstring(argv, 0);
233210
if (0 != chown(where, uid, gid)) {
234-
sys_errnof("Failed to chdir with path: %s", where);
211+
sys_errnof("Failed to chown with path: %s", where);
235212
return janet_wrap_boolean(0);
236213
}
237214
}
@@ -412,9 +389,6 @@ JANET_FN(cfun_dup2, SYS_FUSAGE("dup2", "fd-to fd-from"),
412389
return janet_wrap_boolean(1);
413390
}
414391

415-
/* TODO: need a Janet function to turn a stream into an integer File
416-
* Descriptor */
417-
418392
JANET_FN(cfun_fork, SYS_FUSAGE0("fork"),
419393
"-> _:number pid|throws error_\n\n"
420394
"Creates a fork of this proccess.") {
@@ -531,18 +505,22 @@ JANET_FN(cfun_fcntl, SYS_FUSAGE("fcntl", " file flag"),
531505
janet_fixarity(argc, 2);
532506

533507
if(janet_checktype(argv[0], JANET_ABSTRACT)) {
534-
int fd = fileno(((JanetFile *)
535-
janet_getabstract(argv, 0, &janet_file_type))->file);
536508
int op = 0;
537-
if (janet_keyeq(argv[1], "get-lock"))
538-
op = F_GETLK;
539-
else if (janet_keyeq(argv[1], "set-lock"))
540-
op = F_SETLK;
541-
else if (janet_keyeq(argv[1], "wait-lock"))
542-
op = F_SETLKW;
543-
else {
544-
janet_panic("Slot #2 must be a keyword equal to :get-lock "
545-
"| :set-lock | :wait-lock");
509+
int fd;
510+
if(-1 != (fd = file_to_fd(argv, 0))) {
511+
if (janet_keyeq(argv[1], "get-lock"))
512+
op = F_GETLK;
513+
else if (janet_keyeq(argv[1], "set-lock"))
514+
op = F_SETLK;
515+
else if (janet_keyeq(argv[1], "wait-lock"))
516+
op = F_SETLKW;
517+
else {
518+
janet_panic("Slot #2 must be a keyword equal to :get-lock "
519+
"| :set-lock | :wait-lock");
520+
return janet_wrap_boolean(0);
521+
}
522+
} else {
523+
janet_panic("Invalid File Handle");
546524
return janet_wrap_boolean(0);
547525
}
548526

@@ -861,9 +839,27 @@ JANET_FN(cfun_getgrnam, SYS_FUSAGE("getgrnam", " id-or-groupname"),
861839

862840
return janet_wrap_struct(r);
863841
}
842+
843+
/* nix: stdio.h, *: ?*/
844+
int file_to_fd(Janet *argv, int idx) {
845+
if(janet_checkfile(argv[idx]))
846+
return fileno(janet_unwrapfile(argv[idx], NULL));
847+
return -1;
848+
}
849+
850+
JANET_FN(cfun_fileno, SYS_FUSAGE("fileno", " file"),
851+
"-> _:number|throws error\n\n"
852+
"\t`file` **:core/file**\n\n"
853+
"Returns the integer file descriptor from a :core/file.") {
854+
int ret;
855+
if (-1 != (ret = file_to_fd(argv , 0)))
856+
return janet_wrap_integer(ret);
857+
858+
janet_panic("Invalid File Handle");
859+
return janet_wrap_boolean(0);
860+
}
864861
#else /* Windows */
865862
/* *nix: unistd.h, *: ? */
866-
DEF_NOT_IMPL(cfun_chdir, "sys/windows/chdir");
867863
DEF_NOT_IMPL(cfun_chown, "sys/windows/chown");
868864
DEF_NOT_IMPL(cfun_chroot, "sys/windows/chroot");
869865
DEF_NOT_IMPL(cfun_dup2, "sys/windows/dup2");
@@ -882,6 +878,9 @@ DEF_NOT_IMPL(cfun_getpwnam, "sys/windows/getpwnam");
882878

883879
/* *nix: grp.h, *: ? */
884880
DEF_NOT_IMPL(cfun_getgrnam, "sys/windows/getgrnam");
881+
882+
/* *nix: stdio.h, *: ? */
883+
DEF_NOT_IMPL(cfun_fileno, "sys/windows/fileno");
885884
#endif
886885

887886
/*============================================================================
@@ -892,25 +891,27 @@ DEF_NOT_IMPL(cfun_getgrnam, "sys/windows/getgrnam");
892891
JANET_MODULE_ENTRY(JanetTable *env) {
893892
janet_cfuns_ext(env, "sys", (JanetRegExt[]) {
894893
/* *nix: unistd.h, *: ? */
895-
JANET_REG(SYS_IMPL "/chdir", cfun_chdir),
896-
JANET_REG(SYS_IMPL "/chown", cfun_chown),
897-
JANET_REG(SYS_IMPL "/chroot", cfun_chroot),
898-
JANET_REG(SYS_IMPL "/dup2", cfun_dup2),
899-
JANET_REG(SYS_IMPL "/fork", cfun_fork),
900-
JANET_REG(SYS_IMPL "/setegid", cfun_setegid),
901-
JANET_REG(SYS_IMPL "/seteuid", cfun_seteuid),
902-
JANET_REG(SYS_IMPL "/setgid", cfun_setgid),
903-
JANET_REG(SYS_IMPL "/setuid", cfun_setuid),
904-
JANET_REG(SYS_IMPL "/setsid", cfun_setsid),
905-
906-
/* *nix: fcntl.h, *: ? */
907-
JANET_REG(SYS_IMPL "/fcntl", cfun_fcntl),
908-
909-
/* *nix: pwd.h, *: ? */
910-
JANET_REG(SYS_IMPL "/getpwnam", cfun_getpwnam),
911-
912-
/* *nix: grp.h, *: ? */
913-
JANET_REG(SYS_IMPL "/getgrnam", cfun_getgrnam),
914-
JANET_REG_END
894+
JANET_REG(SYS_IMPL "/chown", cfun_chown),
895+
JANET_REG(SYS_IMPL "/chroot", cfun_chroot),
896+
JANET_REG(SYS_IMPL "/dup2", cfun_dup2),
897+
JANET_REG(SYS_IMPL "/fork", cfun_fork),
898+
JANET_REG(SYS_IMPL "/setegid", cfun_setegid),
899+
JANET_REG(SYS_IMPL "/seteuid", cfun_seteuid),
900+
JANET_REG(SYS_IMPL "/setgid", cfun_setgid),
901+
JANET_REG(SYS_IMPL "/setuid", cfun_setuid),
902+
JANET_REG(SYS_IMPL "/setsid", cfun_setsid),
903+
904+
/* *nix: fcntl.h, *: ? */
905+
JANET_REG(SYS_IMPL "/fcntl", cfun_fcntl),
906+
907+
/* *nix: pwd.h, *: ? */
908+
JANET_REG(SYS_IMPL "/getpwnam", cfun_getpwnam),
909+
910+
/* *nix: grp.h, *: ? */
911+
JANET_REG(SYS_IMPL "/getgrnam", cfun_getgrnam),
912+
913+
/* *nix: stdio.h, *: ? */
914+
JANET_REG(SYS_IMPL "/fileno", cfun_fileno),
915+
JANET_REG_END
915916
});
916917
}

sys.janet

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
new symbol exported.
4444
``
4545
[from to]
46+
# TODO strip the match, it's not needed
4647
(match (length to)
4748
0 (redef+ from to)
4849
_ (each n to (redef+ (string from) n))))
@@ -59,7 +60,7 @@
5960
~(redef-multi* ,(string from) ',collecting)))
6061

6162
# All the system specific functions exported from C
62-
(def- exports '(chdir chown chroot dup2 fork setegid seteuid setgid setuid
63+
(def- exports '(chown chroot dup2 fileno fork setegid seteuid setgid setuid
6364
setsid fcntl getpwnam getgrnam))
6465

6566
# Figuring out which OS and calling the correct function in every wrapper
@@ -68,12 +69,9 @@
6869
:windows 'windows
6970
_ 'nix)]
7071
(each binding exports
71-
(redef- (string "sys/" os "/" binding) (string "_" binding))))
72-
73-
#TODO: also export as publicly $os/$function-name
74-
75-
# chdir - change directory ***************************************************
76-
(redef-multi _chdir chdir change-directory)
72+
(do
73+
(redef+ (string "sys/" os "/" binding) (string os "/" binding))
74+
(redef- (string "sys/" os "/" binding) (string "_" binding)))))
7775

7876
# chown - change fs entry ownership ******************************************
7977
# TODO: support chown with username/groupname instead of just uid/gid, support

0 commit comments

Comments
 (0)