1 /* $NetBSD: popen.c,v 1.4 2008/07/21 14:33:31 lukem Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
34 __COPYRIGHT("@(#) Copyright (c) 1999\
35 The NetBSD Foundation, Inc. All rights reserved.");
39 __RCSID("$NetBSD: popen.c,v 1.4 2008/07/21 14:33:31 lukem Exp $");
42 #include <sys/param.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() */
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
];
69 /* Allocate memory for buffer */
70 if ((buffer
= malloc(BUFSIZE
)) == NULL
)
71 atf_tc_fail("malloc() couldn't allocate %u bytes of memory",
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",
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"); */
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"); */
110 while ((in
= fgetc(pipe
)) != EOF
)
111 if (index
== BUFSIZE
) {
114 /* err(1, "read"); */
117 if ((char)in
!= buffer
[index
++]) {
120 /* err(1, "read"); */
123 if (index
< BUFSIZE
) {
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 */
139 ATF_TP_ADD_TC(tp
, test_popen
);
141 return atf_no_error();