-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmylib.c
More file actions
41 lines (37 loc) · 911 Bytes
/
mylib.c
File metadata and controls
41 lines (37 loc) · 911 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include "mylib.h"
void *emalloc(size_t s) {
void *result = malloc(s);
if (NULL == result) {
fprintf(stderr, "Memory allocation occured!\n");
exit(EXIT_FAILURE);
}
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;
}