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