summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorDan McGee <dpmcgee@gmail.com>2008-09-16 22:20:17 -0500
committerDan McGee <dan@archlinux.org>2008-10-05 19:49:38 -0500
commitcf7ca31c0d2895803a822ecb41fc394c25f05027 (patch)
tree3ff0ff028759974449f7f801857304b9d805decf /util.c
parent8b267356438f7b428d150f34f011e7cdb351c30b (diff)
downloadonkyocontrol-cf7ca31c0d2895803a822ecb41fc394c25f05027.tar.gz
onkyocontrol-cf7ca31c0d2895803a822ecb41fc394c25f05027.zip
More fun changes
* Play around with strdup() since it isn't actually part of ISO C. * Get code to compile under c89, c99, and gnu99 * Rework open_listener() to use getaddrinfo() Signed-off-by: Dan McGee <dpmcgee@gmail.com>
Diffstat (limited to 'util.c')
-rw-r--r--util.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..b422bde
--- /dev/null
+++ b/util.c
@@ -0,0 +1,66 @@
+/*
+ * util.c - Onkyo receiver utility functions
+ *
+ * Copyright (c) 2008 Dan McGee <dpmcgee@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h> /* malloc */
+#include <unistd.h> /* close, read, write */
+#include <errno.h> /* for errno refs */
+#include <string.h> /* memcpy */
+
+void xclose(int fd)
+{
+ while(close(fd) && errno == EINTR);
+}
+
+ssize_t xread(int fd, void *buf, size_t len)
+{
+ ssize_t nr;
+ while(1) {
+ nr = read(fd, buf, len);
+ if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
+ continue;
+ return nr;
+ }
+}
+
+ssize_t xwrite(int fd, const void *buf, size_t len)
+{
+ ssize_t nr;
+ while(1) {
+ nr = write(fd, buf, len);
+ if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
+ continue;
+ return nr;
+ }
+}
+
+/* if using ISO C, strdup() is not actually defined, provide our own */
+#ifndef strdup
+char *strdup(const char *s)
+{
+ char *ret = NULL;
+ if(s) {
+ const size_t len = strlen(s) + 1;
+ ret = malloc(len * sizeof(char));
+ if(ret)
+ memcpy(ret, s, len);
+ }
+ return(ret);
+}
+#endif /* strdup */
+