xref: /illumos-gate/usr/src/lib/libc/port/fp/file_decim.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 #include "lint.h"
28 #include "file64.h"
29 #include "mtlib.h"
30 #include <sys/types.h>
31 #include <ctype.h>
32 #include <stdio.h>
33 #include "base_conversion.h"
34 #include <locale.h>
35 #include <thread.h>
36 #include <synch.h>
37 #include "stdiom.h"
38 #include "libc.h"
39 
40 /* if the _IOWRT flag is set, this must be a call from sscanf */
41 #define	mygetc(iop)	((iop->_flag & _IOWRT) ? \
42 				((*iop->_ptr == '\0') ? EOF : *iop->_ptr++) : \
43 				GETC(iop))
44 
45 #define	myungetc(x, iop)	((iop->_flag & _IOWRT) ? *(--iop->_ptr) : \
46 					UNGETC(x, iop))
47 
48 
49 void
file_to_decimal(char ** ppc,int nmax,int fortran_conventions,decimal_record * pd,enum decimal_string_form * pform,char ** pechar,FILE * pf,int * pnread)50 file_to_decimal(char **ppc, int nmax, int fortran_conventions,
51 		decimal_record *pd, enum decimal_string_form *pform,
52 		char **pechar, FILE *pf, int *pnread)
53 {
54 	char	*cp = *ppc - 1;	/* last character seen */
55 	char	*good = cp;	/* last character accepted */
56 	int	current;	/* *cp or EOF */
57 	int	nread = 0;	/* number of characters read so far */
58 
59 /* if the _IOWRT flag is set, this must be a call from sscanf */
60 #define	NEXT \
61 	if (nread < nmax) { \
62 		current = ((pf->_flag & _IOWRT) ? \
63 		    ((*pf->_ptr == '\0') ? EOF : *pf->_ptr++) : \
64 		    GETC(pf)); \
65 		if (current != EOF) { \
66 			*++cp = (char)current; \
67 			nread++; \
68 		} \
69 	} else { \
70 		current = EOF; \
71 	}
72 
73 	NEXT;
74 
75 #include "char_to_decimal.h"
76 
77 	/*
78 	 * If we read any characters beyond the end of the accepted
79 	 * token, try to push them back.
80 	 */
81 	if (fortran_conventions < 0) {
82 		/* in C99 mode, push back at most one character */
83 		if (cp >= *ppc && current != EOF && myungetc(current, pf)
84 		    != EOF) {
85 			cp--;
86 			nread--;
87 		}
88 	} else {
89 		while (cp >= *ppc) {
90 			if (myungetc((int)(unsigned char)*cp, pf) == EOF)
91 				break;
92 			cp--;
93 			nread--;
94 		}
95 	}
96 
97 	*++cp = '\0';
98 	*pnread = nread;
99 }
100