1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2021 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 if (buf == NULL || hwlen == 0)
52 if (hwlen * 3 > buflen) {
64 p += snprintf(p, 3, "%.2x", *hp++);
71 hwaddr_aton(uint8_t *buffer, const char *addr)
83 case '\n': /* long duid split on lines */
84 case ':': /* typical mac address */
89 /* Ensure that digits are hex */
90 if (isxdigit((unsigned char)c[0]) == 0 ||
91 isxdigit((unsigned char)c[1]) == 0)
96 /* We should have at least two entries 00:01 */
97 if (len == 0 && *p == '\0') {
102 *bp++ = (uint8_t)strtol(c, NULL, 16);
109 readfile(const char *file, void *data, size_t len)
114 fd = open(file, O_RDONLY);
117 bytes = read(fd, data, len);
119 if ((size_t)bytes == len) {
127 writefile(const char *file, mode_t mode, const void *data, size_t len)
132 fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, mode);
135 bytes = write(fd, data, len);
141 filemtime(const char *file, time_t *time)
145 if (stat(file, &st) == -1)
151 /* Handy routine to read very long lines in text files.
152 * This means we read the whole line and avoid any nasty buffer overflows.
153 * We strip leading space and avoid comment lines, making the code that calls
156 get_line(char ** __restrict buf, ssize_t * __restrict buflen)
165 c = memchr(*buf, '\n', (size_t)*buflen);
167 c = memchr(*buf, '\0', (size_t)*buflen);
177 for (; *p == ' ' || *p == '\t'; p++)
179 } while (*p == '\0' || *p == '\n' || *p == '#' || *p == ';');
181 /* Strip embedded comments unless in a quoted string or escaped */
183 for (c = p; *c != '\0'; c++) {
190 else if (*c == '#' && !quoted) {
205 if (statvfs("/", &vfs) == -1)
207 return vfs.f_flag & ST_LOCAL ? 1 : 0;