Thu Jul 28 00:50:23 2011 UTC ()
eliminate alloca for portability
portable getpw{nam,uid}


(christos)
diff -r1.23 -r1.24 src/lib/libedit/filecomplete.c

cvs diff -r1.23 -r1.24 src/lib/libedit/filecomplete.c (expand / switch to unified diff)

--- src/lib/libedit/filecomplete.c 2010/12/06 00:05:38 1.23
+++ src/lib/libedit/filecomplete.c 2011/07/28 00:50:23 1.24
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: filecomplete.c,v 1.23 2010/12/06 00:05:38 dholland Exp $ */ 1/* $NetBSD: filecomplete.c,v 1.24 2011/07/28 00:50:23 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jaromir Dolecek. 8 * by Jaromir Dolecek.
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.
@@ -21,48 +21,45 @@ @@ -21,48 +21,45 @@
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include "config.h" 32#include "config.h"
33#if !defined(lint) && !defined(SCCSID) 33#if !defined(lint) && !defined(SCCSID)
34__RCSID("$NetBSD: filecomplete.c,v 1.23 2010/12/06 00:05:38 dholland Exp $"); 34__RCSID("$NetBSD: filecomplete.c,v 1.24 2011/07/28 00:50:23 christos Exp $");
35#endif /* not lint && not SCCSID */ 35#endif /* not lint && not SCCSID */
36 36
37#include <sys/types.h> 37#include <sys/types.h>
38#include <sys/stat.h> 38#include <sys/stat.h>
39#include <stdio.h> 39#include <stdio.h>
40#include <dirent.h> 40#include <dirent.h>
41#include <string.h> 41#include <string.h>
42#include <pwd.h> 42#include <pwd.h>
43#include <ctype.h> 43#include <ctype.h>
44#include <stdlib.h> 44#include <stdlib.h>
45#include <unistd.h> 45#include <unistd.h>
46#include <limits.h> 46#include <limits.h>
47#include <errno.h> 47#include <errno.h>
48#include <fcntl.h> 48#include <fcntl.h>
49#ifdef HAVE_VIS_H 49#ifdef HAVE_VIS_H
50#include <vis.h> 50#include <vis.h>
51#else 51#else
52#include "np/vis.h" 52#include "vis.h"
53#endif 
54#ifdef HAVE_ALLOCA_H 
55#include <alloca.h> 
56#endif 53#endif
57#include "el.h" 54#include "el.h"
58#include "fcns.h" /* for EL_NUM_FCNS */ 55#include "fcns.h" /* for EL_NUM_FCNS */
59#include "histedit.h" 56#include "histedit.h"
60#include "filecomplete.h" 57#include "filecomplete.h"
61 58
62static const Char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', 59static const Char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@',
63 '$', '>', '<', '=', ';', '|', '&', '{', '(', '\0' }; 60 '$', '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
64 61
65 62
66/********************************/ 63/********************************/
67/* completion functions */ 64/* completion functions */
68 65
@@ -88,31 +85,44 @@ fn_tilde_expand(const char *txt) @@ -88,31 +85,44 @@ fn_tilde_expand(const char *txt)
88 if (temp == NULL) { 85 if (temp == NULL) {
89 temp = strdup(txt + 1); 86 temp = strdup(txt + 1);
90 if (temp == NULL) 87 if (temp == NULL)
91 return NULL; 88 return NULL;
92 } else { 89 } else {
93 len = temp - txt + 1; /* text until string after slash */ 90 len = temp - txt + 1; /* text until string after slash */
94 temp = malloc(len); 91 temp = malloc(len);
95 if (temp == NULL) 92 if (temp == NULL)
96 return NULL; 93 return NULL;
97 (void)strncpy(temp, txt + 1, len - 2); 94 (void)strncpy(temp, txt + 1, len - 2);
98 temp[len - 2] = '\0'; 95 temp[len - 2] = '\0';
99 } 96 }
100 if (temp[0] == 0) { 97 if (temp[0] == 0) {
101 if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf), &pass) != 0) 98#ifdef HAVE_GETPW_R_POSIX
102 pass = NULL; 99 if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
 100 &pass) != 0)
 101 pass = NULL;
 102#elif HAVE_GETPW_R_DRAFT
 103 pass = getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf));
 104#else
 105 pass = getpwuid(getuid());
 106#endif
