OpenCoverage

savedir.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/gnulib/lib/savedir.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* savedir.c -- save the list of files in a directory in a string-
2-
3 Copyright (C) 1990, 1997-2001, 2003-2006, 2009-2018 Free Software-
4 Foundation, Inc.-
5-
6 This program is free software: you can redistribute it and/or modify-
7 it under the terms of the GNU General Public License as published by-
8 the Free Software Foundation; either version 3 of the License, or-
9 (at your option) any later version.-
10-
11 This program 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-
14 GNU General Public License for more details.-
15-
16 You should have received a copy of the GNU General Public License-
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
18-
19/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */-
20-
21#include <config.h>-
22-
23#include "savedir.h"-
24-
25#include <sys/types.h>-
26-
27#include <errno.h>-
28-
29#include "dirent--.h"-
30#ifndef _D_EXACT_NAMLEN-
31# define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)-
32#endif-
33-
34#include <stddef.h>-
35#include <stdlib.h>-
36#include <string.h>-
37-
38#include "xalloc.h"-
39-
40typedef struct-
41{-
42 char *name;-
43#if D_INO_IN_DIRENT-
44 ino_t ino;-
45#endif-
46} direntry_t;-
47-
48/* Compare the names of two directory entries */-
49-
50static int-
51direntry_cmp_name (void const *a, void const *b)-
52{-
53 direntry_t const *dea = a;-
54 direntry_t const *deb = b;-
55-
56 return strcmp (dea->name, deb->name);
never executed: return __extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p ( dea->name ) && __builtin_constant_p ( deb->name ) && (__s1_len = __builtin_strlen ( dea->name ), __s2_len = __builtin_strlen ( deb->name ), (!((size_t)(const void *)(( dea->name ) ...nst unsigned char *) (const char *) ( deb->name ))[2] - __s2[2]); if (__s2_len > 2 && __result == 0) __result = (((const unsigned char *) (const char *) ( deb->name ))[3] - __s2[3]); } } __result; }))) : __builtin_strcmp ( dea->name , deb->name )))); }) ;
never executed: __result = (((const unsigned char *) (const char *) ( dea->name ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( deb->name ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
57}-
58-
59#if D_INO_IN_DIRENT-
60/* Compare the inode numbers of two directory entries */-
61-
62static int-
63direntry_cmp_inode (void const *a, void const *b)-
64{-
65 direntry_t const *dea = a;-
66 direntry_t const *deb = b;-
67-
68 return dea->ino < deb->ino ? -1 : dea->ino > deb->ino;
executed 782804 times by 2 tests: return dea->ino < deb->ino ? -1 : dea->ino > deb->ino;
Executed by:
  • cp
  • mv
782804
69}-
70#endif-
71-
72typedef int (*comparison_function) (void const *, void const *);-
73-
74static comparison_function const comparison_function_table[] =-
75 {-
76 0,-
77 direntry_cmp_name-
78#if D_INO_IN_DIRENT-
79 , direntry_cmp_inode-
80#endif-
81 };-
82-
83/* Return a freshly allocated string containing the file names-
84 in directory DIRP, separated by '\0' characters;-
85 the end is marked by two '\0' characters in a row.-
86 Returned values are sorted according to OPTION.-
87 Return NULL (setting errno) if DIRP cannot be read.-
88 If DIRP is NULL, return NULL without affecting errno. */-
89-
90char *-
91streamsavedir (DIR *dirp, enum savedir_option option)-
92{-
93 char *name_space = NULL;-
94 size_t allocated = 0;-
95 direntry_t *entries = NULL;-
96 size_t entries_allocated = 0;-
97 size_t entries_used = 0;-
98 size_t used = 0;-
99 int readdir_errno;-
100 comparison_function cmp = comparison_function_table[option];-
101-
102 if (dirp == NULL)
dirp == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 33971 times by 2 tests
Evaluated by:
  • cp
  • mv
0-33971
103 return NULL;
never executed: return ((void *)0) ;
0
104-
105 for (;;)-
106 {-
107 struct dirent const *dp;-
108 char const *entry;-
109-
110 errno = 0;-
111 dp = readdir (dirp);-
112 if (! dp)
! dpDescription
TRUEevaluated 33971 times by 2 tests
Evaluated by:
  • cp
  • mv
FALSEevaluated 131902 times by 2 tests
Evaluated by:
  • cp
  • mv
33971-131902
113 break;
executed 33971 times by 2 tests: break;
Executed by:
  • cp
  • mv
33971
114-
115 /* Skip "", ".", and "..". "" is returned by at least one buggy-
116 implementation: Solaris 2.4 readdir on NFS file systems. */-
117 entry = dp->d_name;-
118 if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
entry[entry[0]...1 : 2] != '\0'Description
TRUEevaluated 63960 times by 2 tests
Evaluated by:
  • cp
  • mv
FALSEevaluated 67942 times by 2 tests
Evaluated by:
  • cp
  • mv
63960-67942
119 {-
120 size_t entry_size = _D_EXACT_NAMLEN (dp) + 1;-
121 if (cmp)
cmpDescription
TRUEevaluated 63960 times by 2 tests
Evaluated by:
  • cp
  • mv
FALSEnever evaluated
0-63960
122 {-
123 if (entries_allocated == entries_used)
entries_alloca...= entries_usedDescription
TRUEevaluated 4003 times by 2 tests
Evaluated by:
  • cp
  • mv
FALSEevaluated 59957 times by 2 tests
Evaluated by:
  • cp
  • mv
4003-59957
124 {-
125 size_t n = entries_allocated;-
126 entries = x2nrealloc (entries, &n, sizeof *entries);-
127 entries_allocated = n;-
128 }
executed 4003 times by 2 tests: end of block
Executed by:
  • cp
  • mv
4003
129 entries[entries_used].name = xstrdup (entry);-
130#if D_INO_IN_DIRENT-
131 entries[entries_used].ino = dp->d_ino;-
132#endif-
133 entries_used++;-
134 }
executed 63960 times by 2 tests: end of block
Executed by:
  • cp
  • mv
63960
135 else-
136 {-
137 if (allocated - used <= entry_size)
allocated - used <= entry_sizeDescription
TRUEnever evaluated
FALSEnever evaluated
0
138 {-
139 size_t n = used + entry_size;-
140 if (n < used)
n < usedDescription
TRUEnever evaluated
FALSEnever evaluated
0
141 xalloc_die ();
never executed: xalloc_die ();
0
142 name_space = x2nrealloc (name_space, &n, 1);-
143 allocated = n;-
144 }
never executed: end of block
0
145 memcpy (name_space + used, entry, entry_size);-
146 }
never executed: end of block
0
147 used += entry_size;-
148 }
executed 63960 times by 2 tests: end of block
Executed by:
  • cp
  • mv
63960
149 }
executed 131902 times by 2 tests: end of block
Executed by:
  • cp
  • mv
131902
150-
151 readdir_errno = errno;-
152 if (readdir_errno != 0)
readdir_errno != 0Description
TRUEnever evaluated
FALSEevaluated 33971 times by 2 tests
Evaluated by:
  • cp
  • mv
0-33971
153 {-
154 free (entries);-
155 free (name_space);-
156 errno = readdir_errno;-
157 return NULL;
never executed: return ((void *)0) ;
0
158 }-
159-
160 if (cmp)
cmpDescription
TRUEevaluated 33971 times by 2 tests
Evaluated by:
  • cp
  • mv
FALSEnever evaluated
0-33971
161 {-
162 size_t i;-
163-
164 if (entries_used)
entries_usedDescription
TRUEevaluated 3946 times by 2 tests
Evaluated by:
  • cp
  • mv
FALSEevaluated 30025 times by 2 tests
Evaluated by:
  • cp
  • mv
3946-30025
165 qsort (entries, entries_used, sizeof *entries, cmp);
executed 3946 times by 2 tests: qsort (entries, entries_used, sizeof *entries, cmp);
Executed by:
  • cp
  • mv
3946
166 name_space = xmalloc (used + 1);-
167 used = 0;-
168 for (i = 0; i < entries_used; i++)
i < entries_usedDescription
TRUEevaluated 63960 times by 2 tests
Evaluated by:
  • cp
  • mv
FALSEevaluated 33971 times by 2 tests
Evaluated by:
  • cp
  • mv
33971-63960
169 {-
170 char *dest = name_space + used;-
171 used += stpcpy (dest, entries[i].name) - dest + 1;-
172 free (entries[i].name);-
173 }
executed 63960 times by 2 tests: end of block
Executed by:
  • cp
  • mv
63960
174 free (entries);-
175 }
executed 33971 times by 2 tests: end of block
Executed by:
  • cp
  • mv
33971
176 else if (used == allocated)
used == allocatedDescription
TRUEnever evaluated
FALSEnever evaluated
0
177 name_space = xrealloc (name_space, used + 1);
never executed: name_space = xrealloc (name_space, used + 1);
0
178-
179 name_space[used] = '\0';-
180 return name_space;
executed 33971 times by 2 tests: return name_space;
Executed by:
  • cp
  • mv
33971
181}-
182-
183/* Return a freshly allocated string containing the file names-
184 in directory DIR, separated by '\0' characters;-
185 the end is marked by two '\0' characters in a row.-
186 Return NULL (setting errno) if DIR cannot be opened, read, or closed. */-
187-
188char *-
189savedir (char const *dir, enum savedir_option option)-
190{-
191 DIR *dirp = opendir (dir);-
192 if (! dirp)
! dirpDescription
TRUEnever evaluated
FALSEevaluated 33971 times by 2 tests
Evaluated by:
  • cp
  • mv
0-33971
193 return NULL;
never executed: return ((void *)0) ;
0
194 else-
195 {-
196 char *name_space = streamsavedir (dirp, option);-
197 if (closedir (dirp) != 0)
closedir (dirp) != 0Description
TRUEnever evaluated
FALSEevaluated 33971 times by 2 tests
Evaluated by:
  • cp
  • mv
0-33971
198 {-
199 int closedir_errno = errno;-
200 free (name_space);-
201 errno = closedir_errno;-
202 return NULL;
never executed: return ((void *)0) ;
0
203 }-
204 return name_space;
executed 33971 times by 2 tests: return name_space;
Executed by:
  • cp
  • mv
33971
205 }-
206}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2