Path: blob/main/contrib/libfido2/openbsd-compat/bsd-getline.c
39534 views
/* $NetBSD: getline.c,v 1.1.1.6 2015/01/02 20:34:27 christos Exp $ */12/* NetBSD: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp */34/*-5* Copyright (c) 2011 The NetBSD Foundation, Inc.6* All rights reserved.7*8* This code is derived from software contributed to The NetBSD Foundation9* by Christos Zoulas.10*11* Redistribution and use in source and binary forms, with or without12* modification, are permitted provided that the following conditions13* are met:14* 1. Redistributions of source code must retain the above copyright15* notice, this list of conditions and the following disclaimer.16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19*20* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS21* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED22* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR23* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS24* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE30* POSSIBILITY OF SUCH DAMAGE.31*/3233/* NETBSD ORIGINAL: external/bsd/file/dist/src/getline.c */3435#include "openbsd-compat.h"3637#if 038#include "file.h"39#endif4041#if !HAVE_GETLINE42#include <stdlib.h>43#include <stdio.h>44#ifdef HAVE_UNISTD_H45#include <unistd.h>46#endif47#include <errno.h>48#include <string.h>4950static ssize_t51getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)52{53char *ptr, *eptr;545556if (*buf == NULL || *bufsiz == 0) {57if ((*buf = malloc(BUFSIZ)) == NULL)58return -1;59*bufsiz = BUFSIZ;60}6162for (ptr = *buf, eptr = *buf + *bufsiz;;) {63int c = fgetc(fp);64if (c == -1) {65if (feof(fp)) {66ssize_t diff = (ssize_t)(ptr - *buf);67if (diff != 0) {68*ptr = '\0';69return diff;70}71}72return -1;73}74*ptr++ = (char)c;75if (c == delimiter) {76*ptr = '\0';77return ptr - *buf;78}79if (ptr + 2 >= eptr) {80char *nbuf;81size_t nbufsiz = *bufsiz * 2;82ssize_t d = ptr - *buf;83if ((nbuf = realloc(*buf, nbufsiz)) == NULL)84return -1;85*buf = nbuf;86*bufsiz = nbufsiz;87eptr = nbuf + nbufsiz;88ptr = nbuf + d;89}90}91}9293ssize_t94getline(char **buf, size_t *bufsiz, FILE *fp)95{96return getdelim(buf, bufsiz, '\n', fp);97}9899#endif100101#ifdef TEST102int103main(int argc, char *argv[])104{105char *p = NULL;106ssize_t len;107size_t n = 0;108109while ((len = getline(&p, &n, stdin)) != -1)110(void)printf("%" SIZE_T_FORMAT "d %s", len, p);111free(p);112return 0;113}114#endif115116117