Commit 36d9df9d 36d9df9d53122a0d0009ff0a73df40cb9e013065 by Sean 'Shaleh' Perry

cleaned up unixmbox.c and mailbox.c

added optional argument to from.c
added items to TODO
added Makefile for from.c in examples
1 parent 6cfd1a84
1 Sean 'Shaleh' Perry <shaleh@debian.org> Fri, 8 Oct 1999 01:08:42 -0700
2
3 * fixed the "if empty mailbox, return not implemented"
4 NB: seems there was a large assumption being made:
5 if unixmbox_open() failed, it was because it was not mbox
6 this was wrong for many reasons, so on actual "not mbox", return EBADMSG.
7 Prolly want to come up with a better error, but this works for now
8 * more cleaning in the mailbox code
9 a) added stat() call -- we can bomb earlier, plus detect if passed object
10 is a directory or file
11 b) unixmbox_close() free()'s mbox. This will hamper the use when we add
12 support for more mailboxes later. Need to find a solution. Perhaps
13 a unixmbox_free() call.
14 There was also a small leak there -- mbox->name was being left.
15 c) To aid checking in unixbox_open(), added checks around fgets call.
16 Now properly detects EOF, errors, etc. This was a large cause of the
17 spurious "not implemented" bug.
18 d) all of unixmbox_open()'s function calls should now be checked
19 NB: need to do the same for rest of file
20
1 Sean 'Shaleh' Perry <shaleh@debian.org> Thu, 7 Oct 1999 22:33:24 -0700 21 Sean 'Shaleh' Perry <shaleh@debian.org> Thu, 7 Oct 1999 22:33:24 -0700
2 22
3 * removed spurious code in from.c 23 * removed spurious code in from.c
......
1 + --> fixed/done
2 - --> pending
3 = --> on hold
4 * --> in progress
5
1 - autogen.sh (libtool) complains about libmailbox.la in noinst 6 - autogen.sh (libtool) complains about libmailbox.la in noinst
2 7
3 - libmailbox has a nasty bug -- if the mailbox is empty, the library has no idea 8 + libmailbox has a nasty bug -- if the mailbox is empty, the library has no idea
4 what to do 9 what to do
5 10
11 - libmailbox needs an interface to set flags for each message
12
13 - libmailbox needs an interface to set and create values in the header
14
6 - crypt is linked even if pam was detected, it should be one or the other 15 - crypt is linked even if pam was detected, it should be one or the other
7 16
8 - test daemon code 17 - test daemon code
......
1 CFLAGS = -Wall -pedantic -g
2 INCLUDES = -I../libmailbox
3 LIBS = ../libmailbox/.libs/libmailbox.al
4
5 from: from.c $(LIBS)
6 gcc $(CFLAGS) $(INCLUDES) -o from from.c $(LIBS)
7
8 clean:
9 rm -f from
...@@ -22,23 +22,32 @@ int main(int argc, char *argv[]) { ...@@ -22,23 +22,32 @@ int main(int argc, char *argv[]) {
22 fprintf(stderr, "who am I?\n"); 22 fprintf(stderr, "who am I?\n");
23 exit(-1); 23 exit(-1);
24 } 24 }
25 if (!strcmp(user, "root") && argc == 2)
26 user = argv[1];
27 else if (argc > 2)
28 {
29 fprintf(stderr,
30 "Usage: from [username] (argument valid when ran as root)\n");
31 exit(-1);
32 }
33
25 snprintf (mailpath, 256, "%s/%s", _PATH_MAILDIR, user); 34 snprintf (mailpath, 256, "%s/%s", _PATH_MAILDIR, user);
26 mail = mbox_open(mailpath); 35 mail = mbox_open(mailpath);
27 if( mail == NULL ) { 36 if( mail == NULL ) {
28 perror("mbox_open: "); 37 perror("mbox_open");
29 exit(-1); 38 exit(-1);
30 } 39 }
31 for(i = 0; i < mail->messages; ++i) { 40 for(i = 0; i < mail->messages; ++i) {
32 from = mbox_header_line(mail, i, "From"); 41 from = mbox_header_line(mail, i, "From");
33 if (from == NULL) 42 if (from == NULL)
34 { 43 {
35 perror("mbox_header_line: "); 44 perror("mbox_header_line");
36 exit(-1); 45 exit(-1);
37 } 46 }
38 date = mbox_header_line(mail, i, "Date"); 47 date = mbox_header_line(mail, i, "Date");
39 if (date == NULL) 48 if (date == NULL)
40 { 49 {
41 perror("mbox_header_line: "); 50 perror("mbox_header_line");
42 exit(-1); 51 exit(-1);
43 } 52 }
44 printf("%s %s\n", from, date); 53 printf("%s %s\n", from, date);
......
...@@ -26,6 +26,9 @@ ...@@ -26,6 +26,9 @@
26 #include <errno.h> 26 #include <errno.h>
27 #endif 27 #endif
28 28
29 #include <sys/stat.h>
30 #include <unistd.h>
31
29 int _mbox_dummy1 (mailbox * mbox); 32 int _mbox_dummy1 (mailbox * mbox);
30 int _mbox_dummy2 (mailbox * mbox, unsigned int num); 33 int _mbox_dummy2 (mailbox * mbox, unsigned int num);
31 int _mbox_dummy3 (mailbox * mbox, char *c); 34 int _mbox_dummy3 (mailbox * mbox, char *c);
...@@ -35,6 +38,7 @@ mailbox * ...@@ -35,6 +38,7 @@ mailbox *
35 mbox_open (const char *name) 38 mbox_open (const char *name)
36 { 39 {
37 mailbox *mbox; 40 mailbox *mbox;
41 struct stat st;
38 42
39 if ( name == NULL ) 43 if ( name == NULL )
40 { 44 {
...@@ -57,6 +61,14 @@ mbox_open (const char *name) ...@@ -57,6 +61,14 @@ mbox_open (const char *name)
57 free (mbox); 61 free (mbox);
58 return NULL; 62 return NULL;
59 } 63 }
64 /* check if we can look at file, prep for checks later in function */
65 if (stat (mbox->name, &st) == -1)
66 {
67 free (mbox->name);
68 free (mbox);
69 return NULL; /* errno set by stat() */
70 }
71
60 mbox->messages = 0; 72 mbox->messages = 0;
61 mbox->num_deleted = 0; 73 mbox->num_deleted = 0;
62 mbox->sizes = NULL; 74 mbox->sizes = NULL;
...@@ -71,6 +83,20 @@ mbox_open (const char *name) ...@@ -71,6 +83,20 @@ mbox_open (const char *name)
71 mbox->_get_body = _mbox_dummy4; 83 mbox->_get_body = _mbox_dummy4;
72 mbox->_get_header = _mbox_dummy4; 84 mbox->_get_header = _mbox_dummy4;
73 85
86 if (S_ISDIR (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 {
74 if (unixmbox_open (mbox) == 0) 100 if (unixmbox_open (mbox) == 0)
75 return mbox; 101 return mbox;
76 else 102 else
...@@ -80,18 +106,15 @@ mbox_open (const char *name) ...@@ -80,18 +106,15 @@ mbox_open (const char *name)
80 * unix mbox format message, then try other mailbox types, 106 * unix mbox format message, then try other mailbox types,
81 * otherwise, leave errno set and return NULL 107 * otherwise, leave errno set and return NULL
82 */ 108 */
109 if (errno == EBADMSG)
110 errno = ENOSYS; /* no other mailboxes supported right now */
111 }
83 } 112 }
84
85 /* for example...
86 if (maildir_open (mbox, name) == 1)
87 return mbox;
88 else if (errno != 0)
89 return NULL;
90 else 113 else
91 errno = 0; 114 errno = EINVAL; /* neither directory nor file, so bomb */
92 */
93 115
94 errno = ENOSYS; 116 free (mbox->name);
117 free (mbox);
95 return NULL; 118 return NULL;
96 } 119 }
97 120
......
...@@ -38,10 +38,8 @@ ...@@ -38,10 +38,8 @@
38 #define mbox_lock(m,n) m->_lock(m,n) 38 #define mbox_lock(m,n) m->_lock(m,n)
39 39
40 /* Lock settings */ 40 /* Lock settings */
41 /* 41 /* define this way so that it is opaque and can later become a struct w/o
42 * define this way so that it is opaque and can later become a struct w/o
43 * people noticing (-: 42 * people noticing (-:
44 *
45 */ 43 */
46 enum _mailbox_lock_t {MO_ULOCK, MO_RLOCK, MO_WLOCK}; /* new type */ 44 enum _mailbox_lock_t {MO_ULOCK, MO_RLOCK, MO_WLOCK}; /* new type */
47 typedef enum _mailbox_lock_t mailbox_lock_t; 45 typedef enum _mailbox_lock_t mailbox_lock_t;
......
...@@ -64,12 +64,18 @@ unixmbox_open (mailbox * mbox) ...@@ -64,12 +64,18 @@ unixmbox_open (mailbox * mbox)
64 return -1; 64 return -1;
65 } 65 }
66 66
67 fgets (buf, 80, data->file); 67 if (fgets (buf, 80, data->file) == NULL)
68 {
69 if (feof(data->file))
70 goto END; /* empty file, no messages */
71 unixmbox_close (mbox);
72 return -1;
73 }
68 if (strncmp (buf, "From ", 5)) 74 if (strncmp (buf, "From ", 5))
69 { 75 {
70 /* This is NOT an mbox file */ 76 /* This is NOT an mbox file */
71 unixmbox_close (mbox); 77 unixmbox_close (mbox);
72 errno = 0; 78 errno = EBADMSG; /* use this to signify wrong mbox type */
73 return -1; 79 return -1;
74 } 80 }
75 81
...@@ -79,7 +85,13 @@ unixmbox_open (mailbox * mbox) ...@@ -79,7 +85,13 @@ unixmbox_open (mailbox * mbox)
79 { 85 {
80 /* Beginning of a header */ 86 /* Beginning of a header */
81 while (strchr (buf, '\n') == NULL) 87 while (strchr (buf, '\n') == NULL)
82 fgets (buf, 80, data->file); /* eat the From line */ 88 if (fgets (buf, 80, data->file) == NULL) /* eat the From line */
89 {
90 if (feof (data->file))
91 errno = EIO; /* corrupted mailbox? */
92 unixmbox_close (mbox);
93 return -1;
94 }
83 95
84 mbox->messages++; 96 mbox->messages++;
85 97
...@@ -117,6 +129,13 @@ unixmbox_open (mailbox * mbox) ...@@ -117,6 +129,13 @@ unixmbox_open (mailbox * mbox)
117 } 129 }
118 while (fgets (buf, 80, data->file)); 130 while (fgets (buf, 80, data->file));
119 131
132 if (!feof (data->file)) /* stopped due to error, not EOF */
133 {
134 unixmbox_close (mbox);
135 return -1; /* errno is set */
136 }
137
138 END:
120 mbox->_close = unixmbox_close; 139 mbox->_close = unixmbox_close;
121 mbox->_delete = unixmbox_delete; 140 mbox->_delete = unixmbox_delete;
122 mbox->_undelete = unixmbox_undelete; 141 mbox->_undelete = unixmbox_undelete;
...@@ -149,7 +168,6 @@ unixmbox_close (mailbox * mbox) ...@@ -149,7 +168,6 @@ unixmbox_close (mailbox * mbox)
149 free (data->messages); 168 free (data->messages);
150 free (mbox->sizes); 169 free (mbox->sizes);
151 free (data); 170 free (data);
152 free (mbox);
153 return 0; 171 return 0;
154 } 172 }
155 173
......