File: util.c

package info (click to toggle)
physlock 13-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 136 kB
  • sloc: ansic: 532; makefile: 58
file content (141 lines) | stat: -rw-r--r-- 3,342 bytes parent folder | download
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
/* Copyright 2013 Bert Muennich
 *
 * This file is part of physlock.
 *
 * 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 of the License, 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; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "physlock.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

const char *progname;

static FILE *Stderr;

void error_init(int fd)
{
	if ((fd = dup(fd)) == -1 || (Stderr = fdopen(fd, "w")) == NULL)
		Stderr = stderr;
}

void error(int eval, int err, const char* fmt, ...)
{
	va_list ap;

	fflush(stdout);
	fprintf(Stderr, "%s: ", progname);
	va_start(ap, fmt);
	if (fmt != NULL)
		vfprintf(Stderr, fmt, ap);
	va_end(ap);
	if (err != 0)
		fprintf(Stderr, "%s%s", fmt != NULL ? ": " : "", strerror(err));
	fputc('\n', Stderr);

	if (eval != 0)
		exit(eval);
}

char* estrdup(const char *s) {
	char *d;
	size_t n = strlen(s) + 1;

	d = malloc(n);
	if (d == NULL)
		error(EXIT_FAILURE, errno, NULL);
	memcpy(d, s, n);
	return d;
}

/*
 * Read a file to a buffer.
 * The buffer is ensured to be NULL-terminated.
 * The call always succeeds (it dies() on failure).
 * Returns the number of characters read.
 */
size_t read_file(const char *path, char *buf, size_t len) {
	FILE *ctl_file;
	size_t nread;

	while ((ctl_file = fopen(path, "r")) == NULL && errno == EINTR);
	if (ctl_file == NULL)
		error(EXIT_FAILURE, errno, "%s", path);

	nread = fread(buf, 1, len - 1, ctl_file);
	if (ferror(ctl_file))
		error(EXIT_FAILURE, 0, "%s: Error reading file", path);

	fclose(ctl_file);
	buf[nread] = '\0';

	return nread;
}

/*
 * Write a buffer into a file.
 * Returns the number of characters written or -1 on failure.
 */
CLEANUP ssize_t write_file(const char *path, char *buf, size_t len) {
	FILE *ctl_file;
	size_t nwritten;

	while ((ctl_file = fopen(path, "w+")) == NULL && errno == EINTR);
	if (ctl_file == NULL) {
		error(0, errno, "%s", path);
		return -1;
	}

	nwritten = fwrite(buf, 1, len, ctl_file);
	if (ferror(ctl_file)) {
		error(0, 0, "%s: Error writing file", path);
		return -1;
	}
	fclose(ctl_file);

	return nwritten;
}

/*
 * Read integer from file, and ensure the next character is as expected.
 * The call always succeeds (it dies() on failure).
 */
int read_int_from_file(const char *path, char ending_char) {
	char buf[32], *end;
	int value;

	read_file(path, buf, sizeof(buf));

	value = strtol(buf, &end, 0);
	if (*end && *end != ending_char)
		error(EXIT_FAILURE, 0, "%s: Invalid file content", path);

	return value;
}

/*
 * Write integer to file.
 * Returns the number of characters written or -1 on failure.
 */
CLEANUP ssize_t write_int_to_file(const char *path, int value) {
	char buf[32];

	snprintf(buf, sizeof(buf), "%d\n", value);
	return write_file(path, buf, strlen(buf));
}