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