shithub: npe

Download patch

ref: cb690109707ea6e97f9558e5806d48786b7ffce3
parent: 4ded9d708d8caec3b5b1c48c8887e8e3a838d669
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Fri Mar 19 05:57:13 EDT 2021

stat: extend with S_IF* and errno

--- /dev/null
+++ b/include/npe/errno.h
@@ -1,0 +1,12 @@
+#ifndef _npe_errno_h_
+#define _npe_errno_h_
+
+#include <npe.h>
+
+enum {
+	ENOENT = 2,
+};
+
+extern int errno;
+
+#endif
--- a/include/npe/unistd.h
+++ b/include/npe/unistd.h
@@ -4,29 +4,38 @@
 #include <npe.h>
 
 enum {
-	S_IFMT = 0xff,
-	S_IFDIR = 1<<0,
-
 	F_OK = AEXIST,
 	R_OK = AREAD,
 	W_OK = AWRITE,
 	X_OK = AEXEC,
 
-	S_ISUID = 04000,
-	S_ISGID = 02000,
-	S_IRWXU = 00700,
-	S_IRUSR = 00400,
-	S_IWUSR = 00200,
-	S_IXUSR = 00100,
-	S_IRWXG = 00070,
-	S_IRGRP = 00040,
-	S_IWGRP = 00020,
-	S_IXGRP = 00010,
-	S_IRWXO = 00007,
-	S_IROTH = 00004,
-	S_IWOTH = 00002,
-	S_IXOTH = 00001,
+	S_IFSOCK = -1,
+	S_IFLNK = -1,
+	S_IFBLK = -1,
+	S_IFCHR = -1,
+	S_IFIFO = -1,
+
+	S_IFMT  = 0170000,
+	S_IFREG = 0100000,
+	S_IFDIR = 0040000,
+	S_ISUID = 0004000,
+	S_ISGID = 0002000,
+	S_IRWXU = 0000700,
+	S_IRUSR = 0000400,
+	S_IWUSR = 0000200,
+	S_IXUSR = 0000100,
+	S_IRWXG = 0000070,
+	S_IRGRP = 0000040,
+	S_IWGRP = 0000020,
+	S_IXGRP = 0000010,
+	S_IRWXO = 0000007,
+	S_IROTH = 0000004,
+	S_IWOTH = 0000002,
+	S_IXOTH = 0000001,
 };
+
+#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
+#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
 
 typedef unsigned useconds_t;
 
--- a/libnpe/_npe.c
+++ b/libnpe/_npe.c
@@ -3,6 +3,8 @@
 #include <sys/stat.h>
 #include "_npe.h"
 
+int errno = 0;
+
 /*
  * nsec() is wallclock and can be adjusted by timesync
  * so need to use cycles() instead, but fall back to
--- a/libnpe/opendir.c
+++ b/libnpe/opendir.c
@@ -1,4 +1,5 @@
 #include <dirent.h>
+#include <errno.h>
 #include "_dirent.h"
 
 DIR *
@@ -18,8 +19,12 @@
 	}
 
 	free(dir);
-	if(d == nil)
-		close(fd);
+	if(d == nil){
+		if(fd >= 0)
+			close(fd);
+		else
+			errno = ENOENT;
+	}
 
 	return d;
 }
--- a/libnpe/stat.c
+++ b/libnpe/stat.c
@@ -1,4 +1,5 @@
 #include <unistd.h>
+#include <errno.h>
 
 #undef stat
 
@@ -7,12 +8,19 @@
 {
 	Dir *d;
 
-	if((d = dirstat(filename)) == nil)
+	if((d = dirstat(filename)) == nil){
+		errno = ENOENT;
 		return -1;
+	}
 
 	buf->st_size = d->length;
-	buf->st_mode = d->mode;
 	buf->st_mtime = d->mtime;
+	buf->st_mode = d->mode & 0777;
+	if(d->mode & DMDIR)
+		buf->st_mode |= S_IFDIR;
+	else
+		buf->st_mode |= S_IFREG;
+
 	free(d);
 
 	return 0;