diff options
-rw-r--r-- | CHANGELOG.txt | 9 | ||||
-rw-r--r-- | demo.cc | 37 | ||||
-rw-r--r-- | dux/GNUmakefile | 5 | ||||
-rw-r--r-- | dux/include/dux/io.h | 8 | ||||
-rw-r--r-- | dux/include/dux/thr.h | 30 | ||||
-rw-r--r-- | dux/source/io/cls.c | 12 | ||||
-rw-r--r-- | dux/source/io/cpy.c | 38 | ||||
-rw-r--r-- | dux/source/io/crt.c | 10 | ||||
-rw-r--r-- | dux/source/io/crtdir.c | 4 | ||||
-rw-r--r-- | dux/source/io/opn.c | 49 | ||||
-rw-r--r-- | dux/source/io/red.c | 51 | ||||
-rw-r--r-- | dux/source/io/sttpth.c | 42 | ||||
-rw-r--r-- | dux/source/io/wrt.c | 2 |
13 files changed, 245 insertions, 52 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 92df28a..c2f3082 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -66,6 +66,15 @@ - Add function for opening files in read-only mode: opn; - Add function for closing files: cls; +- Implement opn; +- Update demo; +- Implement red; +- Fix zp prefix on pthinf; +- Remove thr module; +- Implement cpy; +- Implement sttpth; +- Update parameters to sttpth; + | 2↋ - Fix signal pipe being renamed to file; @@ -1,9 +1,11 @@ +#include <cassert> #include <cstdlib> #include <dux/io.h> -#include <dux/thr.h> #include <iostream> +#include <zp/str> int main() { + //::dux_pri("dux {i04}.{i04}, demo\n",dux_api,dux_ext); ::std::cout << "dux " << dux_api << '.' << dux_ext << ", demo" << ::std::endl; ::dux_err err = ::dux_err_oky; @@ -23,16 +25,45 @@ int main() { err = ::dux_crt(&fil,"testdir/dux-demo-file",0644u); chkerr("unable to create file"); - err = ::dux_wrtstr(fil, + char const src[] = "static void msg(void);\n" "\n" "int main(void) {\n" "\tmsg();\n" "\treturn 0x45;\n" "}\n" - ); + "\n" + "#include <ly/io.h>\n" + "\n" + "static void msg(void) {\n" + "\tly_wrtstr(ly_dflout,\"flux ftw\\n\");\n" + "} /* Remember the last newline -> */\n"; + + err = ::dux_wrtstr(fil,src); chkerr("unable to write to file"); err = ::dux_cls(fil); chkerr("unable to close file"); + + ::dux_pthinf pthinf; + err = ::dux_sttpth(&pthinf,"testdir/dux-demo-file"); + chkerr("unable to stat file"); + + assert(pthinf.siz == sizeof (src)-0x1u); + + err = ::dux_cpy("testdir/dux-demo-file.c","testdir/dux-demo-file",0644u); + chkerr("unable to copy file"); + + err = ::dux_opn(&fil,"testdir/dux-demo-file.c"); + chkerr("unable to open file"); + + char buf[sizeof (src)]; + + err = ::dux_red(buf,fil,sizeof (src),nullptr); + chkerr("unable to read file"); + + assert(::zp::strequ(buf,src)); + + err = ::dux_cls(fil); + chkerr("unable to close file"); } diff --git a/dux/GNUmakefile b/dux/GNUmakefile index e077099..adc8420 100644 --- a/dux/GNUmakefile +++ b/dux/GNUmakefile @@ -8,14 +8,17 @@ OBJS := \ \ source/io/cls.o \ source/io/crtdir.o \ + source/io/cpy.o \ source/io/crt.o \ + source/io/opn.o \ + source/io/red.o \ + source/io/sttpth.o \ source/io/wrt.o \ source/io/wrtstr.o HDRS := \ include/dux/dux.h \ include/dux/io.h \ - include/dux/thr.h \ include-private/dux/prv/dux.h CFLAGS := \ diff --git a/dux/include/dux/io.h b/dux/include/dux/io.h index e67dd51..4778988 100644 --- a/dux/include/dux/io.h +++ b/dux/include/dux/io.h @@ -33,7 +33,7 @@ typedef struct { dux_prm prm; bool isdir : 0x1; bool isreg : 0x1; -} zp_pthinf; +} dux_pthinf; struct dux_prv_fil; typedef struct dux_prv_fil dux_fil; @@ -47,8 +47,8 @@ char const * dux_homdir(void); dux_err dux_chgdir(char const * pth); -dux_err dux_sttpth(char const * pth); -dux_err dux_setprm(char const * pth,dux_prm prm); +dux_err dux_setprm(char const * pth,dux_prm prm); +dux_err dux_sttpth(dux_pthinf * inf,char const * pth); dux_err dux_cpy(char const * newpth,char const * pth,dux_prm prm); dux_err dux_mov(char const * newpth,char const * pth); @@ -64,7 +64,7 @@ dux_err dux_cls(dux_fil * fil); dux_err dux_wrt( dux_fil * fil,void const * dat,zp_siz num); dux_err dux_wrtstr(dux_fil * fil,char const * str); -dux_err dux_red( void * buf,dux_fil * fil,zp_siz num,zp_siz * numrd); +dux_err dux_red( void * buf,dux_fil * fil,zp_siz num,zp_siz * numred); dux_prv_cdecend diff --git a/dux/include/dux/thr.h b/dux/include/dux/thr.h deleted file mode 100644 index e86f030..0000000 --- a/dux/include/dux/thr.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - Copyright 2019-2023 Gabriel Jensen. - - This file is part of dux. - dux is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - dux is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License along with dux. If not, see <https://www.gnu.org/licenses>. -*/ - -#if !defined(dux_hdr_thr) -#define dux_hdr_thr - -#include <dux/dux.h> - -dux_prv_cdec - -struct dux_prv_thr; -typedef struct dux_prv_thr dux_thr; - -zp_noret dux_err dux_exi(void * res); - -dux_err dux_slp(zp_f04 sec); - -dux_err dux_crtthr(dux_thr * * thr); -dux_err dux_srtthr(dux_thr * thr,void * (* srt)(dux_thr *,void *),void *); -dux_err dux_joithr(dux_thr * thr,void * * res); - -dux_prv_cdecend - -#endif diff --git a/dux/source/io/cls.c b/dux/source/io/cls.c index 17af339..451ff4e 100644 --- a/dux/source/io/cls.c +++ b/dux/source/io/cls.c @@ -16,19 +16,19 @@ extern int * __errno_location(void); dux_err dux_cls(dux_fil * const restrict fil) { - retry:; - int const cd = (int)zp_syscal(__NR_close,fil->fd); + cls:; - if (cd == -0x1) { + int const cd = (int)zp_syscal(__NR_close,fil->fd); + zp_unlik (cd == -0x1) { switch (*__errno_location()) { default: - goto free; + goto fre; case EINTR: - goto retry; + goto cls; } } -free:; +fre:; free(fil); return dux_err_oky; diff --git a/dux/source/io/cpy.c b/dux/source/io/cpy.c new file mode 100644 index 0000000..83db80c --- /dev/null +++ b/dux/source/io/cpy.c @@ -0,0 +1,38 @@ +/* + Copyright 2019-2023 Gabriel Jensen. + + This file is part of dux. + dux is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + dux is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public License along with dux. If not, see <https://www.gnu.org/licenses>. +*/ + +#include <dux/prv/io.h> + +#include <fcntl.h> +#include <linux/errno.h> +#include <linux/unistd.h> +#include <sys/sendfile.h> +#include <sys/types.h> + +extern int * __errno_location(void); + +dux_err dux_cpy(char const * const newpth,char const * const pth,dux_prm const prm) { + dux_pthinf inf; + dux_err const err = dux_sttpth(&inf,pth); + zp_unlik (err != dux_err_oky) {return err;} + + int const fd = (int)zp_syscal(__NR_openat,AT_FDCWD,pth,O_RDONLY,(mode_t)0x0); + + int const newfd = (int)zp_syscal(__NR_openat,AT_FDCWD,newpth,O_CREAT|O_TRUNC|O_WRONLY,(mode_t)prm); + + ssize_t const numcpy = (ssize_t)zp_syscal(__NR_sendfile,newfd,fd,(off_t *)zp_nulptr,inf.siz); + zp_unlik (numcpy == -0x1) { + switch (*__errno_location()) { + default: + return dux_err_err; + } + } + + return dux_err_oky; +} diff --git a/dux/source/io/crt.c b/dux/source/io/crt.c index aab7a11..b328519 100644 --- a/dux/source/io/crt.c +++ b/dux/source/io/crt.c @@ -16,16 +16,16 @@ extern int * __errno_location(void); -dux_err dux_crt(dux_fil * * const filptr,char const * const pth,zp_i01 const prm) { +dux_err dux_crt(dux_fil * * const filptr,char const * const pth,dux_prm const prm) { dux_fil * fil = malloc(sizeof (struct dux_prv_fil)); if (fil == zp_nulptr) {return dux_err_badalc;} -retry:; - int const fd = (int)zp_syscal(__NR_openat,AT_FDCWD,pth,O_CREAT | O_TRUNC | O_WRONLY,(mode_t)prm); - if (fd == -0x1) { +opn:; + int const fd = (int)zp_syscal(__NR_openat,AT_FDCWD,pth,O_CREAT|O_TRUNC|O_WRONLY,(mode_t)prm); + zp_unlik (fd == -0x1) { int const err = *__errno_location(); - if (err == EINTR) {goto retry;} + zp_lik (err == EINTR) {goto opn;} free(fil); switch (err) { diff --git a/dux/source/io/crtdir.c b/dux/source/io/crtdir.c index 85fb181..54bfdd5 100644 --- a/dux/source/io/crtdir.c +++ b/dux/source/io/crtdir.c @@ -16,9 +16,9 @@ extern int * __errno_location(void); -dux_err dux_crtdir(char const * const pth,zp_i01 const prm) { +dux_err dux_crtdir(char const * const pth,dux_prm const prm) { int const cod = (int)zp_syscal(__NR_mkdir,pth,(mode_t)prm); - if (cod == -0x1) { + zp_unlik (cod == -0x1) { switch (*__errno_location()) { default: return dux_err_err; diff --git a/dux/source/io/opn.c b/dux/source/io/opn.c new file mode 100644 index 0000000..863ae69 --- /dev/null +++ b/dux/source/io/opn.c @@ -0,0 +1,49 @@ +/* + Copyright 2019-2023 Gabriel Jensen. + + This file is part of dux. + dux is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + dux is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public License along with dux. If not, see <https://www.gnu.org/licenses>. +*/ + +#include <dux/prv/io.h> + +#include <fcntl.h> +#include <linux/errno.h> +#include <linux/unistd.h> +#include <stdlib.h> + +extern int * __errno_location(void); + +dux_err dux_opn(dux_fil * * const filptr,char const * const pth) { + dux_fil * fil = malloc(sizeof (struct dux_prv_fil)); + if (fil == zp_nulptr) {return dux_err_badalc;} + +opn:; + int const fd = (int)zp_syscal(__NR_openat,AT_FDCWD,pth,O_RDONLY,(mode_t)0x0); + zp_unlik (fd == -0x1) { + int const err = *__errno_location(); + + zp_lik (err == EINTR) {goto opn;} + + free(fil); + switch (err) { + default: + return dux_err_err; + case EACCES: + return dux_err_badprv; + case EISDIR: + return dux_err_isdir; + case ENOSPC: + return dux_err_spclim; + case ENOTDIR: + return dux_err_nodir; + } + } + + fil->fd = fd; + *filptr = fil; + + return dux_err_oky; +} diff --git a/dux/source/io/red.c b/dux/source/io/red.c new file mode 100644 index 0000000..6ce5958 --- /dev/null +++ b/dux/source/io/red.c @@ -0,0 +1,51 @@ +/* + Copyright 2019-2023 Gabriel Jensen. + + This file is part of dux. + dux is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + dux is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public License along with dux. If not, see <https://www.gnu.org/licenses>. +*/ + +#include <dux/prv/io.h> + +#include <errno.h> +#include <linux/unistd.h> +#include <sys/types.h> + +extern int * __errno_location(void); + +dux_err dux_red(void * const restrict voidbuf,dux_fil * const restrict fil,zp_siz const num,zp_siz * numred) { + zp_unlik (num == 0x0u) {return dux_err_oky;} + + unsigned char const * buf = voidbuf; + + for (size_t rem = num;rem != 0x0u;) { + ssize_t const cod = (ssize_t)zp_syscal(__NR_read,fil->fd,buf,rem); + + zp_unlik (cod == 0x0) { + zp_lik (numred != zp_nulptr) {*numred = num-rem;} + return dux_err_oky; + } + + zp_unlik (cod == -0x1) { + int const err = *__errno_location(); + + zp_lik (err == EINTR) {continue;} + + switch (err) { + default: + return dux_err_err; + case EISDIR: + return dux_err_isdir; + } + } + + buf += (size_t)cod; + rem -= (size_t)cod; + } + + if (numred != zp_nulptr) {*numred = num;} + + return dux_err_oky; +} diff --git a/dux/source/io/sttpth.c b/dux/source/io/sttpth.c new file mode 100644 index 0000000..7e4abb9 --- /dev/null +++ b/dux/source/io/sttpth.c @@ -0,0 +1,42 @@ +/* + Copyright 2019-2023 Gabriel Jensen. + + This file is part of dux. + dux is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + dux is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public License along with dux. If not, see <https://www.gnu.org/licenses>. +*/ + +#include <dux/prv/io.h> + +#include <fcntl.h> +#include <linux/errno.h> +#include <linux/unistd.h> +#include <sys/stat.h> + +extern int * __errno_location(void); + +dux_err dux_sttpth(dux_pthinf * const infptr,char const * const pth) { + dux_pthinf inf; + struct stat sysinf; + + int const cod = (int)zp_syscal(__NR_stat,pth,&sysinf); + zp_unlik (cod == -0x1) { + switch (*__errno_location()) { + default: + return dux_err_err; + case ENOENT: + return dux_err_nofil; + case ENOTDIR: + return dux_err_nodir; + } + } + + inf.siz = (zp_siz)sysinf.st_size; + inf.prm = (dux_prm)sysinf.st_mode&07777u; + inf.isdir = (bool)S_ISDIR(sysinf.st_mode); + inf.isreg = (bool)S_ISREG(sysinf.st_mode); + + *infptr = inf; + return dux_err_oky; +} diff --git a/dux/source/io/wrt.c b/dux/source/io/wrt.c index 62184b0..45e585f 100644 --- a/dux/source/io/wrt.c +++ b/dux/source/io/wrt.c @@ -22,7 +22,7 @@ dux_err dux_wrt(dux_fil * const restrict fil,void const * const restrict voiddat for (size_t rem = num;rem != 0x0u;) { ssize_t const cod = (ssize_t)zp_syscal(__NR_write,fil->fd,dat,rem); - if (cod == -0x1) { + zp_unlik (cod == -0x1) { switch (*__errno_location()) { default: return dux_err_err; |