]>
git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-util.c
2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011-2012 ProFUSION embedded systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "libkmod-private.h"
35 * Read one logical line from a configuration file.
37 * Line endings may be escaped with backslashes, to form one logical line from
38 * several physical lines. No end of line character(s) are included in the
41 * If linenum is not NULL, it is incremented by the number of physical lines
42 * which have been read.
44 char *getline_wrapped(FILE *fp
, unsigned int *linenum
)
48 char *buf
= malloc(size
);
54 int ch
= getc_unlocked(fp
);
62 /* else fall through */
68 buf
= realloc(buf
, size
+ 1);
73 ch
= getc_unlocked(fp
);
80 /* else fall through */
87 buf
= realloc(buf
, size
);
93 inline int alias_normalize(const char *alias
, char buf
[PATH_MAX
], size_t *len
)
97 for (s
= 0; s
< PATH_MAX
- 1; s
++) {
98 const char c
= alias
[s
];
106 while (alias
[s
] != ']' && alias
[s
] != '\0') {
132 inline char *modname_normalize(const char *modname
, char buf
[PATH_MAX
],
137 for (s
= 0; s
< PATH_MAX
- 1; s
++) {
138 const char c
= modname
[s
];
141 else if (c
== '\0' || c
== '.')
155 char *path_to_modname(const char *path
, char buf
[PATH_MAX
], size_t *len
)
159 modname
= basename(path
);
160 if (modname
== NULL
|| modname
[0] == '\0')
163 return modname_normalize(modname
, buf
, len
);
166 inline void *memdup(const void *p
, size_t n
)
173 return memcpy(r
, p
, n
);
176 ssize_t
read_str_safe(int fd
, char *buf
, size_t buflen
)
178 size_t todo
= buflen
- 1;
182 ssize_t r
= read(fd
, buf
+ done
, todo
);
190 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
202 ssize_t
write_str_safe(int fd
, const char *buf
, size_t buflen
)
204 size_t todo
= buflen
;
208 ssize_t r
= write(fd
, buf
+ done
, todo
);
216 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
227 int read_str_long(int fd
, long *value
, int base
)
234 err
= read_str_safe(fd
, buf
, sizeof(buf
));
238 v
= strtol(buf
, &end
, base
);
239 if (end
== buf
|| !isspace(*end
))
246 int read_str_ulong(int fd
, unsigned long *value
, int base
)
253 err
= read_str_safe(fd
, buf
, sizeof(buf
));
257 v
= strtoul(buf
, &end
, base
);
258 if (end
== buf
|| !isspace(*end
))
264 char *strchr_replace(char *s
, int c
, char r
)
268 for (p
= s
; *p
!= '\0'; p
++)
275 bool path_is_absolute(const char *p
)
282 char *path_make_absolute_cwd(const char *p
)
288 if (path_is_absolute(p
))
291 cwd
= get_current_dir_name();
296 cwdlen
= strlen(cwd
);
298 /* cwd + '/' + p + '\0' */
299 r
= realloc(cwd
, cwdlen
+ 1 + plen
+ 1);
306 memcpy(&r
[cwdlen
+ 1], p
, plen
+ 1);
311 #define USEC_PER_SEC 1000000ULL
312 #define NSEC_PER_USEC 1000ULL
313 unsigned long long ts_usec(const struct timespec
*ts
)
315 return (unsigned long long) ts
->tv_sec
* USEC_PER_SEC
+
316 (unsigned long long) ts
->tv_nsec
/ NSEC_PER_USEC
;
319 unsigned long long stat_mstamp(const struct stat
*st
)
321 #ifdef HAVE_STRUCT_STAT_ST_MTIM
322 return ts_usec(&st
->st_mtim
);
324 return (unsigned long long) st
->st_mtime
;