Back to index

courier  0.68.2
Defines | Functions
maildirfilename.c File Reference
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include "maildirmisc.h"

Go to the source code of this file.

Defines

#define dirent   direct
#define NAMLEN(dirent)   (dirent)->d_namlen

Functions

char * maildir_filename (const char *maildir, const char *folder, const char *filename)

Define Documentation

#define dirent   direct

Definition at line 15 of file maildirfilename.c.

#define NAMLEN (   dirent)    (dirent)->d_namlen

Definition at line 16 of file maildirfilename.c.


Function Documentation

char* maildir_filename ( const char *  maildir,
const char *  folder,
const char *  filename 
)

Definition at line 51 of file maildirfilename.c.

{
struct stat stat_buf;
char   *p, *q;
DIR *dirp;
struct dirent *de;
char   *dir;

       if (strchr(filename, '/') || *filename == '.')
       {
              errno=ENOENT;
              return (0);
       }

       dir=maildir_folderdir(maildir, folder);

       if (!dir)     return (0);

       p=malloc(strlen(dir)+strlen(filename)+sizeof("/cur/"));

       if (!p)
       {
              free(dir);
              return (0);
       }

       strcat(strcat(strcpy(p, dir), "/cur/"), filename);

       if (stat(p, &stat_buf) == 0)
       {
              free(dir);
              return (p);
       }

       /* Oh, a wise guy... */

       q=strrchr(p, '/');
       *q=0;
       dirp=opendir(p);
       *q='/';

       if ( dirp == NULL)
       {
              free(dir);
              return p;
       }

       /* Compare filenames, ignore filename size if set by maildirquota */

       while ((de=readdir(dirp)) != NULL)
       {
       const char *a=filename;
       const char *b=de->d_name;

              for (;;)
              {
                     if ( a[0] == ',' && a[1] == 'S' && a[2] == '=')
                     {
                            /* File size - quota shortcut - skip */
                            a += 3;
                            while (*a && isdigit((int)(unsigned char)*a))
                                   ++a;
                     }

                     if ( b[0] == ',' && b[1] == 'S' && b[2] == '=')
                     {
                            /* File size - quota shortcut - skip */
                            b += 3;
                            while (*b && isdigit((int)(unsigned char)*b))
                                   ++b;
                     }

                     if ( (*a == 0 || *a == MDIRSEP[0]) && (*b == 0 || *b == MDIRSEP[0]))
                     {
                            free(p);
                            p=malloc(strlen(dir)+strlen(de->d_name)+
                                   sizeof("/cur/"));
                            if (!p)
                            {
                                   closedir(dirp);
                                   free(dir);
                                   return (0);
                            }

                            strcat(strcat(strcpy(p, dir), "/cur/"),
                                   de->d_name);
                            closedir(dirp);
                            free(dir);
                            return (p);
                     }
                     if ( *a == 0 || *a == MDIRSEP[0] || *b == 0 || *b == MDIRSEP[0] ||
                            *a != *b)
                            break;

                     ++a;
                     ++b;
              }
       }
       closedir(dirp);
       free(dir);
       return (p);
}

Here is the call graph for this function:

Here is the caller graph for this function: