unifdef for S_I{FLNK,FIFO,FSOCK}. For the operations where we use
authorguenther <guenther@openbsd.org>
Thu, 15 Aug 2024 06:27:24 +0000 (06:27 +0000)
committerguenther <guenther@openbsd.org>
Thu, 15 Aug 2024 06:27:24 +0000 (06:27 +0000)
access() (-r, -w, -x, -e) do them without requiring stat() to succeed first.

ok tb@ deraadt@

bin/test/test.c

index 5756645..21c2764 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: test.c,v 1.21 2024/06/18 16:41:39 schwarze Exp $      */
+/*     $OpenBSD: test.c,v 1.22 2024/08/15 06:27:24 guenther Exp $      */
 /*     $NetBSD: test.c,v 1.15 1995/03/21 07:04:06 cgd Exp $    */
 
 /*
@@ -424,13 +424,24 @@ filstat(char *nm, enum token mode)
        struct stat s;
        mode_t i;
 
+       switch (mode) {
+       case FILRD:
+               return access(nm, R_OK) == 0;
+       case FILWR:
+               return access(nm, W_OK) == 0;
+       case FILEX:
+               return access(nm, X_OK) == 0;
+       case FILEXIST:
+               return access(nm, F_OK) == 0;
+       default:
+               break;
+       }
+
        if (mode == FILSYM) {
-#ifdef S_IFLNK
                if (lstat(nm, &s) == 0) {
                        i = S_IFLNK;
                        goto filetype;
                }
-#endif
                return 0;
        }
 
@@ -438,14 +449,6 @@ filstat(char *nm, enum token mode)
                return 0;
 
        switch (mode) {
-       case FILRD:
-               return access(nm, R_OK) == 0;
-       case FILWR:
-               return access(nm, W_OK) == 0;
-       case FILEX:
-               return access(nm, X_OK) == 0;
-       case FILEXIST:
-               return access(nm, F_OK) == 0;
        case FILREG:
                i = S_IFREG;
                goto filetype;
@@ -459,19 +462,11 @@ filstat(char *nm, enum token mode)
                i = S_IFBLK;
                goto filetype;
        case FILFIFO:
-#ifdef S_IFIFO
                i = S_IFIFO;
                goto filetype;
-#else
-               return 0;
-#endif
        case FILSOCK:
-#ifdef S_IFSOCK
                i = S_IFSOCK;
                goto filetype;
-#else
-               return 0;
-#endif
        case FILSUID:
                i = S_ISUID;
                goto filebit;