changeset 1080:84d567d1c641

misc: remove non POSIX extensions
author David Demelier <markand@malikania.fr>
date Mon, 19 Jul 2021 09:14:15 +0200
parents 8f26ee9cc6dd
children 617686cb4e67
files GNUmakefile irccd/jsapi-directory.c irccdctl/irccdctl.c
diffstat 3 files changed, 24 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/GNUmakefile	Fri Jul 16 20:18:29 2021 +0200
+++ b/GNUmakefile	Mon Jul 19 09:14:15 2021 +0200
@@ -17,7 +17,7 @@
 #
 
 CC=             cc
-CFLAGS=         -Wall -Wextra -std=c11
+CFLAGS=         -Wall -Wextra -std=c11 -D_XOPEN_SOURCE=700
 
 PREFIX=         /usr/local
 BINDIR=         ${PREFIX}/bin
--- a/irccd/jsapi-directory.c	Fri Jul 16 20:18:29 2021 +0200
+++ b/irccd/jsapi-directory.c	Mon Jul 19 09:14:15 2021 +0200
@@ -294,8 +294,10 @@
 {
 	const char *path = duk_require_string(ctx, 0);
 	const int flags = duk_opt_int(ctx, 1, 0);
+	int fd;
 	DIR *dp;
 	struct dirent *entry;
+	struct stat st;
 
 	if (!duk_is_constructor_call(ctx))
 		return 0;
@@ -306,8 +308,12 @@
 	duk_push_string(ctx, "entries");
 	duk_push_array(ctx);
 
-	if (!(dp = opendir(path)))
+	if ((fd = open(path, O_RDONLY | O_DIRECTORY)) < 0)
 		jsapi_system_raise(ctx);
+	if (!(dp = fdopendir(fd))) {
+		close(fd);
+		jsapi_system_raise(ctx);
+	}
 
 	for (int i = 0; (entry = readdir(dp)); ) {
 		if (strcmp(entry->d_name, ".") == 0 && !(flags & LIST_DOT))
@@ -318,12 +324,18 @@
 		duk_push_object(ctx);
 		duk_push_string(ctx, entry->d_name);
 		duk_put_prop_string(ctx, -2, "name");
-		duk_push_int(ctx, entry->d_type);
+
+		if (fstatat(fd, entry->d_name, &st, 0) < 0)
+			duk_push_int(ctx, 0);
+		else
+			duk_push_int(ctx, st.st_mode & S_IFMT);
+
 		duk_put_prop_string(ctx, -2, "type");
 		duk_put_prop_index(ctx, -2, i++);
 	}
 
 	closedir(dp);
+	close(fd);
 	duk_def_prop(ctx, -3, DUK_DEFPROP_ENUMERABLE | DUK_DEFPROP_HAVE_VALUE);
 
 	/* this.path property. */
@@ -398,14 +410,14 @@
 static const duk_number_list_entry constants[] = {
 	{ "Dot",                LIST_DOT        },
 	{ "DotDot",             LIST_DOT_DOT    },
-	{ "TypeFile",           DT_REG          },
-	{ "TypeDir",            DT_DIR          },
-	{ "TypeLink",           DT_LNK          },
-	{ "TypeBlock",          DT_BLK          },
-	{ "TypeCharacter",      DT_CHR          },
-	{ "TypeFifo",           DT_FIFO         },
-	{ "TypeSocket",         DT_SOCK         },
-	{ "TypeUnknown",        DT_UNKNOWN      },
+	{ "TypeFile",           S_IFREG         },
+	{ "TypeDir",            S_IFDIR         },
+	{ "TypeLink",           S_IFLNK         },
+	{ "TypeBlock",          S_IFBLK         },
+	{ "TypeCharacter",      S_IFCHR         },
+	{ "TypeFifo",           S_IFIFO         },
+	{ "TypeSocket",         S_IFSOCK        },
+	{ "TypeUnknown",        0               },
 	{ NULL,                 0               }
 };
 
--- a/irccdctl/irccdctl.c	Fri Jul 16 20:18:29 2021 +0200
+++ b/irccdctl/irccdctl.c	Mon Jul 19 09:14:15 2021 +0200
@@ -79,7 +79,7 @@
 	if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof (tv)) < 0 ||
 	    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv)) < 0)
 		irc_util_die("abort: setsockopt: %s\n", strerror(errno));
-	if (connect(sock, (const struct sockaddr *)&sockaddr, SUN_LEN(&sockaddr)) < 0)
+	if (connect(sock, (const struct sockaddr *)&sockaddr, sizeof (sockaddr)) < 0)
 		irc_util_die("abort: connect: %s\n", strerror(errno));
 }