NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: lib/39392 (Document that strftime() is broken by design)



The following reply was made to PR lib/39392; it has been noted by GNATS.

From: Robert Elz <kre%munnari.OZ.AU@localhost>
To: gnats-bugs%NetBSD.org@localhost
Cc: 
Subject: Re: lib/39392 (Document that strftime() is broken by design)
Date: Tue, 04 May 2010 18:02:04 +0700

 Just for the sake of the PR (and in case anyone else ever looks here),
 note that the workaround for this problem is trivial - it is absurdly
 simple to make sure that the result cannot be empty (in cases of no
 error) and simply deal with it...   Maybe the man page should mention
 this as well, or perhaps not.
 
 That is, using the %p example given,
 
        n = strftime(buf, sizeof buf, " %p", tmp);
        if (n == 0) {
                /* certainly an error */
        }
        p_length = n - 1;
        p_value = buf + 1;
 
 Another PR might be reasonable for date(1) to make it use this kind of
 workaround instead of issuing spurious error messages (for "date" rather
 that copying stuff and inserting leading spaces, just leave the command
 line '+' in the string passed to strftime, then delete it from the result
 before printing).
 
 A patch to fix date is appended, in case it is useful for anyone.  This
 is against date.c version 1.50 (which is what I happened to have lying
 around) - if that's not what's in current, adapting this should be trivial.
 
 kre
 
 --- date.c.ORIG        2010-05-04 17:57:45.000000000 +0700
 +++ date.c     2010-05-04 17:56:44.000000000 +0700
 @@ -118,11 +118,11 @@
        if (!rflag && time(&tval) == -1)
                err(EXIT_FAILURE, "time");
  
 -      format = "%a %b %e %H:%M:%S %Z %Y";
 +      format = "+%a %b %e %H:%M:%S %Z %Y";
  
        /* allow the operands in any order */
        if (*argv && **argv == '+') {
 -              format = *argv + 1;
 +              format = *argv;
                ++argv;
        }
  
 @@ -132,14 +132,14 @@
        }
  
        if (*argv && **argv == '+')
 -              format = *argv + 1;
 +              format = *argv;
  
        if ((buf = malloc(bufsiz = 1024)) == NULL)
                goto bad;
        while (strftime(buf, bufsiz, format, localtime(&tval)) == 0)
                if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
                        goto bad;
 -      (void)printf("%s\n", buf);
 +      (void)printf("%s\n", buf + 1);
        free(buf);
        return 0;
  bad:
 
 


Home | Main Index | Thread Index | Old Index