#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 3 /* >= 2 */
char *mygetline(FILE *fp) {
static char inbuff[BUFFSIZE];
char *outbuff_malloc, *tmpbuff;
char *p, *r;
int fEOL;
if ((outbuff_malloc = malloc(1)) == NULL) {
return NULL;
}
*outbuff_malloc = '\0';
fEOL = 0;
do {
r = fgets(inbuff, BUFFSIZE, fp);
if (r == NULL)
break;
for (p = inbuff; *p != '\0'; p++)
;
if (*(p - 1) == '\n')
fEOL = 1;
if ((tmpbuff = realloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1)) == NULL) {
free(outbuff_malloc);
return NULL;
}
strcat(tmpbuff, inbuff);
outbuff_malloc = tmpbuff;
} while (!fEOL);
if (strlen(outbuff_malloc) > 0) {
for (p = outbuff_malloc; *p != '\0'; p++)
;
if (*(p - 1) == '\n')
*(p - 1) = '\0';
return outbuff_malloc;
}
free(outbuff_malloc);
return NULL;
}
struct NAME {
char *name;
struct NAME *next;
};
int putlist(struct NAME **first, char *name) {
char *p;
struct NAME *node;
if (!(p = malloc(strlen(name) + 1)))
return 0;
strcpy(p, name);
if (!(node = malloc(sizeof(struct NAME)))) {
free(p);
return 0;
}
node->name = p;
node->next = *first;
*first = node;
return 0;
}
char *getlist(struct NAME **first) {
char *p;
struct NAME *node;
if (!*first)
return NULL;
node = *first;
p = node->name;
*first = node->next;
free(node);
return p;
}
int main() {
struct NAME *first;
char *p;
first = NULL;
for (;;) {
printf("> ");
p = mygetline(stdin);
if (!p || (*p == 'q' && *(p + 1) == '\0'))
break;
putlist(&first, p);
free(p);
}
free(p);
while (p = getlist(&first)) {
printf("%s\n", p);
free(p);
}
return 0;
}
/* end */