getopt-gnu: flush out another BSD bug
[gnulib.git] / tests / test-getopt.c
1 /* Test of command line argument processing.
2    Copyright (C) 2009 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 #if GNULIB_GETOPT_GNU
22 # include <getopt.h>
23 #endif
24 #include <unistd.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 /* The glibc/gnulib implementation of getopt supports setting optind = 0,
43    but other implementations don't.  */
44 #if defined __GETOPT_PREFIX || (__GLIBC__ >= 2)
45 # define OPTIND_MIN 0
46 #else
47 # define OPTIND_MIN 1
48 #endif
49
50 #include "test-getopt.h"
51 #if GNULIB_GETOPT_GNU
52 # include "test-getopt_long.h"
53 #endif
54
55 int
56 main (void)
57 {
58   setenv ("POSIXLY_CORRECT", "1", 1);
59   test_getopt ();
60
61 #if GNULIB_GETOPT_GNU
62   test_getopt_long_posix ();
63 #endif
64
65   unsetenv ("POSIXLY_CORRECT");
66   test_getopt ();
67
68 #if GNULIB_GETOPT_GNU
69   test_getopt_long ();
70 #endif
71
72   return 0;
73 }