1
/*******************************************************************************
2
* libproxy - A library for proxy configuration
3
* Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
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.
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.
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
******************************************************************************/
27
/* Import libproxy API */
33
void *tmp = malloc(s);
34
if (!tmp) return NULL;
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
47
readline(int fd, char *buffer, size_t bufsize)
51
/* Verify we have an open socket */
52
if (fd < 0) return NULL;
54
/* Read a character. If we don't get a character, return the buffer. */
55
if (read(fd, &c, 1) != 1) return buffer;
57
/* If we are at the end of the line, return. */
58
if (c == '\n') return buffer ? buffer : malloc0(1);
60
/* We have a character, make sure we have a buffer. */
63
assert((buffer = malloc0(1)));
67
/* If our buffer is full, add more to the buffer. */
68
if (bufsize <= strlen(buffer))
71
assert((tmp = malloc(1024 + strlen(buffer) + 1)));
72
memset(tmp, 0, 1024 + strlen(buffer) + 1);
76
bufsize = strlen(buffer) + 1024;
79
strncat(buffer, &c, 1);
80
return readline(fd, buffer, bufsize);
84
* Prints an array of proxies. Proxies are space separated.
85
* @proxies an array containing the proxies returned by libproxy.
88
print_proxies(char **proxies)
90
for (int j = 0; proxies[j] ; j++)
103
main(int argc, char **argv)
105
/* Create the proxy factory object */
106
pxProxyFactory *pf = px_proxy_factory_new();
109
fprintf(stderr, "An unknown error occurred!\n");
112
/* User entered some arguments on startup. skip interactive */
115
for(int i = 1; i < argc ; i++)
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).
122
print_proxies(px_proxy_factory_get_proxies(pf, argv[i]));
125
/* Interactive mode */
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))
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).
136
print_proxies(px_proxy_factory_get_proxies(pf, url));
139
/* Destroy the proxy factory object */
140
px_proxy_factory_free(pf);