File: gopher.c

package info (click to toggle)
snarf 7.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 504 kB
  • sloc: ansic: 3,464; sh: 329; makefile: 62
file content (76 lines) | stat: -rw-r--r-- 1,742 bytes parent folder | download | duplicates (5)
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
/* -*- mode: C; c-basic-offset: 8; indent-tabs-mode: nil; tab-width: 8 -*- */

#include "config.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "url.h"
#include "gopher.h"
#include "options.h"
#include "util.h"
#include "http.h"



int
gopher_transfer(UrlResource *rsrc)
{
        Url *u 		= NULL;
        int sock	= 0;
        FILE *out	= NULL;
        char *request	= NULL;

        u = rsrc->url;


        /* initialize various things */

        rsrc->proxy = get_proxy("GOPHER_PROXY");

        if( rsrc->proxy ) {
                return http_transfer(rsrc);
        }
                
        
        if( !u->path )
                u->path = strdup("");
        else
                if( strlen(u->path) > 1 )
                        u->path = strdup(u->path + 2); /* fixme: mem leak
                                                          skip the leading 
                                                          slash */

        if( !u->file )
                u->file = strdup("");

        if( !u->port )
                u->port = 70;

        if( !rsrc->outfile )
                rsrc->outfile = strdup("gopherindex.txt");

        sock = tcp_connect(u->host, u->port);

        if( !sock )
                return 0;

        request = strconcat(u->path, u->file, "\r\n", NULL);

        if( !request )
                return 0;

        out = open_outfile(rsrc);

        if( !out ) {
                report(ERR, "opening %s: %s", rsrc->outfile, strerror(errno));
                close(sock);
                return 0;
        }
        
        write(sock, request, strlen(request));

        /* never know the size of a gopher file */
        return dump_data(rsrc, sock, out);
}