chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / util.c
1 /*
2  * util.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.2  2008/02/13 09:12:21  james
14  * *** empty log message ***
15  *
16  * Revision 1.1  2008/02/13 01:08:38  james
17  * *** empty log message ***
18  *
19  */
20
21 #include "project.h"
22
23 int
24 wrap_read (int fd, void *buf, int len)
25 {
26   int red;
27
28   red = read (fd, buf, len);
29   if (!red)
30     return -1;
31
32   if ((red < 0) && (errno == EAGAIN))
33     red = 0;
34
35   return red;
36 }
37
38 int
39 wrap_write (int fd, void *buf, int len)
40 {
41   int writ;
42
43   writ = write (fd, buf, len);
44   if (!writ)
45     return -1;
46
47   if ((writ < 0) && (errno == -EAGAIN))
48     writ = 0;
49
50   return writ;
51 }
52
53
54 void
55 set_nonblocking (int fd)
56 {
57   long arg;
58   arg = fcntl (fd, F_GETFL, arg);
59   arg |= O_NONBLOCK;
60   fcntl (fd, F_SETFL, arg);
61 }
62
63 void
64 set_blocking (int fd)
65 {
66   long arg;
67   arg = fcntl (fd, F_GETFL, arg);
68   arg &= ~O_NONBLOCK;
69   fcntl (fd, F_SETFL, arg);
70 }
71
72 void
73 raw_termios (struct termios *termios)
74 {
75
76   termios->c_iflag = ICRNL | IXON;
77   termios->c_oflag = OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
78   termios->c_lflag =
79     ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE;
80
81   termios->c_cc[VINTR] = 003;
82   termios->c_cc[VQUIT] = 034;
83   termios->c_cc[VERASE] = 0177;
84   termios->c_cc[VKILL] = 025;
85   termios->c_cc[VEOF] = 004;
86   termios->c_cc[VEOL] = 0;
87   termios->c_cc[VEOL2] = 0;
88   termios->c_cc[VSTART] = 021;
89   termios->c_cc[VSTOP] = 023;
90   termios->c_cc[VSUSP] = 032;
91   termios->c_cc[VLNEXT] = 026;
92   termios->c_cc[VWERASE] = 027;
93   termios->c_cc[VREPRINT] = 022;
94   termios->c_cc[VDISCARD] = 017;
95
96 }
97
98 void
99 default_termios (struct termios *termios)
100 {
101
102   memset (termios, 0, sizeof (termios));
103
104   raw_termios (termios);
105
106   termios->c_cflag = CS8 | CREAD | CLOCAL;
107
108   cfsetispeed (termios, B9600);
109   cfsetospeed (termios, B9600);
110 }