libite 2.6.1
procval.c
Go to the documentation of this file.
1/* Reading and writing values/strings to/from /proc and /sys style files
2 *
3 * Copyright (c) 2023 Tobias Waldekranz <tobias@waldekranz.com>
4 * Copyright (c) 2023 Joachim Wiberg <troglobit@gmail.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
25
26#include <errno.h>
27#include <stdio.h>
28#include <stdarg.h>
29#include <stdlib.h>
30
31#include "lite.h"
32
44char *vreadsnf(char *line, size_t len, const char *fmt, va_list ap)
45{
46 va_list apc;
47 FILE *fp;
48
49 va_copy(apc, ap);
50 fp = vfopenf("r", fmt, apc);
51 va_end(apc);
52 if (!fp)
53 return NULL;
54
55 if (!fgets(line, len, fp)) {
56 fclose(fp);
57 return NULL;
58 }
59
60 fclose(fp);
61 return chomp(line);
62}
63
64
74char *readsnf(char *line, size_t len, const char *fmt, ...)
75{
76 va_list ap;
77 char *ln;
78
79 va_start(ap, fmt);
80 ln = vreadsnf(line, len, fmt, ap);
81 va_end(ap);
82
83 return ln;
84}
85
94int writesf(const char *str, const char *mode, const char *fmt, ...)
95{
96 va_list ap;
97 FILE *fp;
98
99 va_start(ap, fmt);
100 fp = vfopenf(mode, fmt, ap);
101 va_end(ap);
102 if (!fp)
103 return -1;
104
105 fprintf(fp, "%s\n", str);
106 return fclose(fp);
107}
108
112int vreadllf(long long *value, const char *fmt, va_list ap)
113{
114 char line[0x100];
115
116 if (!vreadsnf(line, sizeof(line), fmt, ap))
117 return -1;
118
119 errno = 0;
120 *value = strtoll(line, NULL, 0);
121
122 return errno ? -1 : 0;
123}
124
133int readllf(long long *value, const char *fmt, ...)
134{
135 va_list ap;
136 int rc;
137
138 va_start(ap, fmt);
139 rc = vreadllf(value, fmt, ap);
140 va_end(ap);
141
142 return rc;
143}
144
145
154int readdf(int *value, const char *fmt, ...)
155{
156 long long tmp;
157 va_list ap;
158 int rc;
159
160 va_start(ap, fmt);
161 rc = vreadllf(&tmp, fmt, ap);
162 va_end(ap);
163
164 if (rc)
165 return rc;
166
167 if (tmp < INT_MIN || tmp > INT_MAX) {
168 errno = ERANGE;
169 return -1;
170 }
171
172 *value = tmp;
173 return 0;
174}
175
184int writellf(long long value, const char *mode, const char *fmt, ...)
185{
186 va_list ap;
187 FILE *fp;
188
189 va_start(ap, fmt);
190 fp = vfopenf(mode, fmt, ap);
191 va_end(ap);
192 if (!fp)
193 return -1;
194
195 fprintf(fp, "%lld\n", value);
196 return fclose(fp);
197}
198
199
208int writedf(int value, const char *mode, const char *fmt, ...)
209{
210 va_list ap;
211 FILE *fp;
212
213 va_start(ap, fmt);
214 fp = vfopenf(mode, fmt, ap);
215 va_end(ap);
216 if (!fp)
217 return -1;
218
219 fprintf(fp, "%d\n", value);
220 return fclose(fp);
221}
222
223
char * chomp(char *str)
Definition chomp.c:38
FILE * vfopenf(const char *mode, const char *fmt, va_list ap)
Definition fopenf.c:40
int writesf(const char *str, const char *mode, const char *fmt,...)
Definition procval.c:94
int readdf(int *value, const char *fmt,...)
Definition procval.c:154
int readllf(long long *value, const char *fmt,...)
Definition procval.c:133
char * readsnf(char *line, size_t len, const char *fmt,...)
Definition procval.c:74
int writellf(long long value, const char *mode, const char *fmt,...)
Definition procval.c:184
char * vreadsnf(char *line, size_t len, const char *fmt, va_list ap)
Definition procval.c:44
int writedf(int value, const char *mode, const char *fmt,...)
Definition procval.c:208
int vreadllf(long long *value, const char *fmt, va_list ap)
Definition procval.c:112