perror_test.c revision 108088
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 108088 2002-12-19 09:46:10Z tjr $");
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>
43108088Stjr
44108088Stjrstatic void cleanup(void);
45108088Stjrstatic char tmpfil[PATH_MAX];
46108088Stjr
47108088Stjrint
48108088Stjrmain(int argc, char *argv[])
49108088Stjr{
50108088Stjr	char lbuf[512];
51108088Stjr	int i;
52108088Stjr	char *s;
53108088Stjr
54108088Stjr	strcpy(tmpfil, _PATH_TMP "perror.XXXXXXXX");
55108088Stjr	if (mkstemp(tmpfil) < 0)
56108088Stjr		err(1, "mkstemp");
57108088Stjr	atexit(cleanup);
58108088Stjr	/* Reopen stderr on a file descriptor other than 2. */
59108088Stjr	fclose(stderr);
60108088Stjr	for (i = 0; i < 3; i++)
61108088Stjr		dup(0);
62108088Stjr	if (freopen(tmpfil, "r+", stderr) == NULL)
63108088Stjr		err(1, "%s", tmpfil);
64108088Stjr
65108088Stjr	/*
66108088Stjr	 * Test that perror() doesn't call strerror() (4.4BSD bug),
67108088Stjr	 * the two ways of omitting a program name, and the formatting when
68108088Stjr	 * a program name is specified.
69108088Stjr	 */
70108088Stjr	s = strerror(ENOENT);
71108088Stjr	assert(strcmp(s, "No such file or directory") == 0);
72108088Stjr	errno = EPERM;
73108088Stjr	perror(NULL);
74108088Stjr	perror("");
75108088Stjr	perror("test-perror");
76108088Stjr	assert(strcmp(s, "No such file or directory") == 0);
77108088Stjr
78108088Stjr	/*
79108088Stjr	 * Read it back to check...
80108088Stjr	 */
81108088Stjr	rewind(stderr);
82108088Stjr	s = fgets(lbuf, sizeof(lbuf), stderr);
83108088Stjr	assert(s != NULL);
84108088Stjr	assert(strcmp(s, "Operation not permitted\n") == 0);
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, "test-perror: Operation not permitted\n") == 0);
91108088Stjr	s = fgets(lbuf, sizeof(lbuf), stderr);
92108088Stjr	assert(s == NULL);
93108088Stjr	fclose(stderr);
94108088Stjr
95108088Stjr	printf("PASS perror()\n");
96108088Stjr
97108088Stjr	return (0);
98108088Stjr}
99108088Stjr
100108088Stjrstatic void
101108088Stjrcleanup(void)
102108088Stjr{
103108088Stjr
104108088Stjr	unlink(tmpfil);
105108088Stjr}
106