Sys: files opened as asked, seek, links, permissions and times open takes a set of Access; openRead and create are two of its uses. seek, removeDir, chmod, symlink, readLink and setWorkDir; Info gains mtime and mode. Both Sys files, each with its test.
| @@ -27,10 +27,10 @@ | ||
| 27 | 27 | Every `Sys` export is written twice, in `Sys.linux-x86_64.raz` and in |
| 28 | 28 | `Sys.raz` over the C library, and tested through both (`test/libc`). |
| 29 | 29 | |
| 30 | -- [ ] 1. The report, version 2026-10.9: every new and changed export of | |
| 30 | +- [x] 1. The report, version 2026-10.9: every new and changed export of | |
| 31 | 31 | `Sys`, `Str`, `Map`, `Sort`, `Math` and `Fmt` (section 11), for |
| 32 | 32 | review before any code. |
| 33 | -- [ ] 2. `Sys`, files: `open` with a set of `Access`, `seek`, `Info`'s | |
| 33 | +- [x] 2. `Sys`, files: `open` with a set of `Access`, `seek`, `Info`'s | |
| 34 | 34 | `mtime` and `mode`, `chmod`, `readLink`, `symlink`, `removeDir`, |
| 35 | 35 | `setWorkDir`. |
| 36 | 36 | - [ ] 3. `Sys`, programs: `pipe`, `spawn` of a `Program`, `wait` of any |
| @@ -20,6 +20,9 @@ | ||
| 20 | 20 | extern proc rename*(old, new: ^U8): I32; |
| 21 | 21 | extern proc unlink*(path: ^U8): I32; |
| 22 | 22 | extern proc mkdir*(path: ^U8; mode: U32): I32; |
| 23 | +extern proc rmdir*(path: ^U8): I32; | |
| 24 | +extern proc chmod*(path: ^U8; mode: U32): I32; | |
| 25 | +extern proc readlink*(path, buf: ^U8; n: U64): I64; | |
| 23 | 26 | extern proc lseek*(fd: I32; off: I64; whence: I32): I64; |
| 24 | 27 | extern proc opendir*(path: ^U8): ^U8; (* DIR * *) |
| 25 | 28 | extern proc readdir*(d: ^U8): ^U8; (* struct dirent * *) |
| @@ -37,6 +40,7 @@ | ||
| 37 | 40 | extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32; |
| 38 | 41 | extern proc getenv*(name: ^U8): ^U8; |
| 39 | 42 | extern proc getcwd*(buf: ^U8; n: U64): ^U8; |
| 43 | +extern proc chdir*(path: ^U8): I32; | |
| 40 | 44 | extern proc stat*(path, st: ^U8): I32; |
| 41 | 45 | extern proc lstat*(path, st: ^U8): I32; |
| 42 | 46 | extern proc symlink*(target, path: ^U8): I32; |
| @@ -8,9 +8,16 @@ | ||
| 8 | 8 | stderr* = 2; |
| 9 | 9 | |
| 10 | 10 | type |
| 11 | + (* What open is asked for: reading, writing or both, and what else | |
| 12 | + it does to the file. *) | |
| 13 | + Access* = (reading, writing, creating, exclusive, truncating, appending); | |
| 14 | + Accesses* = set of Access; | |
| 15 | + | |
| 11 | 16 | (* What info says of a file. *) |
| 12 | 17 | Info* = record |
| 13 | 18 | size*: I64; (* in bytes *) |
| 19 | + mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) | |
| 20 | + mode*: I64; (* its permission bits, rwxrwxrwx *) | |
| 14 | 21 | dir*: BOOL; (* a directory *) |
| 15 | 22 | file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) |
| 16 | 23 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| @@ -39,6 +46,7 @@ | ||
| 39 | 46 | |
| 40 | 47 | var |
| 41 | 48 | cwd: [4096]U8; (* what workDir gives *) |
| 49 | + linked: [4096]U8; (* what readLink gives *) | |
| 42 | 50 | saved: Termios; (* the terminal as rawMode found it *) |
| 43 | 51 | |
| 44 | 52 | (* Writes s to the file descriptor fd; the number of bytes written, or |
| @@ -67,22 +75,50 @@ | ||
| 67 | 75 | return true; |
| 68 | 76 | end; |
| 69 | 77 | |
| 70 | -(* The file at path, opened for reading: its file descriptor, or minus | |
| 71 | - an errno. *) | |
| 72 | -proc openRead*(path: []U8): I64; | |
| 78 | +(* open's flags for how; Linux's numbers *) | |
| 79 | +proc flags(how: Accesses): I32; | |
| 80 | +var f: I32; | |
| 81 | +begin | |
| 82 | + f := 0; (* O_RDONLY *) | |
| 83 | + if writing in how then | |
| 84 | + f := 1; (* O_WRONLY *) | |
| 85 | + if reading in how then f := 2; end; (* O_RDWR *) | |
| 86 | + end; | |
| 87 | + if creating in how then f := f | 40H; end; | |
| 88 | + if exclusive in how then f := f | 80H; end; | |
| 89 | + if truncating in how then f := f | 200H; end; | |
| 90 | + if appending in how then f := f | 400H; end; | |
| 91 | + return f; | |
| 92 | +end; | |
| 93 | + | |
| 94 | +(* The file at path, opened as how asks: its file descriptor, or minus | |
| 95 | + an errno. A file it creates is readable by all, writable by its | |
| 96 | + owner. *) | |
| 97 | +proc open*(path: []U8; how: Accesses): I64; | |
| 73 | 98 | var buf: [4096]U8; |
| 74 | 99 | begin |
| 75 | 100 | if not cstr(path, buf) then return -36; end; (* ENAMETOOLONG *) |
| 76 | - return SYSCALL(2, ADR(buf), 0); (* open, O_RDONLY *) | |
| 101 | + return SYSCALL(2, ADR(buf), flags(how), 1A4H); (* mode 644 *) | |
| 77 | 102 | end; |
| 78 | 103 | |
| 79 | -(* The file at path, made empty or new, opened for writing, readable by | |
| 80 | - all: its file descriptor, or minus an errno. *) | |
| 104 | +(* The file at path, opened for reading. *) | |
| 105 | +proc openRead*(path: []U8): I64; | |
| 106 | +begin | |
| 107 | + return open(path, {reading}); | |
| 108 | +end; | |
| 109 | + | |
| 110 | +(* The file at path, made empty or new, opened for writing. *) | |
| 81 | 111 | proc create*(path: []U8): I64; |
| 82 | -var buf: [4096]U8; | |
| 112 | +begin | |
| 113 | + return open(path, {writing, creating, truncating}); | |
| 114 | +end; | |
| 115 | + | |
| 116 | +(* Moves fd to pos from the start of its file, or from the end if | |
| 117 | + fromEnd: the new position, or minus an errno. *) | |
| 118 | +proc seek*(fd, pos: I64; fromEnd: BOOL): I64; | |
| 83 | 119 | begin |
| 84 | - if not cstr(path, buf) then return -36; end; | |
| 85 | - return SYSCALL(85, ADR(buf), 420); (* creat, mode 644 *) | |
| 120 | + if fromEnd then return SYSCALL(8, fd, pos, 2); end; (* lseek, SEEK_END *) | |
| 121 | + return SYSCALL(8, fd, pos, 0); (* SEEK_SET *) | |
| 86 | 122 | end; |
| 87 | 123 | |
| 88 | 124 | proc close*(fd: I64); |
| @@ -137,6 +173,42 @@ | ||
| 137 | 173 | return SYSCALL(83, ADR(a), 1EDH); (* mkdir, mode 755 *) |
| 138 | 174 | end; |
| 139 | 175 | |
| 176 | +(* Removes the directory at path, which must be empty. *) | |
| 177 | +proc removeDir*(path: []U8): I64; | |
| 178 | +var a: [4096]U8; | |
| 179 | +begin | |
| 180 | + if not cstr(path, a) then return -36; end; | |
| 181 | + return SYSCALL(84, ADR(a)); (* rmdir *) | |
| 182 | +end; | |
| 183 | + | |
| 184 | +(* Sets the permission bits of what path names. *) | |
| 185 | +proc chmod*(path: []U8; mode: I64): I64; | |
| 186 | +var a: [4096]U8; | |
| 187 | +begin | |
| 188 | + if not cstr(path, a) then return -36; end; | |
| 189 | + return SYSCALL(90, ADR(a), mode); | |
| 190 | +end; | |
| 191 | + | |
| 192 | +(* Makes path a symbolic link to target. *) | |
| 193 | +proc symlink*(target, path: []U8): I64; | |
| 194 | +var a, b: [4096]U8; | |
| 195 | +begin | |
| 196 | + if not cstr(target, a) or not cstr(path, b) then return -36; end; | |
| 197 | + return SYSCALL(88, ADR(a), ADR(b)); | |
| 198 | +end; | |
| 199 | + | |
| 200 | +(* What the symbolic link at path names, good until the next call; and | |
| 201 | + 0, or minus an errno. *) | |
| 202 | +proc readLink*(path: []U8): ([]U8, I64); | |
| 203 | +var a: [4096]U8; n: I64; | |
| 204 | +begin | |
| 205 | + if not cstr(path, a) then return "", -36; end; | |
| 206 | + n := SYSCALL(89, ADR(a), ADR(linked), LEN(linked)); (* readlink *) | |
| 207 | + if n < 0 then return "", n; end; | |
| 208 | + if n = LEN(linked) then return "", -36; end; (* it may be cut short *) | |
| 209 | + return linked[0..n], 0; | |
| 210 | +end; | |
| 211 | + | |
| 140 | 212 | (* The size of what path names, whether it is a directory or a regular |
| 141 | 213 | file, following a symbolic link, and whether path is one; and 0, or |
| 142 | 214 | minus an errno. *) |
| @@ -150,6 +222,8 @@ | ||
| 150 | 222 | r := SYSCALL(4, ADR(a), ADR(st)); (* stat *) |
| 151 | 223 | if r # 0 then return i, r; end; |
| 152 | 224 | i.size := st[6]; (* st_size *) |
| 225 | + i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) | |
| 226 | + i.mode := st[3] & 1FFH; | |
| 153 | 227 | i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) |
| 154 | 228 | i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) |
| 155 | 229 | return i, 0; |
| @@ -311,6 +385,14 @@ | ||
| 311 | 385 | return cwd[0..r - 1], 0; |
| 312 | 386 | end; |
| 313 | 387 | |
| 388 | +(* Makes path the working directory. *) | |
| 389 | +proc setWorkDir*(path: []U8): I64; | |
| 390 | +var a: [4096]U8; | |
| 391 | +begin | |
| 392 | + if not cstr(path, a) then return -36; end; | |
| 393 | + return SYSCALL(80, ADR(a)); (* chdir *) | |
| 394 | +end; | |
| 395 | + | |
| 314 | 396 | (* A pseudo-terminal: the file descriptor of its controlling side, or |
| 315 | 397 | minus an errno. Its other side, the terminal its programs run on, is |
| 316 | 398 | unlocked, for spawn to open. *) |
| @@ -512,7 +594,6 @@ | ||
| 512 | 594 | after its end. *) |
| 513 | 595 | proc main*; |
| 514 | 596 | var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; |
| 515 | - dir, name2: [4096]U8; | |
| 516 | 597 | begin |
| 517 | 598 | fd := create("/tmp/raz-sys-test"); |
| 518 | 599 | if fd < 0 then exit(2); end; |
| @@ -543,12 +624,34 @@ | ||
| 543 | 624 | i, err := info("/dev/null"); |
| 544 | 625 | if (err # 0) or i.dir or i.file then exit(2); end; |
| 545 | 626 | remove("/tmp/raz-sys-link"); |
| 546 | - cstr("/tmp/raz-sys-dir", dir); | |
| 547 | - cstr("/tmp/raz-sys-link", name2); | |
| 548 | - if SYSCALL(88, ADR(dir), ADR(name2)) # 0 then exit(2); end; (* symlink *) | |
| 627 | + if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end; | |
| 549 | 628 | i, err := info("/tmp/raz-sys-link"); |
| 550 | 629 | if (err # 0) or not i.dir or not i.symlink then exit(2); end; |
| 630 | + name, err := readLink("/tmp/raz-sys-link"); | |
| 631 | + if (err # 0) or (name # "/tmp/raz-sys-dir") then exit(2); end; | |
| 632 | + name, err := readLink("/tmp/raz-sys-dir"); | |
| 633 | + if err # -22 then exit(2); end; (* EINVAL: not a link *) | |
| 551 | 634 | remove("/tmp/raz-sys-link"); |
| 635 | + (* open as asked, seek, the mode and the time *) | |
| 636 | + remove("/tmp/raz-sys-dir/h"); | |
| 637 | + fd := open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}); | |
| 638 | + if (fd < 0) or (write(fd, "abc") # 3) then exit(2); end; | |
| 639 | + close(fd); | |
| 640 | + if open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}) # -17 then exit(2); end; | |
| 641 | + fd := open("/tmp/raz-sys-dir/h", {writing, appending}); | |
| 642 | + if (fd < 0) or (seek(fd, 0, false) # 0) or (write(fd, "de") # 2) then exit(2); end; | |
| 643 | + close(fd); | |
| 644 | + fd := open("/tmp/raz-sys-dir/h", {reading, writing}); | |
| 645 | + if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end; | |
| 646 | + if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end; | |
| 647 | + if (read(fd, buf) # 5) or (buf[0..5] # "aBcde") then exit(2); end; | |
| 648 | + close(fd); | |
| 649 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 650 | + if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end; | |
| 651 | + if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; | |
| 652 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 653 | + if i.mode # 1EDH then exit(2); end; | |
| 654 | + remove("/tmp/raz-sys-dir/h"); | |
| 552 | 655 | i, err := info("/no/such/file"); |
| 553 | 656 | if err >= 0 then exit(2); end; |
| 554 | 657 | if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; |
| @@ -567,6 +670,8 @@ | ||
| 567 | 670 | if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; |
| 568 | 671 | name, err := workDir(); |
| 569 | 672 | if (err # 0) or (LEN(name) = 0) or (name[0] # '/') then exit(2); end; |
| 673 | + if (setWorkDir("/tmp/raz-sys-dir") # 0) or (removeDir("/tmp/raz-sys-dir") # 0) then exit(2); end; | |
| 674 | + if (setWorkDir("/") # 0) or (removeDir("/tmp/raz-sys-dir") # -2) then exit(2); end; | |
| 570 | 675 | (* a socket: nothing at a path, then a listener that a connection |
| 571 | 676 | makes ready, and a byte from it to the connection *) |
| 572 | 677 | if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *) |
| @@ -4,8 +4,8 @@ | ||
| 4 | 4 | |
| 5 | 5 | It keeps to POSIX's names, and raw mode is cfmakeraw's. What is |
| 6 | 6 | glibc's, on x86-64: where a name lies in struct dirent; struct stat; |
| 7 | - the numbers of the requests TIOCGWINSZ, TIOCSWINSZ and TIOCSCTTY; | |
| 8 | - close_range; and struct sockaddr_un, which on the BSDs starts with a | |
| 7 | + open's flags; the numbers of the requests TIOCGWINSZ, TIOCSWINSZ and | |
| 8 | + TIOCSCTTY; close_range; and struct sockaddr_un, which on the BSDs starts with a | |
| 9 | 9 | byte of length. A C library that differs needs a Sys of its own. *) |
| 10 | 10 | module Sys; |
| 11 | 11 | import C; |
| @@ -20,9 +20,16 @@ | ||
| 20 | 20 | TIOCSCTTY = 540EH; |
| 21 | 21 | |
| 22 | 22 | type |
| 23 | + (* What open is asked for: reading, writing or both, and what else | |
| 24 | + it does to the file. *) | |
| 25 | + Access* = (reading, writing, creating, exclusive, truncating, appending); | |
| 26 | + Accesses* = set of Access; | |
| 27 | + | |
| 23 | 28 | (* What info says of a file. *) |
| 24 | 29 | Info* = record |
| 25 | 30 | size*: I64; (* in bytes *) |
| 31 | + mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) | |
| 32 | + mode*: I64; (* its permission bits, rwxrwxrwx *) | |
| 26 | 33 | dir*: BOOL; (* a directory *) |
| 27 | 34 | file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) |
| 28 | 35 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| @@ -44,6 +51,7 @@ | ||
| 44 | 51 | |
| 45 | 52 | var |
| 46 | 53 | cwd: [4096]U8; (* what workDir gives *) |
| 54 | + linked: [4096]U8; (* what readLink gives *) | |
| 47 | 55 | saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *) |
| 48 | 56 | |
| 49 | 57 | (* minus errno, as a system call returns it *) |
| @@ -86,24 +94,53 @@ | ||
| 86 | 94 | return true; |
| 87 | 95 | end; |
| 88 | 96 | |
| 89 | -(* The file at path, opened for reading: its file descriptor, or minus | |
| 90 | - an errno. *) | |
| 91 | -proc openRead*(path: []U8): I64; | |
| 97 | +(* open's flags for how; glibc's numbers *) | |
| 98 | +proc flags(how: Accesses): I32; | |
| 99 | +var f: I32; | |
| 100 | +begin | |
| 101 | + f := 0; (* O_RDONLY *) | |
| 102 | + if writing in how then | |
| 103 | + f := 1; (* O_WRONLY *) | |
| 104 | + if reading in how then f := 2; end; (* O_RDWR *) | |
| 105 | + end; | |
| 106 | + if creating in how then f := f | 40H; end; | |
| 107 | + if exclusive in how then f := f | 80H; end; | |
| 108 | + if truncating in how then f := f | 200H; end; | |
| 109 | + if appending in how then f := f | 400H; end; | |
| 110 | + return f; | |
| 111 | +end; | |
| 112 | + | |
| 113 | +(* The file at path, opened as how asks: its file descriptor, or minus | |
| 114 | + an errno. A file it creates is readable by all, writable by its | |
| 115 | + owner. *) | |
| 116 | +proc open*(path: []U8; how: Accesses): I64; | |
| 92 | 117 | var buf: [4096]U8; r: I64; |
| 93 | 118 | begin |
| 94 | 119 | if not cstr(path, buf) then return -36; end; |
| 95 | - r := I64(C.open(ADR(buf[0]), 0)); (* O_RDONLY is 0 everywhere *) | |
| 120 | + r := I64(C.open(ADR(buf[0]), flags(how), 1A4H)); (* mode 644 *) | |
| 96 | 121 | if r < 0 then return failed(); end; |
| 97 | 122 | return r; |
| 98 | 123 | end; |
| 99 | 124 | |
| 100 | -(* The file at path, made empty or new, opened for writing, readable by | |
| 101 | - all: its file descriptor, or minus an errno. *) | |
| 125 | +(* The file at path, opened for reading. *) | |
| 126 | +proc openRead*(path: []U8): I64; | |
| 127 | +begin | |
| 128 | + return open(path, {reading}); | |
| 129 | +end; | |
| 130 | + | |
| 131 | +(* The file at path, made empty or new, opened for writing. *) | |
| 102 | 132 | proc create*(path: []U8): I64; |
| 103 | -var buf: [4096]U8; r: I64; | |
| 133 | +begin | |
| 134 | + return open(path, {writing, creating, truncating}); | |
| 135 | +end; | |
| 136 | + | |
| 137 | +(* Moves fd to pos from the start of its file, or from the end if | |
| 138 | + fromEnd: the new position, or minus an errno. *) | |
| 139 | +proc seek*(fd, pos: I64; fromEnd: BOOL): I64; | |
| 140 | +var r: I64; | |
| 104 | 141 | begin |
| 105 | - if not cstr(path, buf) then return -36; end; | |
| 106 | - r := I64(C.creat(ADR(buf[0]), 420)); (* mode 644 *) | |
| 142 | + if fromEnd then r := C.lseek(I32(fd), pos, 2); (* SEEK_END *) | |
| 143 | + else r := C.lseek(I32(fd), pos, 0); end; (* SEEK_SET *) | |
| 107 | 144 | if r < 0 then return failed(); end; |
| 108 | 145 | return r; |
| 109 | 146 | end; |
| @@ -155,6 +192,45 @@ | ||
| 155 | 192 | return 0; |
| 156 | 193 | end; |
| 157 | 194 | |
| 195 | +(* Removes the directory at path, which must be empty. *) | |
| 196 | +proc removeDir*(path: []U8): I64; | |
| 197 | +var a: [4096]U8; | |
| 198 | +begin | |
| 199 | + if not cstr(path, a) then return -36; end; | |
| 200 | + if C.rmdir(ADR(a[0])) # 0 then return failed(); end; | |
| 201 | + return 0; | |
| 202 | +end; | |
| 203 | + | |
| 204 | +(* Sets the permission bits of what path names. *) | |
| 205 | +proc chmod*(path: []U8; mode: I64): I64; | |
| 206 | +var a: [4096]U8; | |
| 207 | +begin | |
| 208 | + if not cstr(path, a) then return -36; end; | |
| 209 | + if C.chmod(ADR(a[0]), U32(mode)) # 0 then return failed(); end; | |
| 210 | + return 0; | |
| 211 | +end; | |
| 212 | + | |
| 213 | +(* Makes path a symbolic link to target. *) | |
| 214 | +proc symlink*(target, path: []U8): I64; | |
| 215 | +var a, b: [4096]U8; | |
| 216 | +begin | |
| 217 | + if not cstr(target, a) or not cstr(path, b) then return -36; end; | |
| 218 | + if C.symlink(ADR(a[0]), ADR(b[0])) # 0 then return failed(); end; | |
| 219 | + return 0; | |
| 220 | +end; | |
| 221 | + | |
| 222 | +(* What the symbolic link at path names, good until the next call; and | |
| 223 | + 0, or minus an errno. *) | |
| 224 | +proc readLink*(path: []U8): ([]U8, I64); | |
| 225 | +var a: [4096]U8; n: I64; | |
| 226 | +begin | |
| 227 | + if not cstr(path, a) then return "", -36; end; | |
| 228 | + n := C.readlink(ADR(a[0]), ADR(linked[0]), U64(LEN(linked))); | |
| 229 | + if n < 0 then return "", failed(); end; | |
| 230 | + if n = LEN(linked) then return "", -36; end; (* it may be cut short *) | |
| 231 | + return linked[0..n], 0; | |
| 232 | +end; | |
| 233 | + | |
| 158 | 234 | (* The size of what path names, whether it is a directory or a regular |
| 159 | 235 | file, following a symbolic link, and whether path is one; and 0, or |
| 160 | 236 | minus an errno. *) |
| @@ -166,6 +242,8 @@ | ||
| 166 | 242 | i.symlink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) |
| 167 | 243 | if C.stat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; |
| 168 | 244 | i.size := st[6]; (* st_size *) |
| 245 | + i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) | |
| 246 | + i.mode := st[3] & 1FFH; | |
| 169 | 247 | i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) |
| 170 | 248 | i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) |
| 171 | 249 | return i, 0; |
| @@ -309,6 +387,15 @@ | ||
| 309 | 387 | return cwd[0..n], 0; |
| 310 | 388 | end; |
| 311 | 389 | |
| 390 | +(* Makes path the working directory. *) | |
| 391 | +proc setWorkDir*(path: []U8): I64; | |
| 392 | +var a: [4096]U8; | |
| 393 | +begin | |
| 394 | + if not cstr(path, a) then return -36; end; | |
| 395 | + if C.chdir(ADR(a[0])) # 0 then return failed(); end; | |
| 396 | + return 0; | |
| 397 | +end; | |
| 398 | + | |
| 312 | 399 | (* A pseudo-terminal: the file descriptor of its controlling side, or |
| 313 | 400 | minus an errno. Its other side, the terminal its programs run on, is |
| 314 | 401 | unlocked, for spawn to open. *) |
| @@ -516,7 +603,6 @@ | ||
| 516 | 603 | socket, sent to after its end. *) |
| 517 | 604 | proc main*; |
| 518 | 605 | var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; |
| 519 | - dir, name2: [4096]U8; | |
| 520 | 606 | begin |
| 521 | 607 | fd := create("/tmp/raz-sys-test"); |
| 522 | 608 | if fd < 0 then exit(2); end; |
| @@ -547,12 +633,34 @@ | ||
| 547 | 633 | i, err := info("/dev/null"); |
| 548 | 634 | if (err # 0) or i.dir or i.file then exit(2); end; |
| 549 | 635 | remove("/tmp/raz-sys-link"); |
| 550 | - cstr("/tmp/raz-sys-dir", dir); | |
| 551 | - cstr("/tmp/raz-sys-link", name2); | |
| 552 | - if C.symlink(ADR(dir[0]), ADR(name2[0])) # 0 then exit(2); end; | |
| 636 | + if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end; | |
| 553 | 637 | i, err := info("/tmp/raz-sys-link"); |
| 554 | 638 | if (err # 0) or not i.dir or not i.symlink then exit(2); end; |
| 639 | + name, err := readLink("/tmp/raz-sys-link"); | |
| 640 | + if (err # 0) or (name # "/tmp/raz-sys-dir") then exit(2); end; | |
| 641 | + name, err := readLink("/tmp/raz-sys-dir"); | |
| 642 | + if err # -22 then exit(2); end; (* EINVAL: not a link *) | |
| 555 | 643 | remove("/tmp/raz-sys-link"); |
| 644 | + (* open as asked, seek, the mode and the time *) | |
| 645 | + remove("/tmp/raz-sys-dir/h"); | |
| 646 | + fd := open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}); | |
| 647 | + if (fd < 0) or (write(fd, "abc") # 3) then exit(2); end; | |
| 648 | + close(fd); | |
| 649 | + if open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}) # -17 then exit(2); end; | |
| 650 | + fd := open("/tmp/raz-sys-dir/h", {writing, appending}); | |
| 651 | + if (fd < 0) or (seek(fd, 0, false) # 0) or (write(fd, "de") # 2) then exit(2); end; | |
| 652 | + close(fd); | |
| 653 | + fd := open("/tmp/raz-sys-dir/h", {reading, writing}); | |
| 654 | + if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end; | |
| 655 | + if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end; | |
| 656 | + if (read(fd, buf) # 5) or (buf[0..5] # "aBcde") then exit(2); end; | |
| 657 | + close(fd); | |
| 658 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 659 | + if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end; | |
| 660 | + if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; | |
| 661 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 662 | + if i.mode # 1EDH then exit(2); end; | |
| 663 | + remove("/tmp/raz-sys-dir/h"); | |
| 556 | 664 | i, err := info("/no/such/file"); |
| 557 | 665 | if err >= 0 then exit(2); end; |
| 558 | 666 | if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; |
| @@ -571,6 +679,8 @@ | ||
| 571 | 679 | if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; |
| 572 | 680 | name, err := workDir(); |
| 573 | 681 | if (err # 0) or (LEN(name) = 0) or (name[0] # '/') then exit(2); end; |
| 682 | + if (setWorkDir("/tmp/raz-sys-dir") # 0) or (removeDir("/tmp/raz-sys-dir") # 0) then exit(2); end; | |
| 683 | + if (setWorkDir("/") # 0) or (removeDir("/tmp/raz-sys-dir") # -2) then exit(2); end; | |
| 574 | 684 | (* a socket: nothing at a path, then a listener that a connection |
| 575 | 685 | makes ready, and a byte from it to the connection *) |
| 576 | 686 | if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *) |