-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylib.c~
60 lines (59 loc) · 1.62 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
#include <stdio.h>
#include <stdlib.h>
#include "mylib.h"
#include <assert.h>
#include <ctype.h>
/*Function that allocates memory to a pointer.
*Parameters: s, which is how much memory you want to allocate
*Prints to standard error if you have a failure
*Returns a pointer
*/
void *emalloc(size_t s){
void *result = malloc(s);
if(NULL == result) {
fprintf(stderr, "memory allocation failure");
exit(EXIT_FAILURE);
}
return result;
}
/*Function that changes the amount of memory that is allocated to a pointer.
*Param: s, which is how much memory you want to allocate
*Param: *p, which is a pointer
*/
void *remalloc(void *p, size_t s) {
void *result = realloc(p, s);
if(NULL == result) {
fprintf(stderr, "memory allocation failure");
exit(EXIT_FAILURE);
}
return result;
}
/*Function that gets the word from the file.
*Param:s, which is is the string
*Param:limit, the maximum size a string can be
*Param:*stream, which is where it wants to get the string from
*/
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;
}