Commit f0fbfc6c f0fbfc6c32b1177f2a30ef7e37b3a6aa6edd00ce by Sean 'Shaleh' Perry

removed my readline.{c,h}

made imap use getline
1 parent f68a0e24
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #ifndef COUNT
7 #define COUNT 128 /* safe, middle of the road value */
8 #endif
9
10 /**
11 * reads a single line from *stream
12 * max line length is max size of size_t
13 *
14 * PRE: stream is an open FILE stream
15 * POST: returns an allocated string containing the line read, or errno is set
16 *
17 * note, user must free the returned string
18 **/
19 char *read_a_line(FILE *stream) {
20
21 char *line = NULL;
22 size_t buffsize = 0; /* counter of alloc'ed space */
23
24 if( feof(stream) ) { /* sent a bum stream */
25 errno = EINVAL;
26 return NULL;
27 }
28
29 do {
30 if ((line = realloc(line, buffsize + (sizeof(char) * COUNT))) == NULL) {
31 errno = ENOMEM;
32 return NULL;
33 }
34 if( fgets(line + (buffsize ? buffsize - 1 : 0),COUNT,stream) == NULL ) {
35 if( buffsize == 0 ) {
36 errno = EINVAL;
37 return NULL; /* eof w/ nothing read */
38 }
39 break; /* read a line w/o a newline char */
40 }
41 buffsize += COUNT;
42 } while( (strchr(line, '\n')) == NULL );
43
44 return line;
45 }
1 #ifndef _mailutils_read_a_line__
2 #define _mailutils_read_a_line__
3
4 char* read_a_line(FILE *stream);
5
6 #endif
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
8 #include "imap.h" 8 #include "imap.h"
9 #include "imap_commands.h" 9 #include "imap_commands.h"
10 #include "command.h" 10 #include "command.h"
11 #include "readline.h"
12 11
13 static void init(void); 12 static void init(void);
14 static int mainloop(void); 13 static int mainloop(void);
...@@ -71,11 +70,12 @@ static void greeting(void) { ...@@ -71,11 +70,12 @@ static void greeting(void) {
71 70
72 static int mainloop(void) { 71 static int mainloop(void) {
73 72
74 char *client_string; 73 char *client_string = NULL;
74 size_t len = 0;
75 Command *command; 75 Command *command;
76 STATUS status; 76 STATUS status;
77 77
78 while( (client_string = read_a_line(input)) != NULL ) { 78 while( getline(&client_string, &len, input) != -1 ) {
79 command = parse_command(client_string); 79 command = parse_command(client_string);
80 status = action(command); 80 status = action(command);
81 81
...@@ -83,11 +83,11 @@ static int mainloop(void) { ...@@ -83,11 +83,11 @@ static int mainloop(void) {
83 command->cmd, status_code[status]); 83 command->cmd, status_code[status]);
84 84
85 free_command(command); 85 free_command(command);
86 free(client_string);
87 86
88 if( state == LOGOUT ) /* all done, let's go */ 87 if( state == LOGOUT ) /* all done, let's go */
89 break; 88 break;
90 } 89 }
90 free(client_string);
91 91
92 return 1; 92 return 1;
93 } 93 }
......
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #ifndef COUNT
7 #define COUNT 128 /* safe, middle of the road value */
8 #endif
9
10 /**
11 * reads a single line from *stream
12 * max line length is max size of size_t
13 *
14 * PRE: stream is an open FILE stream
15 * POST: returns an allocated string containing the line read, or errno is set
16 *
17 * note, user must free the returned string
18 **/
19 char *read_a_line(FILE *stream) {
20
21 char *line = NULL;
22 size_t buffsize = 0; /* counter of alloc'ed space */
23
24 if( feof(stream) ) { /* sent a bum stream */
25 errno = EINVAL;
26 return NULL;
27 }
28
29 do {
30 if ((line = realloc(line, buffsize + (sizeof(char) * COUNT))) == NULL) {
31 errno = ENOMEM;
32 return NULL;
33 }
34 if( fgets(line + (buffsize ? buffsize - 1 : 0),COUNT,stream) == NULL ) {
35 if( buffsize == 0 ) {
36 errno = EINVAL;
37 return NULL; /* eof w/ nothing read */
38 }
39 break; /* read a line w/o a newline char */
40 }
41 buffsize += COUNT;
42 } while( (strchr(line, '\n')) == NULL );
43
44 return line;
45 }
1 #ifndef _MY_read_a_line__
2 #define _MY_read_a_line__
3
4 char* read_a_line(FILE *stream);
5
6 #endif