-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmylib.c
66 lines (62 loc) · 1.24 KB
/
mylib.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
#include <stdio.h> /* for fprintf */
#include <stdlib.h> /* for size_t, malloc, realloc, exit */
#include "mylib.h"
#include <ctype.h>
#include <assert.h>
void *emalloc(size_t s)
{
void *result = malloc(s);
if (NULL == result)
{
fprintf(stderr, "Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
return result;
}
void *erealloc(void *p, size_t s)
{
void *result = realloc(p, s);
if (NULL == result)
{
fprintf(stderr, "memory reallocation failed.\n");
exit(EXIT_FAILURE);
}
else
{
return result;
}
}
int getword(char *s, int limit, FILE *stream)
{
int c;
char *w = s;
assert(limit > 0 && s != NULL && stream != NULL);
/* skip to the start of the word */
while (!isalnum(c = getc(stream)) && EOF != c)
;
if (EOF == c)
{
return EOF;
}
else if (--limit > 0)
{ /* reduce limit by 1 to allow for the \0 */
*w++ = tolower(c);
}
while (--limit > 0)
{
if (isalnum(c = getc(stream)))
{
*w++ = tolower(c);
}
else if ('\'' == c)
{
limit++;
}
else
{
break;
}
}
*w = '\0';
return w - s;
}