xref: /titanic_51/usr/src/cmd/sendmail/libsm/sscanf.c (revision 06e46062ef4f5f4b687cbafb4518fb123fe23920)
1 /*
2  * Copyright (c) 2000-2001 Sendmail, 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include <sm/gen.h>
18 SM_RCSID("@(#)$Id: sscanf.c,v 1.25 2002/02/01 02:28:00 ca Exp $")
19 #include <string.h>
20 #include <sm/varargs.h>
21 #include <sm/io.h>
22 #include "local.h"
23 
24 /*
25 **  SM_EOFREAD -- dummy read function for faked file below
26 **
27 **	Parameters:
28 **		fp -- file pointer
29 **		buf -- location to place read data
30 **		len -- number of bytes to read
31 **
32 **	Returns:
33 **		0 (zero) always
34 */
35 
36 static ssize_t
37 sm_eofread __P((
38 	SM_FILE_T *fp,
39 	char *buf,
40 	size_t len));
41 
42 /* ARGSUSED0 */
43 static ssize_t
44 sm_eofread(fp, buf, len)
45 	SM_FILE_T *fp;
46 	char *buf;
47 	size_t len;
48 {
49 	return 0;
50 }
51 
52 /*
53 **  SM_IO_SSCANF -- scan a string to find data units
54 **
55 **	Parameters:
56 **		str -- strings containing data
57 **		fmt -- format directive for finding data units
58 **		... -- memory locations to place format found data units
59 **
60 **	Returns:
61 **		Failure: SM_IO_EOF
62 **		Success: number of data units found
63 **
64 **	Side Effects:
65 **		Attempts to strlen() 'str'; if not a '\0' terminated string
66 **			then the call may SEGV/fail.
67 **		Faking the string 'str' as a file.
68 */
69 
70 int
71 #if SM_VA_STD
72 sm_io_sscanf(const char *str, char const *fmt, ...)
73 #else /* SM_VA_STD */
74 sm_io_sscanf(str, fmt, va_alist)
75 	const char *str;
76 	char *fmt;
77 	va_dcl
78 #endif /* SM_VA_STD */
79 {
80 	int ret;
81 	SM_FILE_T fake;
82 	SM_VA_LOCAL_DECL
83 
84 	fake.sm_magic = SmFileMagic;
85 	fake.f_flags = SMRD;
86 	fake.f_bf.smb_base = fake.f_p = (unsigned char *) str;
87 	fake.f_bf.smb_size = fake.f_r = strlen(str);
88 	fake.f_file = -1;
89 	fake.f_read = sm_eofread;
90 	fake.f_write = NULL;
91 	fake.f_close = NULL;
92 	fake.f_open = NULL;
93 	fake.f_seek = NULL;
94 	fake.f_setinfo = fake.f_getinfo = NULL;
95 	fake.f_type = "sm_io_sscanf:fake";
96 	fake.f_flushfp = NULL;
97 	fake.f_ub.smb_base = NULL;
98 	fake.f_timeout = SM_TIME_FOREVER;
99 	fake.f_timeoutstate = SM_TIME_BLOCK;
100 	SM_VA_START(ap, fmt);
101 	ret = sm_vfscanf(&fake, SM_TIME_FOREVER, fmt, ap);
102 	SM_VA_END(ap);
103 	return ret;
104 }
105