-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
50 lines (41 loc) · 930 Bytes
/
main.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
/* main.c - Copyright (c) 2019, Sijmen J. Mulder (see LICENSE.md) */
#include <stdio.h>
#include <stdlib.h>
#ifdef __OpenBSD__
# include <unistd.h>
#endif
#include "leftpad.h"
int
main(int argc, char **argv)
{
long min_len;
const char *padding = " ";
char *end;
char *buf;
size_t buf_len;
#ifdef __OpenBSD__
if (pledge("stdio", NULL) == -1) {
fputs("leftpad: pledge() failed\n", stderr);
return 0;
}
#endif
if (argc == 4)
padding = argv[3];
else if (argc != 3) {
fputs("usage: leftpad string length [padding]\n", stderr);
return 1;
}
min_len = strtol(argv[2], &end, 10);
if (!argv[2][0] || end[0] || min_len < 0) {
fputs("leftpad: invalid length", stderr);
return 1;
}
buf_len = leftpad(argv[1], padding, min_len, NULL, 0);
if (!(buf = malloc(buf_len))) {
fputs("leftpad: out of memory", stderr);
return 1;
}
leftpad(argv[1], padding, min_len, buf, buf_len+1);
puts(buf);
return 0;
}