File: rfc822hdr.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 (129 lines) | stat: -rw-r--r-- 1,813 bytes parent folder | download | duplicates (14)
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
/*
** Copyright 2001 Double Precision, Inc.
** See COPYING for distribution information.
*/

#include	<stdio.h>
#include	<ctype.h>
#include	<stdlib.h>
#include	<string.h>
#include	"rfc822hdr.h"

static const char rcsid[]="$Id: rfc822hdr.c,v 1.1 2001/08/12 15:42:02 mrsam Exp $";

/*
** Read the next mail header.
*/

int rfc822hdr_read(struct rfc822hdr *h, FILE *f, off_t *pos, off_t epos)
{
	size_t n=0;
	int c;

	for (;;)
	{
		if ( n >= h->hdrsize)
		{
			size_t hn=h->hdrsize + 1024;
			char *p= h->header ? realloc(h->header, hn):
				malloc(hn);

			if (!p)
				return (-1);

			h->header=p;
			h->hdrsize=hn;
		}

		if (pos && *pos >= epos)
		{
			h->header[n]=0;
			break;
		}

		c=getc(f);
		if (c == EOF)
		{
			if (pos)
				*pos=epos;
			h->header[n]=0;
			break;
		}
		if (pos)
			++*pos;

		h->header[n]=c;
		if (c == '\n')
		{
			if (n == 0)
			{
				if (pos)
					*pos=epos;
				h->header[n]=0;
				break;
			}

			c=getc(f);
			if (c != EOF)
				ungetc(c, f);
			if (c == '\n' || c == '\r' ||
			    !isspace((int)(unsigned char)c))
			{
				h->header[n]=0;
				break;
			}
		}
		n++;
		if (h->maxsize && n + 2 > h->maxsize)
			--n;
	}

	if (n == 0)
	{
		if (pos)
			*pos=epos;
		h->value=h->header;
		return (1);
	}

	for (h->value=h->header; *h->value; ++h->value)
	{
		if (*h->value == ':')
		{
			*h->value++=0;
			while (*h->value &&
			       isspace((int)(unsigned char)*h->value))
				++h->value;
			break;
		}
	}
	return (0);
}

void rfc822hdr_fixname(struct rfc822hdr *h)
{
	char *p;

	for (p=h->header; *p; p++)
	{
		*p=tolower((int)(unsigned char)*p);
	}
}

void rfc822hdr_collapse(struct rfc822hdr *h)
{
	char *p, *q;

	for (p=q=h->value; *p; )
	{
		if (*p == '\n')
		{
			while (*p && isspace((int)(unsigned char)*p))
				++p;
			*q++=' ';
			continue;
		}
		*q++ = *p++;
	}
	*q=0;
}