Remove building with NOCRYPTO option
[minix.git] / tests / lib / libc / stdio / t_popen.c
blob699498c09fcc9b0731c7ecfbf366c07b081f741e
1 /* $NetBSD: t_popen.c,v 1.4 2013/02/15 23:27:19 christos 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: t_popen.c,v 1.4 2013/02/15 23:27:19 christos Exp $");
40 #endif /* not lint */
42 #include <atf-c.h>
44 #include <sys/param.h>
46 #include <errno.h>
47 #include <err.h>
48 #include <paths.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <time.h>
52 #include <unistd.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) \
60 do \
61 { \
62 warn(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];
76 int idx, in;
77 FILE *my_pipe;
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",
88 _PATH_CAT, DATAFILE);
90 if ((my_pipe = popen(command, "w")) == NULL)
91 TEST_ERROR("popen write");
93 if (fwrite(buffer, sizeof(char), BUFSIZE, my_pipe) != BUFSIZE)
94 TEST_ERROR("fwrite");
96 if (pclose(my_pipe) == -1)
97 TEST_ERROR("pclose");
99 (void)snprintf(command, sizeof(command), "%s %s", _PATH_CAT, DATAFILE);
101 if ((my_pipe = popen(command, "r")) == NULL)
102 TEST_ERROR("popen read");
104 idx = 0;
105 while ((in = fgetc(my_pipe)) != EOF)
106 if (idx == BUFSIZE) {
107 errno = EFBIG;
108 TEST_ERROR("read");
110 else if ((char)in != buffer[idx++]) {
111 errno = EINVAL;
112 TEST_ERROR("read");
115 if (idx < BUFSIZE) {
116 errno = EIO;
117 TEST_ERROR("read");
120 if (pclose(my_pipe) == -1)
121 TEST_ERROR("pclose");
124 ATF_TC_CLEANUP(popen_zeropad, tc)
126 (void)unlink(DATAFILE);
129 ATF_TP_ADD_TCS(tp)
132 ATF_TP_ADD_TC(tp, popen_zeropad);
134 return atf_no_error();