xref: /freebsd/contrib/sendmail/libsm/t-fget.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /*
2  * Copyright (c) 2013 Proofpoint, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  */
9 
10 #include <sm/gen.h>
11 SM_IDSTR(id, "@(#)$Id: t-fget.c,v 1.2 2013-11-22 20:51:43 ca Exp $")
12 
13 #include <sm/io.h>
14 #include <sm/string.h>
15 #include <sm/test.h>
16 #include <errno.h>
17 
18 void
19 check(char *msg, int l)
20 {
21 	SM_FILE_T *wfp, *rfp;
22 	char buf[256];
23 	size_t n;
24 	int r, i;
25 	static char fn[] = "tfget";
26 
27 	wfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fn,
28 			   SM_IO_WRONLY_B, NULL);
29 	SM_TEST(wfp != NULL);
30 	for (i = 0; i < l; i++)
31 	{
32 		r = sm_io_putc(wfp, SM_TIME_DEFAULT, msg[i]);
33 		SM_TEST(r >= 0);
34 	}
35 	r = sm_io_close(wfp, SM_TIME_DEFAULT);
36 	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 
61 int
62 main(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