File: maildirgetnew.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (154 lines) | stat: -rw-r--r-- 2,954 bytes parent folder | download | duplicates (10)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
** Copyright 2000-2004 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#if HAVE_DIRENT_H
#include <dirent.h>
#define NAMLEN(dirent) strlen((dirent)->d_name)
#else
#define dirent direct
#define NAMLEN(dirent) (dirent)->d_namlen
#if HAVE_SYS_NDIR_H
#include <sys/ndir.h>
#endif
#if HAVE_SYS_DIR_H
#include <sys/dir.h>
#endif
#if HAVE_NDIR_H
#include <ndir.h>
#endif
#endif
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<stdio.h>
#include	<string.h>
#include	<stdlib.h>
#include	<time.h>
#include	<errno.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif

#include	"maildirmisc.h"

static const char rcsid[]="$Id: maildirgetnew.c,v 1.5 2004/04/21 01:09:15 mrsam Exp $";

static void do_maildir_getnew(const char *, const char *,
			      void (*)(const char *, void *),
			      void *);

void maildir_getnew(const char *maildir, const char *folder,
		    void (*callback_func)(const char *, void *),
		    void *callback_arg)
{
char	*dir=maildir_folderdir(maildir, folder);
char	*newd, *curd;

	if (!dir)	return;

	newd=malloc(strlen(dir)+sizeof("/new"));
	curd=malloc(strlen(dir)+sizeof("/cur"));

	if (newd && curd)
	{
		strcat(strcpy(newd, dir), "/new");
		strcat(strcpy(curd, dir), "/cur");
		do_maildir_getnew(newd, curd, callback_func, callback_arg);
	}

	if (newd)	free(newd);
	if (curd)	free(curd);
	free(dir);
}

static void do_maildir_getnew(const char *newd, const char *curd,
			      void (*callback_func)(const char *, void *),
			      void *callback_arg)
{
	DIR	*dirp;
	struct dirent *de;
	int keepgoing;
	int n;
	char *new_buf[20];
	char *cur_buf[20];

	do
	{
		keepgoing=0;
		n=0;

		dirp=opendir(newd);
		while (dirp && (de=readdir(dirp)) != 0)
		{
			char	*np, *cp;

			if (de->d_name[0] == '.')	continue;

			if ((np=malloc(strlen(newd)+strlen(de->d_name)+2))
			    != 0)
			{
				if ((cp=malloc(strlen(curd)+strlen(de->d_name)
					       + sizeof("/" MDIRSEP "2,")))
				    != 0)
				{
					char *a;

					strcat(strcat(strcpy(np, newd), "/"),
					       de->d_name);
					strcat(strcat(strcpy(cp, curd), "/"),
					       de->d_name);
					a=strchr(cp+strlen(curd), MDIRSEP[0]);
					if (a && strncmp(a, MDIRSEP "2,", 3))
					{
						*a=0;
						a=0;
					}
					if (!a)	strcat(cp, MDIRSEP "2,");
					new_buf[n]=np;
					cur_buf[n]=cp;

					if (++n >= sizeof(cur_buf)/
					    sizeof(cur_buf[0]))
					{
						keepgoing=1;
						break;
					}
				}
				else
					free(np);
			}
		}
		if (dirp)	closedir(dirp);

		while (n)
		{
			char *np, *cp;

			--n;

			np=new_buf[n];
			cp=cur_buf[n];

			if (rename(np, cp))
			{
				fprintf(stderr,
					"ERR: rename(%s,%s) failed:"
					" %s\n", np, cp, strerror(errno));
				keepgoing=0;
				/* otherwise we could have infinite loop */
			}

			if (callback_func)
				(*callback_func)(strrchr(cp, '/')+1,
						 callback_arg);
			free(np);
			free(cp);
		}
	} while (keepgoing);
}