Fri Jun 29 12:51:38 2012 UTC ()
handle realloc failure


(yamt)
diff -r1.50 -r1.51 src/bin/ls/print.c

cvs diff -r1.50 -r1.51 src/bin/ls/print.c (expand / switch to unified diff)

--- src/bin/ls/print.c 2011/03/15 22:53:41 1.50
+++ src/bin/ls/print.c 2012/06/29 12:51:38 1.51
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: print.c,v 1.50 2011/03/15 22:53:41 christos Exp $ */ 1/* $NetBSD: print.c,v 1.51 2012/06/29 12:51:38 yamt Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1989, 1993, 1994 4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Michael Fischbein. 8 * Michael Fischbein.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35#include <sys/cdefs.h> 35#include <sys/cdefs.h>
36#ifndef lint 36#ifndef lint
37#if 0 37#if 0
38static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94"; 38static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94";
39#else 39#else
40__RCSID("$NetBSD: print.c,v 1.50 2011/03/15 22:53:41 christos Exp $"); 40__RCSID("$NetBSD: print.c,v 1.51 2012/06/29 12:51:38 yamt Exp $");
41#endif 41#endif
42#endif /* not lint */ 42#endif /* not lint */
43 43
44#include <sys/param.h> 44#include <sys/param.h>
45#include <sys/stat.h> 45#include <sys/stat.h>
46 46
47#include <err.h> 47#include <err.h>
48#include <errno.h> 48#include <errno.h>
49#include <fts.h> 49#include <fts.h>
50#include <grp.h> 50#include <grp.h>
51#include <pwd.h> 51#include <pwd.h>
52#include <stdio.h> 52#include <stdio.h>
53#include <stdlib.h> 53#include <stdlib.h>
@@ -188,32 +188,36 @@ printcol(DISPLAY *dp) @@ -188,32 +188,36 @@ printcol(DISPLAY *dp)
188 188
189 colwidth += 1; 189 colwidth += 1;
190 190
191 if (termwidth < 2 * colwidth) { 191 if (termwidth < 2 * colwidth) {
192 printscol(dp); 192 printscol(dp);
193 return; 193 return;
194 } 194 }
195 195
196 /* 196 /*
197 * Have to do random access in the linked list -- build a table 197 * Have to do random access in the linked list -- build a table
198 * of pointers. 198 * of pointers.
199 */ 199 */
200 if (dp->entries > lastentries) { 200 if (dp->entries > lastentries) {
201 lastentries = dp->entries; 201 FTSENT **newarray;
202 if ((array = 202
203 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) { 203 newarray = realloc(array, dp->entries * sizeof(FTSENT *));
 204 if (newarray == NULL) {
204 warn(NULL); 205 warn(NULL);
205 printscol(dp); 206 printscol(dp);
 207 return;
206 } 208 }
 209 lastentries = dp->entries;
 210 array = newarray;
207 } 211 }
208 for (p = dp->list, num = 0; p; p = p->fts_link) 212 for (p = dp->list, num = 0; p; p = p->fts_link)
209 if (p->fts_number != NO_PRINT) 213 if (p->fts_number != NO_PRINT)
210 array[num++] = p; 214 array[num++] = p;
211 215
212 numcols = termwidth / colwidth; 216 numcols = termwidth / colwidth;
213 colwidth = termwidth / numcols; /* spread out if possible */ 217 colwidth = termwidth / numcols; /* spread out if possible */
214 numrows = num / numcols; 218 numrows = num / numcols;
215 if (num % numcols) 219 if (num % numcols)
216 ++numrows; 220 ++numrows;
217 221
218 printtotal(dp); /* "total: %u\n" */ 222 printtotal(dp); /* "total: %u\n" */
219 223