250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber/*
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * Copyright (c) 2006 SPARTA, Inc.
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * All rights reserved.
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber *
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * This software was developed by SPARTA ISSO under SPAWAR contract
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * N66001-04-C-6019 ("SEFOS").
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber *
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * Redistribution and use in source and binary forms, with or without
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * modification, are permitted provided that the following conditions
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * are met:
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * 1. Redistributions of source code must retain the above copyright
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * notice, this list of conditions and the following disclaimer.
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * 2. Redistributions in binary form must reproduce the above copyright
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * notice, this list of conditions and the following disclaimer in the
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * documentation and/or other materials provided with the distribution.
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber *
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber * SUCH DAMAGE.
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber */
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber#include <sys/types.h>
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber#include <stdio.h>
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber#include <stdlib.h>
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber#include <string.h>
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber/*
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber * Emulate glibc getline() via BSD fgetln().
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber * Note that outsize is not changed unless memory is allocated.
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber */
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graberssize_t
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Grabergetline(char **outbuf, size_t *outsize, FILE *fp)
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber{
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber size_t len;
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber char *buf;
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber buf = fgetln(fp, &len);
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber if (buf == NULL)
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber return (-1);
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber /* Assumes realloc() accepts NULL for ptr (C99) */
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber if (*outbuf == NULL || *outsize < len + 1) {
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber void *tmp = realloc(*outbuf, len + 1);
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber if (tmp == NULL)
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber return (-1);
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber *outbuf = tmp;
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber *outsize = len + 1;
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber }
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber memcpy(*outbuf, buf, len);
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber (*outbuf)[len] = '\0';
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber return (len);
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber}