Ubuntu CI: make apt update before apt install
[llpp.git] / cutils.c
blob2ba510a31892f907cad22b84a0a394354b6445bf
1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <inttypes.h>
8 #include <sys/time.h>
10 #include "cutils.h"
12 _Noreturn void GCC_FMT_ATTR (3, 4) err (int exitcode, int errno_val,
13 const char *fmt, ...)
15 va_list ap;
17 va_start (ap, fmt);
18 vfprintf (stdout, fmt, ap);
19 va_end (ap);
20 fprintf (stdout, ": %s\n", strerror (errno_val));
21 fflush (stdout);
22 _exit (exitcode);
25 _Noreturn void GCC_FMT_ATTR (2, 3) errx (int exitcode, const char *fmt, ...)
27 va_list ap;
29 va_start (ap, fmt);
30 vfprintf (stdout, fmt, ap);
31 va_end (ap);
32 fputc ('\n', stdout);
33 fflush (stdout);
34 _exit (exitcode);
37 void *parse_pointer (const char *cap, const char *s)
39 void *ptr;
40 int ret = sscanf (s, "%" SCNxPTR, (uintptr_t *) &ptr);
41 if (ret != 1) {
42 errx (1, "%s: cannot parse pointer in `%s' (ret=%d)", cap, s, ret);
44 return ptr;
47 double now (void)
49 struct timeval tv;
50 gettimeofday (&tv, NULL); /* gettimeofday shall always return zero */
51 return tv.tv_sec + tv.tv_usec*1e-6;
54 void fmt_linkn (char *s, const char *c, unsigned int l, int n)
56 div_t d;
57 int sl = 0, nn = n;
59 do { d = div (n, l); sl++; n = d.quot; } while (d.quot);
60 for (int i = 0, n = nn; i < sl; ++i) {
61 d = div (n, l);
62 s[sl-1-i] = c[d.rem];
63 n = d.quot;
65 s[sl] = 0;