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