2009-09-18 01:16:56 +00:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2020-08-31 14:09:36 -06:00
|
|
|
* Copyright (c) 2019-2020 Todd C. Miller <Todd.Miller@sudo.ws>
|
2009-09-18 01:16:56 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-10-26 08:39:09 -06:00
|
|
|
/*
|
|
|
|
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
|
|
|
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
|
|
*/
|
2018-10-21 08:46:05 -06:00
|
|
|
|
2009-09-18 01:16:56 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2019-04-08 10:37:30 -06:00
|
|
|
#ifndef HAVE_GETDELIM
|
2013-04-01 10:19:26 -04:00
|
|
|
|
2009-09-18 01:16:56 +00:00
|
|
|
#include <stdio.h>
|
2015-06-19 14:29:27 -06:00
|
|
|
#include <stdlib.h>
|
2020-05-18 07:59:24 -06:00
|
|
|
#include <string.h>
|
2009-09-18 01:16:56 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
2014-07-22 14:25:16 -06:00
|
|
|
#include "sudo_compat.h"
|
2009-09-18 13:18:56 +00:00
|
|
|
|
2009-09-18 12:23:01 +00:00
|
|
|
ssize_t
|
2019-04-08 10:37:30 -06:00
|
|
|
sudo_getdelim(char **buf, size_t *bufsize, int delim, FILE *fp)
|
2009-09-18 12:23:01 +00:00
|
|
|
{
|
2019-04-08 10:37:30 -06:00
|
|
|
char *cp, *ep;
|
|
|
|
int ch;
|
2009-09-18 12:23:01 +00:00
|
|
|
|
2019-04-08 10:37:30 -06:00
|
|
|
if (*buf == NULL || *bufsize == 0) {
|
|
|
|
char *tmp = realloc(*buf, LINE_MAX);
|
|
|
|
if (tmp == NULL)
|
2010-03-05 16:52:04 -05:00
|
|
|
return -1;
|
2019-04-08 10:37:30 -06:00
|
|
|
*buf = tmp;
|
|
|
|
*bufsize = LINE_MAX;
|
2009-09-18 01:16:56 +00:00
|
|
|
}
|
2019-04-08 10:37:30 -06:00
|
|
|
cp = *buf;
|
|
|
|
ep = cp + *bufsize;
|
2009-09-18 01:16:56 +00:00
|
|
|
|
2019-04-08 10:37:30 -06:00
|
|
|
do {
|
|
|
|
if (cp + 1 >= ep) {
|
2022-03-11 08:00:38 -07:00
|
|
|
char *newbuf = reallocarray(*buf, *bufsize, 2);
|
|
|
|
if (newbuf == NULL)
|
2019-04-08 10:37:30 -06:00
|
|
|
goto bad;
|
2021-02-17 16:46:02 -07:00
|
|
|
*bufsize *= 2;
|
2022-03-11 08:00:38 -07:00
|
|
|
cp = newbuf + (cp - *buf);
|
|
|
|
ep = newbuf + *bufsize;
|
|
|
|
*buf = newbuf;
|
2019-04-08 10:37:30 -06:00
|
|
|
}
|
|
|
|
if ((ch = getc(fp)) == EOF) {
|
|
|
|
if (feof(fp))
|
|
|
|
break;
|
|
|
|
goto bad;
|
2009-09-18 01:16:56 +00:00
|
|
|
}
|
2019-04-08 10:37:30 -06:00
|
|
|
*cp++ = ch;
|
|
|
|
} while (ch != delim);
|
|
|
|
|
|
|
|
/* getdelim(3) should never return a length of 0. */
|
|
|
|
if (cp != *buf) {
|
|
|
|
*cp = '\0';
|
|
|
|
return (ssize_t)(cp - *buf);
|
|
|
|
}
|
|
|
|
bad:
|
|
|
|
/* Error, push back what was read if possible. */
|
|
|
|
while (cp > *buf) {
|
|
|
|
if (ungetc(*cp--, fp) == EOF)
|
2009-09-18 01:16:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-04-08 10:37:30 -06:00
|
|
|
return -1;
|
2009-09-18 01:16:56 +00:00
|
|
|
}
|
2019-04-08 10:37:30 -06:00
|
|
|
#endif /* HAVE_GETDELIM */
|