aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..e8b315e
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,108 @@
+/*
+ * util.c:
+ *
+ * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
+ * All rights reserved.
+ *
+ */
+
+static char rcsid[] = "$Id$";
+
+/*
+ * $Log$
+ * Revision 1.1 2008/02/13 01:08:38 james
+ * *** empty log message ***
+ *
+ */
+
+#include "project.h"
+
+int
+wrap_read (int fd, void *buf, int len)
+{
+ int red;
+
+ red = read (fd, buf, len);
+ if (!red)
+ return -1;
+
+ if ((red < 0) && (errno == EAGAIN))
+ red = 0;
+
+ return red;
+}
+
+int
+wrap_write (int fd, void *buf, int len)
+{
+ int writ;
+
+ writ = write (fd, buf, len);
+ if (!writ)
+ return -1;
+
+ if ((writ < 0) && (errno == -EAGAIN))
+ writ = 0;
+
+ return writ;
+}
+
+
+void
+set_nonblocking (int fd)
+{
+ long arg;
+ arg = fcntl (fd, F_GETFL, arg);
+ arg |= O_NONBLOCK;
+ fcntl (fd, F_SETFL, arg);
+}
+
+void
+set_blocking (int fd)
+{
+ long arg;
+ arg = fcntl (fd, F_GETFL, arg);
+ arg &= ~O_NONBLOCK;
+ fcntl (fd, F_SETFL, arg);
+}
+
+void raw_termios(struct termios *termios)
+{
+
+ termios->c_iflag = ICRNL | IXON;
+ termios->c_oflag = OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
+ termios->c_lflag =
+ ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE;
+
+ termios->c_cc[VINTR] = 003;
+ termios->c_cc[VQUIT] = 034;
+ termios->c_cc[VERASE] = 0177;
+ termios->c_cc[VKILL] = 025;
+ termios->c_cc[VEOF] = 004;
+ termios->c_cc[VEOL] = 0;
+ termios->c_cc[VEOL2] = 0;
+ termios->c_cc[VSTART] = 021;
+ termios->c_cc[VSTOP] = 023;
+ termios->c_cc[VSUSP] = 032;
+ termios->c_cc[VLNEXT] = 026;
+ termios->c_cc[VWERASE] = 027;
+ termios->c_cc[VREPRINT] = 022;
+ termios->c_cc[VDISCARD] = 017;
+
+}
+
+void
+default_termios (struct termios *termios)
+{
+
+ memset (termios, 0, sizeof (termios));
+
+ raw_termios(termios);
+
+ termios->c_cflag = CS8 | CREAD | CLOCAL;
+
+ cfsetispeed (termios, B9600);
+ cfsetospeed (termios, B9600);
+}
+
+