NetBSD KNF'ed
[eleutheria.git] / atf / libc / popen.c
blobf7ca9b6142b0441a047516e9a996ac49d5c71cb5
1 /* $NetBSD: popen.c,v 1.4 2008/07/21 14:33:31 lukem Exp $ */
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Scheler.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1999\
35 The NetBSD Foundation, Inc. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 __RCSID("$NetBSD: popen.c,v 1.4 2008/07/21 14:33:31 lukem Exp $");
40 #endif /* not lint */
42 #include <sys/param.h>
44 #include <atf-c.h>
45 #include <errno.h>
46 #include <paths.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <time.h>
50 #include <unistd.h>
52 #define _PATH_CAT "/bin/cat"
53 #define BUFSIZE (640 * 1024)
54 /* 640KB ought to be enough for everyone. */
55 #define DATAFILE "popen.data"
57 /* Test case -- popen() */
58 ATF_TC(test_popen);
59 ATF_TC_HEAD(test_popen, tc)
61 atf_tc_set_md_var(tc, "descr", "Tests the popen(3) function");
63 ATF_TC_BODY(test_popen, tc)
65 char *buffer, command[MAXPATHLEN];
66 int index, in;
67 FILE *pipe;
69 /* Allocate memory for buffer */
70 if ((buffer = malloc(BUFSIZE)) == NULL)
71 atf_tc_fail("malloc() couldn't allocate %u bytes of memory",
72 BUFSIZE);
73 /* err(1, NULL); */
75 /* Initialize random number generator */
76 srand((unsigned int)time(NULL));
78 /* Populate buffer with random data */
79 for (index = 0 ; index < BUFSIZE; index++)
80 buffer[index] = (char)rand();
83 * Construct the shell command line, e.g:
84 * /bin/cat > popen.data
86 (void)snprintf(command, sizeof(command), "%s > %s",
87 _PATH_CAT, DATAFILE);
89 /* Pipe magic begins here */
90 if ((pipe = popen(command, "w")) == NULL)
91 atf_tc_fail("popen() failed to open pipe in write mode");
92 /* err(1, "popen write"); */
94 if (fwrite(buffer, 1, BUFSIZE, pipe) != BUFSIZE)
95 atf_tc_fail("fwrite() failed to write to pipe");
96 /* err(1, "write"); */
98 if (pclose(pipe) == -1)
99 atf_tc_fail("pclose() failed to close pipe");
100 /* err(1, "pclose"); */
102 /* */
103 (void)snprintf(command, sizeof(command), "%s %s",
104 _PATH_CAT, DATAFILE);
105 if ((pipe = popen(command, "r")) == NULL)
106 atf_tc_fail("popen read");
107 /* err(1, "popen read"); */
109 index = 0;
110 while ((in = fgetc(pipe)) != EOF)
111 if (index == BUFSIZE) {
112 errno = EFBIG;
113 atf_tc_fail("read");
114 /* err(1, "read"); */
116 else
117 if ((char)in != buffer[index++]) {
118 errno = EINVAL;
119 atf_tc_fail("read");
120 /* err(1, "read"); */
123 if (index < BUFSIZE) {
124 errno = EIO;
125 atf_tc_fail("read");
126 /* err(1, "read"); */
129 if (pclose(pipe) == -1)
130 atf_tc_fail("pclose");
131 /* err(1, "pclose"); */
133 (void)unlink(DATAFILE);
136 /* Add test case to test program */
137 ATF_TP_ADD_TCS(tp)
139 ATF_TP_ADD_TC(tp, test_popen);
141 return atf_no_error();