This repository was archived by the owner on Feb 25, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathptymisc.c
More file actions
117 lines (105 loc) · 2 KB
/
Copy pathptymisc.c
File metadata and controls
117 lines (105 loc) · 2 KB
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
#include <sys/types.h>
#include <sys/file.h>
#include <sys/time.h>
#include <errno.h>
extern int errno;
#include <fcntl.h>
#include "ptymisc.h"
#include "config/fdsettrouble.h"
#ifdef DESPERATE_FD_SET
#undef fd_set
#define fd_set long
#endif
extern long time();
long now()
{
return time((long *) 0);
}
int gaargh(n) int n;
{
struct timeval t;
t.tv_sec = 0;
t.tv_usec = n;
return select(0,(fd_set *) 0,(fd_set *) 0,(fd_set *) 0,&t);
}
int setnonblock(fd)
int fd;
{
return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) | FNDELAY); /* XXX */
}
int unsetnonblock(fd)
int fd;
{
return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) & ~FNDELAY); /*XXX*/
}
int forceopen(fd)
int fd;
{
int fdnull;
if (fcntl(fd,F_GETFL,0) != -1) /* it's open already */
return 0;
fdnull = open("/dev/null",O_RDWR,0);
if (fdnull == -1)
fdnull = open("/",O_RDONLY,0);
if (fdnull == -1)
return -1;
if (fdnull != fd)
{
if (dup2(fdnull,fd) == -1)
{
close(fdnull);
return -1;
}
close(fdnull);
}
return 0;
}
int respeq(resp,str)
char *resp;
char *str;
{
return scan_strncmp(resp,str,6) == 6;
}
int bread(fd,buf,n)
int fd; char *buf; int n;
{
int r; int tot; tot = 0;
while (n)
{
r = read(fd,buf,n);
if (r == 0) break;
if (r == -1)
if ((errno == EINTR) || (errno == EWOULDBLOCK)) continue;
else return -1; /* XXX: losing data! */
buf += r; tot += r; n -= r;
}
return tot;
}
int bwrite(fd,buf,n)
int fd; char *buf; int n;
{
int w; int tot; tot = 0;
while (n)
{
w = write(fd,buf,n);
if (w == 0) break; /* XXX: can happen under System V [sigh] */
if (w == -1)
if ((errno == EINTR) || (errno == EWOULDBLOCK)) continue;
else return -1; /* XXX: losing data! */
buf += w; tot += w; n -= w;
}
return tot;
}
int lflock(fd)
int fd;
{
/* must depend only on write access */
/* does not need to disappear automatically upon close() */
/* but should disappear automatically upon crash of any sort */
return flock(fd,LOCK_EX);
}
int lfunlock(fd)
int fd;
{
return flock(fd,LOCK_UN);
}