/* Copyright (c) 2010 The NetBSD Foundation, Inc.1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */2425#include "atf-c/tp.h"2627#include <string.h>28#include <unistd.h>2930#include <atf-c.h>3132ATF_TC(getopt);33ATF_TC_HEAD(getopt, tc)34{35atf_tc_set_md_var(tc, "descr", "Checks if getopt(3) global state is "36"reset by the test program driver so that test cases can use "37"getopt(3) again");38}39ATF_TC_BODY(getopt, tc)40{41/* Provide an option that is unknown to the test program driver and42* one that is, together with an argument that would be swallowed by43* the test program option if it were recognized. */44int argc = 4;45char arg1[] = "progname";46char arg2[] = "-Z";47char arg3[] = "-s";48char arg4[] = "foo";49char *const argv[] = { arg1, arg2, arg3, arg4, NULL };5051int ch;52bool zflag;5354/* Given that this obviously is a test program, and that we used the55* same driver to start, we can test getopt(3) right here without doing56* any fancy stuff. */57zflag = false;58while ((ch = getopt(argc, argv, ":Z")) != -1) {59switch (ch) {60case 'Z':61zflag = true;62break;6364case '?':65default:66if (optopt != 's')67atf_tc_fail("Unexpected unknown option -%c found", optopt);68}69}7071ATF_REQUIRE(zflag);72ATF_REQUIRE_EQ_MSG(1, argc - optind, "Invalid number of arguments left "73"after the call to getopt(3)");74ATF_CHECK_STREQ_MSG("foo", argv[optind], "The non-option argument is "75"invalid");76}7778/* ---------------------------------------------------------------------79* Main.80* --------------------------------------------------------------------- */8182ATF_TP_ADD_TCS(tp)83{84ATF_TP_ADD_TC(tp, getopt);8586return atf_no_error();87}888990