1249727Sgshapiro/*
2249727Sgshapiro * Copyright (c) 2013 Sendmail, Inc. and its suppliers.
3249727Sgshapiro *	All rights reserved.
4249727Sgshapiro *
5249727Sgshapiro * By using this file, you agree to the terms and conditions set
6249727Sgshapiro * forth in the LICENSE file which can be found at the top level of
7249727Sgshapiro * the sendmail distribution.
8249727Sgshapiro */
9249727Sgshapiro
10249727Sgshapiro#include <sm/gen.h>
11249727SgshapiroSM_IDSTR(id, "@(#)$Id: t-fget.c,v 1.1 2013/03/12 15:24:50 ca Exp $")
12249727Sgshapiro
13249727Sgshapiro#include <sm/io.h>
14249727Sgshapiro#include <sm/string.h>
15249727Sgshapiro#include <sm/test.h>
16249727Sgshapiro#include <errno.h>
17249727Sgshapiro
18249727Sgshapirovoid
19249727Sgshapirocheck(char *msg, int l)
20249727Sgshapiro{
21249727Sgshapiro	SM_FILE_T *wfp, *rfp;
22249727Sgshapiro	char buf[256];
23249727Sgshapiro	size_t n;
24249727Sgshapiro	int r, i;
25249727Sgshapiro	static char fn[] = "tfget";
26249727Sgshapiro
27249727Sgshapiro	wfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fn,
28249727Sgshapiro			   SM_IO_WRONLY_B, NULL);
29249727Sgshapiro	SM_TEST(wfp != NULL);
30249727Sgshapiro	for (i = 0; i < l; i++)
31249727Sgshapiro	{
32249727Sgshapiro		r = sm_io_putc(wfp, SM_TIME_DEFAULT, msg[i]);
33249727Sgshapiro		SM_TEST(r >= 0);
34249727Sgshapiro	}
35249727Sgshapiro	r = sm_io_close(wfp, SM_TIME_DEFAULT);
36249727Sgshapiro	SM_TEST(r == 0);
37249727Sgshapiro
38249727Sgshapiro	rfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fn,
39249727Sgshapiro			   SM_IO_RDONLY_B, NULL);
40249727Sgshapiro	SM_TEST(rfp != NULL);
41249727Sgshapiro	n = sizeof(buf);
42249727Sgshapiro	r = sm_io_fgets(rfp, SM_TIME_DEFAULT, buf, n);
43249727Sgshapiro	if (l == 0)
44249727Sgshapiro	{
45249727Sgshapiro		SM_TEST(r == -1);
46249727Sgshapiro		SM_TEST(errno == 0);
47249727Sgshapiro	}
48249727Sgshapiro	else
49249727Sgshapiro	{
50249727Sgshapiro		SM_TEST(r == l);
51249727Sgshapiro		if (r != l)
52249727Sgshapiro			fprintf(stderr, "buf='%s', in='%s', r=%d, l=%d\n",
53249727Sgshapiro				buf, msg, r, l);
54249727Sgshapiro	}
55249727Sgshapiro	SM_TEST(memcmp(buf, msg, l) == 0);
56249727Sgshapiro	r = sm_io_close(rfp, SM_TIME_DEFAULT);
57249727Sgshapiro	SM_TEST(r == 0);
58249727Sgshapiro}
59249727Sgshapiro
60249727Sgshapiro
61249727Sgshapiroint
62249727Sgshapiromain(argc, argv)
63249727Sgshapiro	int argc;
64249727Sgshapiro	char **argv;
65249727Sgshapiro{
66249727Sgshapiro	char res[256];
67249727Sgshapiro	int l;
68249727Sgshapiro
69249727Sgshapiro	sm_test_begin(argc, argv, "test fget");
70249727Sgshapiro
71249727Sgshapiro	check("", strlen(""));
72249727Sgshapiro	check("\n", strlen("\n"));
73249727Sgshapiro	check("test\n", strlen("test\n"));
74249727Sgshapiro
75249727Sgshapiro	l = snprintf(res, sizeof(res), "%c%s\n", '\0', "test ing");
76249727Sgshapiro	check(res, l);
77249727Sgshapiro
78249727Sgshapiro	l = snprintf(res, sizeof(res), "%c%s%c\n", '\0', "test ing", '\0');
79249727Sgshapiro	check(res, l);
80249727Sgshapiro
81249727Sgshapiro	l = snprintf(res, sizeof(res), "%c%s%c%s\n",
82249727Sgshapiro		'\0', "test ing", '\0', "eol");
83249727Sgshapiro	check(res, l);
84249727Sgshapiro
85249727Sgshapiro	return sm_test_end();
86249727Sgshapiro}
87