ltoa in stdlib.h 
Author Message
 ltoa in stdlib.h

I'm porting ansii c code from a UNIX environment to a Linux environment
using gcc 3.0.2

I've noticed that there is no ltoa in stdlib.h and was wondering if
anyone knows of an equilivant for this environment?

Thanks,
Caroline

--
Posted via http://www.*-*-*.com/



Mon, 26 Sep 2005 23:43:41 GMT  
 ltoa in stdlib.h

Quote:
> I'm porting ansii c code from a UNIX environment to a Linux environment
> using gcc 3.0.2
> I've noticed that there is no ltoa in stdlib.h and was wondering if
> anyone knows of an equilivant for this environment?

Taking a wild guess about what ltoa is supposed to do, I'd say sprintf
is your friend.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.*-*-*.com/ ~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"When a man talks dirty to a woman, that's {*filter*} harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
   - Ruben Stiller



Tue, 27 Sep 2005 00:34:46 GMT  
 ltoa in stdlib.h

Quote:
>I'm porting ansii c code from a UNIX environment to a Linux environment
>using gcc 3.0.2

>I've noticed that there is no ltoa in stdlib.h and was wondering if
>anyone knows of an equilivant for this environment?

No conforming <stdlib.h> is allowed to define/declare an identifier
called ltoa.  Furthemore, the Unix standard doesn't define such a thing,
either.  So, you should explain what such a function is supposed to
do, since we (or, at least, most of us) are not mind readers.

The function sprintf() may (or may not) solve your problem.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 27 Sep 2005 00:48:04 GMT  
 ltoa in stdlib.h

Thats what I figured - working on changing the code now.
Thanks!

--
Posted via http://dbforums.com



Tue, 27 Sep 2005 01:33:31 GMT  
 ltoa in stdlib.h

Originally posted by Dan Pop

Quote:
> In  C.Commins  writes:

> >I'm porting ansii c code from a UNIX environment to a Linux
>     environment
> >using gcc 3.0.2
> >I've noticed that there is no ltoa in stdlib.h and was wondering
>     if
> >anyone knows of an equilivant for this environment?

> No conforming  is allowed to define/declare an identifier
> called ltoa.  Furthemore, the Unix standard doesn't define such
> a thing,
> either.  So, you should explain what such a function is supposed to
> do, since we (or, at least, most of us) are not mind readers.

> The function sprintf() may (or may not) solve your problem.

> Dan
> --
> Dan Pop
> DESY Zeuthen, RZ group


Sorry about not supplying an example.
I'm only converting an int to a string.  The code I'm maintaining has
ltoa all over the place.  Sprintf will work - just thought it would be a
lot less work if I could find a similar function to use instead.

Here is an example of the code :

int get_date_tim(date_str,tim_str)
char    *date_str,*tim_str;
{
        struct  tm *x;
        long    clock;
        char    *ptr;

        time(&clock);
        x = localtime(&clock);
        ptr = ltoa(x->tm_year);
        strcpy(date_str, ptr);
        ptr = ltoa(x->tm_mon+1);
        /* if month is less than 10 then we need to zero prefix month */
        if ((x->tm_mon+1) < 10)
                strcat(date_str, "0") ;
        strcat(date_str, ptr);



Tue, 27 Sep 2005 02:47:15 GMT  
 ltoa in stdlib.h

Quote:

>... Sprintf will work - just thought it would be a
>lot less work if I could find a similar function to use instead.

It would have been even *less* work if they had done it right in the
first place :-)

Quote:
>Here is an example of the code :

>int get_date_tim(date_str,tim_str)
>char    *date_str,*tim_str;
>{
>        struct  tm *x;
>        long    clock;
>        char    *ptr;

>        time(&clock);
>        x = localtime(&clock);
>        ptr = ltoa(x->tm_year);
>        strcpy(date_str, ptr);
>        ptr = ltoa(x->tm_mon+1);
>        /* if month is less than 10 then we need to zero prefix month */
>        if ((x->tm_mon+1) < 10)
>                strcat(date_str, "0") ;
>        strcat(date_str, ptr);

This is wonderfully awful code. :-)

