server/public.c
/* Parse a string as a size with possible scaling suffix, or return -1
 * after reporting the error.
 */
Parse a string as a size with possible scaling suffix, or return -1
after reporting the error.
Parse a string as a size with possible scaling suffix or return - 1 after reporting the error .
int64_t
nbdkit_parse_size (const char *str)
{
  int64_t size;
  char *end;
  uint64_t scale = 1;
  errno = 0;
  size = strtoimax (str, &end, 10);
  if (str == end) {
    nbdkit_error ("could not parse size string (%s)", str);
    return -1;
  }
  if (size < 0) {
    nbdkit_error ("size cannot be negative (%s)", str);
    return -1;
  }
  if (errno) {
    nbdkit_error ("size (%s) exceeds maximum value", str);
    return -1;
  }
  switch (*end) {
  case '\0':
    end--; 
    break;
  case 'e': case 'E':
    scale *= 1024;
  case 'p': case 'P':
    scale *= 1024;
  case 't': case 'T':
    scale *= 1024;
  case 'g': case 'G':
    scale *= 1024;
  case 'm': case 'M':
    scale *= 1024;
  case 'k': case 'K':
    scale *= 1024;
  case 'b': case 'B':
    break;
  case 's': case 'S':
    scale = 512;
    break;
  default:
    nbdkit_error ("could not parse size: unknown suffix '%s'", end);
    return -1;
  }
  if (end[1]) {
    nbdkit_error ("could not parse size: unknown suffix '%s'", end);
    return -1;
  }
  if (INT64_MAX / scale < size) {
    nbdkit_error ("overflow computing size (%s)", str);
    return -1;
  }
  return size * scale;
}
int64_t nbdkit_parse_size ( const char * str ) { int64_t size ; char * end ; uint64_t scale = 1 ; errno = 0 ; size = strtoimax ( str , & end , 10 ) ; if ( str == end ) { nbdkit_error ( " " , str ) ; return -1 ; } if ( size < 0 ) { nbdkit_error ( " " , str ) ; return -1 ; } if ( errno ) { nbdkit_error ( " " , str ) ; return -1 ; } switch ( * end ) { case ' \0 ' : end -- ; break ; case ' ' : case ' ' : scale *= 1024 ; case ' ' : case ' ' : scale *= 1024 ; case ' ' : case ' ' : scale *= 1024 ; case ' ' : case ' ' : scale *= 1024 ; case ' ' : case ' ' : scale *= 1024 ; case ' ' : case ' ' : scale *= 1024 ; case ' ' : case ' ' : break ; case ' ' : case ' ' : scale = 512 ; break ; default : nbdkit_error ( " " , end ) ; return -1 ; } if ( end [ 1 ] ) { nbdkit_error ( " " , end ) ; return -1 ; } if ( INT64_MAX / scale < size ) { nbdkit_error ( " " , str ) ; return -1 ; } return size * scale ; }
Parse a string as a size with possible scaling suffix, or return -1
 after reporting the error.
Parse a string as a size with possible scaling suffix or return - 1 after reporting the error .
/* Disk sizes cannot usefully exceed off_t (which is signed) and
   * cannot be negative.  */ /* XXX Should we also parse things like '1.5M'? */ /* XXX Should we allow hex? If so, hex cannot use scaling suffixes,
   * because some of them are valid hex digits */ /* No suffix */ /* Safe, since we already filtered out empty string */ /* Powers of 1024 */ /* fallthru */ /* fallthru */ /* fallthru */ /* fallthru */ /* fallthru */ /* fallthru */ /* "sectors", ie. units of 512 bytes, even if that's not the real
     * sector size */ /* XXX Maybe we should support 'MiB' as a synonym for 'M'; and 'MB'
   * for powers of 1000, for similarity to GNU tools. But for now,
   * anything beyond 'M' is dropped.  */