Blame gnulib-tests/test-getopt-main.h

Packit 709fb3
/* Test of command line argument processing.
Packit 709fb3
   Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Bruno Haible <bruno@clisp.org>, 2009.  */
Packit 709fb3
Packit 709fb3
#include "signature.h"
Packit 709fb3
SIGNATURE_CHECK (getopt, int, (int, char * const[], char const *));
Packit 709fb3
Packit 709fb3
#include <stdio.h>
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
#include <string.h>
Packit 709fb3
#include <unistd.h>
Packit 709fb3
Packit 709fb3
/* This test intentionally remaps stderr.  So, we arrange to have fd 10
Packit 709fb3
   (outside the range of interesting fd's during the test) set up to
Packit 709fb3
   duplicate the original stderr.  */
Packit 709fb3
Packit 709fb3
#define BACKUP_STDERR_FILENO 10
Packit 709fb3
#define ASSERT_STREAM myerr
Packit 709fb3
#include "macros.h"
Packit 709fb3
Packit 709fb3
static FILE *myerr;
Packit 709fb3
Packit 709fb3
#include "test-getopt.h"
Packit 709fb3
#if TEST_GETOPT_GNU
Packit 709fb3
# include "test-getopt_long.h"
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main (void)
Packit 709fb3
{
Packit 709fb3
   /* This test validates that stderr is used correctly, so move the
Packit 709fb3
      original into fd 10.  */
Packit 709fb3
  if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
Packit 709fb3
      || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
Packit 709fb3
    return 2;
Packit 709fb3
Packit 709fb3
  ASSERT (freopen (TEST_GETOPT_TMP_NAME, "w", stderr) == stderr);
Packit 709fb3
Packit 709fb3
  /* These default values are required by POSIX.  */
Packit 709fb3
  ASSERT (optind == 1);
Packit 709fb3
  ASSERT (opterr != 0);
Packit 709fb3
Packit 709fb3
  setenv ("POSIXLY_CORRECT", "1", 1);
Packit 709fb3
  test_getopt ();
Packit 709fb3
Packit 709fb3
#if TEST_GETOPT_GNU
Packit 709fb3
  test_getopt_long_posix ();
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  unsetenv ("POSIXLY_CORRECT");
Packit 709fb3
  test_getopt ();
Packit 709fb3
Packit 709fb3
#if TEST_GETOPT_GNU
Packit 709fb3
  test_getopt_long ();
Packit 709fb3
  test_getopt_long_only ();
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  ASSERT (fclose (stderr) == 0);
Packit 709fb3
  ASSERT (remove (TEST_GETOPT_TMP_NAME) == 0);
Packit 709fb3
Packit 709fb3
  return 0;
Packit 709fb3
}