Probably this whole thing should be tossed and replaced with
strftime() in whatever calls get_date_tim() in the first place.
Making wild assumptions about the missing remainder of the code,
however, we might use:

    int get_date_tim(char *dste_str, char *tim_str) {
        struct tm *x;
        time_t clock;

        if (time(&clock) == (time_t)-1)
            panic("get_date_tim(): unable to obtain time");
        x = localtime(&clock);
        /* ??? why tm_year without adding 1900 first? seems wrong */
        sprintf(date_str, "%d%02d%02d", x->tm_year, x->tm_mon + 1, x->tm_mday);
        sprintf(tim_str, "%02d%02d%02d", x->tm_hour, x->tm_min, x->tm_sec);
        return 0; /* 0 => success */
    }
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (4039.22'N, 11150.29'W)
email: forget about it    http://67.40.109.61/torek/  (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.



Tue, 27 Sep 2005 03:54:46 GMT  
 ltoa in stdlib.h

Quote:

> I'm porting ansii c code from a UNIX environment to a Linux environment
> using gcc 3.0.2

> I've noticed that there is no ltoa in stdlib.h and was wondering if
> anyone knows of an equilivant for this environment?

sprintf() is the answer, just as it is for 'itoa'.


Tue, 27 Sep 2005 05:40:51 GMT  
 ltoa in stdlib.h


Quote:

> Originally posted by Dan Pop
> > In  C.Commins  writes:

> > >I'm porting ansii c code from a UNIX environment to a Linux
> >     environment
> > >using gcc 3.0.2
> > >I've noticed that there is no ltoa in stdlib.h and was wondering
> >     if
> > >anyone knows of an equilivant for this environment?

> > No conforming  is allowed to define/declare an identifier
> > called ltoa.  Furthemore, the Unix standard doesn't define such
> > a thing,
> > either.  So, you should explain what such a function is supposed to
> > do, since we (or, at least, most of us) are not mind readers.

> > The function sprintf() may (or may not) solve your problem.

> > Dan
> > --
> > Dan Pop
> > DESY Zeuthen, RZ group

> Sorry about not supplying an example.
> I'm only converting an int to a string.  The code I'm maintaining has
> ltoa all over the place.  Sprintf will work - just thought it would be a
> lot less work if I could find a similar function to use instead.

> Here is an example of the code :

> int get_date_tim(date_str,tim_str)
> char    *date_str,*tim_str;
> {
>         struct  tm *x;
>         long    clock;
>         char    *ptr;

>         time(&clock);
>         x = localtime(&clock);
>         ptr = ltoa(x->tm_year);
>         strcpy(date_str, ptr);
>         ptr = ltoa(x->tm_mon+1);

Be careful here.  It looks like your ltoa function not only converts a long
to a string, but also allocates memory for that string.  "ptr" above never
has memory allocated for it, so ltoa must do it (assuming we don't have
undefined behavior here).

So if that's the case, you can't just blindly replace ltoa with sprintf.
You have to allocate memory for "ptr" and then use sprintf.

                        Dan



Tue, 27 Sep 2005 06:22:25 GMT  
 ltoa in stdlib.h

Quote:


>>int get_date_tim(date_str,tim_str) [chop]

>This is wonderfully awful code. :-)

Indeed.

Quote:
>Probably this whole thing should be tossed and replaced with
>strftime() in whatever calls get_date_tim() in the first place.

True, _unless_ that is performance critical code.  In some early
versions of Boa (web server), such a call to strftime consumed
about a third of the CPU cycles.  Rewriting the function to
trivial in-line C (e.g.,
    a = t->tm_sec;
    *p-- = '0' + a % 10;
    *p-- = '0' + a / 10;
    *p-- = ':';
    a = t->tm_min;
    *p-- = '0' + a % 10;
    *p-- = '0' + a / 10;
    *p-- = ':';
) made that performance hit disappear.

        - Larry



Tue, 27 Sep 2005 06:49:51 GMT  
 ltoa in stdlib.h

Quote:

> > Originally posted by Dan Pop
> > > In  C.Commins  writes:

