*BSD News Article 5291


Return to BSD News archive

Newsgroups: comp.unix.bsd
Path: sserve!manuel!munnari.oz.au!spool.mu.edu!wupost!darwin.sura.net!haven.umd.edu!uunet!sun-barr!ames!pasteur!cory.Berkeley.EDU!sim
From: sim@cory.Berkeley.EDU (Peng-Toh Sim)
Subject: Re: strtod.c -- Where's the source Luke?
Message-ID: <1992Sep19.180229.9750@pasteur.Berkeley.EDU>
Sender: nntp@pasteur.Berkeley.EDU (NNTP Poster)
Nntp-Posting-Host: cory
Organization: University of California, at Berkeley
References: <DJM.92Sep15104153@frob.eng.umd.edu> <1992Sep18.154311.20396@qualcomm.com> <1992Sep18.222250.15477@serval.net.wsu.edu>
Date: Sat, 19 Sep 1992 18:02:29 GMT
Lines: 59

In article <1992Sep18.222250.15477@serval.net.wsu.edu> hlu@eecs.wsu.edu (H.J. Lu) writes:
[stuff deleted]
>The only free strtod () which I know work are the one in libg++/iostream/dtoa.C
>in libg++.a 2.2 and the one hacked by me. Any other free ones are just toys.
>If your C compiler, as or stdio are linked with those strtod (), you are in
>trouble, just I like what I had with the early verions of the Linux C
>library. I learned a hard lesson. Trust me on this.
>
>H.J. 

Hi

Want to know something strange with the stdio package in 386bsd?
It does NOT use strtod or strtol!!!
So it does not depend on the existence of these two to work.
A quick hack to get a quick strtod :
Use the scanf internal routines!
Toy or not I don't know...
If scanf("%lg") works, then this should work.

PT

Here's the code:

#include <stdio.h>
#include <string.h>

/* ARGSUSED */
static int
eofread(cookie, buf, len)
        void *cookie;
        char *buf;
        int len;
{
        return (0);
}

double strtod(const char *str, char **endp)
{
        int ret;
        FILE f;
        double val;
        int l;

        f._flags = __SRD;
        f._bf._base = f._p = (unsigned char *)str;
        f._bf._size = f._r = l = strlen(str);
        f._read = eofread;
        f._ub._base = NULL;
        f._lb._base = NULL;
        ret = fscanf(&f, "%lg", &val);
        if (ret == 0) {
            *endp = (char *) str;
            return 0.0;
        }
        *endp = (char *) &(str[l - f._r]);
        return val;
}