t-fget.c revision 266527
1119895Sanholt/*
2119895Sanholt * Copyright (c) 2013 Proofpoint, Inc. and its suppliers.
3119895Sanholt *	All rights reserved.
4119895Sanholt *
5119895Sanholt * By using this file, you agree to the terms and conditions set
6119895Sanholt * forth in the LICENSE file which can be found at the top level of
7119895Sanholt * the sendmail distribution.
8119895Sanholt */
9119895Sanholt
10119895Sanholt#include <sm/gen.h>
11119895SanholtSM_IDSTR(id, "@(#)$Id: t-fget.c,v 1.2 2013-11-22 20:51:43 ca Exp $")
12119895Sanholt
13119895Sanholt#include <sm/io.h>
14119895Sanholt#include <sm/string.h>
15119895Sanholt#include <sm/test.h>
16119895Sanholt#include <errno.h>
17119895Sanholt
18119895Sanholtvoid
19119895Sanholtcheck(char *msg, int l)
20119895Sanholt{
21119895Sanholt	SM_FILE_T *wfp, *rfp;
22119895Sanholt	char buf[256];
23119895Sanholt	size_t n;
24119895Sanholt	int r, i;
25119895Sanholt	static char fn[] = "tfget";
26119895Sanholt
27119895Sanholt	wfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fn,
28119895Sanholt			   SM_IO_WRONLY_B, NULL);
29119895Sanholt	SM_TEST(wfp != NULL);
30119895Sanholt	for (i = 0; i < l; i++)
31119895Sanholt	{
32119895Sanholt		r = sm_io_putc(wfp, SM_TIME_DEFAULT, msg[i]);
33119895Sanholt		SM_TEST(r >= 0);
34119895Sanholt	}
35119895Sanholt	r = sm_io_close(wfp, SM_TIME_DEFAULT);
36119895Sanholt	SM_TEST(r == 0);
37
38	rfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fn,
39			   SM_IO_RDONLY_B, NULL);
40	SM_TEST(rfp != NULL);
41	n = sizeof(buf);
42	r = sm_io_fgets(rfp, SM_TIME_DEFAULT, buf, n);
43	if (l == 0)
44	{
45		SM_TEST(r == -1);
46		SM_TEST(errno == 0);
47	}
48	else
49	{
50		SM_TEST(r == l);
51		if (r != l)
52			fprintf(stderr, "buf='%s', in='%s', r=%d, l=%d\n",
53				buf, msg, r, l);
54	}
55	SM_TEST(memcmp(buf, msg, l) == 0);
56	r = sm_io_close(rfp, SM_TIME_DEFAULT);
57	SM_TEST(r == 0);
58}
59
60
61int
62main(argc, argv)
63	int argc;
64	char **argv;
65{
66	char res[256];
67	int l;
68
69	sm_test_begin(argc, argv, "test fget");
70
71	check("", strlen(""));
72	check("\n", strlen("\n"));
73	check("test\n", strlen("test\n"));
74
75	l = snprintf(res, sizeof(res), "%c%s\n", '\0', "test ing");
76	check(res, l);
77
78	l = snprintf(res, sizeof(res), "%c%s%c\n", '\0', "test ing", '\0');
79	check(res, l);
80
81	l = snprintf(res, sizeof(res), "%c%s%c%s\n",
82		'\0', "test ing", '\0', "eol");
83	check(res, l);
84
85	return sm_test_end();
86}
87