summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.txt11
-rw-r--r--demo.cc74
-rw-r--r--dux/GNUmakefile7
-rw-r--r--dux/include/dux/dux.h6
-rw-r--r--dux/include/dux/io.h28
-rw-r--r--dux/source/dux/envvar.c41
-rw-r--r--dux/source/dux/errmsg.c8
-rw-r--r--dux/source/io/cpy.c6
-rw-r--r--dux/source/io/crt.c2
-rw-r--r--dux/source/io/crtdir.c2
-rw-r--r--dux/source/io/curdir.c14
-rw-r--r--dux/source/io/homdir.c14
-rw-r--r--dux/source/io/opn.c2
-rw-r--r--dux/source/io/red.c2
-rw-r--r--dux/source/io/sttpth.c2
-rw-r--r--dux/source/io/wrt.c4
-rw-r--r--dux/source/io/wrtstr.c2
17 files changed, 190 insertions, 35 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 285e5ba..e695960 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -78,6 +78,17 @@
- Fix installation target;
- Update gitignore;
+- Perform better error handling in cpy;
+- Add new error 'memlim';
+- Add new error 'io';
+- Implement homdir;
+- Rename opnwr to opnrw;
+- Add function for getting environment variables: envvar;
+- Make homdir and curdir write to user-provided buffers;
+- Update demo;
+- Implement curdir;
+- Update coding style with regards to pointers and references;
+
| 2↋
- Fix signal pipe being renamed to file;
diff --git a/demo.cc b/demo.cc
index 534c7d9..de1bd73 100644
--- a/demo.cc
+++ b/demo.cc
@@ -1,22 +1,77 @@
-#include <cassert>
#include <cstdlib>
#include <dux/io.h>
#include <iostream>
#include <zp/str>
+template<typename ltyp,typename rtyp> static auto chk(long const lin,::zp::i8 const mth,ltyp const& lval,rtyp const& rval) {
+ auto const cmp = [](::zp::i8 const mth,auto const& lval,auto const& rval) -> bool {
+ switch (mth) {
+ default:
+ ::zp::unrch();
+ case 0x0u:
+ return lval == rval;
+ case 0x1u:
+ return lval >= rval;
+ case 0x2u:
+ return lval > rval;
+ case 0x3u:
+ return lval <= rval;
+ case 0x4u:
+ return lval < rval;
+ case 0x5u:
+ return lval != rval;
+ }
+ };
+
+ auto const str = [](::zp::i8 const mth) -> char const * {
+ switch (mth) {
+ default:
+ ::zp::unrch();
+ case 0x0u:
+ return "!=";
+ case 0x1u:
+ return "<";
+ case 0x2u:
+ return "<=";
+ case 0x3u:
+ return ">";
+ case 0x4u:
+ return ">=";
+ case 0x5u:
+ return "==";
+ }
+ };
+
+ if (!cmp(mth,lval,rval)) {
+ ::std::cout << lin << ": " << lval << ' ' << str(mth) << ' ' << rval << ::std::endl;
+ ::std::exit(EXIT_SUCCESS);
+ }
+};
+#define chkeq(lval,rval) (::chk(__LINE__,0x0u,(lval),(rval)))
+#define chkge(lval,rval) (::chk(__LINE__,0x1u,(lval),(rval)))
+#define chkgt(lval,rval) (::chk(__LINE__,0x2u,(lval),(rval)))
+#define chkle(lval,rval) (::chk(__LINE__,0x3u,(lval),(rval)))
+#define chklt(lval,rval) (::chk(__LINE__,0x4u,(lval),(rval)))
+#define chkne(lval,rval) (::chk(__LINE__,0x5u,(lval),(rval)))
+
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;
- auto const chkerr = [&err](char const * const msg) noexcept {
+ 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);
}
};
+ char * const homdir = new char[::dux_homdir(nullptr)];
+ ::dux_homdir(homdir);
+ ::std::cout << "Home directory: " << homdir << ::std::endl;
+ delete[] homdir;
+
err = ::dux_crtdir("testdir",0755u);
chkerr("unable to create directory");
@@ -25,7 +80,7 @@ int main() {
err = ::dux_crt(&fil,"testdir/dux-demo-file",0644u);
chkerr("unable to create file");
- char const src[] =
+ constexpr char src[] =
"static void msg(void);\n"
"\n"
"int main(void) {\n"
@@ -38,6 +93,7 @@ int main() {
"static void msg(void) {\n"
"\tly_wrtstr(ly_dflout,\"flux ftw\\n\");\n"
"} /* Remember the last newline -> */\n";
+ constexpr ::zp::siz srclen = sizeof (src)-0x1u;
err = ::dux_wrtstr(fil,src);
chkerr("unable to write to file");
@@ -49,7 +105,7 @@ int main() {
err = ::dux_sttpth(&pthinf,"testdir/dux-demo-file");
chkerr("unable to stat file");
- assert(pthinf.siz == sizeof (src)-0x1u);
+ chkeq(pthinf.siz,srclen);
err = ::dux_cpy("testdir/dux-demo-file.c","testdir/dux-demo-file",0644u);
chkerr("unable to copy file");
@@ -57,12 +113,16 @@ int main() {
err = ::dux_opn(&fil,"testdir/dux-demo-file.c");
chkerr("unable to open file");
- char buf[sizeof (src)];
+ char buf[srclen+0x1u];
+ buf[srclen] = '\x00';
- err = ::dux_red(buf,fil,sizeof (src),nullptr);
+ ::zp::siz numred = 0x0u;
+ err = ::dux_red(buf,fil,srclen,&numred);
chkerr("unable to read file");
- assert(::zp::strequ(buf,src));
+ chkeq(numred,srclen);
+
+ chkeq(::zp::strequ(buf,src),true);
err = ::dux_cls(fil);
chkerr("unable to close file");
diff --git a/dux/GNUmakefile b/dux/GNUmakefile
index c083dca..e12f954 100644
--- a/dux/GNUmakefile
+++ b/dux/GNUmakefile
@@ -4,12 +4,15 @@ endif
OBJS := \
source/dux/abr.o \
+ source/dux/envvar.o \
source/dux/errmsg.o \
\
source/io/cls.o \
+ source/io/crt.o \
source/io/crtdir.o \
source/io/cpy.o \
- source/io/crt.o \
+ source/io/curdir.o \
+ source/io/homdir.o \
source/io/opn.o \
source/io/red.o \
source/io/sttpth.o \
@@ -55,7 +58,7 @@ $(OBJS): $(HDRS)
install: $(LIB)
mkdir -pvm755 "$(HDRDIR)/dux"
mkdir -pvm755 "$(LIBDIR)"
- install -vm644 "include/dux/"{"dux","io"}".h" "$(HDRDIR)/dux"
+ install -vm644 "include/dux/"{"dux","io","thr"}".h" "$(HDRDIR)/dux"
install -vm755 "$(LIB)" "$(LIBDIR)"
clean:
diff --git a/dux/include/dux/dux.h b/dux/include/dux/dux.h
index 538c2d5..23baa4e 100644
--- a/dux/include/dux/dux.h
+++ b/dux/include/dux/dux.h
@@ -35,7 +35,9 @@ typedef enum {
dux_err_badval,
dux_err_eof,
dux_err_exs,
+ dux_err_io,
dux_err_isdir,
+ dux_err_memlim,
dux_err_nodir,
dux_err_nofil,
dux_err_redonl,
@@ -44,7 +46,9 @@ typedef enum {
zp_noret void dux_abr(void);
-zp_unseq char const * dux_errmsg(dux_err err);
+zp_unseq char const* dux_errmsg(dux_err err);
+
+zp_sizerr dux_envvar(char * buf,char const* nam);
dux_prv_cdecend
diff --git a/dux/include/dux/io.h b/dux/include/dux/io.h
index 4778988..ea16bfa 100644
--- a/dux/include/dux/io.h
+++ b/dux/include/dux/io.h
@@ -42,28 +42,28 @@ extern dux_fil * dux_dfli;
extern dux_fil * dux_dflo;
extern dux_fil * dux_log;
-char const * dux_curdir(void);
-char const * dux_homdir(void);
+zp_sizerr dux_curdir(char * buf);
+zp_sizerr dux_homdir(char * buf);
-dux_err dux_chgdir(char const * pth);
+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_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_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_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_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);
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_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_prv_cdecend
diff --git a/dux/source/dux/envvar.c b/dux/source/dux/envvar.c
new file mode 100644
index 0000000..bbb4690
--- /dev/null
+++ b/dux/source/dux/envvar.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/dux.h>
+
+#include <zp/mem.h>
+#include <zp/str.h>
+
+extern char * * __environ;
+
+zp_sizerr dux_envvar(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 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;
+
+ if (buf != zp_nulptr) {zp_memcpy(buf,val,valsiz);}
+
+ return valsiz;
+
+ nxt:;
+ }
+
+ return -0x1;
+}
diff --git a/dux/source/dux/errmsg.c b/dux/source/dux/errmsg.c
index f0ef2ce..31381a1 100644
--- a/dux/source/dux/errmsg.c
+++ b/dux/source/dux/errmsg.c
@@ -9,12 +9,12 @@
#include <dux/prv/dux.h>
-char const * dux_errmsg(dux_err const err) {
+char const* dux_errmsg(dux_err const err) {
switch (err) {
case dux_err_oky:
return "okay";
case dux_err_err:
- return "error";
+ return "generic error";
case dux_err_badalc:
return "bad memory allocation";
case dux_err_badfil:
@@ -29,8 +29,12 @@ char const * dux_errmsg(dux_err const err) {
return "end of file";
case dux_err_exs:
return "file already exists";
+ case dux_err_io:
+ return "input/output error";
case dux_err_isdir:
return "is directory";
+ case dux_err_memlim:
+ return "memory limit reached";
case dux_err_nodir:
return "no such directory";
case dux_err_nofil:
diff --git a/dux/source/io/cpy.c b/dux/source/io/cpy.c
index 83db80c..652ef71 100644
--- a/dux/source/io/cpy.c
+++ b/dux/source/io/cpy.c
@@ -17,7 +17,7 @@
extern int * __errno_location(void);
-dux_err dux_cpy(char const * const newpth,char const * const pth,dux_prm const prm) {
+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;}
@@ -31,6 +31,10 @@ dux_err dux_cpy(char const * const newpth,char const * const pth,dux_prm const p
switch (*__errno_location()) {
default:
return dux_err_err;
+ case EIO:
+ return dux_err_io;
+ case ENOMEM:
+ return dux_err_memlim;
}
}
diff --git a/dux/source/io/crt.c b/dux/source/io/crt.c
index b328519..a2552b1 100644
--- a/dux/source/io/crt.c
+++ b/dux/source/io/crt.c
@@ -16,7 +16,7 @@
extern int * __errno_location(void);
-dux_err dux_crt(dux_fil * * const filptr,char const * const pth,dux_prm 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;}
diff --git a/dux/source/io/crtdir.c b/dux/source/io/crtdir.c
index 54bfdd5..c77edb4 100644
--- a/dux/source/io/crtdir.c
+++ b/dux/source/io/crtdir.c
@@ -16,7 +16,7 @@
extern int * __errno_location(void);
-dux_err dux_crtdir(char const * const pth,dux_prm 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);
zp_unlik (cod == -0x1) {
switch (*__errno_location()) {
diff --git a/dux/source/io/curdir.c b/dux/source/io/curdir.c
new file mode 100644
index 0000000..ac437a4
--- /dev/null
+++ b/dux/source/io/curdir.c
@@ -0,0 +1,14 @@
+/*
+ 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>
+
+zp_sizerr dux_curdir(char * const restrict buf) {
+ return dux_envvar(buf,"PWD"); // This is only temporary and may not always work.
+}
diff --git a/dux/source/io/homdir.c b/dux/source/io/homdir.c
new file mode 100644
index 0000000..3725985
--- /dev/null
+++ b/dux/source/io/homdir.c
@@ -0,0 +1,14 @@
+/*
+ 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>
+
+zp_sizerr dux_homdir(char * const restrict buf) {
+ return dux_envvar(buf,"HOME");
+}
diff --git a/dux/source/io/opn.c b/dux/source/io/opn.c
index 863ae69..43f95b2 100644
--- a/dux/source/io/opn.c
+++ b/dux/source/io/opn.c
@@ -16,7 +16,7 @@
extern int * __errno_location(void);
-dux_err dux_opn(dux_fil * * const filptr,char const * const pth) {
+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;}
diff --git a/dux/source/io/red.c b/dux/source/io/red.c
index 6ce5958..c876d5a 100644
--- a/dux/source/io/red.c
+++ b/dux/source/io/red.c
@@ -18,7 +18,7 @@ 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;
+ 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);
diff --git a/dux/source/io/sttpth.c b/dux/source/io/sttpth.c
index 7e4abb9..ceda9dc 100644
--- a/dux/source/io/sttpth.c
+++ b/dux/source/io/sttpth.c
@@ -16,7 +16,7 @@
extern int * __errno_location(void);
-dux_err dux_sttpth(dux_pthinf * const infptr,char const * const pth) {
+dux_err dux_sttpth(dux_pthinf * const infptr,char const* const pth) {
dux_pthinf inf;
struct stat sysinf;
diff --git a/dux/source/io/wrt.c b/dux/source/io/wrt.c
index 45e585f..7472abf 100644
--- a/dux/source/io/wrt.c
+++ b/dux/source/io/wrt.c
@@ -15,10 +15,10 @@
extern int * __errno_location(void);
-dux_err dux_wrt(dux_fil * const restrict fil,void const * const restrict voiddat,zp_siz const num) {
+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;
+ 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);
diff --git a/dux/source/io/wrtstr.c b/dux/source/io/wrtstr.c
index 8d6e3f9..efa03eb 100644
--- a/dux/source/io/wrtstr.c
+++ b/dux/source/io/wrtstr.c
@@ -11,6 +11,6 @@
#include <zp/str.h>
-dux_err dux_wrtstr(dux_fil * const restrict fil,char const * const restrict str) {
+dux_err dux_wrtstr(dux_fil * const restrict fil,char const* const restrict str) {
return dux_wrt(fil,str,zp_strlen(str));
}