add fgetline(3) implementation

This commit is contained in:
Ariadne Conill 2020-07-18 18:13:54 -06:00
parent 32560de5ca
commit 26106dff75
4 changed files with 151 additions and 1 deletions

View file

@ -6,7 +6,8 @@ LIBIFUPDOWN_SRC = \
libifupdown/list.c \ libifupdown/list.c \
libifupdown/dict.c \ libifupdown/dict.c \
libifupdown/interface.c \ libifupdown/interface.c \
libifupdown/interface-file.c libifupdown/interface-file.c \
libifupdown/fgetline.c
LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o} LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o}
LIBIFUPDOWN_LIB = libifupdown.a LIBIFUPDOWN_LIB = libifupdown.a

123
libifupdown/fgetline.c Normal file
View file

@ -0,0 +1,123 @@
/*
* libifupdown/fgetline.c
* Purpose: portable fgetline(3)
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
*
* Permission to use, copy, modify, and/or 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.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include "libifupdown/fgetline.h"
char *
lif_fgetline(char *line, size_t size, FILE *stream)
{
char *s = line;
char *end = line + size - 2;
bool quoted = false;
int c = '\0', c2;
if (s == NULL)
return NULL;
while (s < end && (c = getc(stream)) != EOF)
{
if (c == '\\' && !quoted)
{
quoted = true;
continue;
}
else if (c == '#')
{
if (!quoted) {
/* Skip the rest of the line */
do {
c = getc(stream);
} while (c != '\n' && c != EOF);
*s++ = c;
break;
}
quoted = false;
continue;
}
else if (c == '\n')
{
if (quoted)
{
/* Trim spaces */
do {
c2 = getc(stream);
} while (c2 == '\t' || c2 == ' ');
ungetc(c2, stream);
quoted = false;
continue;
}
else
{
*s++ = c;
}
break;
}
else if (c == '\r')
{
*s++ = '\n';
if ((c2 = getc(stream)) == '\n')
{
if (quoted)
{
quoted = false;
continue;
}
break;
}
ungetc(c2, stream);
if (quoted)
{
quoted = false;
continue;
}
break;
}
else
{
if (quoted) {
*s++ = '\\';
quoted = false;
}
*s++ = c;
}
}
if (c == EOF && (s == line || ferror(stream)))
return NULL;
*s = '\0';
/* Remove newline character. */
if (s > line && *(--s) == '\n') {
*s = '\0';
if (s > line && *(--s) == '\r')
*s = '\0';
}
return line;
}

25
libifupdown/fgetline.h Normal file
View file

@ -0,0 +1,25 @@
/*
* libifupdown/fgetline.h
* Purpose: portable fgetline(3)
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
*
* Permission to use, copy, modify, and/or 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.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#ifndef LIBIFUPDOWN_FGETLINE_H__GUARD
#define LIBIFUPDOWN_FGETLINE_H__GUARD
extern char *lif_fgetline(char *line, size_t size, FILE *stream);
#endif

View file

@ -20,5 +20,6 @@
#include "libifupdown/dict.h" #include "libifupdown/dict.h"
#include "libifupdown/interface.h" #include "libifupdown/interface.h"
#include "libifupdown/interface-file.h" #include "libifupdown/interface-file.h"
#include "libifupdown/fgetline.h"
#endif #endif