Fix compile time warnings and a couple other cleanups.
[cboard.git] / src / ics.c
blobb221109f419f4356bf497091a9c29407219c79c5
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2006 Ben Kibbey <bjk@arbornet.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <netdb.h>
29 #include <string.h>
30 #include <sys/time.h>
31 #include <fcntl.h>
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
37 #include "common.h"
38 #include "ics.h"
40 int parse_ics_output(char *str)
42 return 0;
45 int send_to_ics(int sockfd, const char *format, ...)
47 va_list ap;
48 int len;
49 char *line;
50 int try = 0;
52 va_start(ap, format);
53 #ifdef HAVE_VASPRINTF
54 len = vasprintf(&line, format, ap);
55 #else
56 line = Malloc(LINE_MAX);
57 len = vsnprintf(line, LINE_MAX, format, ap);
58 #endif
59 va_end(ap);
61 while (1) {
62 int n;
63 fd_set fds;
64 struct timeval tv;
66 FD_ZERO(&fds);
67 FD_SET(sockfd, &fds);
69 tv.tv_sec = 0;
70 tv.tv_usec = 0;
72 if ((n = select(sockfd + 1, NULL, &fds, NULL, &tv)) > 0) {
73 if (FD_ISSET(sockfd, &fds)) {
74 n = send(sockfd, line, len, 0);
76 if (n == -1) {
77 if (errno == EAGAIN)
78 continue;
80 message(ERROR, ANYKEY, "Attempt #%i. send(): %s", ++try,
81 strerror(errno));
82 continue;
85 if (len != n) {
86 message(NULL, ANYKEY, "try #%i: send() error to socket. "
87 "Expected %i, got %i.", ++try, len, n);
88 continue;
91 break;
94 else {
95 /* timeout */
99 free(line);
100 return 0;
103 int ics_connect(int *sockfd)
105 struct sockaddr_in sa;
106 struct hostent *h;
108 memset(&sa, 0, sizeof(struct sockaddr_in));
110 if ((h = gethostbyname(config.ics_server)) == NULL) {
111 message(ERROR, ANYKEY, "%s: %s", config.ics_server, hstrerror(h_errno));
112 return 1;
116 if (inet_aton(argv[1], &sa.sin_addr) == 0)
117 errx(EXIT_FAILURE, "malformed address");
120 sa.sin_addr.s_addr = inet_addr(h->h_addr);
121 sa.sin_family = AF_INET;
122 sa.sin_port = htons(config.ics_port);
124 if ((*sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
125 message(ERROR, ANYKEY, "socket(): %s", strerror(errno));
126 return 1;
129 fcntl(*sockfd, F_SETFL, O_NONBLOCK);
132 if (bind(*sockfd, (struct sockaddr *)&sa, sizeof(struct sockaddr)) == -1) {
133 message(ERROR, ANYKEY, "bind(): %s", strerror(errno));
134 return 1;
138 if (connect(*sockfd, (struct sockaddr *)&sa, sizeof(struct sockaddr))
139 == -1) {
140 message(ERROR, ANYKEY, "connect(): %s", strerror(errno));
141 return 1;
144 return 0;