OpenCoverage

zgetline.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/lib/sh/zgetline.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* zgetline - read a line of input from a specified file descriptor and return-
2 a pointer to a newly-allocated buffer containing the data. */-
3-
4/* Copyright (C) 2008,2009 Free Software Foundation, Inc.-
5-
6 This file is part of GNU Bash, the Bourne Again SHell.-
7-
8 Bash is free software: you can redistribute it and/or modify-
9 it under the terms of the GNU General Public License as published by-
10 the Free Software Foundation, either version 3 of the License, or-
11 (at your option) any later version.-
12-
13 Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.-
20*/-
21-
22#include <config.h>-
23-
24#include <sys/types.h>-
25-
26#if defined (HAVE_UNISTD_H)-
27# include <unistd.h>-
28#endif-
29-
30#include <errno.h>-
31#include "xmalloc.h"-
32-
33#if !defined (errno)-
34extern int errno;-
35#endif-
36-
37extern ssize_t zread __P((int, char *, size_t));-
38extern ssize_t zreadc __P((int, char *));-
39extern ssize_t zreadintr __P((int, char *, size_t));-
40extern ssize_t zreadcintr __P((int, char *));-
41-
42typedef ssize_t breadfunc_t __P((int, char *, size_t));-
43typedef ssize_t creadfunc_t __P((int, char *));-
44-
45/* Initial memory allocation for automatic growing buffer in zreadlinec */-
46#define GET_LINE_INITIAL_ALLOCATION 16-
47-
48/* Derived from GNU libc's getline.-
49 The behavior is almost the same as getline. See man getline.-
50 The differences are-
51 (1) using file descriptor instead of FILE *;-
52 (2) the order of arguments: the file descriptor comes first;-
53 (3) the addition of a fourth argument, DELIM; sets the delimiter to-
54 be something other than newline if desired. If setting DELIM,-
55 the next argument should be 1; and-
56 (4) the addition of a fifth argument, UNBUFFERED_READ; this argument-
57 controls whether get_line uses buffering or not to get a byte data-
58 from FD. get_line uses zreadc if UNBUFFERED_READ is zero; and-
59 uses zread if UNBUFFERED_READ is non-zero.-
60-
61 Returns number of bytes read or -1 on error. */-
62-
63ssize_t-
64zgetline (fd, lineptr, n, delim, unbuffered_read)-
65 int fd;-
66 char **lineptr;-
67 size_t *n;-
68 int delim;-
69 int unbuffered_read;-
70{-
71 int nr, retval;-
72 char *line, c;-
73-
74 if (lineptr == 0 || n == 0 || (*lineptr == 0 && *n != 0))
lineptr == 0Description
TRUEnever evaluated
FALSEevaluated 161 times by 1 test
Evaluated by:
  • Self test
n == 0Description
TRUEnever evaluated
FALSEevaluated 161 times by 1 test
Evaluated by:
  • Self test
*lineptr == 0Description
TRUEevaluated 23 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 138 times by 1 test
Evaluated by:
  • Self test
*n != 0Description
TRUEnever evaluated
FALSEevaluated 23 times by 1 test
Evaluated by:
  • Self test
0-161
75 return -1;
never executed: return -1;
0
76-
77 nr = 0;-
78 line = *lineptr;-
79 -
80 while (1)-
81 {-
82 retval = unbuffered_read ? zread (fd, &c, 1) : zreadc(fd, &c);
unbuffered_readDescription
TRUEevaluated 24 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2509 times by 1 test
Evaluated by:
  • Self test
24-2509
83-
84 if (retval <= 0)
retval <= 0Description
TRUEevaluated 27 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2506 times by 1 test
Evaluated by:
  • Self test
27-2506
85 {-
86 if (line && nr > 0)
lineDescription
TRUEevaluated 16 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 11 times by 1 test
Evaluated by:
  • Self test
nr > 0Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
7-16
87 line[nr] = '\0';
executed 7 times by 1 test: line[nr] = '\0';
Executed by:
  • Self test
7
88 break;
executed 27 times by 1 test: break;
Executed by:
  • Self test
27
89 }-
90-
91 if (nr + 2 >= *n)
nr + 2 >= *nDescription
TRUEevaluated 20 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2486 times by 1 test
Evaluated by:
  • Self test
20-2486
92 {-
93 size_t new_size;-
94-
95 new_size = (*n == 0) ? GET_LINE_INITIAL_ALLOCATION : *n * 2;
(*n == 0)Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 8 times by 1 test
Evaluated by:
  • Self test
8-12
96 line = (*n >= new_size) ? NULL : xrealloc (*lineptr, new_size);
(*n >= new_size)Description
TRUEnever evaluated
FALSEevaluated 20 times by 1 test
Evaluated by:
  • Self test
0-20
97-
98 if (line)
lineDescription
TRUEevaluated 20 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-20
99 {-
100 *lineptr = line;-
101 *n = new_size;-
102 }
executed 20 times by 1 test: end of block
Executed by:
  • Self test
20
103 else-
104 {-
105 if (*n > 0)
*n > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
106 {-
107 (*lineptr)[*n - 1] = '\0';-
108 nr = *n - 2;-
109 }
never executed: end of block
0
110 break;
never executed: break;
0
111 }-
112 }-
113-
114 line[nr] = c;-
115 nr++;-
116-
117 if (c == delim)
c == delimDescription
TRUEevaluated 134 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2372 times by 1 test
Evaluated by:
  • Self test
134-2372
118 {-
119 line[nr] = '\0';-
120 break;
executed 134 times by 1 test: break;
Executed by:
  • Self test
134
121 }-
122 }
executed 2372 times by 1 test: end of block
Executed by:
  • Self test
2372
123-
124 return nr - 1;
executed 161 times by 1 test: return nr - 1;
Executed by:
  • Self test
161
125}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2