diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CHANGELOG.txt | 18 | ||||
-rw-r--r-- | demo.cc | 31 | ||||
-rw-r--r-- | dux/GNUmakefile | 9 | ||||
-rw-r--r-- | dux/include-private/dux/prv/io.h | 23 | ||||
-rw-r--r-- | dux/include/dux/dux.h | 5 | ||||
-rw-r--r-- | dux/include/dux/io.h | 18 | ||||
-rw-r--r-- | dux/source/dux/abr.c | 19 | ||||
-rw-r--r-- | dux/source/dux/errmsg.c | 4 | ||||
-rw-r--r-- | dux/source/io/cls.c | 35 | ||||
-rw-r--r-- | dux/source/io/crt.c | 49 | ||||
-rw-r--r-- | dux/source/io/crtdir.c | 35 | ||||
-rw-r--r-- | dux/source/io/wrt.c | 45 | ||||
-rw-r--r-- | dux/source/io/wrtstr.c | 16 |
14 files changed, 292 insertions, 16 deletions
@@ -2,4 +2,5 @@ *.o *.so /demo +/testdir /tmp diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 17b8fb4..92df28a 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -48,6 +48,24 @@ - Fix installation target; +- Support Linux AMD64; +- Implement crtfil; +- Add private header for I/O; +- Implement abr; +- Remove parameters from abr; +- Update demo; +- Implement crtdir; +- Implement wrt; +- Rename badprm value to noprv; +- Update gitignore; +- Add function for opening files: +- Remove iomod type; +- Rename crtfil to crt; +- Implement wrtstr; +- Add function for opening files in read-write mode: opnwr; +- Add function for opening files in read-only mode: opn; +- Add function for closing files: cls; + | 2↋ - Fix signal pipe being renamed to file; @@ -1,7 +1,38 @@ +#include <cstdlib> #include <dux/io.h> #include <dux/thr.h> #include <iostream> int main() { ::std::cout << "dux " << dux_api << '.' << dux_ext << ", demo" << ::std::endl; + + ::dux_err err = ::dux_err_oky; + + auto const chkerr = [&err](char const * const msg) noexcept { + if (err != ::dux_err_oky) { + ::std::cout << msg << ": " << ::dux_errmsg(err) << ::std::endl; + ::std::exit(EXIT_FAILURE); + } + }; + + err = ::dux_crtdir("testdir",0755u); + chkerr("unable to create directory"); + + ::dux_fil * fil = nullptr; + + err = ::dux_crt(&fil,"testdir/dux-demo-file",0644u); + chkerr("unable to create file"); + + err = ::dux_wrtstr(fil, + "static void msg(void);\n" + "\n" + "int main(void) {\n" + "\tmsg();\n" + "\treturn 0x45;\n" + "}\n" + ); + chkerr("unable to write to file"); + + err = ::dux_cls(fil); + chkerr("unable to close file"); } diff --git a/dux/GNUmakefile b/dux/GNUmakefile index e2b3783..e077099 100644 --- a/dux/GNUmakefile +++ b/dux/GNUmakefile @@ -3,7 +3,14 @@ dbg := false endif OBJS := \ - source/dux/errmsg.o + source/dux/abr.o \ + source/dux/errmsg.o \ + \ + source/io/cls.o \ + source/io/crtdir.o \ + source/io/crt.o \ + source/io/wrt.o \ + source/io/wrtstr.o HDRS := \ include/dux/dux.h \ diff --git a/dux/include-private/dux/prv/io.h b/dux/include-private/dux/prv/io.h new file mode 100644 index 0000000..f2503e4 --- /dev/null +++ b/dux/include-private/dux/prv/io.h @@ -0,0 +1,23 @@ +/* + 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_prv_io) +#define dux_hdr_prv_io + +#include <dux/io.h> + +dux_prv_cdec + +struct dux_prv_fil { + int fd; +}; + +dux_prv_cdecend + +#endif diff --git a/dux/include/dux/dux.h b/dux/include/dux/dux.h index e764f81..538c2d5 100644 --- a/dux/include/dux/dux.h +++ b/dux/include/dux/dux.h @@ -31,7 +31,7 @@ typedef enum { dux_err_badalc, dux_err_badfil, dux_err_badfmt, - dux_err_badprm, + dux_err_badprv, dux_err_badval, dux_err_eof, dux_err_exs, @@ -42,8 +42,7 @@ typedef enum { dux_err_spclim, } dux_err; -zp_noret void dux_prv_abr(char const * fil,long lin,char const * fun,char const * msg,...); -#define dux_abr(msg) (dux_prv_abr(__FILE__,__LINE__,__func__,(msg))) +zp_noret void dux_abr(void); zp_unseq char const * dux_errmsg(dux_err err); diff --git a/dux/include/dux/io.h b/dux/include/dux/io.h index 5417f60..e67dd51 100644 --- a/dux/include/dux/io.h +++ b/dux/include/dux/io.h @@ -16,12 +16,6 @@ dux_prv_cdec typedef zp_i01 dux_prm; -typedef enum { - dux_iomod_red, - dux_iomod_redwrt, - dux_iomod_wrt, -} dux_iomod; - struct dux_prv_dsc { bool _dsc; }; @@ -56,14 +50,18 @@ 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_crtdir(char const * pth,dux_prm prm); - -dux_err dux_crtfil(dux_fil * * fil,char const * pth,dux_prm prm); - dux_err dux_cpy(char const * newpth,char const * pth,dux_prm prm); dux_err dux_mov(char const * newpth,char const * pth); dux_err dux_rem(char const * pth); +dux_err dux_crtdir(char const * pth,dux_prm prm); + +dux_err dux_crt( dux_fil * * fil,char const * pth,dux_prm prm); +dux_err dux_opn( dux_fil * * fil,char const * pth); +dux_err dux_opnwr(dux_fil * * fil,char const * pth,struct dux_prv_dsc); + +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); diff --git a/dux/source/dux/abr.c b/dux/source/dux/abr.c new file mode 100644 index 0000000..24fbee3 --- /dev/null +++ b/dux/source/dux/abr.c @@ -0,0 +1,19 @@ +/* + 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/dux.h> + +#include <linux/unistd.h> +#include <signal.h> +#include <sys/types.h> + +void dux_abr(void) { + zp_syscal(__NR_kill,(pid_t)zp_syscal(__NR_getpid),SIGABRT); + zp_unrch(); +} diff --git a/dux/source/dux/errmsg.c b/dux/source/dux/errmsg.c index 9b4fe99..f0ef2ce 100644 --- a/dux/source/dux/errmsg.c +++ b/dux/source/dux/errmsg.c @@ -21,8 +21,8 @@ char const * dux_errmsg(dux_err const err) { return "bad file"; case dux_err_badfmt: return "bad format specifier"; - case dux_err_badprm: - return "bad permissions"; + case dux_err_badprv: + return "bad privileges"; case dux_err_badval: return "bad value"; case dux_err_eof: diff --git a/dux/source/io/cls.c b/dux/source/io/cls.c new file mode 100644 index 0000000..17af339 --- /dev/null +++ b/dux/source/io/cls.c @@ -0,0 +1,35 @@ +/* + 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 <stdlib.h> + +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); + + if (cd == -0x1) { + switch (*__errno_location()) { + default: + goto free; + case EINTR: + goto retry; + } + } + +free:; + free(fil); + + return dux_err_oky; +} diff --git a/dux/source/io/crt.c b/dux/source/io/crt.c new file mode 100644 index 0000000..aab7a11 --- /dev/null +++ b/dux/source/io/crt.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_crt(dux_fil * * const filptr,char const * const pth,zp_i01 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) { + int const err = *__errno_location(); + + if (err == EINTR) {goto retry;} + + 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/crtdir.c b/dux/source/io/crtdir.c new file mode 100644 index 0000000..85fb181 --- /dev/null +++ b/dux/source/io/crtdir.c @@ -0,0 +1,35 @@ +/* + 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/stat.h> +#include <sys/types.h> + +extern int * __errno_location(void); + +dux_err dux_crtdir(char const * const pth,zp_i01 const prm) { + int const cod = (int)zp_syscal(__NR_mkdir,pth,(mode_t)prm); + if (cod == -0x1) { + switch (*__errno_location()) { + default: + return dux_err_err; + case EACCES: + return dux_err_badprv; + case EEXIST: + break; + case EROFS: + return dux_err_redonl; + } + } + + return dux_err_oky; +} diff --git a/dux/source/io/wrt.c b/dux/source/io/wrt.c new file mode 100644 index 0000000..62184b0 --- /dev/null +++ b/dux/source/io/wrt.c @@ -0,0 +1,45 @@ +/* + 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_wrt(dux_fil * const restrict fil,void const * const restrict voiddat,zp_siz const num) { + zp_unlik (num == 0x0u) {return dux_err_oky;} + + unsigned char const * dat = 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) { + switch (*__errno_location()) { + default: + return dux_err_err; + case EBADF: + return dux_err_badfil; + case EFBIG: + return dux_err_spclim; + case EINTR: + continue; + case ENOSPC: + return dux_err_spclim; + } + } + + dat += (size_t)cod; + rem -= (size_t)cod; + } + + return dux_err_oky; +} diff --git a/dux/source/io/wrtstr.c b/dux/source/io/wrtstr.c new file mode 100644 index 0000000..8d6e3f9 --- /dev/null +++ b/dux/source/io/wrtstr.c @@ -0,0 +1,16 @@ +/* + 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 <zp/str.h> + +dux_err dux_wrtstr(dux_fil * const restrict fil,char const * const restrict str) { + return dux_wrt(fil,str,zp_strlen(str)); +} |