> > > >I'm porting ansii c code from a UNIX environment to a Linux
> > >     environment
> > > >using gcc 3.0.2
> > > >I've noticed that there is no ltoa in stdlib.h and was wondering
> > >     if
> > > >anyone knows of an equilivant for this environment?

> > > No conforming  is allowed to define/declare an identifier
> > > called ltoa.  Furthemore, the Unix standard doesn't define such
> > > a thing,
> > > either.  So, you should explain what such a function is supposed to
> > > do, since we (or, at least, most of us) are not mind readers.

> > > The function sprintf() may (or may not) solve your problem.

> > > Dan
> > > --
> > > Dan Pop
> > > DESY Zeuthen, RZ group

> > Sorry about not supplying an example.
> > I'm only converting an int to a string.  The code I'm maintaining has
> > ltoa all over the place.  Sprintf will work - just thought it would be a
> > lot less work if I could find a similar function to use instead.

> > Here is an example of the code :

> > int get_date_tim(date_str,tim_str)
> > char    *date_str,*tim_str;
> > {
> >         struct  tm *x;
> >         long    clock;
> >         char    *ptr;

> >         time(&clock);
> >         x = localtime(&clock);
> >         ptr = ltoa(x->tm_year);
> >         strcpy(date_str, ptr);
> >         ptr = ltoa(x->tm_mon+1);

> Be careful here.  It looks like your ltoa function not only converts a
long
> to a string, but also allocates memory for that string.  "ptr" above never
> has memory allocated for it, so ltoa must do it (assuming we don't have
> undefined behavior here).

> So if that's the case, you can't just blindly replace ltoa with sprintf.
> You have to allocate memory for "ptr" and then use sprintf.

    It could also return a pointer to a static buffer.  He'll have to check
the docs for it, but it doesn't sound like a terribly difficult function to
rewrite.


Tue, 27 Sep 2005 08:52:58 GMT  
 ltoa in stdlib.h

Quote:

> Originally posted by Dan Pop
> > In  C.Commins  writes:

> > >I'm porting ansii c code from a UNIX environment to a Linux
> >     environment
> > >using gcc 3.0.2
> > >I've noticed that there is no ltoa in stdlib.h and was wondering
> >     if
> > >anyone knows of an equilivant for this environment?

> > No conforming  is allowed to define/declare an identifier
> > called ltoa.  Furthemore, the Unix standard doesn't define such
> > a thing,
> > either.  So, you should explain what such a function is supposed to
> > do, since we (or, at least, most of us) are not mind readers.

> > The function sprintf() may (or may not) solve your problem.

> > Dan
> > --
> > Dan Pop
> > DESY Zeuthen, RZ group

> Sorry about not supplying an example.
> I'm only converting an int to a string.  The code I'm maintaining has
> ltoa all over the place.  Sprintf will work - just thought it would be a
> lot less work if I could find a similar function to use instead.

> Here is an example of the code :

> int get_date_tim(date_str,tim_str)
> char    *date_str,*tim_str;
> {
>         struct  tm *x;
>         long    clock;
>         char    *ptr;

>         time(&clock);
>         x = localtime(&clock);
>         ptr = ltoa(x->tm_year);
>         strcpy(date_str, ptr);
>         ptr = ltoa(x->tm_mon+1);
>         /* if month is less than 10 then we need to zero prefix month */
>         if ((x->tm_mon+1) < 10)
>                 strcat(date_str, "0") ;
>         strcat(date_str, ptr);

#define ltoa(lv) sprintf(s, "%ld", lv)

however your original code is thoroughly broken, since ptr doesn't
point to anything.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Tue, 27 Sep 2005 09:44:20 GMT  
 ltoa in stdlib.h

Quote:


> > Originally posted by Dan Pop
> > > In  C.Commins  writes:

> > > >I'm porting ansii c code from a UNIX environment to a Linux
> > >     environment
> > > >using gcc 3.0.2
> > > >I've noticed that there is no ltoa in stdlib.h and was wondering
> > >     if
> > > >anyone knows of an equilivant for this environment?

> > > No conforming  is allowed to define/declare an identifier
> > > called ltoa.  Furthemore, the Unix standard doesn't define such
> > > a thing,
> > > either.  So, you should explain what such a function is supposed to
> > > do, since we (or, at least, most of us) are not mind readers.

> > > The function sprintf() may (or may not) solve your problem.

> > > Dan
> > > --
> > > Dan Pop
> > > DESY Zeuthen, RZ group

> > Sorry about not supplying an example.
> > I'm only converting an int to a string.  The code I'm maintaining has
> > ltoa all over the place.  Sprintf will work - just thought it would be a
> > lot less work if I could find a similar function to use instead.

> > Here is an example of the code :

> > int get_date_tim(date_str,tim_str)
> > char    *date_str,*tim_str;
> > {
> >         struct  tm *x;
> >         long    clock;
> >         char    *ptr;

> >         time(&clock);
> >         x = localtime(&clock);
> >         ptr = ltoa(x->tm_year);
> >         strcpy(date_str, ptr);
> >         ptr = ltoa(x->tm_mon+1);
> >         /* if month is less than 10 then we need to zero prefix month */
> >         if ((x->tm_mon+1) < 10)
> >                 strcat(date_str, "0") ;
> >         strcat(date_str, ptr);

> #define ltoa(lv) sprintf(s, "%ld", lv)

> however your original code is thoroughly broken, since ptr doesn't
> point to anything.

Cancel that - I was spouting nonsense.  Just write a replacement
ltoa.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Tue, 27 Sep 2005 10:56:30 GMT  
 ltoa in stdlib.h

Quote:


>> > Originally posted by Dan Pop
>> > > In  C.Commins  writes:

>> > > >I'm porting ansii c code from a UNIX environment to a Linux
>> > >     environment
>> > > >using gcc 3.0.2
>> > > >I've noticed that there is no ltoa in stdlib.h and was wondering
>> > >     if
>> > > >anyone knows of an equilivant for this environment?

>> > > No conforming  is allowed to define/declare an identifier
>> > > called ltoa.  Furthemore, the Unix standard doesn't define such
>> > > a thing,
>> > > either.  So, you should explain what such a function is supposed to
>> > > do, since we (or, at least, most of us) are not mind readers.

>> > > The function sprintf() may (or may not) solve your problem.

>> > Sorry about not supplying an example.
>> > I'm only converting an int to a string.  The code I'm maintaining has
>> > ltoa all over the place.  Sprintf will work - just thought it would be a
>> > lot less work if I could find a similar function to use instead.

>> > Here is an example of the code :

>> > int get_date_tim(date_str,tim_str)
>> > char    *date_str,*tim_str;
>> > {
>> >         struct  tm *x;
>> >         long    clock;
>> >         char    *ptr;

>> >         time(&clock);
>> >         x = localtime(&clock);
>> >         ptr = ltoa(x->tm_year);
>> >         strcpy(date_str, ptr);
>> >         ptr = ltoa(x->tm_mon+1);
>> >         /* if month is less than 10 then we need to zero prefix month */
>> >         if ((x->tm_mon+1) < 10)
>> >                 strcat(date_str, "0") ;
>> >         strcat(date_str, ptr);

>> #define ltoa(lv) sprintf(s, "%ld", lv)

>> however your original code is thoroughly broken, since ptr doesn't
>> point to anything.

>Cancel that - I was spouting nonsense.

Which could have been trivially avoided by engaging your brain... ;-)

Quote:
>Just write a replacement ltoa.

Which is as trivial as:

    #include <stdio.h>
    #include <limits.h>
    #include <assert.h>

    char *ltoa(long n)
    {
        static char buff[25];      /* should be enough for 64-bit longs */
        assert (LONG_MAX < 1e23);
        sprintf(buff, "%ld", n);
        return buff;
    }

With the usual caveats of the functions returning the address of an
internal buffer.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 27 Sep 2005 23:39:26 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. itoa, ltoa and time functions

2. UNIX equiv of ltoa

3. UNIX equiv of ltoa??

4. Error using ltoa

5. ltoa

6. How to use ltoa() in UNIX

7. including stdlib.h

8. RAND_MAX & stdlib.h

9. Problems with strtol() from <stdlib.h>

10. how to deal with stdlib.h

11. stdlib.h

12. stdlib.h

 

 
Powered by phpBB® Forum Software