aboutsummaryrefslogtreecommitdiffstats
path: root/usr/kinit/getintfile.c
blob: 069870d0699ac658866b022e65051924d7840f33 (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
/*
 * getintfile.c
 *
 * Open a file and read it, assuming it contains a single long value.
 * Return 0 if we read a valid value, otherwise -1.
 */

#include <stdio.h>
#include <stdlib.h>

int getintfile(const char *path, long *val)
{
	char buffer[64];
	char *ep;
	FILE *f;

	f = fopen(path, "r");
	if ( !f )
		return -1;

	ep = buffer + fread(buffer, 1, sizeof buffer-1, f);
	fclose(f);
	*ep = '\0';

	*val = strtol(buffer, &ep, 0);
	if ( *ep && *ep != '\n' )
		return -1;
	else
		return 0;
}