2 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * This file is also used by the test suite. Do not #include "apps.h".
15 #include "internal/nelem.h"
17 #if !defined(OPENSSL_SYS_MSDOS)
25 #include <openssl/bio.h>
26 #include <openssl/x509v3.h>
28 #define MAX_OPT_HELP_WIDTH 30
29 const char OPT_HELP_STR
[] = "-H";
30 const char OPT_MORE_STR
[] = "-M";
31 const char OPT_SECTION_STR
[] = "-S";
32 const char OPT_PARAM_STR
[] = "-P";
41 static const OPTIONS
*unknown
;
42 static const OPTIONS
*opts
;
46 * Return the simple name of the program; removing various platform gunk.
48 #if defined(OPENSSL_SYS_WIN32)
50 const char *opt_path_end(const char *filename
)
54 /* find the last '/', '\' or ':' */
55 for (p
= filename
+ strlen(filename
); --p
> filename
; )
56 if (*p
== '/' || *p
== '\\' || *p
== ':') {
63 char *opt_progname(const char *argv0
)
69 p
= opt_path_end(argv0
);
71 /* Strip off trailing nonsense. */
74 (strcmp(&p
[n
- 4], ".exe") == 0 || strcmp(&p
[n
- 4], ".EXE") == 0))
77 /* Copy over the name, in lowercase. */
78 if (n
> sizeof(prog
) - 1)
80 for (q
= prog
, i
= 0; i
< n
; i
++, p
++)
81 *q
++ = tolower((unsigned char)*p
);
86 #elif defined(OPENSSL_SYS_VMS)
88 const char *opt_path_end(const char *filename
)
92 /* Find last special character sys:[foo.bar]openssl */
93 for (p
= filename
+ strlen(filename
); --p
> filename
;)
94 if (*p
== ':' || *p
== ']' || *p
== '>') {
101 char *opt_progname(const char *argv0
)
105 /* Find last special character sys:[foo.bar]openssl */
106 p
= opt_path_end(argv0
);
108 strncpy(prog
, p
, sizeof(prog
) - 1);
109 prog
[sizeof(prog
) - 1] = '\0';
110 if (q
!= NULL
&& q
- p
< sizeof(prog
))
117 const char *opt_path_end(const char *filename
)
121 /* Could use strchr, but this is like the ones above. */
122 for (p
= filename
+ strlen(filename
); --p
> filename
;)
130 char *opt_progname(const char *argv0
)
134 p
= opt_path_end(argv0
);
135 strncpy(prog
, p
, sizeof(prog
) - 1);
136 prog
[sizeof(prog
) - 1] = '\0';
141 char *opt_appname(const char *arg0
)
143 size_t len
= strlen(prog
);
146 snprintf(prog
+ len
, sizeof(prog
) - len
- 1, " %s", arg0
);
150 char *opt_getprog(void)
155 /* Set up the arg parsing. */
156 char *opt_init(int ac
, char **av
, const OPTIONS
*o
)
165 /* Check all options up until the PARAM marker (if present) */
166 for (; o
->name
!= NULL
&& o
->name
!= OPT_PARAM_STR
; ++o
) {
172 if (o
->name
== OPT_HELP_STR
173 || o
->name
== OPT_MORE_STR
174 || o
->name
== OPT_SECTION_STR
)
179 /* Make sure options are legit. */
180 OPENSSL_assert(o
->name
[0] != '-');
181 OPENSSL_assert(o
->retval
> 0);
183 case 0: case '-': case '/': case '<': case '>': case 'E': case 'F':
184 case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
185 case 'u': case 'c': case ':':
191 /* Make sure there are no duplicates. */
192 for (next
= o
+ 1; next
->name
; ++next
) {
194 * Some compilers inline strcmp and the assert string is too long.
196 duplicated
= strcmp(o
->name
, next
->name
) == 0;
197 OPENSSL_assert(!duplicated
);
200 if (o
->name
[0] == '\0') {
201 OPENSSL_assert(unknown
== NULL
);
203 OPENSSL_assert(unknown
->valtype
== 0 || unknown
->valtype
== '-');
209 static OPT_PAIR formats
[] = {
210 {"PEM/DER", OPT_FMT_PEMDER
},
211 {"pkcs12", OPT_FMT_PKCS12
},
212 {"smime", OPT_FMT_SMIME
},
213 {"engine", OPT_FMT_ENGINE
},
214 {"msblob", OPT_FMT_MSBLOB
},
215 {"nss", OPT_FMT_NSS
},
216 {"text", OPT_FMT_TEXT
},
217 {"http", OPT_FMT_HTTP
},
218 {"pvk", OPT_FMT_PVK
},
222 /* Print an error message about a failed format parse. */
223 int opt_format_error(const char *s
, unsigned long flags
)
227 if (flags
== OPT_FMT_PEMDER
) {
228 opt_printf_stderr("%s: Bad format \"%s\"; must be pem or der\n",
231 opt_printf_stderr("%s: Bad format \"%s\"; must be one of:\n",
233 for (ap
= formats
; ap
->name
; ap
++)
234 if (flags
& ap
->retval
)
235 opt_printf_stderr(" %s\n", ap
->name
);
240 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
241 int opt_format(const char *s
, unsigned long flags
, int *result
)
245 opt_printf_stderr("%s: Bad format \"%s\"\n", prog
, s
);
249 if ((flags
& OPT_FMT_PEMDER
) == 0)
250 return opt_format_error(s
, flags
);
251 *result
= FORMAT_ASN1
;
255 if ((flags
& OPT_FMT_TEXT
) == 0)
256 return opt_format_error(s
, flags
);
257 *result
= FORMAT_TEXT
;
261 if ((flags
& OPT_FMT_NSS
) == 0)
262 return opt_format_error(s
, flags
);
263 if (strcmp(s
, "NSS") != 0 && strcmp(s
, "nss") != 0)
264 return opt_format_error(s
, flags
);
265 *result
= FORMAT_NSS
;
269 if ((flags
& OPT_FMT_SMIME
) == 0)
270 return opt_format_error(s
, flags
);
271 *result
= FORMAT_SMIME
;
275 if ((flags
& OPT_FMT_MSBLOB
) == 0)
276 return opt_format_error(s
, flags
);
277 *result
= FORMAT_MSBLOB
;
281 if ((flags
& OPT_FMT_ENGINE
) == 0)
282 return opt_format_error(s
, flags
);
283 *result
= FORMAT_ENGINE
;
287 if ((flags
& OPT_FMT_HTTP
) == 0)
288 return opt_format_error(s
, flags
);
289 *result
= FORMAT_HTTP
;
292 if ((flags
& OPT_FMT_PKCS12
) == 0)
293 return opt_format_error(s
, flags
);
294 *result
= FORMAT_PKCS12
;
298 if (s
[1] == '\0' || strcmp(s
, "PEM") == 0 || strcmp(s
, "pem") == 0) {
299 if ((flags
& OPT_FMT_PEMDER
) == 0)
300 return opt_format_error(s
, flags
);
301 *result
= FORMAT_PEM
;
302 } else if (strcmp(s
, "PVK") == 0 || strcmp(s
, "pvk") == 0) {
303 if ((flags
& OPT_FMT_PVK
) == 0)
304 return opt_format_error(s
, flags
);
305 *result
= FORMAT_PVK
;
306 } else if (strcmp(s
, "P12") == 0 || strcmp(s
, "p12") == 0
307 || strcmp(s
, "PKCS12") == 0 || strcmp(s
, "pkcs12") == 0) {
308 if ((flags
& OPT_FMT_PKCS12
) == 0)
309 return opt_format_error(s
, flags
);
310 *result
= FORMAT_PKCS12
;
312 opt_printf_stderr("%s: Bad format \"%s\"\n", prog
, s
);
320 /* Return string representing the given format. */
321 const char *format2str(int format
)
325 return "(undefined)";
349 /* Print an error message about unsuitable/unsupported format requested. */
350 void print_format_error(int format
, unsigned long flags
)
352 (void)opt_format_error(format2str(format
), flags
);
355 /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */
356 int opt_cipher(const char *name
, const EVP_CIPHER
**cipherp
)
358 *cipherp
= EVP_get_cipherbyname(name
);
359 if (*cipherp
!= NULL
)
361 opt_printf_stderr("%s: Unknown cipher: %s\n", prog
, name
);
366 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
368 int opt_md(const char *name
, const EVP_MD
**mdp
)
370 *mdp
= EVP_get_digestbyname(name
);
373 opt_printf_stderr("%s: Unknown option or message digest: %s\n", prog
,
374 name
!= NULL
? name
: "\"\"");
378 /* Look through a list of name/value pairs. */
379 int opt_pair(const char *name
, const OPT_PAIR
* pairs
, int *result
)
383 for (pp
= pairs
; pp
->name
; pp
++)
384 if (strcmp(pp
->name
, name
) == 0) {
385 *result
= pp
->retval
;
388 opt_printf_stderr("%s: Value must be one of:\n", prog
);
389 for (pp
= pairs
; pp
->name
; pp
++)
390 opt_printf_stderr("\t%s\n", pp
->name
);
394 /* Look through a list of valid names */
395 int opt_string(const char *name
, const char **options
)
399 for (p
= options
; *p
!= NULL
; p
++)
400 if (strcmp(*p
, name
) == 0)
402 opt_printf_stderr("%s: Value must be one of:\n", prog
);
403 for (p
= options
; *p
!= NULL
; p
++)
404 opt_printf_stderr("\t%s\n", *p
);
408 /* Parse an int, put it into *result; return 0 on failure, else 1. */
409 int opt_int(const char *value
, int *result
)
413 if (!opt_long(value
, &l
))
417 opt_printf_stderr("%s: Value \"%s\" outside integer range\n",
424 static void opt_number_error(const char *v
)
427 struct strstr_pair_st
{
431 {"0x", "a hexadecimal"},
432 {"0X", "a hexadecimal"},
436 for (i
= 0; i
< OSSL_NELEM(b
); i
++) {
437 if (strncmp(v
, b
[i
].prefix
, strlen(b
[i
].prefix
)) == 0) {
438 opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n",
443 opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog
, v
);
447 /* Parse a long, put it into *result; return 0 on failure, else 1. */
448 int opt_long(const char *value
, long *result
)
455 l
= strtol(value
, &endp
, 0);
458 || ((l
== LONG_MAX
|| l
== LONG_MIN
) && errno
== ERANGE
)
459 || (l
== 0 && errno
!= 0)) {
460 opt_number_error(value
);
469 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
470 defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
471 !defined(OPENSSL_NO_INTTYPES_H)
473 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
474 int opt_imax(const char *value
, intmax_t *result
)
481 m
= strtoimax(value
, &endp
, 0);
484 || ((m
== INTMAX_MAX
|| m
== INTMAX_MIN
) && errno
== ERANGE
)
485 || (m
== 0 && errno
!= 0)) {
486 opt_number_error(value
);
495 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
496 int opt_umax(const char *value
, uintmax_t *result
)
503 m
= strtoumax(value
, &endp
, 0);
506 || (m
== UINTMAX_MAX
&& errno
== ERANGE
)
507 || (m
== 0 && errno
!= 0)) {
508 opt_number_error(value
);
519 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
521 int opt_ulong(const char *value
, unsigned long *result
)
528 l
= strtoul(value
, &endptr
, 0);
531 || ((l
== ULONG_MAX
) && errno
== ERANGE
)
532 || (l
== 0 && errno
!= 0)) {
533 opt_number_error(value
);
543 * We pass opt as an int but cast it to "enum range" so that all the
544 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
545 * in gcc do the right thing.
547 enum range
{ OPT_V_ENUM
};
549 int opt_verify(int opt
, X509_VERIFY_PARAM
*vpm
)
555 const X509_VERIFY_PARAM
*vtmp
;
557 OPENSSL_assert(vpm
!= NULL
);
558 OPENSSL_assert(opt
> OPT_V__FIRST
);
559 OPENSSL_assert(opt
< OPT_V__LAST
);
561 switch ((enum range
)opt
) {
566 otmp
= OBJ_txt2obj(opt_arg(), 0);
568 opt_printf_stderr("%s: Invalid Policy %s\n", prog
, opt_arg());
571 X509_VERIFY_PARAM_add0_policy(vpm
, otmp
);
574 /* purpose name -> purpose index */
575 i
= X509_PURPOSE_get_by_sname(opt_arg());
577 opt_printf_stderr("%s: Invalid purpose %s\n", prog
, opt_arg());
581 /* purpose index -> purpose object */
582 xptmp
= X509_PURPOSE_get0(i
);
584 /* purpose object -> purpose value */
585 i
= X509_PURPOSE_get_id(xptmp
);
587 if (!X509_VERIFY_PARAM_set_purpose(vpm
, i
)) {
588 opt_printf_stderr("%s: Internal error setting purpose %s\n",
593 case OPT_V_VERIFY_NAME
:
594 vtmp
= X509_VERIFY_PARAM_lookup(opt_arg());
596 opt_printf_stderr("%s: Invalid verify name %s\n",
600 X509_VERIFY_PARAM_set1(vpm
, vtmp
);
602 case OPT_V_VERIFY_DEPTH
:
605 X509_VERIFY_PARAM_set_depth(vpm
, i
);
607 case OPT_V_VERIFY_AUTH_LEVEL
:
610 X509_VERIFY_PARAM_set_auth_level(vpm
, i
);
613 if (!opt_imax(opt_arg(), &t
))
615 if (t
!= (time_t)t
) {
616 opt_printf_stderr("%s: epoch time out of range %s\n",
620 X509_VERIFY_PARAM_set_time(vpm
, (time_t)t
);
622 case OPT_V_VERIFY_HOSTNAME
:
623 if (!X509_VERIFY_PARAM_set1_host(vpm
, opt_arg(), 0))
626 case OPT_V_VERIFY_EMAIL
:
627 if (!X509_VERIFY_PARAM_set1_email(vpm
, opt_arg(), 0))
630 case OPT_V_VERIFY_IP
:
631 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm
, opt_arg()))
634 case OPT_V_IGNORE_CRITICAL
:
635 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_IGNORE_CRITICAL
);
637 case OPT_V_ISSUER_CHECKS
:
638 /* NOP, deprecated */
640 case OPT_V_CRL_CHECK
:
641 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_CRL_CHECK
);
643 case OPT_V_CRL_CHECK_ALL
:
644 X509_VERIFY_PARAM_set_flags(vpm
,
645 X509_V_FLAG_CRL_CHECK
|
646 X509_V_FLAG_CRL_CHECK_ALL
);
648 case OPT_V_POLICY_CHECK
:
649 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_POLICY_CHECK
);
651 case OPT_V_EXPLICIT_POLICY
:
652 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_EXPLICIT_POLICY
);
654 case OPT_V_INHIBIT_ANY
:
655 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_INHIBIT_ANY
);
657 case OPT_V_INHIBIT_MAP
:
658 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_INHIBIT_MAP
);
660 case OPT_V_X509_STRICT
:
661 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_X509_STRICT
);
663 case OPT_V_EXTENDED_CRL
:
664 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_EXTENDED_CRL_SUPPORT
);
666 case OPT_V_USE_DELTAS
:
667 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_USE_DELTAS
);
669 case OPT_V_POLICY_PRINT
:
670 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_NOTIFY_POLICY
);
672 case OPT_V_CHECK_SS_SIG
:
673 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_CHECK_SS_SIGNATURE
);
675 case OPT_V_TRUSTED_FIRST
:
676 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_TRUSTED_FIRST
);
678 case OPT_V_SUITEB_128_ONLY
:
679 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_SUITEB_128_LOS_ONLY
);
681 case OPT_V_SUITEB_128
:
682 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_SUITEB_128_LOS
);
684 case OPT_V_SUITEB_192
:
685 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_SUITEB_192_LOS
);
687 case OPT_V_PARTIAL_CHAIN
:
688 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_PARTIAL_CHAIN
);
690 case OPT_V_NO_ALT_CHAINS
:
691 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_NO_ALT_CHAINS
);
693 case OPT_V_NO_CHECK_TIME
:
694 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_NO_CHECK_TIME
);
696 case OPT_V_ALLOW_PROXY_CERTS
:
697 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_ALLOW_PROXY_CERTS
);
712 * Parse the next flag (and value if specified), return 0 if done, -1 on
713 * error, otherwise the flag's retval.
723 ossl_uintmax_t umval
;
725 /* Look at current arg; at end of the list? */
731 /* If word doesn't start with a -, we're done. */
735 /* Hit "--" ? We're done. */
737 if (strcmp(p
, "--") == 0)
740 /* Allow -nnn and --nnn */
745 /* If we have --flag=foo, snip it off */
746 if ((arg
= strchr(p
, '=')) != NULL
)
748 for (o
= opts
; o
->name
; ++o
) {
749 /* If not this option, move on to the next one. */
750 if (!(strcmp(p
, "h") == 0 && strcmp(o
->name
, "help") == 0)
751 && strcmp(p
, o
->name
) != 0)
754 /* If it doesn't take a value, make sure none was given. */
755 if (o
->valtype
== 0 || o
->valtype
== '-') {
757 opt_printf_stderr("%s: Option -%s does not take a value\n",
764 /* Want a value; get the next param if =foo not used. */
766 if (argv
[opt_index
] == NULL
) {
767 opt_printf_stderr("%s: Option -%s needs a value\n",
771 arg
= argv
[opt_index
++];
774 /* Syntax-check value. */
775 switch (o
->valtype
) {
782 if (opt_isdir(arg
) > 0)
784 opt_printf_stderr("%s: Not a directory: %s\n", prog
, arg
);
794 if (!opt_int(arg
, &ival
))
796 if (o
->valtype
== 'p' && ival
<= 0) {
797 opt_printf_stderr("%s: Non-positive number \"%s\" for -%s\n",
803 if (!opt_imax(arg
, &imval
))
807 if (!opt_umax(arg
, &umval
))
811 if (!opt_long(arg
, &lval
))
815 if (!opt_ulong(arg
, &ulval
))
823 o
->valtype
== 'c' ? OPT_FMT_PDS
:
824 o
->valtype
== 'E' ? OPT_FMT_PDE
:
825 o
->valtype
== 'F' ? OPT_FMT_PEMDER
826 : OPT_FMT_ANY
, &ival
))
828 opt_printf_stderr("%s: Invalid format \"%s\" for -%s\n",
833 /* Return the flag value. */
836 if (unknown
!= NULL
) {
838 return unknown
->retval
;
840 opt_printf_stderr("%s: Unknown option: -%s\n", prog
, p
);
844 /* Return the most recent flag parameter. */
850 /* Return the most recent flag. */
856 /* Return the unknown option. */
857 char *opt_unknown(void)
862 /* Return the rest of the arguments after parsing flags. */
863 char **opt_rest(void)
865 return &argv
[opt_index
];
868 /* How many items in remaining args? */
869 int opt_num_rest(void)
874 for (pp
= opt_rest(); *pp
; pp
++, i
++)
879 /* Return a string describing the parameter type. */
880 static const char *valtype2param(const OPTIONS
*o
)
882 switch (o
->valtype
) {
905 return "PEM|DER|ENGINE";
918 void opt_print(const OPTIONS
*o
, int doingparams
, int width
)
924 help
= o
->helpstr
? o
->helpstr
: "(No additional info)";
925 if (o
->name
== OPT_HELP_STR
) {
926 opt_printf_stderr(help
, prog
);
929 if (o
->name
== OPT_SECTION_STR
) {
930 opt_printf_stderr("\n");
931 opt_printf_stderr(help
, prog
);
934 if (o
->name
== OPT_PARAM_STR
) {
935 opt_printf_stderr("\nParameters:\n");
940 memset(start
, ' ', sizeof(start
) - 1);
941 start
[sizeof(start
) - 1] = '\0';
943 if (o
->name
== OPT_MORE_STR
) {
944 /* Continuation of previous line; pad and print. */
946 opt_printf_stderr("%s %s\n", start
, help
);
950 /* Build up the "-flag [param]" part. */
956 p
+= strlen(strcpy(p
, o
->name
));
959 if (o
->valtype
!= '-') {
961 p
+= strlen(strcpy(p
, valtype2param(o
)));
964 if ((int)(p
- start
) >= MAX_OPT_HELP_WIDTH
) {
966 opt_printf_stderr("%s\n", start
);
967 memset(start
, ' ', sizeof(start
));
970 opt_printf_stderr("%s %s\n", start
, help
);
973 void opt_help(const OPTIONS
*list
)
976 int i
, sawparams
= 0, width
= 5;
980 /* Starts with its own help message? */
981 standard_prolog
= list
[0].name
!= OPT_HELP_STR
;
983 /* Find the widest help. */
984 for (o
= list
; o
->name
; o
++) {
985 if (o
->name
== OPT_MORE_STR
)
987 i
= 2 + (int)strlen(o
->name
);
988 if (o
->valtype
!= '-')
989 i
+= 1 + strlen(valtype2param(o
));
990 if (i
< MAX_OPT_HELP_WIDTH
&& i
> width
)
992 OPENSSL_assert(i
< (int)sizeof(start
));
995 if (standard_prolog
) {
996 opt_printf_stderr("Usage: %s [options]\n", prog
);
997 if (list
[0].name
!= OPT_SECTION_STR
)
998 opt_printf_stderr("Valid options are:\n", prog
);
1001 /* Now let's print. */
1002 for (o
= list
; o
->name
; o
++) {
1003 if (o
->name
== OPT_PARAM_STR
)
1005 opt_print(o
, sawparams
, width
);
1009 /* opt_isdir section */
1011 # include <windows.h>
1012 int opt_isdir(const char *name
)
1015 # if defined(UNICODE) || defined(_UNICODE)
1016 size_t i
, len_0
= strlen(name
) + 1;
1017 WCHAR tempname
[MAX_PATH
];
1019 if (len_0
> MAX_PATH
)
1022 # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
1023 if (!MultiByteToWideChar(CP_ACP
, 0, name
, len_0
, tempname
, MAX_PATH
))
1025 for (i
= 0; i
< len_0
; i
++)
1026 tempname
[i
] = (WCHAR
)name
[i
];
1028 attr
= GetFileAttributes(tempname
);
1030 attr
= GetFileAttributes(name
);
1032 if (attr
== INVALID_FILE_ATTRIBUTES
)
1034 return ((attr
& FILE_ATTRIBUTE_DIRECTORY
) != 0);
1037 # include <sys/stat.h>
1039 # if defined(_S_IFMT) && defined(_S_IFDIR)
1040 # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
1042 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
1046 int opt_isdir(const char *name
)
1048 # if defined(S_ISDIR)
1051 if (stat(name
, &st
) == 0)
1052 return S_ISDIR(st
.st_mode
);