tio_clr.c - sacc - sacc (saccomys): simple gopher client.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       tio_clr.c (1406B)
       ---
            1 #include <string.h>
            2 #include <unistd.h>
            3 #include <netdb.h>
            4 
            5 #include <sys/socket.h>
            6 
            7 #include "common.h"
            8 #include "io.h"
            9 
           10 static int
           11 setup_clr(void)
           12 {
           13         return 0;
           14 }
           15 
           16 static int
           17 close_clr(struct cnx *c)
           18 {
           19         return close(c->sock);
           20 }
           21 
           22 static int
           23 connect_clr(struct cnx *c, struct addrinfo *ai, const char *host)
           24 {
           25         return connect(c->sock, ai->ai_addr, ai->ai_addrlen);
           26 }
           27 
           28 static void
           29 connerr_clr(struct cnx *c, const char *host, const char *port, int err)
           30 {
           31         if (c->sock == -1)
           32                 diag("Can't open socket: %s", strerror(err));
           33         else
           34                 diag("Can't connect to: %s:%s: %s", host, port, strerror(err));
           35 }
           36 
           37 static char *
           38 parseurl_clr(char *url)
           39 {
           40         char *p;
           41 
           42         if (p = strstr(url, "://")) {
           43                 if (strncmp(url, "gopher", p - url))
           44                         die("Protocol not supported: %.*s", p - url, url);
           45 
           46                 url = p + 3;
           47         }
           48 
           49         return url;
           50 }
           51 
           52 static ssize_t
           53 read_clr(struct cnx *c, void *buf, size_t bs)
           54 {
           55         return read(c->sock, buf, bs);
           56 }
           57 
           58 static ssize_t
           59 write_clr(struct cnx *c, void *buf, size_t bs)
           60 {
           61         return write(c->sock, buf, bs);
           62 }
           63 
           64 int (*iosetup)(void) = setup_clr;
           65 int (*ioclose)(struct cnx *) = close_clr;
           66 int (*ioconnect)(struct cnx *, struct addrinfo *, const char *) = connect_clr;
           67 void (*ioconnerr)(struct cnx *, const char *, const char *, int) = connerr_clr;
           68 char *(*ioparseurl)(char *) = parseurl_clr;
           69 ssize_t (*ioread)(struct cnx *, void *, size_t) = read_clr;
           70 ssize_t (*iowrite)(struct cnx *, void *, size_t) = write_clr;