File: __getline.c

package info (click to toggle)
libc-sparc 5.3.12-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 18,664 kB
  • ctags: 53,237
  • sloc: ansic: 181,379; asm: 5,080; makefile: 3,340; lex: 521; sh: 439; yacc: 401; awk: 28
file content (135 lines) | stat: -rw-r--r-- 3,398 bytes parent folder | download | duplicates (2)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Copyright (C) 1993  Hongjiu Lu
This file is part of the Linux C Library.

The Linux C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

The Linux C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details. */

#ifdef __linux__
#include <ansidecl.h>
#include <stdlib.h>
#include <gnu/types.h>

#if !defined(ssize_t) && !defined(_SSIZE_T)
#define _SSIZE_T
#define ssize_t __ssize_t
#endif
  
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>

#ifndef MAX_CANON
#define MAX_CANON	256
#endif

/* Read up to (and including) a newline from STREAM into
 * *LINEPTR (and null-terminate it). *LINEPTR is a pointer
 * returned from malloc (or NULL), pointing to *N characters
 * of space.  It is realloc'd as necessary.  Returns the
 * number of characters read (not including the null
 * terminator), or -1 on error or EOF.
 */
#ifdef __linux__
ssize_t
DEFUN(__getline, (lineptr, n, stream),
      char **lineptr AND size_t *n AND FILE *stream)
#else       
__getline (char **lineptr, int *n, FILE *stream)
#endif
{
  char *ptr;	/* the latest section of the buffer */
  char *line;	/* Pointer to the buffer */
  int size;	/* Size of the buffer */
  int count;	/* # of chars in the buffer */

  if (lineptr == NULL || n == NULL)
  {
    errno = EINVAL;
    return -1;
  }

  if (ferror (stream))
    return -1;

  /* No buffer or too small, we allocate one. */
  if (*lineptr == NULL || *n < 2)
  {
    line = (*lineptr == NULL) ?
	malloc (MAX_CANON) : realloc (*lineptr, MAX_CANON);
    if (line == NULL) return -1;
    *lineptr = line;
    *n = MAX_CANON;
  }
  else
  {
    /* Assume the buffer size is `*n'. */
    line = *lineptr;
  }

  size = *n;
  count = 0;
  ptr = line;

  for (;;)
  {

    /* We call fgets () first. For the first time, our
     * buffer is *n. Next time, it is MAX_CANON chars
     * longer. Count in 1 char for the last '\0'. Our
     * additional buffer is MAX_CANON + 1 char long.
     */
    if (fgets (ptr, (count > 0 ? MAX_CANON + 1: size), stream)
	== NULL)
    {
      /* Check if we read in anything. */
      return (count) ? count : -1;
    }

    /* How many chars we have read in so far. */
    count += strlen (ptr);

    /* If the buffer is full and we still haven't seen the
     * newline, we need to expand the buffer and call
     * fgets () again. Caution: there may be no newline
     * at EOF. We will catch it when we call fgets () next
     * time.
     */
    if ((count >= size - 1) && line [count - 1] != '\n')
    {
      /* Expand the buffer by MAX_CANON chars. */
      size += MAX_CANON;
      line = realloc (line, size); 
      if (line == NULL) return -1;
      *lineptr = line;
      *n = size;

      /* ptr points to the newly expanded buffer. We
       * start at the previous '\0'.
       */
      ptr = &line [count];
    }
    else break;
  }

  return count;
}

#if 0
void
main ()
{
  char *buffer = NULL;
  int len, ret;

  while ((ret = __getline (&buffer, &len, stdin)) != -1)
    printf ("size: %d, len: %d: %s", len, ret, buffer);
}
#endif