| 1 |
/* Test of command line argument processing. |
| 2 |
Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Bruno Haible <[email protected]>, 2009. */ |
| 18 |
|
| 19 |
#include "signature.h" |
| 20 |
SIGNATURE_CHECK (getopt, int, (int, char * const[], char const *)); |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <unistd.h> |
| 26 |
|
| 27 |
/* This test intentionally remaps stderr. So, we arrange to have fd 10 |
| 28 |
(outside the range of interesting fd's during the test) set up to |
| 29 |
duplicate the original stderr. */ |
| 30 |
|
| 31 |
#define BACKUP_STDERR_FILENO 10 |
| 32 |
#define ASSERT_STREAM myerr |
| 33 |
#include "macros.h" |
| 34 |
|
| 35 |
static FILE *myerr; |
| 36 |
|
| 37 |
#include "test-getopt.h" |
| 38 |
#if TEST_GETOPT_GNU |
| 39 |
# include "test-getopt_long.h" |
| 40 |
#endif |
| 41 |
|
| 42 |
int |
| 43 |
main (void) |
| 44 |
{ |
| 45 |
/* This test validates that stderr is used correctly, so move the |
| 46 |
original into fd 10. */ |
| 47 |
if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO |
| 48 |
|| (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL) |
| 49 |
return 2; |
| 50 |
|
| 51 |
ASSERT (freopen (TEST_GETOPT_TMP_NAME, "w", stderr) == stderr); |
| 52 |
|
| 53 |
/* These default values are required by POSIX. */ |
| 54 |
ASSERT (optind == 1); |
| 55 |
ASSERT (opterr != 0); |
| 56 |
|
| 57 |
setenv ("POSIXLY_CORRECT", "1", 1); |
| 58 |
test_getopt (); |
| 59 |
|
| 60 |
#if TEST_GETOPT_GNU |
| 61 |
test_getopt_long_posix (); |
| 62 |
#endif |
| 63 |
|
| 64 |
unsetenv ("POSIXLY_CORRECT"); |
| 65 |
test_getopt (); |
| 66 |
|
| 67 |
#if TEST_GETOPT_GNU |
| 68 |
test_getopt_long (); |
| 69 |
test_getopt_long_only (); |
| 70 |
#endif |
| 71 |
|
| 72 |
ASSERT (fclose (stderr) == 0); |
| 73 |
ASSERT (remove (TEST_GETOPT_TMP_NAME) == 0); |
| 74 |
|
| 75 |
return test_exit_status; |
| 76 |
} |