~ubuntu-branches/ubuntu/precise/libproxy/precise-updates

« back to all changes in this revision

Viewing changes to .pc/03_format-security.patch/src/bin/proxy.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2011-12-05 12:27:23 UTC
  • mfrom: (1.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20111205122723-dzs4whuxill65a7g
Tags: 0.3.1-4ubuntu1
* Merge with Debian; remaining changes:
  - debian/control.in: Drop libwebkitgtk-1.0-0 | libmozjs2d recommends to
    suggests, as we otherwise pull in the sizable libwebkitgtk-1.0-0 into the
    Kubuntu CDs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * libproxy - A library for proxy configuration
 
3
 * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
18
 ******************************************************************************/
 
19
#define _BSD_SOURCE
 
20
 
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <assert.h>
 
24
#include <unistd.h>
 
25
#include <string.h>
 
26
 
 
27
/* Import libproxy API */
 
28
#include <proxy.h>
 
29
 
 
30
void *
 
31
malloc0(size_t s)
 
32
{
 
33
        void *tmp = malloc(s);
 
34
        if (!tmp) return NULL;
 
35
        memset(tmp, '\0', s);
 
36
        return tmp;
 
37
}
 
38
 
 
39
/**
 
40
 * Reads a single line of text from the specified file descriptor
 
41
 * @fd File descriptor to read from
 
42
 * @buffer The buffer to write to (usually NULL)
 
43
 * @bufsize The size of the buffer (usually 0)
 
44
 * @return Newly allocated string containing one line only
 
45
 */
 
46
static char *
 
47
readline(int fd, char *buffer, size_t bufsize)
 
48
{
 
49
        char c = '\0';
 
50
 
 
51
        /* Verify we have an open socket */
 
52
        if (fd < 0) return NULL;
 
53
 
 
54
        /* Read a character.  If we don't get a character, return the buffer. */
 
55
        if (read(fd, &c, 1) != 1) return buffer;
 
56
 
 
57
        /* If we are at the end of the line, return. */
 
58
        if (c == '\n') return buffer ? buffer : malloc0(1);
 
59
 
 
60
        /* We have a character, make sure we have a buffer. */
 
61
        if (!buffer)
 
62
        {
 
63
                assert((buffer = malloc0(1)));
 
64
                bufsize = 0;
 
65
        }
 
66
 
 
67
        /* If our buffer is full, add more to the buffer. */
 
68
        if (bufsize <= strlen(buffer))
 
69
        {
 
70
                char *tmp = NULL;
 
71
                assert((tmp = malloc(1024 + strlen(buffer) + 1)));
 
72
                memset(tmp, 0, 1024 + strlen(buffer) + 1);
 
73
                strcpy(tmp, buffer);
 
74
                free(buffer);
 
75
                buffer = tmp;
 
76
                bufsize = strlen(buffer) + 1024;
 
77
        }
 
78
 
 
79
        strncat(buffer, &c, 1);
 
80
        return readline(fd, buffer, bufsize);
 
81
}
 
82
 
 
83
/**
 
84
 * Prints an array of proxies. Proxies are space separated.
 
85
 * @proxies an array containing the proxies returned by libproxy.
 
86
 */
 
87
void
 
88
print_proxies(char **proxies)
 
89
{
 
90
        for (int j = 0; proxies[j] ; j++)
 
91
        {
 
92
                printf(proxies[j]);
 
93
                if (proxies[j+1])
 
94
                        printf(" ");
 
95
                else
 
96
                        printf("\n");
 
97
                free(proxies[j]);
 
98
        }
 
99
        free(proxies);
 
100
}
 
101
 
 
102
int
 
103
main(int argc, char **argv)
 
104
{
 
105
        /* Create the proxy factory object */
 
106
        pxProxyFactory *pf = px_proxy_factory_new();
 
107
        if (!pf)
 
108
        {
 
109
                fprintf(stderr, "An unknown error occurred!\n");
 
110
                return 1;
 
111
        }
 
112
        /* User entered some arguments on startup. skip interactive */
 
113
        if (argc > 1)
 
114
        {
 
115
                for(int i = 1; i < argc ; i++)
 
116
                {
 
117
                        /*
 
118
                         * Get an array of proxies to use. These should be used
 
119
                         * in the order returned. Only move on to the next proxy
 
120
                         * if the first one fails (etc).
 
121
                         */
 
122
                        print_proxies(px_proxy_factory_get_proxies(pf, argv[i]));
 
123
                }
 
124
        }
 
125
        /* Interactive mode */
 
126
        else
 
127
        {
 
128
                /* For each URL we read on STDIN, get the proxies to use */
 
129
                for (char *url = NULL ; (url = readline(STDIN_FILENO, NULL, 0)) ; free(url))
 
130
                {
 
131
                        /*
 
132
                         * Get an array of proxies to use. These should be used
 
133
                         * in the order returned. Only move on to the next proxy
 
134
                         * if the first one fails (etc).
 
135
                         */
 
136
                        print_proxies(px_proxy_factory_get_proxies(pf, url));
 
137
                }
 
138
        }
 
139
        /* Destroy the proxy factory object */
 
140
        px_proxy_factory_free(pf);
 
141
        return 0;
 
142
}