*BSD News Article 5682


Return to BSD News archive

Newsgroups: comp.unix.bsd
Path: sserve!manuel!munnari.oz.au!spool.mu.edu!uunet!math.fu-berlin.de!hrz.tu-chemnitz.de!wutcd
From: wutcd@chemnitz.guug.de (Joerg Wunsch)
Subject: [386BSD] HERE IS the source for strtod()
Message-ID: <wutcd.717634980@hadrian>
Keywords: strtod, 386BSD
Sender: bin@hrz.tu-chemnitz.de (Owner of all binaries)
Organization: tu-chemnitz
Date: Sun, 27 Sep 1992 23:03:00 GMT
Lines: 161


Hi netters;-)

i tracked down the strtod problem, and i found (well-hidden) the real source.
The man pages tell everything:

``This manual page represents intent instead of actual practice.
The strtod function is not currently available.''

But, obviously, there is already a function capable of converting strings
to double: atof(). It is used also for the various *scanf() functions.
But, atof() isn't accomplished by calling strtod() - see the BUGS section
of man page again. Instead, there is a source for atof() in
..../src/lib/libc/i386/stdlib/atof.c
                 ^^^^

Thus, it is an easy job to modify this source so it can return a pointer
to the end of converted string (if the caller is interested in). In a later
release, this should be done within two separate files, ``strtod.c'', and
``atof.c''. I made a patch only to ``atof.c'', so the former atof() be-
comes ``strtod()'' now, and atof() calls it like ANSI tells. This is an
easy way to patch the existing source file:

*** atof.dist.c	Sat Jun 13 22:26:27 1992
--- atof.c	Sat Sep 26 14:42:14 1992
***************
*** 47,52 ****
--- 47,53 ----
  #include <stdlib.h>
  #include <math.h>
  #include <ctype.h>
+ #include <errno.h>
  
  static double twoemax = 9007199254740992.;	/*2^53*/
  
***************
*** 79,86 ****
  };
  
  double
! atof(p)
  	register const char *p;
  {
  	register int c;
  	register int exp = 0;
--- 80,88 ----
  };
  
  double
! strtod(p, endp)
  	register const char *p;
+ 	char **endp;
  {
  	register int c;
  	register int exp = 0;
***************
*** 90,95 ****
--- 92,98 ----
  	int bexp;
  	int neg = 1;
  	int negexp = 1;
+ 	const char *oldp = p;
  
  	while (isspace(*p))
  		++p;
***************
*** 138,143 ****
--- 141,154 ----
  		exp >>= 1;
  	}
  
+ 	/* according to ANSI, check for over-/underflow */
+ 	if(exp > 0) {
+ 		if(endp)
+ 			*endp = oldp;
+ 		errno = ERANGE;
+ 		return neg < 0? -HUGE_VAL: HUGE_VAL;
+ 	}
+ 
  	if (bexp < 0)
  		fl /= flexp;
  	else
***************
*** 145,149 ****
--- 156,171 ----
  
  	fl = ldexp(fl, bexp);
  
+ 	if(endp)
+ 		*endp = p;
  	return neg < 0 ? -fl : fl;
  }
+ 
+ 
+ double
+ atof(p)
+ 	const char *p;
+ {
+ 	return strtod(p, (char **)NULL);
+ }
+ 



***********************
Since my compiler complained much about ``fatal signal 6'' (a real
bug - but this will be another posting), here is the atof.s file i
compiled after some tricking with the help of gdb:

begin 644 atof.s.Z
M'YV0(YP\^1($"A0%9\:,^3+F31LX:=B4(>-"AP(79,+0":,@@8LP;-*<<0-"
MAH(O=.Z\*=,F#!Z+'LF\J2-&(@@89'*X@`'C1HP<.634H'&#!@R@,W@J+;,B
M1HV.'T..+'FR#!XX-6"Z8//&S9F;4+EZ!1&C9PT8-832H!&VZU<8;<>6O5&4
M[HP;-N*^U4L6!@X9,VS$P%$CKT>Q>P^[[1O4KU.C?'/<I5$CAT^3BN7&V&P#
MAN"[?%O$$'I#<HT9H#-_W7SV=`R\3U6#:#$#1PP;.&;(V,QWM`P<-&3<@,%;
MMN@8P7'<F%&V=^WD/W_SW5U;1F/AO6WDJ(%C>XW7%^E8I0,5I$B2)ID,@:%5
M)DV;.&/L5$J_OGT83(F7EXK^XAFN-8'PQ1QTR$''&V2<1*"!",($1QUSH,$&
M""64(08<';7QAAT35C@''"Q4>&%'<[P'`@DTX!!B&1]V]&"$'4Z4AHL02D@A
MBS,F\**-(N*1X883XH""B'"DL*(8/B:`QQMRQ#@&'BL^^2.')\+`0@LI#FEA
MD5-.2(*5+0A)9`I=5GDE#5I>2&8"&E+YY96WI<DEFT">.)<,VNF6(IPRR+EF
MFU[&<*4,,/A99@N$^KDB&3D">B.C@Z(Y9ID]#BKFEG]N.$>`:>)A9(4N=20>
M@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K&4I12\E<)<7G+&'XR
M<,3<EDVV&1L%FFDT>:OE$J/XFRGBJ$SX'!,D&1FC.6CQ6FD@TS;UB"M<>JP,
M<I"#&]Z@2S,ABE_I\Y<@?XE'@I[H.#G0#@QD`!@9>-*C$B1*BK1#@]MPCF(T
MO.4-[N<E*X$J2?C:C>9H>)8;F'2`,*4</8-HSIN>M)2_;%CSX)=1,S"JD<K,
MJ-[L!9=MKG*I:80>U%@65:HQX2PPL:;[\-8O[MV41C#"FLNXQU06C@$D,5/9
M,Y-VS:S:\TT!E>H-D>;1O*IIH(ZJ%$(5VM(]UG1?>=NHK3"#5;B-`0USZ,CZ
M\L5+%I5OK%>T[)K@\(:VG55'G0T@#VL5T\72='H"_2P3_X@X8='00342K8YB
MV[W>?<]#X3/;+LD7R:NUH#/^5!MG/2M'T!+7H"X*[:.J^"P[E*$C<B@#>3QB
MGJF8Q`7_>4.`OJ"1-Q"-NP<R`VS32B0S?NA(.>/>E]!J([O"54`+.E""$C#9
E,/%V;9@%WV:5^]GARM:L%9PM>4<YN[#AEFRZ-=5]K>9<Z$I7`4`+
`
end


Assemble this file, and then ``ar -r /usr/lib/libc.a atof.o'', 
followed by ``ranlib /usr/lib/libc.a''.

	Be happy
	J"org
--
J"org Wunsch, TCD GmbH Dresden, R"ahnitzgasse 18, D-O-8060 Dresden
                                                              \     ~.~. 
	joerg_wunsch%bonnie@hadrian.hrz.tu-chemnitz.de         \   ~  | ~ 
	voice: +49-351-5965 137                                 \    ===