xref: /illumos-gate/usr/src/lib/libc/port/stdio/vscanf.c (revision ddb365bfc9e868ad24ccdcb0dc91af18b10df082)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #ifndef _C89_INTMAX32
31 #pragma weak _vscanf = vscanf
32 #pragma weak _vfscanf = vfscanf
33 #pragma weak _vsscanf = vsscanf
34 #endif
35 
36 #include "lint.h"
37 #include "file64.h"
38 #include "mtlib.h"
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <string.h>
42 #include <thread.h>
43 #include <synch.h>
44 #include "libc.h"
45 #include "stdiom.h"
46 #include "mse.h"
47 #include <stdio_ext.h>
48 
49 
50 /*
51  * 32-bit shadow functions _vscanf_c89(), _vfscanf_c89(), _vsscanf_c89()
52  * are included here.
53  * When using the c89 compiler to build 32-bit applications, the size
54  * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
55  * The shadow function uses 32-bit size of intmax_t for %j conversion.
56  * The #pragma redefine_extname in <stdio.h> selects the proper routine
57  * at compile time for the user application.
58  * NOTE: the shadow function only exists in the 32-bit library.
59  */
60 
61 int
62 #ifdef _C89_INTMAX32	/* _C89_INTMAX32 version in 32-bit libc only */
63 _vscanf_c89(const char *fmt, va_list ap)
64 #else
65 vscanf(const char *fmt, va_list ap)
66 #endif
67 {
68 	rmutex_t	*lk;
69 	int	ret;
70 
71 	FLOCKFILE(lk, stdin);
72 
73 	_SET_ORIENTATION_BYTE(stdin);
74 
75 #ifdef _C89_INTMAX32
76 	ret = __doscan_u(stdin, fmt, ap, _F_INTMAX32);
77 #else
78 	ret = __doscan_u(stdin, fmt, ap, 0);
79 #endif
80 
81 	FUNLOCKFILE(lk);
82 	return (ret);
83 }
84 
85 int
86 #ifdef _C89_INTMAX32	/* _C89_INTMAX32 version in 32-bit libc only */
87 _vfscanf_c89(FILE *iop, const char *fmt, va_list ap)
88 #else
89 vfscanf(FILE *iop, const char *fmt, va_list ap)
90 #endif
91 {
92 	rmutex_t	*lk;
93 	int	ret;
94 
95 	FLOCKFILE(lk, iop);
96 
97 	_SET_ORIENTATION_BYTE(iop);
98 
99 #ifdef _C89_INTMAX32
100 	ret = __doscan_u(iop, fmt, ap, _F_INTMAX32);
101 #else
102 	ret = __doscan_u(iop, fmt, ap, 0);
103 #endif
104 	FUNLOCKFILE(lk);
105 	return (ret);
106 }
107 
108 int
109 #ifdef _C89_INTMAX32	/* _C89_INTMAX32 version in 32-bit libc only */
110 _vsscanf_c89(const char *str, const char *fmt, va_list ap)
111 #else
112 vsscanf(const char *str, const char *fmt, va_list ap)
113 #endif
114 {
115 	FILE strbuf;
116 
117 	/*
118 	 * The dummy FILE * created for sscanf has the _IOWRT
119 	 * flag set to distinguish it from scanf and fscanf
120 	 * invocations.
121 	 */
122 	strbuf._flag = _IOREAD | _IOWRT;
123 	strbuf._ptr = strbuf._base = (unsigned char *)str;
124 	strbuf._cnt = strlen(str);
125 	SET_FILE(&strbuf, _NFILE);
126 
127 	/*
128 	 * Mark the stream so that routines called by __doscan_u()
129 	 * do not do any locking. In particular this avoids a NULL
130 	 * lock pointer being used by getc() causing a core dump.
131 	 * See bugid -  1210179 program SEGV's in sscanf if linked with
132 	 * the libthread.
133 	 * This also makes sscanf() quicker since it does not need
134 	 * to do any locking.
135 	 */
136 	if (__fsetlocking(&strbuf, FSETLOCKING_BYCALLER) == -1) {
137 		return (-1);	/* this should never happen */
138 	}
139 
140 	/* as this stream is local to this function, no locking is be done */
141 #ifdef _C89_INTMAX32
142 	return (__doscan_u(&strbuf, fmt, ap, _F_INTMAX32));
143 #else
144 	return (__doscan_u(&strbuf, fmt, ap, 0));
145 #endif
146 }
147