iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐙

GNU CoreUtils sleep supports infinity

に公開

https://twitter.com/mattn_jp/status/1459142544831311880

I came across this, so I decided to check the source code to see if it was true. First, before diving in:

$ sleep info
sleep: invalid time interval ‘info’
Try 'sleep --help' for more information.

I see, it seems intentional.

$ sleep inf

Apparently, both inf and infinity can be specified. I tried searching the CoreUtils source on GitHub, but nothing relevant came up.

https://github.com/coreutils/coreutils

I realized I wouldn't know for sure without investigating, so I performed a git clone. I'm the type of person who can't sleep once I get curious about something until I fully understand it.

The starting point is coreutils/src/sleep.c.

    for (int i = optind; i < argc; i++)
      {
        double s;
        char const *p;
        if (! (xstrtod (argv[i], &p, &s, cl_strtod) || errno == ERANGE)
            /* Nonnegative interval.  */
            || ! (0 <= s)
            /* No extra chars after the number and an optional s,m,h,d char.  */
            || (*p && *(p+1))
            /* Check any suffix char and update S based on the suffix.  */
            || ! apply_suffix (&s, *p))
          {
            error (0, 0, _("invalid time interval %s"), quote (argv[i]));
            ok = false;
          }

        seconds += s;
      }

It seems xstrtod is parsing the arguments. xstrtod just passes cl_strtod, which was passed as an argument, like a callback. This is inside gnulib, which is a git submodule. gnulib/lib/xstrtod.c

DOUBLE                                                                
CL_STRTOD (char const *nptr, char **restrict endptr)                  
{                                                                     
  char *end;                                                          
  DOUBLE d = STRTOD (nptr, &end);                                     
  if (*end)                                                           
    {                                                                 
      int strtod_errno = errno;                                       
      char *c_end;                                                    
      DOUBLE c = C_STRTOD (nptr, &c_end);                             
      if (end < c_end)                                                
        d = c, end = c_end;                                           
      else                                                            
        errno = strtod_errno;                                         
    }                                                                 
  if (endptr)                                                         
    *endptr = end;                                                    
  return d;                                                           
}                                                                     

So STRTOD (strtod) is the main part. It's inside strtod.c.

/* Check for infinities and NaNs.  */             
else if (c_tolower (*s) == 'i'                    
         && c_tolower (s[1]) == 'n'               
         && c_tolower (s[2]) == 'f')              
  {                                               
    s += 3;                                       
    if (c_tolower (*s) == 'i'                     
        && c_tolower (s[1]) == 'n'                
        && c_tolower (s[2]) == 'i'                
        && c_tolower (s[3]) == 't'                
        && c_tolower (s[4]) == 'y')               
      s += 5;                                     
    num = HUGE_VAL;                               
    errno = saved_errno;                          
  }                                               
else if (c_tolower (*s) == 'n'                    
         && c_tolower (s[1]) == 'a'               
         && c_tolower (s[2]) == 'n')              
  {                                               
    s += 3;                                       
    if (*s == '(')                                
      {                                           
        const char *p = s + 1;                    

I see. In gnulib's strtod, if it's inf or infinity, the maximum double value HUGE_VAL is obtained. It also includes checks to return NAN for nan, but in this function, nan is determined as the return value and ultimately results in an error.

else if (c_tolower (*s) == 'n'                                       
         && c_tolower (s[1]) == 'a'                                  
         && c_tolower (s[2]) == 'n')                                 
  {                                                                  
    s += 3;                                                          
    if (*s == '(')                                                   
      {                                                              
        const char *p = s + 1;                                       
        while (c_isalnum (*p))                                       
          p++;                                                       
        if (*p == ')')                                               
          s = p + 1;                                                 
      }                                                              
                                                                     
    /* If the underlying implementation misparsed the NaN, assume    
       its result is incorrect, and return a NaN.  Normally it's     
       better to use the underlying implementation's result, since a 
       nice implementation populates the bits of the NaN according   
       to interpreting n-char-sequence as a hexadecimal number.  */  
    if (s != end || num == num)                                      
      num = NAN;                                                     
    errno = saved_errno;                                             
  }                                                                  
else                                                                 
  {                                                                 

In summary:

Discussion