1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * NAME
22  *	write02.c
23  *
24  * DESCRIPTION
25  *	Basic functionality test: does the return from write match the count
26  *	of the number of bytes written.
27  *
28  *
29  * ALGORITHM
30  *	Create a file and write some bytes out to it.
31  *	Check the return count against the number returned.
32  *
33  * USAGE:  <for command-line>
34  *      write02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
35  *      where,  -c n : Run n copies concurrently.
36  *              -e   : Turn on errno logging.
37  *              -i n : Execute test n times.
38  *              -I x : Execute test for x seconds.
39  *              -P x : Pause for x seconds between iterations.
40  *              -t   : Turn on syscall timing.
41  *
42  * History
43  *	07/2001 John George
44  *		-Ported
45  *
46  * Restrictions
47  *	None
48  */
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <errno.h>
53 #include <stdio.h>
54 #include "test.h"
55 
56 char *TCID = "write02";
57 int TST_TOTAL = 1;
58 
59 void cleanup(void);
60 void setup(void);
61 
62 char pfiln[40] = "";
63 
main(int argc,char ** argv)64 int main(int argc, char **argv)
65 {
66 	int lc;
67 
68 	int cwrite;
69 	int fild;
70 	int iws;
71 	int badcount = 0;
72 	char pwbuf[BUFSIZ + 1];
73 
74 	tst_parse_opts(argc, argv, NULL, NULL);
75 
76 	setup();		/* global setup for test */
77 
78 	/* The following loop checks looping state if -i option given */
79 	for (lc = 0; TEST_LOOPING(lc); lc++) {
80 
81 		/* reset tst_count in case we are looping */
82 		tst_count = 0;
83 
84 //block1:
85 		tst_resm(TINFO, "Block 1: test to see write() returns proper "
86 			 "write count");
87 
88 		for (iws = 0; iws < BUFSIZ; iws++) {
89 			pwbuf[iws] = 'A' + (iws % 26);
90 		}
91 		pwbuf[BUFSIZ] = '\n';
92 
93 		if ((fild = creat(pfiln, 0777)) == -1) {
94 			tst_brkm(TBROK, cleanup, "Can't creat Xwrit");
95 		}
96 		for (iws = BUFSIZ; iws > 0; iws--) {
97 			if ((cwrite = write(fild, pwbuf, iws)) != iws) {
98 				badcount++;
99 				tst_resm(TINFO, "bad write count");
100 			}
101 		}
102 		if (badcount != 0) {
103 			tst_resm(TFAIL, "write() FAILED to return proper cnt");
104 		} else {
105 			tst_resm(TPASS, "write() PASSED");
106 		}
107 		tst_resm(TINFO, "block 1 passed");
108 		close(fild);
109 	}
110 	cleanup();
111 	tst_exit();
112 }
113 
114 /*
115  * setup() - performs all ONE TIME setup for this test
116  */
setup(void)117 void setup(void)
118 {
119 
120 	tst_sig(FORK, DEF_HANDLER, cleanup);
121 
122 	umask(0);
123 
124 	/* Pause if that option was specified
125 	 * TEST_PAUSE contains the code to fork the test with the -i option.
126 	 * You want to make sure you do this before you create your temporary
127 	 * directory.
128 	 */
129 	TEST_PAUSE;
130 
131 	tst_tmpdir();
132 
133 	sprintf(pfiln, "write1.%d", getpid());
134 }
135 
136 /*
137  * cleanup() - performs all ONE TIME cleanup for this test at completion or
138  * premature exit.
139  */
cleanup(void)140 void cleanup(void)
141 {
142 
143 	unlink(pfiln);
144 
145 	tst_rmdir();
146 }
147