sacc

sacc (saccomys): simple gopher client.
Log | Files | Refs | LICENSE

io_clr.c (1329B)


      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 close_clr(struct cnx *c)
     12 {
     13 	return close(c->sock);
     14 }
     15 
     16 static int
     17 connect_clr(struct cnx *c, struct addrinfo *ai, const char *host)
     18 {
     19 	return connect(c->sock, ai->ai_addr, ai->ai_addrlen);
     20 }
     21 
     22 static void
     23 connerr_clr(struct cnx *c, const char *host, const char *port, int err)
     24 {
     25 	if (c->sock == -1)
     26 		diag("Can't open socket: %s", strerror(err));
     27 	else
     28 		diag("Can't connect to: %s:%s: %s", host, port, strerror(err));
     29 }
     30 
     31 static char *
     32 parseurl_clr(char *url)
     33 {
     34 	char *p;
     35 
     36 	if (p = strstr(url, "://")) {
     37 		if (strncmp(url, "gopher", p - url))
     38 			die("Protocol not supported: %.*s", p - url, url);
     39 
     40 		url = p + 3;
     41 	}
     42 
     43 	return url;
     44 }
     45 
     46 static ssize_t
     47 read_clr(struct cnx *c, void *buf, size_t bs)
     48 {
     49 	return read(c->sock, buf, bs);
     50 }
     51 
     52 static ssize_t
     53 write_clr(struct cnx *c, void *buf, size_t bs)
     54 {
     55 	return write(c->sock, buf, bs);
     56 }
     57 
     58 int (*ioclose)(struct cnx *) = close_clr;
     59 int (*ioconnect)(struct cnx *, struct addrinfo *, const char *) = connect_clr;
     60 void (*ioconnerr)(struct cnx *, const char *, const char *, int) = connerr_clr;
     61 char *(*ioparseurl)(char *) = parseurl_clr;
     62 ssize_t (*ioread)(struct cnx *, void *, size_t) = read_clr;
     63 ssize_t (*iowrite)(struct cnx *, void *, size_t) = write_clr;