File: rfc822.c

package info (click to toggle)
cdebconf 0.138lenny3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,184 kB
  • ctags: 1,371
  • sloc: ansic: 14,158; sh: 469; makefile: 429; sql: 51; perl: 13
file content (118 lines) | stat: -rw-r--r-- 2,808 bytes parent folder | download | duplicates (8)
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
#include <stdio.h>
#include <ctype.h>

#include "common.h"
#include "rfc822.h"
#include "strutl.h"


/*
 * Function: rfc822_parse_stanza
 * Input: a FILE pointer to an open readable file containing a stanza in rfc822 
 *    format.
 * Output: a pointer to a dynamically allocated rfc822_header structure
 * Description: parse a stanza from file into the returned header struct
 */

struct rfc822_header* rfc822_parse_stanza(FILE *file)
{
    struct rfc822_header *head, **tail, *cur;
    static size_t buflen = 8192;
    static char *buf = NULL;

    if (!buf) {
        buf = malloc(buflen * sizeof *buf);
        if (!buf)
            DIE("Out of memory");
    }

    head = NULL;
    tail = &head;
    cur = NULL;

    /*    fprintf(stderr,"rfc822_parse_stanza(file)\n");*/
    while (fgets(buf, buflen, file))
    {
        char *tmp;
        size_t tmplen = strlen(buf);

        if (*buf == '\n')
            break;

        while (buf[tmplen - 1] != '\n') {
            buflen += 8192;
            buf = realloc(buf, buflen * sizeof *buf);
            if (!buf)
                DIE("Out of memory");
            if (!fgets(buf + tmplen, buflen - tmplen, file))
                break;
            tmplen += strlen(buf + tmplen);
        }

        CHOMP(buf);
        tmp = buf;

        if (isspace(*tmp))
        {
            /* continuation line, just append it */
            int len;

            if (cur == NULL)
                break; /* should report an error here */

            len = strlen(cur->value) + strlen(tmp) + 2;

            cur->value = realloc(cur->value, len);
            strvacat(cur->value, len, "\n", tmp, NULL);
        } 
        else 
        {
            while (*tmp != 0 && *tmp != ':')
                tmp++;
            *tmp++ = '\0';

            cur = NEW(struct rfc822_header);
            if (cur == NULL)
                return NULL;
            memset(cur, '\0',sizeof(struct rfc822_header));    

            cur->header = strdup(buf);

            while (isspace(*tmp))
                tmp++;

            cur->value = strdup(unescapestr(tmp));

            *tail = cur;
            tail = &cur->next;
        }
    }

    return head;
}


char *rfc822_header_lookup(struct rfc822_header *list, const char* key)
{
/*    fprintf(stderr,"rfc822_header_lookup(list,key=%s)\n",key);*/
    while (list && (strcasecmp(key, list->header) != 0))
        list = list->next;
    if (!list)
        return NULL;
/*    fprintf(stderr,"rfc822_header_lookup returning: '%s'\n", list->value);*/
    return list->value;
}


void rfc822_header_destroy(struct rfc822_header *list)
{
    struct rfc822_header *cur = list, *next;

    while (cur) {
        free(cur->header);
        free(cur->value);
        next = cur->next;
        DELETE(cur);
        cur = next;
    }
}