w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

line.c
Go to the documentation of this file.
1 /* line.c: return the next line from a file, or NULL.
2 
3  Copyright 1992, 1993, 1995, 1996, 2008, 2013, 2014, 2020 Karl Berry.
4  Copyright 1998, 1999, 2001, 2005 Olaf Weber.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this library; if not, see <http://www.gnu.org/licenses/>. */
18 
19 #include <kpathsea/config.h>
20 #include <kpathsea/line.h>
21 
22 #ifdef WIN32
23 #undef getc
24 #undef ungetc
25 #define getc win32_getc
26 #define ungetc win32_ungetc
27 #define FLOCKFILE(x)
28 #define FUNLOCKFILE(x)
29 
30 #else /* not WIN32 */
31 /* By POSIX, getc() has to be thread-safe, which means (un)locking on
32  every character read. It is much faster to lock the stream (once),
33  use getc_unlocked to read, and then unlock the stream. We need to be
34  thread-safe especially for the sake of MPlib.
35 
36  Perhaps we will be lucky enough to be able to do this
37  unconditionally, without checking in configure. We'll see. */
38 #undef getc
39 #define getc getc_unlocked
40 #define FLOCKFILE(x) flockfile(x)
41 #define FUNLOCKFILE(x) funlockfile(x)
42 #endif /* not WIN32 */
43 
44 /* Allocate in increments of this size. */
45 #define BLOCK_SIZE 75
46 
47 char *
49 {
50  int c;
51  unsigned limit = BLOCK_SIZE;
52  unsigned loc = 0;
53  char *line = xmalloc (limit);
54 
55  FLOCKFILE (f);
56 
57  while ((c = getc (f)) != EOF && c != '\n' && c != '\r') {
58  /* Silently drop null bytes. */
59  if (c == 0) {
60  continue;
61  }
62 
63  line[loc] = c;
64  loc++;
65 
66  /* By testing after the assignment, we guarantee that we'll always
67  have space for the null we append below. We know we always
68  have room for the first char, since we start with BLOCK_SIZE. */
69  if (loc == limit) {
70  limit += BLOCK_SIZE;
71  line = xrealloc (line, limit);
72  }
73  }
74 
75  /* If we read anything, return it, even a partial last-line-if-file
76  which is not properly terminated. */
77  if (loc == 0 && c == EOF) {
78  /* At end of file. */
79  free (line);
80  line = NULL;
81  } else {
82  /* Terminate the string. We can't represent nulls in the file,
83  but this doesn't matter. */
84  line[loc] = 0;
85  /* Absorb LF of a CRLF pair. */
86  if (c == '\r') {
87  c = getc (f);
88  if (c != '\n') {
89  ungetc (c, f);
90  }
91  }
92  }
93 
94  FUNLOCKFILE (f);
95 
96  return line;
97 }
#define loc
Definition: aptex-macros.h:65
#define free(a)
Definition: decNumber.cpp:310
mpz_t * f
Definition: gen-fib.c:34
#define c(n)
Definition: gpos-common.c:150
static void * xrealloc(char *oldp, unsigned size)
Definition: gsftopk.c:643
#define NULL
Definition: ftobjs.h:61
#define EOF
Definition: afmparse.c:59
#define xmalloc(size)
Definition: writet1.c:33
#define FLOCKFILE(x)
Definition: line.c:40
#define FUNLOCKFILE(x)
Definition: line.c:41
char * read_line(FILE *f)
Definition: line.c:48
#define BLOCK_SIZE
Definition: line.c:45
#define getc
Definition: line.c:39
char line[1024]
Definition: process_score.c:29
Definition: bdf.c:133
#define FILE
Definition: t1stdio.h:34
#define ungetc(c, f)
Definition: t1stdio.h:106
#define limit(x)
Definition: yuvsplittoppm.c:26