1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
/* $Id: msg.c,v 1.2 2004/11/30 21:26:37 pzn Exp $ */
#include <string.h>
#include <glib/gslist.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#ifndef _DIRENT_HAVE_D_TYPE
# error "sorry, _DIRENT_HAVE_D_TYPE is not defined..."
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
#include "header.h"
#include "common.h"
static GSList * msg_filename = NULL;
static GSList * msg_size = NULL;
static int msg_counter = 0;
static int msg_totalsize = 0;
/* return 1 if filename is a valid message, else 0 */
static int msg_check_name (char * filename) {
if (filename[0] == '.')
/* is a hidden file... not allowed */
return 0;
if (filename[strlen(filename)-1] == '~')
/* is a backup... not allowed */
return 0;
if (strncasecmp(filename, "readme", 6) == 0)
return 0;
return 1;
}
/* return the real size of a message
* included in size: fakepop added header size
* "\n" -> "\r\n" end of line conversions */
static int msg_calc_size(char * msgfilename) {
FILE * fh;
char * filename;
char * msgline;
int i, j, size = 0;
asprintf (&filename, "%s%s", MSGDIR, msgfilename);
fh = fopen (filename,"r");
free (filename);
if (fh == NULL) return 0;
msgline = malloc (MSG_MAX_LINE_SIZE + 1);
assert (msgline != NULL);
while (fgets(msgline,MSG_MAX_LINE_SIZE,fh) != NULL) {
msgline[MSG_MAX_LINE_SIZE] = 0;
j = strlen(msgline);
if (j < 2) j = 2;
for (i = j-2; i < j; i++) {
if ((msgline[i] == '\r') || (msgline[i] == '\n')) {
msgline[i] = 0;
break;
}
}
size += strlen(msgline) + 2; /* 2 is strlen("\r\n") */
}
fclose(fh);
free (msgline);
size += header_size();
return size;
}
/* initialize gslists, calculate total messages and sizes */
void msg_init (void) {
DIR *dp;
struct dirent *ep;
/* free lists if msg_init called again */
if (msg_filename != NULL) {
g_slist_free(msg_filename);
msg_filename = NULL;
}
if (msg_size != NULL) {
g_slist_free(msg_size);
msg_size = NULL;
}
msg_counter = 0;
msg_totalsize = 0;
dp = opendir (MSGDIR);
assert(dp != NULL);
while ((ep = readdir (dp))) {
if ((ep->d_type == DT_REG) || (ep->d_type == DT_LNK)) {
if (msg_check_name(ep->d_name)) {
int size;
msg_counter++;
size = msg_calc_size(ep->d_name);
msg_size = g_slist_append(msg_size, GINT_TO_POINTER (size));
msg_filename = g_slist_append(msg_filename, strdup(ep->d_name));
msg_totalsize += size;
}
}
}
closedir (dp);
}
/* return if a message exist */
int msg_exist (int msg) {
if ((msg < 1) ||
(msg > msg_counter)) {
return 0;
}
return 1;
}
/* return the total number of messages */
int msg_gettotalmsgs (void) {
return msg_counter;
}
/* return the size of a message */
int msg_getsize (int msg) {
if (! msg_exist(msg))
return 0;
return (int) g_slist_nth_data (msg_size, msg - 1);
}
/* return the filename of a message */
char * msg_getfilename (int msg) {
if (! msg_exist(msg))
return NULL;
return g_slist_nth_data(msg_filename, msg - 1);
}
/* return the sum of the sizes of all messages */
int msg_gettotalsize (void) {
return msg_totalsize;
}
|