1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2002
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/* 01/02/2003	Port to LTP avenkat@us.ibm.com */
21f08c3bdfSopenharmony_ci/* 06/30/2001	Port to Linux	nsharoff@us.ibm.com */
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci/*
24f08c3bdfSopenharmony_ci * fmtmsg(3C) and addseverity(3C)
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci * ALGORITHM
27f08c3bdfSopenharmony_ci *      Check basic functionality using various messages and severity levels.
28f08c3bdfSopenharmony_ci */
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci#include <sys/types.h>
31f08c3bdfSopenharmony_ci#include <sys/stat.h>
32f08c3bdfSopenharmony_ci#include <fcntl.h>
33f08c3bdfSopenharmony_ci#include <ctype.h>
34f08c3bdfSopenharmony_ci#include <stdio.h>
35f08c3bdfSopenharmony_ci#if !defined(__UCLIBC__)
36f08c3bdfSopenharmony_ci#include <fmtmsg.h>		/* interface definition */
37f08c3bdfSopenharmony_ci#endif
38f08c3bdfSopenharmony_ci#include <string.h>
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci#include <stdlib.h>
41f08c3bdfSopenharmony_ci#include <unistd.h>
42f08c3bdfSopenharmony_ci#include <errno.h>
43f08c3bdfSopenharmony_ci#include "test.h"
44f08c3bdfSopenharmony_ci#define FAILED 0
45f08c3bdfSopenharmony_ci#define PASSED 1
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cichar *TCID = "fmtms01";
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ciint local_flag = PASSED;
50f08c3bdfSopenharmony_ciint block_number;
51f08c3bdfSopenharmony_ciFILE *temp;
52f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ciint anyfail();
55f08c3bdfSopenharmony_ciint blenter();
56f08c3bdfSopenharmony_ciint blexit();
57f08c3bdfSopenharmony_civoid setup();
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cichar ch;
60f08c3bdfSopenharmony_cichar buf[80];
61f08c3bdfSopenharmony_cichar *str1 = "LTP:fmtmsg: INFO: LTP fmtmsg() test1 message, NOT an error";
62f08c3bdfSopenharmony_cichar *str2 = "TO FIX: This is correct output, no action needed  LTP:msg:001";
63f08c3bdfSopenharmony_cichar *str3 = "LTP:fmtmsg: LTP_TEST: LTP fmtmsg() test2 message, NOT an error";
64f08c3bdfSopenharmony_cichar *str4 = "TO FIX: This is correct output, no action needed  LTP:msg:002";
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_civoid clearbuf(void)
67f08c3bdfSopenharmony_ci{
68f08c3bdfSopenharmony_ci	int i;
69f08c3bdfSopenharmony_ci	for (i = 0; i < 80; i++)
70f08c3bdfSopenharmony_ci		buf[i] = '\0';
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci#if !defined(__UCLIBC__)
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ciint main(int argc, char *argv[])
76f08c3bdfSopenharmony_ci{
77f08c3bdfSopenharmony_ci	int fd, ret_val;
78f08c3bdfSopenharmony_ci	FILE *fp;
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	tst_parse_opts(argc, argv, NULL, NULL);
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	setup();
83f08c3bdfSopenharmony_ci/*--------------------------------------------------------------*/
84f08c3bdfSopenharmony_ci	blenter();
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	/* Check that system SEV_LEVEL output is correct */
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	close(2);		/* redirect stderr to file */
89f08c3bdfSopenharmony_ci	fd = creat("fmtfile", 0644);
90f08c3bdfSopenharmony_ci	ret_val = fmtmsg(MM_PRINT | MM_SOFT, "LTP:fmtmsg", MM_INFO,
91f08c3bdfSopenharmony_ci			 "LTP fmtmsg() test1 message, NOT an error",
92f08c3bdfSopenharmony_ci			 "This is correct output, no action needed",
93f08c3bdfSopenharmony_ci			 "LTP:msg:001");
94f08c3bdfSopenharmony_ci	close(fd);
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	if (ret_val != 0) {
97f08c3bdfSopenharmony_ci		fprintf(temp, "fmtmsg returned %d, expected 0\n\n", ret_val);
98f08c3bdfSopenharmony_ci		local_flag = FAILED;
99f08c3bdfSopenharmony_ci	}
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	fp = fopen("fmtfile", "r");
102f08c3bdfSopenharmony_ci	clearbuf();
103f08c3bdfSopenharmony_ci	fread(buf, sizeof(buf[0]), strlen(str1), fp);
104f08c3bdfSopenharmony_ci	if (strcmp(str1, buf) != 0) {
105f08c3bdfSopenharmony_ci		fprintf(temp, "Expected string: %s\n", str1);
106f08c3bdfSopenharmony_ci		fprintf(temp, "does not match\n");
107f08c3bdfSopenharmony_ci		fprintf(temp, "received string: %s\n\n", buf);
108f08c3bdfSopenharmony_ci		local_flag = FAILED;
109f08c3bdfSopenharmony_ci	}
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci	/* Read past spaces in output */
112f08c3bdfSopenharmony_ci	fread(&ch, sizeof(ch), 1, fp);
113f08c3bdfSopenharmony_ci	while (isspace(ch))
114f08c3bdfSopenharmony_ci		fread(&ch, sizeof(ch), 1, fp);
115f08c3bdfSopenharmony_ci	ungetc(ch, fp);
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci	clearbuf();
118f08c3bdfSopenharmony_ci	fread(buf, sizeof(buf[0]), strlen(str2), fp);
119f08c3bdfSopenharmony_ci	fclose(fp);
120f08c3bdfSopenharmony_ci	if (strcmp(str2, buf) != 0) {
121f08c3bdfSopenharmony_ci		fprintf(temp, "Expected string: %s\n", str2);
122f08c3bdfSopenharmony_ci		fprintf(temp, "does not match\n");
123f08c3bdfSopenharmony_ci		fprintf(temp, "received string: %s\n\n", buf);
124f08c3bdfSopenharmony_ci		local_flag = FAILED;
125f08c3bdfSopenharmony_ci	}
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci	blexit();
128f08c3bdfSopenharmony_ci/*--------------------------------------------------------------*/
129f08c3bdfSopenharmony_ci	blenter();
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci	/* Check that a system defined SEV_LEVEL cannot get redefined */
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_ci	ret_val = addseverity(3, "INVALID");
134f08c3bdfSopenharmony_ci	if (ret_val != MM_NOTOK) {
135f08c3bdfSopenharmony_ci		fprintf(temp, "addseverity returned %d, expected MM_NOTOK\n",
136f08c3bdfSopenharmony_ci			ret_val);
137f08c3bdfSopenharmony_ci		local_flag = FAILED;
138f08c3bdfSopenharmony_ci	}
139f08c3bdfSopenharmony_ci
140f08c3bdfSopenharmony_ci	blexit();
141f08c3bdfSopenharmony_ci/*--------------------------------------------------------------*/
142f08c3bdfSopenharmony_ci	blenter();
143f08c3bdfSopenharmony_ci
144f08c3bdfSopenharmony_ci	/* Check that we can define our own */
145f08c3bdfSopenharmony_ci	/* SEV_LEVEL and output is correct  */
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_ci	ret_val = addseverity(5, "LTP_TEST");
148f08c3bdfSopenharmony_ci	if (ret_val != MM_OK) {
149f08c3bdfSopenharmony_ci		fprintf(temp, "addseverity returned %d, expected MM_OK\n",
150f08c3bdfSopenharmony_ci			ret_val);
151f08c3bdfSopenharmony_ci		local_flag = FAILED;
152f08c3bdfSopenharmony_ci	}
153f08c3bdfSopenharmony_ci
154f08c3bdfSopenharmony_ci	close(2);		/* redirect stderr to file */
155f08c3bdfSopenharmony_ci	fd = creat("fmtfile", 0644);
156f08c3bdfSopenharmony_ci	ret_val = fmtmsg(MM_PRINT | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5,
157f08c3bdfSopenharmony_ci			 "LTP fmtmsg() test2 message, NOT an error",
158f08c3bdfSopenharmony_ci			 "This is correct output, no action needed",
159f08c3bdfSopenharmony_ci			 "LTP:msg:002");
160f08c3bdfSopenharmony_ci	close(fd);
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_ci	if (ret_val != 0) {
163f08c3bdfSopenharmony_ci		fprintf(temp, "fmtmsg returned %d, expected 0\n", ret_val);
164f08c3bdfSopenharmony_ci		local_flag = FAILED;
165f08c3bdfSopenharmony_ci	}
166f08c3bdfSopenharmony_ci
167f08c3bdfSopenharmony_ci	fp = fopen("fmtfile", "r");
168f08c3bdfSopenharmony_ci	clearbuf();
169f08c3bdfSopenharmony_ci	fread(buf, sizeof(buf[0]), strlen(str3), fp);
170f08c3bdfSopenharmony_ci	if (strcmp(str3, buf) != 0) {
171f08c3bdfSopenharmony_ci		fprintf(temp, "Expected string: %s\n", str3);
172f08c3bdfSopenharmony_ci		fprintf(temp, "does not match\n");
173f08c3bdfSopenharmony_ci		fprintf(temp, "received string: %s\n\n", buf);
174f08c3bdfSopenharmony_ci		local_flag = FAILED;
175f08c3bdfSopenharmony_ci	}
176f08c3bdfSopenharmony_ci
177f08c3bdfSopenharmony_ci	/* Read past spaces in output */
178f08c3bdfSopenharmony_ci	fread(&ch, sizeof(ch), 1, fp);
179f08c3bdfSopenharmony_ci	while (isspace(ch))
180f08c3bdfSopenharmony_ci		fread(&ch, sizeof(ch), 1, fp);
181f08c3bdfSopenharmony_ci	ungetc(ch, fp);
182f08c3bdfSopenharmony_ci
183f08c3bdfSopenharmony_ci	clearbuf();
184f08c3bdfSopenharmony_ci	fread(buf, sizeof(buf[0]), strlen(str4), fp);
185f08c3bdfSopenharmony_ci	if (strcmp(str4, buf) != 0) {
186f08c3bdfSopenharmony_ci		fprintf(temp, "Expected string: %s\n", str4);
187f08c3bdfSopenharmony_ci		fprintf(temp, "does not match\n");
188f08c3bdfSopenharmony_ci		fprintf(temp, "received string: %s\n\n", buf);
189f08c3bdfSopenharmony_ci		local_flag = FAILED;
190f08c3bdfSopenharmony_ci	}
191f08c3bdfSopenharmony_ci
192f08c3bdfSopenharmony_ci	fclose(fp);
193f08c3bdfSopenharmony_ci	remove("fmtfile");
194f08c3bdfSopenharmony_ci
195f08c3bdfSopenharmony_ci	blexit();
196f08c3bdfSopenharmony_ci/*--------------------------------------------------------------*/
197f08c3bdfSopenharmony_ci	blenter();
198f08c3bdfSopenharmony_ci
199f08c3bdfSopenharmony_ci	/* Test result of writing to /dev/console */
200f08c3bdfSopenharmony_ci
201f08c3bdfSopenharmony_ci	ret_val = fmtmsg(MM_CONSOLE | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5,
202f08c3bdfSopenharmony_ci			 "LTP fmtmsg() test3 message, NOT an error",
203f08c3bdfSopenharmony_ci			 "This is correct output, no action needed",
204f08c3bdfSopenharmony_ci			 "LTP:msg:003");
205f08c3bdfSopenharmony_ci	if (ret_val != MM_OK) {
206f08c3bdfSopenharmony_ci		fprintf(temp, "fmtmsg returned %d, expected MM_OK\n", ret_val);
207f08c3bdfSopenharmony_ci		fprintf(temp, "failed to write to console\n\n");
208f08c3bdfSopenharmony_ci		local_flag = FAILED;
209f08c3bdfSopenharmony_ci	}
210f08c3bdfSopenharmony_ci
211f08c3bdfSopenharmony_ci	blexit();
212f08c3bdfSopenharmony_ci	anyfail();
213f08c3bdfSopenharmony_ci	tst_exit();
214f08c3bdfSopenharmony_ci}
215f08c3bdfSopenharmony_ci
216f08c3bdfSopenharmony_ciint anyfail(void)
217f08c3bdfSopenharmony_ci{
218f08c3bdfSopenharmony_ci	(local_flag == FAILED) ? tst_resm(TFAIL,
219f08c3bdfSopenharmony_ci					  "Test failed") : tst_resm(TPASS,
220f08c3bdfSopenharmony_ci								    "Test passed");
221f08c3bdfSopenharmony_ci	tst_rmdir();
222f08c3bdfSopenharmony_ci	tst_exit();
223f08c3bdfSopenharmony_ci}
224f08c3bdfSopenharmony_ci
225f08c3bdfSopenharmony_civoid setup(void)
226f08c3bdfSopenharmony_ci{
227f08c3bdfSopenharmony_ci	temp = stderr;
228f08c3bdfSopenharmony_ci	tst_tmpdir();
229f08c3bdfSopenharmony_ci}
230f08c3bdfSopenharmony_ci
231f08c3bdfSopenharmony_ciint blenter(void)
232f08c3bdfSopenharmony_ci{
233f08c3bdfSopenharmony_ci	local_flag = PASSED;
234f08c3bdfSopenharmony_ci	return 0;
235f08c3bdfSopenharmony_ci}
236f08c3bdfSopenharmony_ci
237f08c3bdfSopenharmony_ciint blexit(void)
238f08c3bdfSopenharmony_ci{
239f08c3bdfSopenharmony_ci	(local_flag == FAILED) ? tst_resm(TFAIL,
240f08c3bdfSopenharmony_ci					  "Test failed") : tst_resm(TPASS,
241f08c3bdfSopenharmony_ci								    "Test passed");
242f08c3bdfSopenharmony_ci	return 0;
243f08c3bdfSopenharmony_ci}
244f08c3bdfSopenharmony_ci
245f08c3bdfSopenharmony_ci#else
246f08c3bdfSopenharmony_ci
247f08c3bdfSopenharmony_ciint main(void)
248f08c3bdfSopenharmony_ci{
249f08c3bdfSopenharmony_ci	tst_brkm(TCONF, NULL, "test is not available on uClibc");
250f08c3bdfSopenharmony_ci}
251f08c3bdfSopenharmony_ci
252f08c3bdfSopenharmony_ci#endif /* if !defined(__UCLIBC__) */
253