leafnode  1.12.0
About: Leafnode is a store & forward NNTP proxy for small (dialup) sites.
  Fossies Dox: leafnode-1.12.0.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

getaline.c
Go to the documentation of this file.
1/*
2 getaline - fetch a single line from a stdio stream, arbitrary length
3 Copyright (C) 2000 - 2010 Matthias Andree
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 As special permission, when linked against the leafnode package,
11 leafnode programs themselves need not be put under the GPL.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include "leafnode.h"
24
25#include <stdio.h>
26#include <sys/types.h>
27#include <syslog.h>
28#include <string.h>
29#include <sys/stat.h>
30#include "ln_log.h"
31
32static char *
33ygetaline(int mode /** 0: regular, 1: free memory */, FILE * f)
34{
35 static char *buf; /* buffer for line */
36 static size_t size; /* size of buffer */
37 ssize_t len; /* # of chars stored into buf before '\0' */
38 int have_logged = 0; /* variable to track if current input line was already logged */
39
40 if (mode) {
41 free(buf);
42 return NULL;
43 }
44
45 len = getline(&buf, &size, f);
46 if (len < 0) {
47 if (ferror(f)) {
48 if (debug)
49 syslog(LOG_DEBUG, "< (ERROR: %s)", strerror(errno));
50 } else {
51 if (debug > 1)
52 syslog(LOG_DEBUG, "< (EOF)");
53 }
54 return 0;
55 }
56 if (len && (buf[len - 1] == '\n')) { /* go back on top of the newline */
57 --len;
58 while (len && (buf[len - 1] == '\r')) /* also delete CR */
59 --len;
60 } else {
61 struct stat st;
62 int err = 0;
63 /* allow missing LF only on regular files */
64
65 if (fstat(fileno(f),&st) || !S_ISREG(st.st_mode)) err = 1;
66
67 if (err || debug)
69 "<%s (%s: no trailing LF)", buf, err ? "error" : "debug");
70 have_logged = 1;
71
72 if (err)
73 return NULL;
74 }
75
76 buf[len] = '\0'; /* unconditionally terminate string,
77 possibly overwriting newline */
78
79 if (debug > 1 && !have_logged)
80 syslog(LOG_DEBUG, "<%s", buf);
81 return buf;
82}
83
84char *getaline(FILE *f)
85{
86 return ygetaline(0, f);
87}
88
89void freegetaline(void)
90{
91 (void)ygetaline(1, NULL);
92}
int debug
Definition: applyfilter.c:30
char * getaline(FILE *f)
Definition: getaline.c:84
static char * ygetaline(int mode, FILE *f)
Definition: getaline.c:33
void freegetaline(void)
Definition: getaline.c:89
ssize_t getline(char **pto, size_t *size, FILE *stream)
Definition: getline.c:54
void ln_log(int sev, int ctx, const char *format,...)
Definition: ln_log.c:103
#define LNLOG_SERR
Definition: ln_log.h:13
#define LNLOG_CTOP
Definition: ln_log.h:22
#define LNLOG_SDEBUG
Definition: ln_log.h:17
#define len
Definition: mastring.c:31
mode_t mode
Definition: miscutil.c:126