perror_test.c revision 244038
1108088Stjr/*-
2108088Stjr * Copyright (c) 2002 Tim J. Robbins
3108088Stjr * All rights reserved.
4108088Stjr *
5108088Stjr * Redistribution and use in source and binary forms, with or without
6108088Stjr * modification, are permitted provided that the following conditions
7108088Stjr * are met:
8108088Stjr * 1. Redistributions of source code must retain the above copyright
9108088Stjr *    notice, this list of conditions and the following disclaimer.
10108088Stjr * 2. Redistributions in binary form must reproduce the above copyright
11108088Stjr *    notice, this list of conditions and the following disclaimer in the
12108088Stjr *    documentation and/or other materials provided with the distribution.
13108088Stjr *
14108088Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15108088Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16108088Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17108088Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18108088Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19108088Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20108088Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21108088Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22108088Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23108088Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24108088Stjr * SUCH DAMAGE.
25108088Stjr */
26108088Stjr
27108088Stjr/*
28108088Stjr * Test program for perror() as specified by IEEE Std. 1003.1-2001 and
29108088Stjr * ISO/IEC 9899:1999.
30108088Stjr */
31108088Stjr
32108088Stjr#include <sys/cdefs.h>
33108088Stjr__FBSDID("$FreeBSD: head/tools/regression/lib/libc/stdio/test-perror.c 244038 2012-12-08 19:42:15Z jilles $");
34108088Stjr
35108088Stjr#include <assert.h>
36108088Stjr#include <err.h>
37108088Stjr#include <errno.h>
38108088Stjr#include <limits.h>
39108088Stjr#include <paths.h>
40108088Stjr#include <stdio.h>
41108088Stjr#include <stdlib.h>
42108088Stjr#include <string.h>
43244038Sjilles#include <unistd.h>
44108088Stjr
45108088Stjrstatic void cleanup(void);
46108088Stjrstatic char tmpfil[PATH_MAX];
47108088Stjr
48108088Stjrint
49108088Stjrmain(int argc, char *argv[])
50108088Stjr{
51108088Stjr	char lbuf[512];
52108088Stjr	int i;
53108088Stjr	char *s;
54108088Stjr
55137587Snik	printf("1..1\n");
56137587Snik
57108088Stjr	strcpy(tmpfil, _PATH_TMP "perror.XXXXXXXX");
58108088Stjr	if (mkstemp(tmpfil) < 0)
59108088Stjr		err(1, "mkstemp");
60108088Stjr	atexit(cleanup);
61108088Stjr	/* Reopen stderr on a file descriptor other than 2. */
62108088Stjr	fclose(stderr);
63108088Stjr	for (i = 0; i < 3; i++)
64108088Stjr		dup(0);
65108088Stjr	if (freopen(tmpfil, "r+", stderr) == NULL)
66108088Stjr		err(1, "%s", tmpfil);
67108088Stjr
68108088Stjr	/*
69108088Stjr	 * Test that perror() doesn't call strerror() (4.4BSD bug),
70108088Stjr	 * the two ways of omitting a program name, and the formatting when
71108088Stjr	 * a program name is specified.
72108088Stjr	 */
73108088Stjr	s = strerror(ENOENT);
74108088Stjr	assert(strcmp(s, "No such file or directory") == 0);
75108088Stjr	errno = EPERM;
76108088Stjr	perror(NULL);
77108088Stjr	perror("");
78108088Stjr	perror("test-perror");
79108088Stjr	assert(strcmp(s, "No such file or directory") == 0);
80108088Stjr
81108088Stjr	/*
82108088Stjr	 * Read it back to check...
83108088Stjr	 */
84108088Stjr	rewind(stderr);
85108088Stjr	s = fgets(lbuf, sizeof(lbuf), stderr);
86108088Stjr	assert(s != NULL);
87108088Stjr	assert(strcmp(s, "Operation not permitted\n") == 0);
88108088Stjr	s = fgets(lbuf, sizeof(lbuf), stderr);
89108088Stjr	assert(s != NULL);
90108088Stjr	assert(strcmp(s, "Operation not permitted\n") == 0);
91108088Stjr	s = fgets(lbuf, sizeof(lbuf), stderr);
92108088Stjr	assert(s != NULL);
93108088Stjr	assert(strcmp(s, "test-perror: Operation not permitted\n") == 0);
94108088Stjr	s = fgets(lbuf, sizeof(lbuf), stderr);
95108088Stjr	assert(s == NULL);
96108088Stjr	fclose(stderr);
97108088Stjr
98137587Snik	printf("ok 1 - perror()\n");
99108088Stjr
100108088Stjr	return (0);
101108088Stjr}
102108088Stjr
103108088Stjrstatic void
104108088Stjrcleanup(void)
105108088Stjr{
106108088Stjr
107108088Stjr	unlink(tmpfil);
108108088Stjr}
109