1 /* $NetBSD: t_popen.c,v 1.4 2013/02/15 23:27:19 christos 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: t_popen.c,v 1.4 2013/02/15 23:27:19 christos Exp $");
44 #include <sys/param.h>
54 #define _PATH_CAT "/bin/cat"
55 #define BUFSIZE (640*1024)
56 /* 640KB ought to be enough for everyone. */
57 #define DATAFILE "popen.data"
59 #define TEST_ERROR(a) \
63 atf_tc_fail("Check stderr for error details."); \
64 } while ( /*CONSTCOND*/ 0 )
66 ATF_TC_WITH_CLEANUP(popen_zeropad
);
67 ATF_TC_HEAD(popen_zeropad
, tc
)
70 atf_tc_set_md_var(tc
, "descr", "output format zero padding");
73 ATF_TC_BODY(popen_zeropad
, tc
)
75 char *buffer
, command
[MAXPATHLEN
];
79 if ((buffer
= malloc(BUFSIZE
)) == NULL
)
80 atf_tc_skip("Unable to allocate buffer.");
82 srand ((unsigned int)time(NULL
));
84 for (idx
= 0; idx
< BUFSIZE
; idx
++)
85 buffer
[idx
]=(char)rand();
87 (void)snprintf(command
, sizeof(command
), "%s >%s",
90 if ((my_pipe
= popen(command
, "w")) == NULL
)
91 TEST_ERROR("popen write");
93 if (fwrite(buffer
, sizeof(char), BUFSIZE
, my_pipe
) != BUFSIZE
)
96 if (pclose(my_pipe
) == -1)
99 (void)snprintf(command
, sizeof(command
), "%s %s", _PATH_CAT
, DATAFILE
);
101 if ((my_pipe
= popen(command
, "r")) == NULL
)
102 TEST_ERROR("popen read");
105 while ((in
= fgetc(my_pipe
)) != EOF
)
106 if (idx
== BUFSIZE
) {
110 else if ((char)in
!= buffer
[idx
++]) {
120 if (pclose(my_pipe
) == -1)
121 TEST_ERROR("pclose");
124 ATF_TC_CLEANUP(popen_zeropad
, tc
)
126 (void)unlink(DATAFILE
);
132 ATF_TP_ADD_TC(tp
, popen_zeropad
);
134 return atf_no_error();