103 } else { 107 } else {
 108#ifdef HAVE_GETPW_R_POSIX
104 if (getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf), &pass) != 0) 109 if (getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf), &pass) != 0)
105 pass = NULL; 110 pass = NULL;
 111#elif HAVE_GETPW_R_DRAFT
 112 pass = getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf));
 113#else
 114 pass = getpwnam(temp);
 115#endif
106 } 116 }
107 free(temp); /* value no more needed */ 117 free(temp); /* value no more needed */
108 if (pass == NULL) 118 if (pass == NULL)
109 return (strdup(txt)); 119 return (strdup(txt));
110 120
111 /* update pointer txt to point at string immedially following */ 121 /* update pointer txt to point at string immedially following */
112 /* first slash */ 122 /* first slash */
113 txt += len; 123 txt += len;
114 124
115 temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1); 125 temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1);
116 if (temp == NULL) 126 if (temp == NULL)
117 return NULL; 127 return NULL;
118 (void)sprintf(temp, "%s/%s", pass->pw_dir, txt); 128 (void)sprintf(temp, "%s/%s", pass->pw_dir, txt);
@@ -338,27 +348,27 @@ _fn_qsort_string_compare(const void *i1, @@ -338,27 +348,27 @@ _fn_qsort_string_compare(const void *i1,
338 348
339/* 349/*
340 * Display list of strings in columnar format on readline's output stream. 350 * Display list of strings in columnar format on readline's output stream.
341 * 'matches' is list of strings, 'num' is number of strings in 'matches', 351 * 'matches' is list of strings, 'num' is number of strings in 'matches',
342 * 'width' is maximum length of string in 'matches'. 352 * 'width' is maximum length of string in 'matches'.
343 * 353 *
344 * matches[0] is not one of the match strings, but it is counted in 354 * matches[0] is not one of the match strings, but it is counted in
345 * num, so the strings are matches[1] *through* matches[num-1]. 355 * num, so the strings are matches[1] *through* matches[num-1].
346 */ 356 */
347void 357void
348fn_display_match_list (EditLine *el, char **matches, size_t num, size_t width) 358fn_display_match_list (EditLine *el, char **matches, size_t num, size_t width)
349{ 359{
350 size_t line, lines, col, cols, thisguy; 360 size_t line, lines, col, cols, thisguy;
351 int screenwidth = el->el_term.t_size.h; 361 int screenwidth = el->el_terminal.t_size.h;
352 362
353 /* Ignore matches[0]. Avoid 1-based array logic below. */ 363 /* Ignore matches[0]. Avoid 1-based array logic below. */
354 matches++; 364 matches++;
355 num--; 365 num--;
356 366
357 /* 367 /*
358 * Find out how many entries can be put on one line; count 368 * Find out how many entries can be put on one line; count
359 * with one space between strings the same way it's printed. 369 * with one space between strings the same way it's printed.
360 */ 370 */
361 cols = screenwidth / (width + 1); 371 cols = screenwidth / (width + 1);
362 if (cols == 0) 372 if (cols == 0)
363 cols = 1; 373 cols = 1;
364 374
@@ -422,31 +432,27 @@ fn_complete(EditLine *el, @@ -422,31 +432,27 @@ fn_complete(EditLine *el,
422 complet_func = fn_filename_completion_function; 432 complet_func = fn_filename_completion_function;
423 if (!app_func) 433 if (!app_func)
424 app_func = append_char_function; 434 app_func = append_char_function;
425 435
426 /* We now look backwards for the start of a filename/variable word */ 436 /* We now look backwards for the start of a filename/variable word */
427 li = FUN(el,line)(el); 437 li = FUN(el,line)(el);
428 ctemp = li->cursor; 438 ctemp = li->cursor;
429 while (ctemp > li->buffer 439 while (ctemp > li->buffer
430 && !Strchr(word_break, ctemp[-1]) 440 && !Strchr(word_break, ctemp[-1])
431 && (!special_prefixes || !Strchr(special_prefixes, ctemp[-1]) ) ) 441 && (!special_prefixes || !Strchr(special_prefixes, ctemp[-1]) ) )
432 ctemp--; 442 ctemp--;
433 443
434 len = li->cursor - ctemp; 444 len = li->cursor - ctemp;
435#if defined(__SSP__) || defined(__SSP_ALL__) 
436 temp = malloc(sizeof(*temp) * (len + 1)); 445 temp = malloc(sizeof(*temp) * (len + 1));
437#else 
438 temp = alloca(sizeof(*temp) * (len + 1)); 
439#endif 
440 (void)Strncpy(temp, ctemp, len); 446 (void)Strncpy(temp, ctemp, len);
441 temp[len] = '\0'; 447 temp[len] = '\0';
442 448
443 /* these can be used by function called in completion_matches() */ 449 /* these can be used by function called in completion_matches() */
444 /* or (*attempted_completion_function)() */ 450 /* or (*attempted_completion_function)() */
445 if (point != 0) 451 if (point != 0)
446 *point = (int)(li->cursor - li->buffer); 452 *point = (int)(li->cursor - li->buffer);
447 if (end != NULL) 453 if (end != NULL)
448 *end = (int)(li->lastchar - li->buffer); 454 *end = (int)(li->lastchar - li->buffer);
449 455
450 if (attempted_completion_function) { 456 if (attempted_completion_function) {
451 int cur_off = (int)(li->cursor - li->buffer); 457 int cur_off = (int)(li->cursor - li->buffer);
452 matches = (*attempted_completion_function) (ct_encode_string(temp, &el->el_scratch), 458 matches = (*attempted_completion_function) (ct_encode_string(temp, &el->el_scratch),
@@ -541,29 +547,27 @@ fn_complete(EditLine *el, @@ -541,29 +547,27 @@ fn_complete(EditLine *el,
541 } else { 547 } else {
542 /* lcd is not a valid object - further specification */ 548 /* lcd is not a valid object - further specification */
543 /* is needed */ 549 /* is needed */
544 el_beep(el); 550 el_beep(el);
545 retval = CC_NORM; 551 retval = CC_NORM;
546 } 552 }
547 553
548 /* free elements of array and the array itself */ 554 /* free elements of array and the array itself */
549 for (i = 0; matches[i]; i++) 555 for (i = 0; matches[i]; i++)
550 free(matches[i]); 556 free(matches[i]);
551 free(matches); 557 free(matches);
552 matches = NULL; 558 matches = NULL;
553 } 559 }
554#if defined(__SSP__) || defined(__SSP_ALL__) 
555 free(temp); 560 free(temp);
556#endif 
557 return retval; 561 return retval;
558} 562}
559 563
560/* 564/*
561 * el-compatible wrapper around rl_complete; needed for key binding 565 * el-compatible wrapper around rl_complete; needed for key binding
562 */ 566 */
563/* ARGSUSED */ 567/* ARGSUSED */
564unsigned char 568unsigned char
565_el_fn_complete(EditLine *el, int ch __attribute__((__unused__))) 569_el_fn_complete(EditLine *el, int ch __attribute__((__unused__)))
566{ 570{
567 return (unsigned char)fn_complete(el, NULL, NULL, 571 return (unsigned char)fn_complete(el, NULL, NULL,
568 break_chars, NULL, NULL, 100, 572 break_chars, NULL, NULL, 100,
569 NULL, NULL, NULL, NULL); 573 NULL, NULL, NULL, NULL);