aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
blob: ba1e30946584a627019b4166e4a0b1f4065fad6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * util.c:
 *
 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id$";

/*
 * $Log$
 * Revision 1.4  2008/02/23 13:05:58  staffcvs
 * *** empty log message ***
 *
 * Revision 1.3  2008/02/13 16:57:29  james
 * *** empty log message ***
 *
 * Revision 1.2  2008/02/13 09:12:21  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/13 01:08:38  james
 * *** empty log message ***
 *
 */

#include "project.h"

int
wrap_read (int fd, void *buf, int len)
{
  int red;

  red = read (fd, buf, len);
  if (!red)
    return -1;

  if ((red < 0) && (errno == EAGAIN))
    red = 0;

  return red;
}

int
wrap_write (int fd, void *buf, int len)
{
  int writ;

  errno = 0;

  writ = write (fd, buf, len);

  if (!writ)
    return -1;

  if ((writ < 0) && (errno == EAGAIN))
    writ = 0;

  return writ;
}


void
set_nonblocking (int fd)
{
  long arg;
  arg = fcntl (fd, F_GETFL, arg);
  arg |= O_NONBLOCK;
  fcntl (fd, F_SETFL, arg);
}

void
set_blocking (int fd)
{
  long arg;
  arg = fcntl (fd, F_GETFL, arg);
  arg &= ~O_NONBLOCK;
  fcntl (fd, F_SETFL, arg);
}

void
raw_termios (struct termios *termios)
{

  termios->c_iflag = 0;
/*ICRNL | IXON;*/
  termios->c_oflag = NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
  termios->c_lflag = 0;
/*
    ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE;
*/

  termios->c_cc[VINTR] = 003;
  termios->c_cc[VQUIT] = 034;
  termios->c_cc[VERASE] = 0177;
  termios->c_cc[VKILL] = 025;
  termios->c_cc[VEOF] = 004;
  termios->c_cc[VEOL] = 0;
  termios->c_cc[VEOL2] = 0;
  termios->c_cc[VSTART] = 021;
  termios->c_cc[VSTOP] = 023;
  termios->c_cc[VSUSP] = 032;
  termios->c_cc[VLNEXT] = 026;
  termios->c_cc[VWERASE] = 027;
  termios->c_cc[VREPRINT] = 022;
  termios->c_cc[VDISCARD] = 017;

}

void
default_termios (struct termios *termios)
{

//  memset (termios, 0, sizeof (termios));

  raw_termios (termios);

  termios->c_cflag = CS8 | CREAD | CLOCAL;
  termios->c_iflag |= PARMRK | INPCK;

  //cfsetispeed (termios, B9600);
  //cfsetospeed (termios, B9600);
}