-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.c
59 lines (53 loc) · 1.1 KB
/
gen.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
/* gen.c: General all-purpose functions.
*
* Copyright (C) 2010 Brian Raiter.
* This program is free software. See README for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "gen.h"
/* Exit the program with an error message.
*/
void croak(char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
fputc('\n', stderr);
va_end(args);
exit(EXIT_FAILURE);
}
/* Allocate memory or exit.
*/
void *allocate(unsigned int size)
{
void *p;
p = malloc(size);
if (!p) {
fputs("memory allocation failed\n", stderr);
exit(EXIT_FAILURE);
}
return p;
}
/* Find the next place to break a word-wrapped string of text,
* automatically skipping leading whitespace.
*/
int textbreak(char const **pstr, int width)
{
char const *str;
int brk, n;
while (**pstr == ' ')
++*pstr;
str = *pstr;
if (*str == '\0')
return 0;
brk = 0;
for (n = 1 ; n < width ; ++n) {
if (str[n] == '\0')
return n;
else if (str[n] == ' ' && str[n - 1] != ' ')
brk = n;
}
return brk ? brk : width;
}