Commit 953d5e9e 953d5e9e0921167f53d25b85c94421b39c6991c7 by Sean 'Shaleh' Perry

added showmail

more mailbox cleanups
1 parent 36d9df9d
1 Sean 'Shaleh' Perry <shaleh@debian.org> Fri, 8 Oct 1999 18:13:45 -0700
2
3 * added showmail.c to examples
4 * libmailbox: some minor code cleanups
5
1 Sean 'Shaleh' Perry <shaleh@debian.org> Fri, 8 Oct 1999 01:08:42 -0700 6 Sean 'Shaleh' Perry <shaleh@debian.org> Fri, 8 Oct 1999 01:08:42 -0700
2 7
3 * fixed the "if empty mailbox, return not implemented" 8 * fixed the "if empty mailbox, return not implemented"
......
...@@ -5,5 +5,8 @@ LIBS = ../libmailbox/.libs/libmailbox.al ...@@ -5,5 +5,8 @@ LIBS = ../libmailbox/.libs/libmailbox.al
5 from: from.c $(LIBS) 5 from: from.c $(LIBS)
6 gcc $(CFLAGS) $(INCLUDES) -o from from.c $(LIBS) 6 gcc $(CFLAGS) $(INCLUDES) -o from from.c $(LIBS)
7 7
8 showmail: showmail.c $(LIBS)
9 gcc $(CFLAGS) $(INCLUDES) -o showmail showmail.c $(LIBS)
10
8 clean: 11 clean:
9 rm -f from 12 rm -f from showmail
......
...@@ -44,7 +44,7 @@ int main(int argc, char *argv[]) { ...@@ -44,7 +44,7 @@ int main(int argc, char *argv[]) {
44 perror("mbox_header_line"); 44 perror("mbox_header_line");
45 exit(-1); 45 exit(-1);
46 } 46 }
47 date = mbox_header_line(mail, i, "Date"); 47 date = mbox_header_line(mail, i, "Received");
48 if (date == NULL) 48 if (date == NULL)
49 { 49 {
50 perror("mbox_header_line"); 50 perror("mbox_header_line");
......
1 #include "mailbox.h"
2 #include <paths.h>
3
4 int main (int argc, char *argv[])
5 {
6 mailbox *mbox;
7 char *user;
8 char *body;
9 char mailpath[256];
10 int i;
11
12 user = getenv("USER");
13 snprintf(mailpath, 256, "%s/%s", _PATH_MAILDIR, user);
14 mbox = mbox_open(mailpath);
15 for(i = 0; i < mbox->messages; ++i)
16 {
17 body = mbox_get_body(mbox, i);
18 printf("%s\n", body);
19 }
20 mbox_close(mbox);
21 }
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
26 #include <errno.h> 26 #include <errno.h>
27 #endif 27 #endif
28 28
29 #include <ctype.h>
29 #include <sys/stat.h> 30 #include <sys/stat.h>
30 #include <unistd.h> 31 #include <unistd.h>
31 32
...@@ -83,19 +84,7 @@ mbox_open (const char *name) ...@@ -83,19 +84,7 @@ mbox_open (const char *name)
83 mbox->_get_body = _mbox_dummy4; 84 mbox->_get_body = _mbox_dummy4;
84 mbox->_get_header = _mbox_dummy4; 85 mbox->_get_header = _mbox_dummy4;
85 86
86 if (S_ISDIR (st.st_mode)) 87 if (S_ISREG (st.st_mode))
87 {
88 /* for example...
89 if (maildir_open (mbox, name) == 1)
90 return mbox;
91 else if (errno != 0)
92 return NULL;
93 else
94 errno = 0;
95 */
96 errno = ENOSYS;
97 }
98 else if (S_ISREG (st.st_mode))
99 { 88 {
100 if (unixmbox_open (mbox) == 0) 89 if (unixmbox_open (mbox) == 0)
101 return mbox; 90 return mbox;
...@@ -110,6 +99,14 @@ mbox_open (const char *name) ...@@ -110,6 +99,14 @@ mbox_open (const char *name)
110 errno = ENOSYS; /* no other mailboxes supported right now */ 99 errno = ENOSYS; /* no other mailboxes supported right now */
111 } 100 }
112 } 101 }
102 else if (S_ISDIR (st.st_mode))
103 {
104 /* for example...
105 if (maildir_open (mbox, name) == 1)
106 return mbox;
107 */
108 errno = ENOSYS;
109 }
113 else 110 else
114 errno = EINVAL; /* neither directory nor file, so bomb */ 111 errno = EINVAL; /* neither directory nor file, so bomb */
115 112
...@@ -125,8 +122,9 @@ char * ...@@ -125,8 +122,9 @@ char *
125 mbox_header_line (mailbox *mbox, unsigned int num, const char *header) 122 mbox_header_line (mailbox *mbox, unsigned int num, const char *header)
126 { 123 {
127 char *full, *tmp, *line; 124 char *full, *tmp, *line;
128 int i = 0, j = 0, try = 1; 125 int try = 1;
129 unsigned int len, lh; 126 size_t i = 0, j = 0;
127 size_t len, lh;
130 128
131 if ( mbox == NULL || header == NULL ) 129 if ( mbox == NULL || header == NULL )
132 { 130 {
...@@ -143,21 +141,19 @@ mbox_header_line (mailbox *mbox, unsigned int num, const char *header) ...@@ -143,21 +141,19 @@ mbox_header_line (mailbox *mbox, unsigned int num, const char *header)
143 tmp = NULL; 141 tmp = NULL;
144 142
145 /* First get the appropriate line at the beginning */ 143 /* First get the appropriate line at the beginning */
146 /* FIXME: hmm len - (lh + 2) *COULD* be negative, but should never be */
147 for (i=0; i < len-(lh+2); i++) 144 for (i=0; i < len-(lh+2); i++)
148 { 145 {
149 if (try == 1) 146 if (try == 1)
150 { 147 {
151 if (!strncasecmp(&full[i], header, lh) && full[i+lh] == ':') 148 if (!strncasecmp(&full[i], header, lh) && full[i+lh] == ':')
152 { 149 {
153 full[len-i] = '\0';
154 tmp = strdup (&full[i+lh+2]); 150 tmp = strdup (&full[i+lh+2]);
155 if (tmp == NULL) 151 if (tmp == NULL)
156 { 152 {
157 free(full); 153 free(full);
158 return NULL; 154 return NULL;
159 } 155 }
160 i = len; 156 break;
161 } 157 }
162 else 158 else
163 try = 0; 159 try = 0;
...@@ -178,7 +174,7 @@ mbox_header_line (mailbox *mbox, unsigned int num, const char *header) ...@@ -178,7 +174,7 @@ mbox_header_line (mailbox *mbox, unsigned int num, const char *header)
178 len = strlen (tmp); 174 len = strlen (tmp);
179 for (i = 0; i < len; i++) 175 for (i = 0; i < len; i++)
180 { 176 {
181 if (tmp[i] == '\n' && i < (len - 1) && (tmp[i+1] == ' ' || tmp[i+1] == '\t')) 177 if (tmp[i] == '\n' && i < (len - 1) && isspace (tmp[i+1]))
182 { 178 {
183 if (tmp[i+1] == '\t') 179 if (tmp[i+1] == '\t')
184 tmp[i + 1] = ' '; 180 tmp[i + 1] = ' ';
...@@ -188,7 +184,7 @@ mbox_header_line (mailbox *mbox, unsigned int num, const char *header) ...@@ -188,7 +184,7 @@ mbox_header_line (mailbox *mbox, unsigned int num, const char *header)
188 else if (tmp[i] == '\n') 184 else if (tmp[i] == '\n')
189 { 185 {
190 tmp[i] = '\0'; 186 tmp[i] = '\0';
191 i = len; 187 break;
192 } 188 }
193 } 189 }
194 line = strdup (tmp); 190 line = strdup (tmp);
...@@ -225,7 +221,7 @@ mbox_body_lines (mailbox *mbox, unsigned int num, unsigned int lines) ...@@ -225,7 +221,7 @@ mbox_body_lines (mailbox *mbox, unsigned int num, unsigned int lines)
225 { 221 {
226 full[i+1] = '\0'; 222 full[i+1] = '\0';
227 buf = strdup (full); 223 buf = strdup (full);
228 i = len; 224 break;
229 } 225 }
230 } 226 }
231 } 227 }
......
...@@ -330,8 +330,16 @@ unixmbox_get_body (mailbox * mbox, unsigned int num) ...@@ -330,8 +330,16 @@ unixmbox_get_body (mailbox * mbox, unsigned int num)
330 } 330 }
331 331
332 memset (buf, 0, size + 1); 332 memset (buf, 0, size + 1);
333 fsetpos (data->file, &(data->messages[num].body)); 333 if (fsetpos (data->file, &(data->messages[num].body)) == -1)
334 fread (buf, size, sizeof (char), data->file); 334 {
335 free (buf);
336 return NULL;
337 }
338 if (fread (buf, sizeof (char), size, data->file) < size)
339 {
340 free (buf);
341 return NULL;
342 }
335 return buf; 343 return buf;
336 } 344 }
337 345
...@@ -368,8 +376,16 @@ unixmbox_get_header (mailbox * mbox, unsigned int num) ...@@ -368,8 +376,16 @@ unixmbox_get_header (mailbox * mbox, unsigned int num)
368 376
369 memset (buf, 0, size + 1); 377 memset (buf, 0, size + 1);
370 378
371 fsetpos (data->file, &(data->messages[num].header)); 379 if (fsetpos (data->file, &(data->messages[num].header)) == -1)
372 fread (buf, size, sizeof (char), data->file); 380 {
381 free (buf);
382 return NULL;
383 }
384 if (fread (buf, sizeof (char), size, data->file) < size)
385 {
386 free (buf);
387 return NULL;
388 }
373 return buf; 389 return buf;
374 } 390 }
375 391
......