(syscall-open-logic pathname oflags mode x86) → (mv * x86)
From the
Given a pathname for a file,
The file offset is set to the beginning of the file (see
The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read- only, write-only, or read/write, respectively.
In addition, zero or more file creation flags and file status flags can be bitwise-or'd in flags. The file creation flags are O_CREAT, O_EXCL, O_NOCTTY, and O_TRUNC.
Function:
(defun syscall-open-logic (pathname oflags mode x86) (declare (xargs :stobjs (x86))) (declare (ignorable pathname oflags mode x86)) (declare (xargs :guard (and (stringp pathname) (natp oflags) (natp mode)))) (let ((__function__ 'syscall-open-logic)) (declare (ignorable __function__)) (b* (((mv new-fd x86) (pop-x86-oracle x86)) ((when (not (natp new-fd))) (b* ((- (cw "~%Error: File Descriptor ill-formed. Maybe these books were ~ not built with X86ISA_EXEC set to t? See :doc x86isa-build-instructions.~%~%")) (x86 (!ms (list (rip x86) "File Descriptor ill-formed. Maybe these books were not built with X86ISA_EXEC set to t? See :doc x86sa-build-instructions." (ms x86)) x86))) (mv -1 x86))) ((when (and (not (equal (logand 3 mode) *o_rdonly*)) (not (equal (logand 3 mode) *o_wronly*)) (not (equal (logand 3 mode) *o_rdwr*)))) (b* ((- (cw "~%Error: File mode is not an appropriate access mode.~%~%"))) (mv -1 x86))) (obj-fd-field (read-x86-file-des new-fd x86)) (obj-contents-field (read-x86-file-contents pathname x86)) ((mv flg obj-contents-field x86) (if (equal (logand *o_creat* mode) *o_creat*) (if (and (file-descriptor-fieldp obj-fd-field) (file-contents-fieldp obj-contents-field)) (if (equal (logand *o_excl* mode) *o_excl*) (mv t obj-contents-field x86) (mv nil obj-contents-field x86)) (b* ((x86 (write-x86-file-contents pathname (acons :contents "" nil) x86)) (- (cw "here 0"))) (mv nil (acons :contents "" nil) x86))) (mv nil obj-contents-field x86))) ((when flg) (b* ((- (cw "~%Error: [O_EXCL Mode] O_CREAT used but file already exists.~%~%"))) (mv -1 x86))) (x86 (if (and (equal (logand *o_trunc* mode) *o_trunc*) (file-descriptor-fieldp obj-fd-field) (file-contents-fieldp obj-contents-field)) (write-x86-file-contents pathname (acons :contents "" nil) x86) x86)) (x86 (if (equal obj-contents-field nil) (write-x86-file-contents pathname (acons :contents "" nil) x86) x86)) (fd-field (put-assoc :name pathname (put-assoc :offset 0 (put-assoc :mode oflags (put-assoc :permissions mode nil))))) (x86 (write-x86-file-des new-fd fd-field x86))) (mv new-fd x86))))