xref: /freebsd/contrib/sendmail/libsm/sscanf.c (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
1 /*
2  * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers.
3  *      All rights reserved.
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Chris Torek.
9  *
10  * By using this file, you agree to the terms and conditions set
11  * forth in the LICENSE file which can be found at the top level of
12  * the sendmail distribution.
13  */
14 
15 #include <sm/gen.h>
16 SM_RCSID("@(#)$Id: sscanf.c,v 1.26 2013-11-22 20:51:43 ca Exp $")
17 #include <string.h>
18 #include <sm/varargs.h>
19 #include <sm/io.h>
20 #include "local.h"
21 
22 /*
23 **  SM_EOFREAD -- dummy read function for faked file below
24 **
25 **	Parameters:
26 **		fp -- file pointer
27 **		buf -- location to place read data
28 **		len -- number of bytes to read
29 **
30 **	Returns:
31 **		0 (zero) always
32 */
33 
34 static ssize_t
35 sm_eofread __P((
36 	SM_FILE_T *fp,
37 	char *buf,
38 	size_t len));
39 
40 /* ARGSUSED0 */
41 static ssize_t
42 sm_eofread(fp, buf, len)
43 	SM_FILE_T *fp;
44 	char *buf;
45 	size_t len;
46 {
47 	return 0;
48 }
49 
50 /*
51 **  SM_IO_SSCANF -- scan a string to find data units
52 **
53 **	Parameters:
54 **		str -- strings containing data
55 **		fmt -- format directive for finding data units
56 **		... -- memory locations to place format found data units
57 **
58 **	Returns:
59 **		Failure: SM_IO_EOF
60 **		Success: number of data units found
61 **
62 **	Side Effects:
63 **		Attempts to strlen() 'str'; if not a '\0' terminated string
64 **			then the call may SEGV/fail.
65 **		Faking the string 'str' as a file.
66 */
67 
68 int
69 #if SM_VA_STD
70 sm_io_sscanf(const char *str, char const *fmt, ...)
71 #else /* SM_VA_STD */
72 sm_io_sscanf(str, fmt, va_alist)
73 	const char *str;
74 	char *fmt;
75 	va_dcl
76 #endif /* SM_VA_STD */
77 {
78 	int ret;
79 	SM_FILE_T fake;
80 	SM_VA_LOCAL_DECL
81 
82 	fake.sm_magic = SmFileMagic;
83 	fake.f_flags = SMRD;
84 	fake.f_bf.smb_base = fake.f_p = (unsigned char *) str;
85 	fake.f_bf.smb_size = fake.f_r = strlen(str);
86 	fake.f_file = -1;
87 	fake.f_read = sm_eofread;
88 	fake.f_write = NULL;
89 	fake.f_close = NULL;
90 	fake.f_open = NULL;
91 	fake.f_seek = NULL;
92 	fake.f_setinfo = fake.f_getinfo = NULL;
93 	fake.f_type = "sm_io_sscanf:fake";
94 	fake.f_flushfp = NULL;
95 	fake.f_ub.smb_base = NULL;
96 	fake.f_timeout = SM_TIME_FOREVER;
97 	fake.f_timeoutstate = SM_TIME_BLOCK;
98 	SM_VA_START(ap, fmt);
99 	ret = sm_vfscanf(&fake, SM_TIME_FOREVER, fmt, ap);
100 	SM_VA_END(ap);
101 	return ret;
102 }
103