--- pilot-mail.old Fri Apr 28 15:46:36 2000 +++ pilot-mail.c Fri Apr 28 16:32:22 2000 @@ -8,6 +8,8 @@ * Modifications by Diego Zamboni to allow it to read mail from an MH-style * mailbox into the Pilot. * + * Patched by vsync to fix mutilated MH folder handling. + * */ #define PILOTPORT "" @@ -26,6 +28,8 @@ #include #include #include +#include +#include #include "pi-source.h" #include "pi-socket.h" #include "pi-mail.h" @@ -47,11 +51,34 @@ (*msg)=0; } -int openmhmsg(char *dir, int num) { +/* Given a dirent, this function will return zero unless the filename +** is entirely numeric (MH folder entries, for example). */ +int isnumberfile(const struct dirent *file) { + char *c = file->d_name; + + while (isdigit(*c++)) ; + return !(*(c-1)); +} + +/* qsort routine to sort strings numerically in reverse order */ +int numsortstring(const struct dirent **a, const struct dirent **b) { + int i, j; + + i = atoi((*a)->d_name); + j = atoi((*b)->d_name); + + if (i == j) + return 0; + if (i < j) + return 1; + return -1; +} + +int openmhmsg(char *dir, char *num) { char filename[1000]; - sprintf(filename, "%s/%d", dir, num); + sprintf(filename, "%s/%s", dir, num); return open(filename, O_RDONLY); } @@ -726,13 +753,20 @@ } if (strlen(mh_dir)) { + struct dirent **namelist; + int num; + char *file; - fprintf(stderr, "Reading directory %s... ", mh_dir); - fflush(stderr); + + fprintf(stderr, "Reading directory %s... ", mh_dir); + fflush(stderr); /* MH directory reading section */ - - for(i=1;;i++) { + + num = scandir(mh_dir, &namelist, isnumberfile, numsortstring); + if (num < 0) + perror("scandir"); + else while (num--) { int len; char * msg; int h; @@ -748,12 +782,13 @@ t.sentTo = 0; t.body = 0; t.dated = 0; - - if((mhmsg=openmhmsg(mh_dir, i))<0) { + + file = namelist[num]->d_name; + if((mhmsg=openmhmsg(mh_dir, file))<0) { break; } - fprintf(stderr, "%d ", i); + fprintf(stderr, "%s ", file); fflush(stderr); /* Read the message */ @@ -796,14 +831,15 @@ if (h) { /* Oops, incomplete message, still reading headers */ - fprintf(stderr, "Incomplete message %d\n", i); + fprintf(stderr, "Incomplete message %s\n", file); free_Mail(&t); continue; } if (strlen(msg) > p.truncate) { /* We could truncate it, but we won't for now */ - fprintf(stderr, "Message %d too large (%ld bytes)\n", i, (long)strlen(msg)); + fprintf(stderr, "Message %s too large (%ld bytes)\n", + file, (long)strlen(msg)); free_Mail(&t); continue; } @@ -816,10 +852,10 @@ rec++; if (strcmp(pop_keep,"delete")==0) { char filename[1000]; - sprintf(filename, "%s/%d", mh_dir, i); + sprintf(filename, "%s/%s", mh_dir, file); close(mhmsg); if(unlink(filename)) { - fprintf(stderr, "Error deleting message %d\n", i); + fprintf(stderr, "Error deleting message %s\n", file); dupe++; } continue; @@ -836,7 +872,7 @@ endmh: fprintf(stderr, "\n"); - + free(namelist); } end: