VirtualBox

source: kBuild/vendor/grep/current/gnulib-tests/test-getopt-main.h

Last change on this file was 3529, checked in by bird, 3 years ago

Imported grep 3.7 from grep-3.7.tar.gz (sha256: c22b0cf2d4f6bbe599c902387e8058990e1eee99aef333a203829e5fd3dbb342), applying minimal auto-props.

  • Property svn:eol-style set to native
File size: 2.1 KB
Line 
1/* Test of command line argument processing.
2 Copyright (C) 2009-2021 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 <bruno@clisp.org>, 2009. */
18
19#include "signature.h"
20SIGNATURE_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
35static FILE *myerr;
36
37#include "test-getopt.h"
38#if TEST_GETOPT_GNU
39# include "test-getopt_long.h"
40#endif
41
42int
43main (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 0;
76}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette