summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--CHANGELOG.txt12
-rw-r--r--demo.cc16
-rw-r--r--dux/GNUmakefile18
-rw-r--r--dux/include/dux/dux.h12
-rw-r--r--dux/include/dux/fs.h54
-rw-r--r--dux/include/dux/io.h39
-rw-r--r--dux/source/dux/abr.c6
-rw-r--r--dux/source/dux/errmsg.c61
-rw-r--r--dux/source/dux/getenv.c (renamed from dux/source/dux/envvar.c)10
-rw-r--r--dux/source/fs/cpy.c (renamed from dux/source/io/cpy.c)0
-rw-r--r--dux/source/fs/crtdir.c (renamed from dux/source/io/crtdir.c)0
-rw-r--r--dux/source/fs/curdir.c (renamed from dux/source/io/curdir.c)2
-rw-r--r--dux/source/fs/homdir.c (renamed from dux/source/io/homdir.c)2
-rw-r--r--dux/source/fs/mov.c41
-rw-r--r--dux/source/fs/setprm.c (renamed from dux/source/io/wrtstr.c)24
-rw-r--r--dux/source/fs/sttpth.c (renamed from dux/source/io/sttpth.c)0
-rw-r--r--dux/source/io/opn.c16
-rw-r--r--dux/source/io/wrt.c5
19 files changed, 238 insertions, 81 deletions
diff --git a/.gitignore b/.gitignore
index 4a58a83..1df0c5e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@
*.so
/demo
/testdir
+vgcore.*
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index e695960..1fc68c2 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -89,6 +89,18 @@
- Implement curdir;
- Update coding style with regards to pointers and references;
+- Implement opnrw;
+- Implement setprm;
+- Add new module 'fs';
+- Move to fs: chgdir, crtdir, cpy, curdir, homdir, mov, rem, setprm, and sttpth;
+- Rename envvar to getenv;
+- Bring back message to abr;
+- Make errmsg write message to caller-provided buffer;
+- Make getenv, homdir, and curdir not include null-terminator in output;
+- Update gitignore;
+- Implement wrtstr in the same file as wrt;
+- Implement mov;
+
| 2↋
- Fix signal pipe being renamed to file;
diff --git a/demo.cc b/demo.cc
index de1bd73..d97eead 100644
--- a/demo.cc
+++ b/demo.cc
@@ -62,13 +62,25 @@ int main() {
auto const chkerr = [&err](char const* const msg) noexcept {
if (err != ::dux_err_oky) {
- ::std::cout << msg << ": " << ::dux_errmsg(err) << ::std::endl;
+ ::zp::siz const errmsglen = ::dux_errmsg(nullptr,err);
+ char * const errmsg = new char[errmsglen+0x1u];
+
+ ::dux_errmsg(errmsg,err);
+ errmsg[errmsglen] = '\x00';
+
+ ::std::cout << msg << ": " << errmsg << ::std::endl;
+ delete[] errmsg;
+
::std::exit(EXIT_FAILURE);
}
};
- char * const homdir = new char[::dux_homdir(nullptr)];
+ ::zp::siz const homdirlen = ::dux_homdir(nullptr);
+ char * const homdir = new char[homdirlen+0x1u];
+
::dux_homdir(homdir);
+ homdir[homdirlen] = '\x00';
+
::std::cout << "Home directory: " << homdir << ::std::endl;
delete[] homdir;
diff --git a/dux/GNUmakefile b/dux/GNUmakefile
index e12f954..1c7ae20 100644
--- a/dux/GNUmakefile
+++ b/dux/GNUmakefile
@@ -4,20 +4,22 @@ endif
OBJS := \
source/dux/abr.o \
- source/dux/envvar.o \
source/dux/errmsg.o \
+ source/dux/getenv.o \
+ \
+ source/fs/crtdir.o \
+ source/fs/cpy.o \
+ source/fs/curdir.o \
+ source/fs/homdir.o \
+ source/fs/mov.o \
+ source/fs/setprm.o \
+ source/fs/sttpth.o \
\
source/io/cls.o \
source/io/crt.o \
- source/io/crtdir.o \
- source/io/cpy.o \
- source/io/curdir.o \
- source/io/homdir.o \
source/io/opn.o \
source/io/red.o \
- source/io/sttpth.o \
- source/io/wrt.o \
- source/io/wrtstr.o
+ source/io/wrt.o
HDRS := \
include/dux/dux.h \
diff --git a/dux/include/dux/dux.h b/dux/include/dux/dux.h
index 23baa4e..4ba484a 100644
--- a/dux/include/dux/dux.h
+++ b/dux/include/dux/dux.h
@@ -7,6 +7,12 @@
You should have received a copy of the GNU Lesser General Public License along with dux. If not, see <https://www.gnu.org/licenses>.
*/
+/*
+ Header dependencies:
+
+ zp/zp.h > dux/dux.h > dux/fs.h > dux/io.h
+*/
+
#if !defined(dux_hdr_dux)
#define dux_hdr_dux
@@ -44,11 +50,11 @@ typedef enum {
dux_err_spclim,
} dux_err;
-zp_noret void dux_abr(void);
+zp_noret void dux_abr(char const* msg);
-zp_unseq char const* dux_errmsg(dux_err err);
+zp_sizerr dux_errmsg(char * buf,dux_err err);
-zp_sizerr dux_envvar(char * buf,char const* nam);
+zp_sizerr dux_getenv(char * buf,char const* nam);
dux_prv_cdecend
diff --git a/dux/include/dux/fs.h b/dux/include/dux/fs.h
new file mode 100644
index 0000000..ebb821e
--- /dev/null
+++ b/dux/include/dux/fs.h
@@ -0,0 +1,54 @@
+/*
+ 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_fs)
+#define dux_hdr_fs
+
+#include <dux/dux.h>
+
+dux_prv_cdec
+
+typedef zp_i01 dux_prm;
+
+struct dux_prv_dsc {
+ bool _dsc;
+};
+
+#if zp_std_cxx
+#define dux_kep (::dux_prv_dsc {false,})
+#define dux_dsc (::dux_prv_dsc {true,})
+#else
+#define dux_kep ((struct dux_prv_dsc) {._dsc = false,})
+#define dux_dsc ((struct dux_prv_dsc) {._dsc = true,})
+#endif
+
+typedef struct {
+ zp_siz siz;
+ dux_prm prm;
+ bool isdir : 0x1;
+ bool isreg : 0x1;
+} dux_pthinf;
+
+zp_sizerr dux_curdir(char * buf);
+zp_sizerr dux_homdir(char * buf);
+
+dux_err dux_chgdir(char const* pth);
+
+dux_err dux_setprm(char const* pth,dux_prm prm);
+dux_err dux_sttpth(dux_pthinf * inf,char const* pth);
+
+dux_err dux_crtdir(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_prv_cdecend
+
+#endif
diff --git a/dux/include/dux/io.h b/dux/include/dux/io.h
index ea16bfa..f524a77 100644
--- a/dux/include/dux/io.h
+++ b/dux/include/dux/io.h
@@ -10,31 +10,10 @@
#if !defined(dux_hdr_io)
#define dux_hdr_io
-#include <dux/dux.h>
+#include <dux/fs.h>
dux_prv_cdec
-typedef zp_i01 dux_prm;
-
-struct dux_prv_dsc {
- bool _dsc;
-};
-
-#if zp_std_cxx
-#define dux_kep (::dux_prv_dsc {false,})
-#define dux_dsc (::dux_prv_dsc {true,})
-#else
-#define dux_kep ((struct dux_prv_dsc) {._dsc = false,})
-#define dux_dsc ((struct dux_prv_dsc) {._dsc = true,})
-#endif
-
-typedef struct {
- zp_siz siz;
- dux_prm prm;
- bool isdir : 0x1;
- bool isreg : 0x1;
-} dux_pthinf;
-
struct dux_prv_fil;
typedef struct dux_prv_fil dux_fil;
@@ -42,20 +21,6 @@ extern dux_fil * dux_dfli;
extern dux_fil * dux_dflo;
extern dux_fil * dux_log;
-zp_sizerr dux_curdir(char * buf);
-zp_sizerr dux_homdir(char * buf);
-
-dux_err dux_chgdir(char const* pth);
-
-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);
-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_opnrw(dux_fil * * fil,char const* pth,struct dux_prv_dsc);
@@ -64,7 +29,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 * numred);
+dux_err dux_red( void * buf,dux_fil * fil,zp_siz num,zp_siz * numred);
dux_prv_cdecend
diff --git a/dux/source/dux/abr.c b/dux/source/dux/abr.c
index 24fbee3..e09830b 100644
--- a/dux/source/dux/abr.c
+++ b/dux/source/dux/abr.c
@@ -12,8 +12,12 @@
#include <linux/unistd.h>
#include <signal.h>
#include <sys/types.h>
+#include <unistd.h>
+#include <zp/str.h>
+
+void dux_abr(char const * const restrict msg) {
+ zp_syscal(__NR_write,STDERR_FILENO,msg,zp_strlen(msg));
-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 31381a1..247152a 100644
--- a/dux/source/dux/errmsg.c
+++ b/dux/source/dux/errmsg.c
@@ -9,40 +9,65 @@
#include <dux/prv/dux.h>
-char const* dux_errmsg(dux_err const err) {
+#include <zp/mem.h>
+
+#define set(_msg) (msg = (_msg),len = sizeof ((_msg))-0x1u)
+
+zp_sizerr dux_errmsg(char * const restrict buf,dux_err const err) {
+ char const* msg;
+ zp_siz len;
+
switch (err) {
case dux_err_oky:
- return "okay";
+ set("okay");
+ break;
case dux_err_err:
- return "generic error";
+ set("generic error");
+ break;
case dux_err_badalc:
- return "bad memory allocation";
+ set("bad memory allocation");
+ break;
case dux_err_badfil:
- return "bad file";
+ set("bad file");
+ break;
case dux_err_badfmt:
- return "bad format specifier";
+ set("bad format specifier");
+ break;
case dux_err_badprv:
- return "bad privileges";
+ set("bad privileges");
+ break;
case dux_err_badval:
- return "bad value";
+ set("bad value");
+ break;
case dux_err_eof:
- return "end of file";
+ set("end of file");
+ break;
case dux_err_exs:
- return "file already exists";
+ set("file already exists");
+ break;
case dux_err_io:
- return "input/output error";
+ set("input/output error");
+ break;
case dux_err_isdir:
- return "is directory";
+ set("is directory");
+ break;
case dux_err_memlim:
- return "memory limit reached";
+ set("memory limit reached");
+ break;
case dux_err_nodir:
- return "no such directory";
+ set("no such directory");
+ break;
case dux_err_nofil:
- return "no such file or directory";
+ set("no such file or directory");
+ break;
case dux_err_redonl:
- return "file is read only";
+ set("file is read only");
+ break;
case dux_err_spclim:
- return "space limit reached";
+ set("space limit reached");
+ break;
}
- zp_unrch();
+
+ if (buf != zp_nulptr) {zp_memcpy(buf,msg,len);}
+ return len;
}
diff --git a/dux/source/dux/envvar.c b/dux/source/dux/getenv.c
index bbb4690..3080a96 100644
--- a/dux/source/dux/envvar.c
+++ b/dux/source/dux/getenv.c
@@ -14,25 +14,25 @@
extern char * * __environ;
-zp_sizerr dux_envvar(char * const restrict buf,char const* const restrict nam) {
+zp_sizerr dux_getenv(char * const restrict buf,char const* const restrict nam) {
zp_unlik (*nam == '\x00') {return -0x1;}
char * * env = __environ;
for (char * var = *env++;var != zp_nulptr;var = *env++) {
char * const equpos = zp_strsrh(var,'=');
- char * pos = var;
+ char * pos = var;
char const* nampos = nam;
for (;pos != equpos;) {
zp_lik (*pos++ != *nampos++) {goto nxt;}
}
char * const val = equpos+0x1u;
- zp_siz const valsiz = zp_strlen(val)+0x1u;
+ zp_siz const vallen = zp_strlen(val);
- if (buf != zp_nulptr) {zp_memcpy(buf,val,valsiz);}
+ if (buf != zp_nulptr) {zp_memcpy(buf,val,vallen);}
- return valsiz;
+ return vallen;
nxt:;
}
diff --git a/dux/source/io/cpy.c b/dux/source/fs/cpy.c
index 652ef71..652ef71 100644
--- a/dux/source/io/cpy.c
+++ b/dux/source/fs/cpy.c
diff --git a/dux/source/io/crtdir.c b/dux/source/fs/crtdir.c
index c77edb4..c77edb4 100644
--- a/dux/source/io/crtdir.c
+++ b/dux/source/fs/crtdir.c
diff --git a/dux/source/io/curdir.c b/dux/source/fs/curdir.c
index ac437a4..c28fae8 100644
--- a/dux/source/io/curdir.c
+++ b/dux/source/fs/curdir.c
@@ -10,5 +10,5 @@
#include <dux/prv/io.h>
zp_sizerr dux_curdir(char * const restrict buf) {
- return dux_envvar(buf,"PWD"); // This is only temporary and may not always work.
+ return dux_getenv(buf,"PWD"); // This is only temporary and may not always work.
}
diff --git a/dux/source/io/homdir.c b/dux/source/fs/homdir.c
index 3725985..c023fe4 100644
--- a/dux/source/io/homdir.c
+++ b/dux/source/fs/homdir.c
@@ -10,5 +10,5 @@
#include <dux/prv/io.h>
zp_sizerr dux_homdir(char * const restrict buf) {
- return dux_envvar(buf,"HOME");
+ return dux_getenv(buf,"HOME");
}
diff --git a/dux/source/fs/mov.c b/dux/source/fs/mov.c
new file mode 100644
index 0000000..99a4f3e
--- /dev/null
+++ b/dux/source/fs/mov.c
@@ -0,0 +1,41 @@
+/*
+ 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_mov(char const* const newpth,char const* const pth) {
+ int const cod = (int)zp_syscal(__NR_renameat,AT_FDCWD,pth,AT_FDCWD,newpth);
+ zp_unlik (cod == -0x1) {
+ int const err = *__errno_location();
+
+ switch (err) {
+ default:
+ return dux_err_err;
+ case EACCES:
+ return dux_err_badprv;
+ case EDQUOT:
+ case ENOSPC:
+ return dux_err_spclim;
+ case ENOTDIR:
+ return dux_err_nodir;
+ case EROFS:
+ return dux_err_io;
+ }
+ }
+
+ return dux_err_oky;
+}
diff --git a/dux/source/io/wrtstr.c b/dux/source/fs/setprm.c
index efa03eb..d591872 100644
--- a/dux/source/io/wrtstr.c
+++ b/dux/source/fs/setprm.c
@@ -9,8 +9,26 @@
#include <dux/prv/io.h>
-#include <zp/str.h>
+#include <fcntl.h>
+#include <linux/errno.h>
+#include <linux/unistd.h>
+#include <stdlib.h>
-dux_err dux_wrtstr(dux_fil * const restrict fil,char const* const restrict str) {
- return dux_wrt(fil,str,zp_strlen(str));
+extern int * __errno_location(void);
+
+dux_err dux_setprm(char const* const pth,dux_prm const prm) {
+set:;
+ int const cod = (int)zp_syscal(__NR_chmod,pth,(mode_t)prm);
+ zp_unlik (cod == -0x1) {
+ int const err = *__errno_location();
+
+ zp_lik (err == EINTR) {goto set;}
+
+ switch (err) {
+ default:
+ return dux_err_err;
+ }
+ }
+
+ return dux_err_oky;
}
diff --git a/dux/source/io/sttpth.c b/dux/source/fs/sttpth.c
index ceda9dc..ceda9dc 100644
--- a/dux/source/io/sttpth.c
+++ b/dux/source/fs/sttpth.c
diff --git a/dux/source/io/opn.c b/dux/source/io/opn.c
index 43f95b2..bc02f91 100644
--- a/dux/source/io/opn.c
+++ b/dux/source/io/opn.c
@@ -16,12 +16,16 @@
extern int * __errno_location(void);
-dux_err dux_opn(dux_fil * * const filptr,char const* const pth) {
+static dux_err dux_det_opn(dux_fil * * const filptr,char const* const pth,bool const wrt,struct dux_prv_dsc const dsc) {
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);
+ int flg = 0x0;
+ flg |= wrt ? O_RDWR : O_RDONLY;
+ flg |= dsc._dsc ? O_TRUNC : 0x0;
+
+ int const fd = (int)zp_syscal(__NR_openat,AT_FDCWD,pth,flg,(mode_t)0x0);
zp_unlik (fd == -0x1) {
int const err = *__errno_location();
@@ -47,3 +51,11 @@ opn:;
return dux_err_oky;
}
+
+dux_err dux_opn(dux_fil * * const fil,char const* const pth) {
+ return dux_det_opn(fil,pth,false,dux_kep);
+}
+
+dux_err dux_opnrw(dux_fil * * const fil,char const* const pth,struct dux_prv_dsc const dsc) {
+ return dux_det_opn(fil,pth,true,dsc);
+}
diff --git a/dux/source/io/wrt.c b/dux/source/io/wrt.c
index 7472abf..685f5a4 100644
--- a/dux/source/io/wrt.c
+++ b/dux/source/io/wrt.c
@@ -12,6 +12,7 @@
#include <errno.h>
#include <linux/unistd.h>
#include <sys/types.h>
+#include <zp/str.h>
extern int * __errno_location(void);
@@ -43,3 +44,7 @@ dux_err dux_wrt(dux_fil * const restrict fil,void const* const restrict voiddat,
return dux_err_oky;
}
+
+dux_err dux_wrtstr(dux_fil * const restrict fil,char const* const restrict str) {
+ return dux_wrt(fil,str,zp_strlen(str));
+}