~ubuntu-core-dev/update-notifier/ubuntu

« back to all changes in this revision

Viewing changes to src/rfc822.c

  • Committer: Balint Reczey
  • Date: 2020-06-11 18:46:02 UTC
  • Revision ID: balint.reczey@canonical.com-20200611184602-2rv1zan3xu723x2u
Moved to git at https://git.launchpad.net/update-notifier

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifdef HAVE_CONFIG_H
2
 
#include "config.h"
3
 
#endif
4
 
 
5
 
#include <stdio.h>
6
 
#include <ctype.h>
7
 
 
8
 
 
9
 
#include "rfc822.h"
10
 
 
11
 
#include <glib.h>
12
 
#include <stdarg.h>
13
 
#include <stdlib.h>
14
 
#include <string.h>
15
 
 
16
 
 
17
 
/*
18
 
 * Function: rfc822db_parse_stanza
19
 
 * Input: a FILE pointer to an open readable file containing a stanza in rfc822 
20
 
 *    format.
21
 
 * Output: a pointer to a dynamically allocated rfc822_header structure
22
 
 * Description: parse a stanza from file into the returned header struct
23
 
 * Assumptions: no lines are over 8192 bytes long.
24
 
 */
25
 
 
26
 
struct rfc822_header* rfc822_parse_stanza(FILE *file)
27
 
{
28
 
    struct rfc822_header *head, **tail, *cur;
29
 
    char buf[8192];
30
 
 
31
 
    head = NULL;
32
 
    tail = &head;
33
 
    cur = NULL;
34
 
 
35
 
    /*    fprintf(stderr,"rfc822db_parse_stanza(file)\n");*/
36
 
    while (fgets(buf, sizeof(buf), file))
37
 
    {
38
 
        char *tmp = buf;
39
 
 
40
 
        if (*tmp == '\n')
41
 
            break;
42
 
 
43
 
        if (buf[strlen(buf)-1] == '\n') 
44
 
           buf[strlen(buf)-1] = '\0';
45
 
 
46
 
        // " ." -> \n 
47
 
        if (isspace(*tmp) && 
48
 
            *(tmp+1) != 0 && *(tmp+1) == '.' &&
49
 
            *(tmp+2) == 0)
50
 
        {
51
 
           gchar *now = cur->value;
52
 
           cur->value = g_strconcat(now, "\n", NULL);
53
 
           g_free(now);
54
 
        }
55
 
        else if (isspace(*tmp))
56
 
        {
57
 
           gchar *now = cur->value;
58
 
           if(strlen(now) == 0 || g_str_has_suffix(now, " ") || g_str_has_suffix(now, "\n"))
59
 
              cur->value = g_strconcat(now, g_strstrip(tmp), NULL);
60
 
           else       
61
 
              cur->value = g_strconcat(now, " ", g_strstrip(tmp), NULL);
62
 
           g_free(now);
63
 
        } 
64
 
        else 
65
 
        {
66
 
            while (*tmp != 0 && *tmp != ':')
67
 
                tmp++;
68
 
            *tmp++ = '\0';
69
 
 
70
 
            cur = g_new0(struct rfc822_header,1);
71
 
            if (cur == NULL)
72
 
                return NULL;
73
 
            cur->header = strdup(buf);
74
 
 
75
 
            while (isspace(*tmp))
76
 
                tmp++;
77
 
            cur->value = strdup(tmp);
78
 
 
79
 
            *tail = cur;
80
 
            tail = &cur->next;
81
 
        }
82
 
    }
83
 
 
84
 
    return head;
85
 
}
86
 
 
87
 
 
88
 
char *rfc822_header_lookup(struct rfc822_header *list, const char* key)
89
 
{
90
 
/*    fprintf(stderr,"rfc822db_header_lookup(list,key=%s)\n",key);*/
91
 
    while (list && (strcasecmp(key, list->header) != 0))
92
 
        list = list->next;
93
 
    if (!list)
94
 
        return NULL;
95
 
/*    fprintf(stderr,"rfc822db_header_lookup returning: '%s'\n", list->value);*/
96
 
    return list->value;
97
 
}
98
 
 
99
 
 
100
 
void rfc822_header_free_all(struct rfc822_header *list)
101
 
{
102
 
   while (list) { 
103
 
      struct rfc822_header *now = list;
104
 
      g_free(list->header);
105
 
      g_free(list->value);
106
 
 
107
 
      list = list->next;
108
 
      g_free(now);
109
 
   }
110
 
}