-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathstrings.c
65 lines (50 loc) · 1011 Bytes
/
strings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <myst/eraise.h>
#include <myst/strings.h>
char* myst_strdup(const char* s)
{
return strdup(s);
}
int myst_eprintf(const char* format, ...)
{
va_list ap;
va_start(ap, format);
int n = vfprintf(stderr, format, ap);
va_end(ap);
return n;
}
int myst_printf(const char* format, ...)
{
va_list ap;
va_start(ap, format);
int n = vfprintf(stdout, format, ap);
va_end(ap);
return n;
}
int myst_str2int(const char* s, int* x)
{
int ret = 0;
long tmp;
ECHECK(myst_str2long(s, &tmp));
if (tmp < INT_MIN || tmp > INT_MAX)
ERAISE(-ERANGE);
*x = (int)tmp;
done:
return ret;
}
int myst_str2long(const char* s, long* x)
{
int ret = 0;
char* end;
long tmp = strtol(s, &end, 10);
if (!end || *end)
ERAISE(-EINVAL);
*x = tmp;
done:
return ret;
}