1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2023 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/statvfs.h>
41 #include "if-options.h"
44 hwaddr_ntoa(const void *hwaddr, size_t hwlen, char *buf, size_t buflen)
46 const unsigned char *hp, *ep;
49 /* Allow a hwlen of 0 to be an empty string. */
50 if (buf == NULL || buflen == 0) {
55 if (hwlen * 3 > buflen) {
56 /* We should still terminate the string just in case. */
68 p += snprintf(p, 3, "%.2x", *hp++);
75 hwaddr_aton(uint8_t *buffer, const char *addr)
87 case '\n': /* long duid split on lines */
88 case ':': /* typical mac address */
93 /* Ensure that digits are hex */
94 if (isxdigit((unsigned char)c[0]) == 0 ||
95 isxdigit((unsigned char)c[1]) == 0)
100 /* We should have at least two entries 00:01 */
101 if (len == 0 && *p == '\0') {
106 *bp++ = (uint8_t)strtol(c, NULL, 16);
113 readfile(const char *file, void *data, size_t len)
118 fd = open(file, O_RDONLY);
121 bytes = read(fd, data, len);
123 if ((size_t)bytes == len) {
131 writefile(const char *file, mode_t mode, const void *data, size_t len)
136 fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, mode);
139 bytes = write(fd, data, len);
145 filemtime(const char *file, time_t *time)
149 if (stat(file, &st) == -1)
155 /* Handy routine to read very long lines in text files.
156 * This means we read the whole line and avoid any nasty buffer overflows.
157 * We strip leading space and avoid comment lines, making the code that calls
160 get_line(char ** __restrict buf, ssize_t * __restrict buflen)
169 c = memchr(*buf, '\n', (size_t)*buflen);
171 c = memchr(*buf, '\0', (size_t)*buflen);
181 for (; *p == ' ' || *p == '\t'; p++)
183 } while (*p == '\0' || *p == '\n' || *p == '#' || *p == ';');
185 /* Strip embedded comments unless in a quoted string or escaped */
187 for (c = p; *c != '\0'; c++) {
194 else if (*c == '#' && !quoted) {
209 if (statvfs("/", &vfs) == -1)
211 return vfs.f_flag & ST_LOCAL ? 1 : 0;