File: db_getlong.c

package info (click to toggle)
htdig 1%3A3.2.0b6-21
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,292 kB
  • sloc: ansic: 49,632; cpp: 46,468; sh: 17,400; xml: 4,180; perl: 2,543; makefile: 888; php: 79; asm: 14
file content (85 lines) | stat: -rw-r--r-- 1,800 bytes parent folder | download | duplicates (9)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997, 1998, 1999
 *	Sleepycat Software.  All rights reserved.
 */

#include "db_config.h"

#ifndef lint
static const char sccsid[] = "@(#)db_getlong.c	11.3 (Sleepycat) 10/29/99";
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#endif

#include "db_int.h"

/*
 * CDB___db_getlong --
 *	Return a long value inside of basic parameters.
 *
 * PUBLIC: int CDB___db_getlong
 * PUBLIC:     __P((DB *, const char *, char *, long, long, long *));
 */
int
CDB___db_getlong(dbp, progname, p, min, max, storep)
	DB *dbp;
	const char *progname;
	char *p;
	long min, max, *storep;
{
	long val;
	char *end;

	CDB___os_set_errno(0);
	val = strtol(p, &end, 10);
	if ((val == LONG_MIN || val == LONG_MAX) &&
	    CDB___os_get_errno() == ERANGE) {
		if (dbp == NULL) {
			fprintf(stderr,
			    "%s: %s: %s\n", progname, p, strerror(ERANGE));
			exit(1);
		}
		dbp->err(dbp, ERANGE, "%s", p);
		return (1);
	}
	if (p[0] == '\0' || end[0] != '\0') {
		if (dbp == NULL) {
			fprintf(stderr,
			    "%s: %s: Invalid numeric argument\n", progname, p);
			exit(1);
		}
		dbp->errx(dbp, "%s: Invalid numeric argument", p);
		return (1);
	}
	if (val < min) {
		if (dbp == NULL) {
			fprintf(stderr,
			    "%s: %s: Less than minimum value (%ld)\n",
			    progname, p, min);
			exit(1);
		}
		dbp->errx(dbp, "%s: Less than minimum value (%ld)", p, min);
		return (1);
	}
	if (val > max) {
		if (dbp == NULL) {
			fprintf(stderr,
			    "%s: %s: Greater than maximum value (%ld)\n",
			    progname, p, max);
			exit(1);
		}
		dbp->errx(dbp, "%s: Greater than maximum value (%ld)", p, max);
		exit(1);
	}
	*storep = val;
	return (0);
}