summaryrefslogtreecommitdiffstats
path: root/usr/kinit/getintfile.c
blob: 41ba4755087e6c6d5866c3b01f1155d9415766f2 (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
/*
 * 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>

#include "kinit.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;
}