summaryrefslogtreecommitdiffstats
path: root/sqwebmail/mailinglist.c
blob: dc5da7c4082796fc5f627e68ffb50a6c40d01992 (plain)
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
#include "config.h"

/*
*/

#include	"sqwebmail.h"
#include	"mailinglist.h"
#include	"rfc822/rfc822.h"
#include	<stdio.h>
#include	<string.h>
#include	<ctype.h>
#include	<stdlib.h>
#include	<unistd.h>
#include	<sys/types.h>
#include	<sys/stat.h>

#define	MAILINGLISTS	"sqwebmail-mailinglists"
#define	MAILINGLISTSTMP	"sqwebmail-mailinglists.tmp"

char *getmailinglists()
{
	FILE *fp=fopen(MAILINGLISTS, "r");
	struct stat stat_buf;
	char *buf;
	int l;

	if (!fp)
		return (0);

	if (fstat(fileno(fp), &stat_buf) != 0 ||
	    (buf=malloc(stat_buf.st_size+1)) == NULL)
	{
		fclose(fp);
		return (0);
	}

	l=fread(buf, 1, stat_buf.st_size, fp);
	fclose(fp);

	if (l < 0)
		l=0;
	buf[l]=0;
	return (buf);
}

void savemailinglists(const char *p)
{
	FILE *fp;
	int lastc;

	if ((fp=fopen(MAILINGLISTSTMP, "w")) == NULL)
		return;

	for (lastc='\n'; *p; p++)
	{
		if (isspace((int)(unsigned char)*p) && *p != '\n')
			continue;

		if (*p == '\n' && lastc == '\n')
			continue;

		putc(*p, fp);
		lastc=*p;
	}
	
	fprintf(fp, "%s", p);
	fflush(fp);
	if (ferror(fp))
	{
		fclose(fp);
		unlink(MAILINGLISTSTMP);
		return;
	}
	fclose(fp);
	rename (MAILINGLISTSTMP, MAILINGLISTS);
}