summaryrefslogtreecommitdiff
path: root/unix.c
blob: c87f0d9ac359f8a71cbf44d3295eebdd62c9712d (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

static char rcsid[] = "$Header: /home/sater/bridge/bigdeal/RCS/unix.c,v 1.11 2000/08/25 15:55:53 sater Exp $";

#include <sys/termios.h>
#include <sys/time.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "types.h"
#include "bigdeal.h"
#include "collect.h"

extern FILE *flog;

#ifdef TIOCSETA
/*
 * For example on BSDI
 */
#define GTTY	TIOCGETA
#define STTY	TIOCSETA

#else
/*
 * For example on SunOS
 */

#define GTTY	TCGETS
#define STTY	TCSETSF

#endif /* TIOCSETA */

static int subsecbits = 5;
#define MAX_INTERVAL_ENTROPY	6

void
gettod(struct timeval *tp)
/*
 * Fill in *tp with gettimeofday values
 * Side effect: calculate approx precision
 */
{
	int b;

	gettimeofday(tp, (struct timezone *) 0);
	/* Find how many bits are set in usec field */
	for(b = 0; b < 20; b++) {
		if (tp->tv_usec&(1<<b))
			break;
	}
	if (20-b > subsecbits) {
		subsecbits = 20-b;
	}
}

int
getchtm(int *nbits)
/*
 * Read one character from standard input
 * Time the wait, and use bits for random
 * MUST be in cbreak mode for this to work
 */
{
	struct timeval t1, t2;
	long sdiff, mdiff;
	int c;
	long shift;
	int b;

	gettod(&t1);
	c = getchar();
	gettod(&t2);
	mdiff = t2.tv_usec - t1.tv_usec;
	sdiff = t2.tv_sec - t1.tv_sec;
	if (sdiff > 1000)
		sdiff = 1000;
	mdiff += sdiff * 1000000;
	/*
	 * Everything slower than 1/8 second can be faked
	 * Trust at most half of the rest
	 */
	for (shift = mdiff, b= 0; shift>3 ; shift >>= 2, b++)
		;
	if (b <= MAX_INTERVAL_ENTROPY)
		*nbits = b;
	else
		*nbits = MAX_INTERVAL_ENTROPY;
	collect_more((byte *) &t1, sizeof(t1), 0);
	collect_more((byte *) &t2, sizeof(t2), 0);
	if (flog)
		fprintf(flog, "Collected character %d, timediff %ld, timebits %d\n",
			c, mdiff, *nbits);
	return c;
}

void
os_collect() {
	int pid;
	struct timeval t;

	pid = getpid();
	/* Trust about 8 bits of randomness in pid */
	collect_more((byte *) &pid, sizeof(pid), 8);
	gettod(&t);
	/* Trust 6 bits of seconds, and half the subsecond bits */
	collect_more((byte *) &t, sizeof(t), 6+subsecbits/2);
	if (flog)
		fprintf(flog, "First TOD=(%ld, %ld), subsecbits = %d\n",
			t.tv_sec, t.tv_usec, subsecbits);
}

static struct termios tios;

void
cbreak()
/*
 * Set terminal to state where characters can be read one at a time
 * Also disable echo
 */
{
	struct termios newtios;

	newtios = tios;

	/* fiddle newtios */
	newtios.c_lflag &= ~(ICANON|ECHO);
	newtios.c_cc[VMIN] = 1;
	newtios.c_cc[VTIME] = 0;
	ioctl(0, STTY, &newtios);
}

void
cooked()
/*
 * Reset terminal to original state
 */
{

	ioctl(0, STTY, &tios);
}

void
os_start() {

	ioctl(0, GTTY, &tios);
}

void
os_finish() {

	cooked();
}

int
legal_filename_prefix(char *s)
/*
 * Legal prefix to make legal name when three letter suffix added
 */
{

	if (*s == 0)	/* Too short */
		return 0;
	if (strlen(s) > 10)	/* Worst case, System V */
		return 0;
	while (*s) {
		if (*s == '/' || *s == ' ')	/* disallow space */
			return 0;
		s++;
	}
	return 1;
}

char *os_init_file_name()
{

	return ".bigdealrc";
}