File: io.c

package info (click to toggle)
sformat 3.5-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,588 kB
  • ctags: 5,796
  • sloc: ansic: 32,949; sh: 2,138; makefile: 187; pascal: 42
file content (439 lines) | stat: -rw-r--r-- 9,213 bytes parent folder | download | duplicates (2)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* @(#)io.c	1.22 01/02/19 Copyright 1988 J. Schilling */
#ifndef lint
static	char sccsid[] =
	"@(#)io.c	1.22 01/02/19 Copyright 1988 J. Schilling";
#endif
/*
 *	Copyright (c) 1988 J. Schilling
 */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <mconfig.h>
#include <stdio.h>
#include <standard.h>
#include <ctype.h>
#include <vadefs.h>
#include <stdxlib.h>
#include <strdefs.h>
#include <utypes.h>
#include <schily.h>

#include "fmt.h"

LOCAL BOOL	cvt_blocks __PR((char *, long *, long, long, struct disk *));
LOCAL void	prt_std	   __PR((char *, long, long, long, struct disk *));
LOCAL void	prt_blocks __PR((char *, long, long, long, struct disk *));

EXPORT
char *skipwhite(s)
		 const char	*s;
{
	register const Uchar	*p = (const Uchar *)s;

	while (*p) {
		if (!isspace(*p))
			break;
		p++;
	}
	return ((char *)p);
}

/* ARGSUSED */
EXPORT
BOOL
cvt_std(linep, lp, mini, maxi, dp)
	char	*linep;
	long	*lp;
	long	mini;
	long	maxi;
	struct disk	*dp;
{
	long	l	= -1L;

/*	printf("cvt_std(\"%s\", %d, %d, %d);\n", linep, *lp, mini, maxi);*/

	if (linep[0] == '?') {
		printf("Enter a number in the range from %ld to %ld\n",
								mini, maxi);
		printf("The default radix is 10\n");
		printf("Precede number with '0x' for hexadecimal or with '0' for octal\n");
		printf("Shorthands are:\n");
		printf("\t'^' for minimum value (%ld)\n", mini);
		printf("\t'$' for maximum value (%ld)\n", maxi);
		printf("\t'+' for incrementing value to %ld\n", *lp + 1);
		printf("\t'-' for decrementing value to %ld\n", *lp - 1);
		return (FALSE);
	}
	if (linep[0] == '^' && *skipwhite(&linep[1]) == '\0') {
		l = mini;
	} else if (linep[0] == '$' && *skipwhite(&linep[1]) == '\0') {
		l = maxi;
	} else if (linep[0] == '+' && *skipwhite(&linep[1]) == '\0') {
		if (*lp < maxi)
			l = *lp + 1;
	} else if (linep[0] == '-' && *skipwhite(&linep[1]) == '\0') {
		if (*lp > mini)
			l = *lp - 1;
	} else if (*astol(linep, &l)) {
		printf("Not a number: '%s'.\n", linep);
		return (FALSE);
	}
	if (l < mini || l > maxi) {
		printf("'%s' is out of range.\n", linep);
		return (FALSE);
	}
	*lp = l;
	return (TRUE);
}

LOCAL BOOL
cvt_blocks(linep, lp, mini, maxi, dp)
	char	*linep;
	long	*lp;
	long	mini;
	long	maxi;
	struct disk	*dp;
{
	long	l;
	long	cyls	= 0L;
	long	heads	= 0L;
	long	secs	= 0L;
	char	*hp;
	char	*sp;

	if (linep[0] == '?') {
		printf("To enter blocks as simple block count:\n");
		(void) cvt_std(linep, lp, mini, maxi, dp);
		printf("\tNOTE: '$' will fill this partition to the end of the disk.\n");
		printf("To enter blocks as mega bytes:\n");
		printf("\tEnter size in megabytes as floating point number.\n");
		printf("\t(must be directly followed by 'm' or 'M' e.g. 12.5M)\n");
		printf("To enter blocks as cyls/tracks/sectors:\n");
		printf("\tEnter a number in the form cyls/tracks/secs or cyls/tracks or cyls/\n");
		(void) cvt_bcyls(linep, lp, mini, maxi, dp);
		return (FALSE);
	}
	/*
	 * If line contains no slash
	 * and no character which tells us to use megabytes
	 * convert as blocknumber.
	 */
	if (linep[0] != '>' &&
			!strchr(linep, '/') &&
			!strchr(linep, '.') &&
			!strchr(linep, 'm') && !strchr(linep, 'M'))
		return (cvt_std(linep, lp, mini, maxi, dp));

	sp = hp = strchr(linep, '/');
	if (hp) {
		/*
		 * It's a cyl/track/sec notation.
		 */
		*hp++ = '\0';
		if (!(sp = strchr(hp, '/'))) {
			sp = &hp[-1];		
		} else {
			*sp++ = '\0';
		}

		if (linep[0] && !cvt_std(linep, &cyls, 0L, dp->lncyl, dp))
			return (FALSE);
		if (hp[0] && !cvt_std(hp, &heads, 0L, dp->lhead-1L, dp))
			return (FALSE);
		if (sp[0] && !cvt_std(sp, &secs, 0L, dp->lspt-1L, dp))
			return (FALSE);

		l = secs + heads * dp->lspt +
			cyls * (dp->lhead * dp->lspt);

		if (l < mini || l > maxi) {
			printf("'%ld' is out of range.\n", l);
			return (FALSE);
		}
	} else if (linep[0] == '>') {
		/*
		 * It's ---> reach to the next partition's beginning.
		 */
		return (cvt_bcyls(linep, lp, mini, maxi, dp));
	} else {
		int a;
		double d;
/*
 * atof() should be in stdlib.h. If there is no stdlib.h, stdxlib.h has the def
 */
/*		extern	double atof();*/
		/*
		 * It's megabytes
		 */
		d = atof(linep);
		d *= 2048.0;	/* Now we have 512 Byte sectors */
		l = d;

		/*
		 * Round up to next cylinder.
		 */
		a = l / (dp->lhead * dp->lspt);
		if (l % (dp->lhead * dp->lspt)) {
			l = ++a * (dp->lhead * dp->lspt);
		}
	}
	*lp = l;
	return (TRUE);
}

/* ARGSUSED */
LOCAL void
prt_std(s, l, mini, maxi, dp)
	char	*s;
	long	l;
	long	mini;
	long	maxi;
	struct disk *dp;
{
	printf("%s %ld (%ld - %ld)/<cr>:", s, l, mini, maxi);
}

LOCAL void
prt_blocks(s, l, mini, maxi, dp)
	char	*s;
	long	l;
	long	mini;
	long	maxi;
	struct disk	*dp;
{
	long	cyl;
	long	head;
	long	sec;

	cyl = l / (dp->lhead*dp->lspt);
	head = (l - (cyl * dp->lhead * dp->lspt)) / dp->lspt;
	sec = l - (cyl * dp->lhead * dp->lspt)
		- (head * dp->lspt);

	printf("%s %ld, %ld/%ld/%ld (%ld - %ld)/<cr>:", s, l, cyl, head, sec,
								mini, maxi);
}

EXPORT
BOOL getvalue(s, lp, mini, maxi, prt, cvt, dp)
	char	*s;
	long	*lp;
	long	mini;
	long	maxi;
	void	(*prt) __PR((char *, long, long, long, struct disk *));
	BOOL	(*cvt) __PR((char *, long *, long, long, struct disk *));
	struct disk	*dp;
{
	char	line[128];
	char	*linep;

	for(;;) {
		(*prt)(s, *lp, mini, maxi, dp);
		flush();
		line[0] = '\0';
		if (getline(line, 80) == EOF)
			exit(EX_BAD);

		linep = skipwhite(line);
		/*
		 * Nicht initialisierte Variablen
		 * duerfen nicht uebernommen werden
		 */
		if (linep[0] == '\0' && *lp != -1L)
			return (FALSE);

		if (strlen(linep) == 0) {
			/* Leere Eingabe */
		} else if ((*cvt)(linep, lp, mini, maxi, dp))
			return (TRUE);
	}
	/* NOTREACHED */
}

EXPORT
BOOL getlong(s, lp, mini, maxi)
	char	*s;
	long	*lp;
	long	mini;
	long	maxi;
{
	return (getvalue(s, lp, mini, maxi, prt_std, cvt_std, (void *)0));
}

EXPORT
BOOL getint(s, ip, mini, maxi)
	char	*s;
	int	*ip;
	int	mini;
	int	maxi;
{
	long	l = *ip;
	BOOL	ret;

	ret = getlong(s, &l, (long)mini, (long)maxi);
	*ip = l;
	return (ret);
}

EXPORT
BOOL getdiskcyls(s, lp, mini, maxi)
	char	*s;
	long	*lp;
	long	mini;
	long	maxi;
{
	return (getvalue(s, lp, mini, maxi, prt_std, cvt_cyls, (void *)0));
}

EXPORT
BOOL getdiskblocks(s, lp, mini, maxi, dp)
	char	*s;
	long	*lp;
	long	mini;
	long	maxi;
	struct disk	*dp;
{
	return (getvalue(s, lp, mini, maxi, prt_blocks, cvt_blocks, dp));
}

/* VARARGS1 */
#ifdef	PROTOTYPES
EXPORT BOOL yes(char *form, ...)
#else
EXPORT
BOOL yes(form, va_alist)
	char	*form;
	va_dcl
#endif
{
	va_list	args;
	char okbuf[10];

again:
#ifdef	PROTOTYPES
	va_start(args, form);
#else
	va_start(args);
#endif
	printf("%r", form, args);
	va_end(args);
	flush();
	if (getline(okbuf, sizeof(okbuf)) == EOF)
		exit(EX_BAD);
	if (okbuf[0] == '?') {
		printf("Enter 'y', 'Y', 'yes' or 'YES' if you agree with the previous asked question.\n");
		printf("All other input will be handled as if the question has beed answered with 'no'.\n");
		goto again;
	}
	if(streql(okbuf, "y") || streql(okbuf, "yes") ||
	   streql(okbuf, "Y") || streql(okbuf, "YES"))
		return(TRUE);
	else
		return(FALSE);
}

void prbytes(s, cp, n)
		char		*s;
	register unsigned char	*cp;
	register int		n;
{
	printf(s);
	while (--n >= 0)
		printf(" %02X", *cp++);
	printf("\n");
}

EXPORT
char	*permstring(s)
	const char	*s;
{
	char	*p;

	if ((p = malloc(strlen(s)+1)) != NULL)
		strcpy(p, s);
	return (p);
}

EXPORT
struct strval *
strval(val, sp)
	int	val;
	struct strval *sp;
{
	while (sp->s_name) {
		if (val == sp->s_val)
			return sp;
		sp++;
	}
	return (struct strval *)0;
}

EXPORT
struct strval *
namestrval(name, sp)
	const char	*name;
	struct strval	*sp;
{
	while (sp->s_name) {
		if (strcmp(name, sp->s_name) == 0)
			return sp;
		sp++;
	}
	return (struct strval *)0;
}

EXPORT
BOOL getstrval(s, lp, sp, deflt)
	const char	*s;
	long		*lp;
	struct strval	*sp;
	long		deflt;
{
	struct strval *csp;
	char	line[128];
	char	*linep;

	for(;;) {
		csp = strval(*lp, sp);
		printf("%s [%s]:", s, csp->s_name);
		flush();
		line[0] = '\0';
		if (getline(line, 80) == EOF)
			exit(EX_BAD);

		linep = skipwhite(line);
		/*
		 * Nicht initialisierte Variablen
		 * duerfen nicht uebernommen werden
		 */
		if (linep[0] == '\0' && *lp != -1L)
			return (FALSE);

		if (strlen(linep) == 0) {
			/* Leere Eingabe */
		} else if (streql(linep, "?") || streql(linep, "help")) {
			printf("Possible values are:\n");
			for (csp = sp; csp->s_name; csp++) {
				printf("%s\t%s\n", csp->s_name, csp->s_text);
			}
		} else if ((csp = namestrval(linep, sp)) != 0) {
			*lp = csp->s_val;
			return (TRUE);
		}
	}
	/* NOTREACHED */
}