blob: 5eecf9dccbdfb598aa00ca87a3d7df0c4620f976 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/*
lk_readnl.c - read a line from file into a buffer
version 1.0.0, April 25th, 2008
Copyright (c) 2008 Borut Razem
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Borut Razem
borut.razem@siol.net
*/
#include "lk_readnl.h"
/*******************************************************************************
lk_readnl
lk_readnl() reads in at most one less than size characters from stream and stores
them into the buffer pointed to by s. Reading stops after an EOF or a newline.
The newline character is not stored into the buffer. A '\0' is stored after the
last character in the buffer. All the characters between size and the newline or
EOF are skipped.
lk_readnl() return s on success, and NULL on error or when end of file occurs
while no characters have been read.
*******************************************************************************/
char *
lk_readnl (char *s, int size, FILE * stream)
{
static char eof_f = 0;
int c = '\0';
char *s_o;
char prev_c;
if (eof_f)
{
eof_f = 0;
return NULL;
}
s_o = s;
--size; /* for null terminator */
while (size > 0)
{
prev_c = c;
if ((c = getc (stream)) == '\n' || c == EOF)
break;
if (prev_c == '\r')
{
*s++ = prev_c;
if (--size <= 0)
break;
}
if (c != '\r')
{
*s++ = c;
--size;
}
}
*s = '\0';
while (c != '\n' && c != EOF)
c = getc (stream);
if (c == EOF)
{
if (s == s_o)
return NULL;
eof_f = 1;
}
return s_o;
}
|