mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-01 03:53:33 +00:00
e963d9c8e3
Thanks to all the refactorings we now have the ability to enforce reasonable limitations on the amount of resources any individual compile or test can consume. Those limits are currently: - `-C 8` seconds of 3.1ghz CPU time - `-M 256mebibytes` of virtual memory - `-F 100megabyte` limit on file size Only one file currently needs to exceed these limits: o/$(MODE)/third_party/python/Objects/unicodeobject.o: \ QUOTA += -C16 # overrides cpu limit to 16 seconds This change introduces a new sizetol() function to LIBC_FMT for parsing byte or bit size strings with Si unit suffixes. Functions like atoi() have been rewritten too.
90 lines
3.4 KiB
C
90 lines
3.4 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
|
│ │
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
│ │
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/fmt/conv.h"
|
|
#include "libc/fmt/fmt.h"
|
|
|
|
static int GetExponent(int c) {
|
|
switch (c) {
|
|
case '\0':
|
|
case ' ':
|
|
case '\t':
|
|
return 0;
|
|
case 'k':
|
|
case 'K':
|
|
return 1;
|
|
case 'm':
|
|
case 'M':
|
|
return 2;
|
|
case 'g':
|
|
case 'G':
|
|
return 3;
|
|
case 't':
|
|
case 'T':
|
|
return 4;
|
|
case 'p':
|
|
case 'P':
|
|
return 5;
|
|
case 'e':
|
|
case 'E':
|
|
return 6;
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Converts size string to long.
|
|
*
|
|
* The following unit suffixes may be used
|
|
*
|
|
* - `k` or `K` for kilo (multiply by 𝑏¹)
|
|
* - `m` or `M` for mega (multiply by 𝑏²)
|
|
* - `g` or `G` for giga (multiply by 𝑏³)
|
|
* - `t` or `T` for tera (multiply by 𝑏⁴)
|
|
* - `p` or `P` for peta (multiply by 𝑏⁵)
|
|
* - `e` or `E` for exa (multiply by 𝑏⁶)
|
|
*
|
|
* If a permitted alpha character is supplied, then any additional
|
|
* characters after it (e.g. kbit, Mibit, TiB) are ignored. Spaces
|
|
* before the integer are ignored, and overflows will be detected.
|
|
*
|
|
* @param s is non-null nul-terminated input string
|
|
* @param b is multiplier which should be 1000 or 1024
|
|
* @return size greater than or equal 0 or -1 on error
|
|
*/
|
|
long sizetol(const char *s, long b) {
|
|
long x;
|
|
int c, e;
|
|
do {
|
|
c = *s++;
|
|
} while (c == ' ' || c == '\t');
|
|
if (!isdigit(c)) return -1;
|
|
x = 0;
|
|
do {
|
|
if (__builtin_mul_overflow(x, 10, &x) ||
|
|
__builtin_add_overflow(x, c - '0', &x)) {
|
|
return -1;
|
|
}
|
|
} while (isdigit((c = *s++)));
|
|
if ((e = GetExponent(c)) == -1) return -1;
|
|
while (e--) {
|
|
if (__builtin_mul_overflow(x, b, &x)) return -1;
|
|
}
|
|
return x;
|
|
}
|