1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
6 * Copyright (C) 2002-2008 Marcel Holtmann <marcel@holtmann.org>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25 #include <sys/types.h>
26 #include <errno.h>
27 #include <signal.h>
28
29 #ifndef min
30 #define min(a,b) ( (a)<(b) ? (a):(b) )
31 #endif
32
33 /* IO cancelation */
34 extern volatile sig_atomic_t __io_canceled;
35
io_init(void)36 static inline void io_init(void)
37 {
38 __io_canceled = 0;
39 }
40
io_cancel(void)41 static inline void io_cancel(void)
42 {
43 __io_canceled = 1;
44 }
45
46 /* Read exactly len bytes (Signal safe)*/
read_n(int fd,char * buf,int len)47 static inline int read_n(int fd, char *buf, int len)
48 {
49 register int t = 0, w;
50
51 while (!__io_canceled && len > 0) {
52 if ((w = read(fd, buf, len)) < 0) {
53 if (errno == EINTR || errno == EAGAIN)
54 continue;
55 return -1;
56 }
57 if (!w)
58 return 0;
59 len -= w;
60 buf += w;
61 t += w;
62 }
63
64 return t;
65 }
66
67 /* Write exactly len bytes (Signal safe)*/
write_n(int fd,char * buf,int len)68 static inline int write_n(int fd, char *buf, int len)
69 {
70 register int t = 0, w;
71
72 while (!__io_canceled && len > 0) {
73 if ((w = write(fd, buf, len)) < 0) {
74 if (errno == EINTR || errno == EAGAIN)
75 continue;
76 return -1;
77 }
78 if (!w)
79 return 0;
80 len -= w;
81 buf += w;
82 t += w;
83 }
84
85 return t;
86 }
87
88 int ms_dun(int fd, int server, int timeo);
89