update nearly all FSF copyright year lists to include 2010
[gnulib.git] / tests / test-getopt.c
1 /* Test of command line argument processing.
2    Copyright (C) 2009, 2010 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 <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009.  */
18
19 #include <config.h>
20
21 /* None of the files accessed by this test are large, so disable the
22    ftell link warning if we are not using the gnulib ftell module.  */
23 #if !GNULIB_FTELL
24 # undef GL_LINK_WARNING
25 # define GL_LINK_WARNING(ignored) ((void) 0)
26 #endif
27
28 #if GNULIB_GETOPT_GNU
29 # include <getopt.h>
30
31 # ifndef __getopt_argv_const
32 #  define __getopt_argv_const const
33 # endif
34 # include "signature.h"
35 SIGNATURE_CHECK (getopt_long, int, (int, char *__getopt_argv_const *,
36                                     char const *, struct option const *,
37                                     int *));
38 SIGNATURE_CHECK (getopt_long_only, int, (int, char *__getopt_argv_const *,
39                                          char const *, struct option const *,
40                                          int *));
41
42 #endif
43
44 #include <unistd.h>
45
46 #include "signature.h"
47 SIGNATURE_CHECK (getopt, int, (int, char * const[], char const *));
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 /* This test intentionally remaps stderr.  So, we arrange to have fd 10
54    (outside the range of interesting fd's during the test) set up to
55    duplicate the original stderr.  */
56
57 #define BACKUP_STDERR_FILENO 10
58 #define ASSERT_STREAM myerr
59 #include "macros.h"
60
61 static FILE *myerr;
62
63 #include "test-getopt.h"
64 #if GNULIB_GETOPT_GNU
65 # include "test-getopt_long.h"
66 #endif
67
68 int
69 main (void)
70 {
71    /* This test validates that stderr is used correctly, so move the
72       original into fd 10.  */
73   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
74       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
75     return 2;
76
77   ASSERT (freopen ("test-getopt.tmp", "w", stderr) == stderr);
78
79   /* These default values are required by POSIX.  */
80   ASSERT (optind == 1);
81   ASSERT (opterr != 0);
82
83   setenv ("POSIXLY_CORRECT", "1", 1);
84   test_getopt ();
85
86 #if GNULIB_GETOPT_GNU
87   test_getopt_long_posix ();
88 #endif
89
90   unsetenv ("POSIXLY_CORRECT");
91   test_getopt ();
92
93 #if GNULIB_GETOPT_GNU
94   test_getopt_long ();
95   test_getopt_long_only ();
96 #endif
97
98   ASSERT (fclose (stderr) == 0);
99   ASSERT (remove ("test-getopt.tmp") == 0);
100
101   return 0;
102 }