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.5  2008/02/24 00:42:53  james
14  * *** empty log message ***
15  *
16  * Revision 1.4  2008/02/23 13:05:58  staffcvs
17  * *** empty log message ***
18  *
19  * Revision 1.3  2008/02/13 16:57:29  james
20  * *** empty log message ***
21  *
22  * Revision 1.2  2008/02/13 09:12:21  james
23  * *** empty log message ***
24  *
25  * Revision 1.1  2008/02/13 01:08:38  james
26  * *** empty log message ***
27  *
28  */
29
30 #include "project.h"
31
32 int
33 wrap_read (int fd, void *buf, int len)
34 {
35   int red;
36
37   red = read (fd, buf, len);
38   if (!red)
39     return -1;
40
41   if ((red < 0) && (errno == EAGAIN))
42     red = 0;
43
44   return red;
45 }
46
47 int
48 wrap_write (int fd, void *buf, int len)
49 {
50   int writ;
51
52   errno = 0;
53
54   writ = write (fd, buf, len);
55
56   if (!writ)
57     return -1;
58
59   if ((writ < 0) && (errno == EAGAIN))
60     writ = 0;
61
62   return writ;
63 }
64
65
66 void
67 set_nonblocking (int fd)
68 {
69   long arg;
70   arg = fcntl (fd, F_GETFL, arg);
71   arg |= O_NONBLOCK;
72   fcntl (fd, F_SETFL, arg);
73 }
74
75 void
76 set_blocking (int fd)
77 {
78   long arg;
79   arg = fcntl (fd, F_GETFL, arg);
80   arg &= ~O_NONBLOCK;
81   fcntl (fd, F_SETFL, arg);
82 }
83
84
85
86 void
87 default_termios (struct termios *termios)
88 {
89   termios->c_iflag = PARMRK | INPCK;
90   termios->c_oflag = NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
91   termios->c_lflag = 0;
92   termios->c_cflag = CS8 | CREAD | CLOCAL;
93
94   termios->c_cc[VINTR] = 003;
95   termios->c_cc[VQUIT] = 034;
96   termios->c_cc[VERASE] = 0177;
97   termios->c_cc[VKILL] = 025;
98   termios->c_cc[VEOF] = 004;
99   termios->c_cc[VEOL] = 0;
100   termios->c_cc[VEOL2] = 0;
101   termios->c_cc[VSTART] = 021;
102   termios->c_cc[VSTOP] = 023;
103   termios->c_cc[VSUSP] = 032;
104   termios->c_cc[VLNEXT] = 026;
105   termios->c_cc[VWERASE] = 027;
106   termios->c_cc[VREPRINT] = 022;
107   termios->c_cc[VDISCARD] = 017;
108
109
110 }
111
112 void
113 client_termios (struct termios *termios)
114 {
115   memset (termios, 0, sizeof (termios));
116
117   termios->c_iflag = ICRNL | IXON | PARMRK | INPCK;
118   termios->c_oflag = OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
119   termios->c_lflag =
120     ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE;
121   termios->c_cflag = CS8 | CREAD | CLOCAL;
122
123   termios->c_cc[VINTR] = 003;
124   termios->c_cc[VQUIT] = 034;
125   termios->c_cc[VERASE] = 0177;
126   termios->c_cc[VKILL] = 025;
127   termios->c_cc[VEOF] = 004;
128   termios->c_cc[VEOL] = 0;
129   termios->c_cc[VEOL2] = 0;
130   termios->c_cc[VSTART] = 021;
131   termios->c_cc[VSTOP] = 023;
132   termios->c_cc[VSUSP] = 032;
133   termios->c_cc[VLNEXT] = 026;
134   termios->c_cc[VWERASE] = 027;
135   termios->c_cc[VREPRINT] = 022;
136   termios->c_cc[VDISCARD] = 017;
137
138
139   cfsetispeed (termios, B9600);
140   cfsetospeed (termios, B9600);
141 }