t-fget.c revision 266692
139287Ssos/*
239643Syokota * Copyright (c) 2013 Proofpoint, Inc. and its suppliers.
339287Ssos *	All rights reserved.
439287Ssos *
5146736Sdelphij * By using this file, you agree to the terms and conditions set
6146736Sdelphij * forth in the LICENSE file which can be found at the top level of
7146736Sdelphij * the sendmail distribution.
839287Ssos */
939287Ssos
1039287Ssos#include <sm/gen.h>
1139287SsosSM_IDSTR(id, "@(#)$Id: t-fget.c,v 1.2 2013-11-22 20:51:43 ca Exp $")
1239643Syokota
1339643Syokota#include <sm/io.h>
1439287Ssos#include <sm/string.h>
1539287Ssos#include <sm/test.h>
1639287Ssos#include <errno.h>
1739287Ssos
1839643Syokotavoid
1939643Syokotacheck(char *msg, int l)
2039643Syokota{
2139643Syokota	SM_FILE_T *wfp, *rfp;
2239643Syokota	char buf[256];
2339643Syokota	size_t n;
2439643Syokota	int r, i;
2539643Syokota	static char fn[] = "tfget";
2639643Syokota
2739643Syokota	wfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fn,
2839287Ssos			   SM_IO_WRONLY_B, NULL);
2939287Ssos	SM_TEST(wfp != NULL);
3039287Ssos	for (i = 0; i < l; i++)
31119420Sobrien	{
32119420Sobrien		r = sm_io_putc(wfp, SM_TIME_DEFAULT, msg[i]);
33119420Sobrien		SM_TEST(r >= 0);
3442504Syokota	}
3539287Ssos	r = sm_io_close(wfp, SM_TIME_DEFAULT);
3656836Speter	SM_TEST(r == 0);
3739287Ssos
3839287Ssos	rfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fn,
3939287Ssos			   SM_IO_RDONLY_B, NULL);
4039668Syokota	SM_TEST(rfp != NULL);
4139287Ssos	n = sizeof(buf);
4239287Ssos	r = sm_io_fgets(rfp, SM_TIME_DEFAULT, buf, n);
4366859Sphk	if (l == 0)
4466859Sphk	{
4539287Ssos		SM_TEST(r == -1);
4639287Ssos		SM_TEST(errno == 0);
4739287Ssos	}
4842504Syokota	else
4942504Syokota	{
5039287Ssos		SM_TEST(r == l);
5139667Syokota		if (r != l)
5239287Ssos			fprintf(stderr, "buf='%s', in='%s', r=%d, l=%d\n",
5339667Syokota				buf, msg, r, l);
54130585Sphk	}
5539287Ssos	SM_TEST(memcmp(buf, msg, l) == 0);
5639287Ssos	r = sm_io_close(rfp, SM_TIME_DEFAULT);
5739287Ssos	SM_TEST(r == 0);
5839287Ssos}
5939287Ssos
6051654Sphk
6139287Ssosint
6239287Ssosmain(argc, argv)
6351404Syokota	int argc;
6439287Ssos	char **argv;
6539287Ssos{
6639287Ssos	char res[256];
6739591Syokota	int l;
6839591Syokota
6939591Syokota	sm_test_begin(argc, argv, "test fget");
7039591Syokota
7148104Syokota	check("", strlen(""));
7239591Syokota	check("\n", strlen("\n"));
73149640Srodrigc	check("test\n", strlen("test\n"));
7439591Syokota
7539287Ssos	l = snprintf(res, sizeof(res), "%c%s\n", '\0', "test ing");
7639287Ssos	check(res, l);
7739287Ssos
7839287Ssos	l = snprintf(res, sizeof(res), "%c%s%c\n", '\0', "test ing", '\0');
7939287Ssos	check(res, l);
8039287Ssos
8148104Syokota	l = snprintf(res, sizeof(res), "%c%s%c%s\n",
8239287Ssos		'\0', "test ing", '\0', "eol");
8339287Ssos	check(res, l);
84149640Srodrigc
8539287Ssos	return sm_test_end();
8639287Ssos}
8739287Ssos