File: getintfile.c

package info (click to toggle)
klibc 2.0.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,248 kB
  • sloc: ansic: 47,887; asm: 2,394; perl: 758; makefile: 193; sh: 183
file content (30 lines) | stat: -rw-r--r-- 493 bytes parent folder | download | duplicates (10)
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;
}