1d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan/* 2d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * dhcpcd - DHCP client daemon 3d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * Copyright (c) 2006-2009 Roy Marples <roy@marples.name> 4d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * All rights reserved 5d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan 6d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * Redistribution and use in source and binary forms, with or without 7d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * modification, are permitted provided that the following conditions 8d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * are met: 9d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * 1. Redistributions of source code must retain the above copyright 10d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * notice, this list of conditions and the following disclaimer. 11d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * 2. Redistributions in binary form must reproduce the above copyright 12d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * notice, this list of conditions and the following disclaimer in the 13d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * documentation and/or other materials provided with the distribution. 14d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * 15d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan * SUCH DAMAGE. 26d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan */ 27d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan 28d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan#include <errno.h> 29d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan#include <stdint.h> 30d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan#include <stdlib.h> 31d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan#include <stdio.h> 32d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan#include <string.h> 33d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan 34d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan#include "getline.h" 35d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan 36d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan/* Redefine a small buffer for our simple text config files */ 37d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan#undef BUFSIZ 38d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan#define BUFSIZ 128 39d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan 40d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tanssize_t 41d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tangetline(char ** __restrict buf, size_t * __restrict buflen, 42d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan FILE * __restrict fp) 43d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan{ 44d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan size_t bytes, newlen; 45d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan char *newbuf, *p; 46d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan 47d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan if (buf == NULL || buflen == NULL) { 48d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan errno = EINVAL; 49d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan return -1; 50d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan } 51d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan if (*buf == NULL) 52d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan *buflen = 0; 53d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan 54d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan bytes = 0; 55d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan do { 56d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan if (feof(fp)) 57d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan break; 58d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan if (*buf == NULL || bytes != 0 || *buflen < BUFSIZ) { 59d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan newlen = *buflen + BUFSIZ; 60d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan newbuf = realloc(*buf, newlen); 61d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan if (newbuf == NULL) 62d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan return -1; 63d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan *buf = newbuf; 64d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan *buflen = newlen; 65d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan } 66d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan p = *buf + bytes; 67d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan memset(p, 0, BUFSIZ); 68d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan if (fgets(p, BUFSIZ, fp) == NULL) 69d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan break; 70d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan bytes += strlen(p); 71d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan } while (bytes == 0 || *(*buf + (bytes - 1)) != '\n'); 72d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan if (bytes == 0) 73d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan return -1; 74d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan return bytes; 75d7ed851d6fc26c5e7db96971dbe6b44342b97727Samuel Tan} 76