Logo Search packages:      
Sourcecode: mailleds version File versions

mailfile.c




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include "config.h"
#include "mailleds.h"


extern int uid;
extern sig_atomic_t exit_now;
extern char *username;
extern char opt_u;

char *
 get_mailfile()
{
      char *mailfile;
      if (opt_u || (mailfile = getenv("MAIL")) == (char *) NULL) {
            mailfile = xmalloc((strlen(MAILDIR) + 1 + strlen(username) + 1) * sizeof(char));
            sprintf(mailfile, "%s/%s", MAILDIR, username);
      }
      return (mailfile);
}

/* Count number of mails not having a "Status: R" header */
int count_mail(mailbox)
FILE *mailbox;
{
      char buf[1024];
      long mails_nr=0,read_nr=0,body_bytes;

      while(fgets(buf,1024,mailbox)!=NULL){
            if(!strncasecmp("status: ",buf,8))
                  if(index(buf+8,'R')!=NULL)read_nr++;
            if(!strncasecmp("content-length: ",buf,16))
                  body_bytes=atol(buf+16);
            if(*buf==10){
                  fseek(mailbox,body_bytes+1,SEEK_CUR);
                  mails_nr++;
            }
      }
      rewind(mailbox);
      return(mails_nr-read_nr);
}

/* Test and count the number of 'new' messages in a maildir */
int
 count_maildir(dir)
char *dir;
{
      struct dirent **namelist;
      char *newdir;
      int count;
      
      newdir=malloc(strlen(dir)+6);
      
      strcpy(newdir, dir);
      strcat(newdir, "/new/");
            
      count = scandir(newdir, &namelist, 0, 0) -2;
      
      free(newdir);
      
      return count;
}

/* Test and open the mailbox file -- hang on the mailbox if -f */
FILE *
 open_mailbox_file(file)
char *file;
{
      FILE *mailbox;
      char *errmsg;
      if ((mailbox = fopen(file, "r")) == (FILE *) NULL) {
            if (errno == ENOENT) {
                  while ((mailbox = fopen(file, "r")) == NULL) {
                        if (exit_now)
                              exit_cleanly();
                        sleep(INTERVAL_BETWEEN_CHECKS);
                  }
            } else {
                  errmsg = xmalloc((strlen("mailleds: ") + strlen(file) + 1) * sizeof(char));
                  sprintf(errmsg, "mailleds: %s", file);
                  perror(errmsg);
                  exit_fatal("Cannot read mailbox file");
            }
      }
      return (mailbox);
}

Generated by  Doxygen 1.6.0   Back to index