Thu Jul 28 17:33:40 2011 UTC ()
handle systems without getpwent_r


(christos)
diff -r1.93 -r1.94 src/lib/libedit/readline.c

cvs diff -r1.93 -r1.94 src/lib/libedit/readline.c (expand / switch to unified diff)

--- src/lib/libedit/readline.c 2011/07/28 00:54:26 1.93
+++ src/lib/libedit/readline.c 2011/07/28 17:33:39 1.94
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: readline.c,v 1.93 2011/07/28 00:54:26 christos Exp $ */ 1/* $NetBSD: readline.c,v 1.94 2011/07/28 17:33:39 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,27 +21,27 @@ @@ -21,27 +21,27 @@
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: readline.c,v 1.93 2011/07/28 00:54:26 christos Exp $"); 34__RCSID("$NetBSD: readline.c,v 1.94 2011/07/28 17:33:39 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>
@@ -1663,53 +1663,62 @@ tilde_expand(char *name) @@ -1663,53 +1663,62 @@ tilde_expand(char *name)
1663 return fn_tilde_expand(name); 1663 return fn_tilde_expand(name);
1664} 1664}
1665 1665
1666char * 1666char *
1667filename_completion_function(const char *name, int state) 1667filename_completion_function(const char *name, int state)
1668{ 1668{
1669 return fn_filename_completion_function(name, state); 1669 return fn_filename_completion_function(name, state);
1670} 1670}
1671 1671
1672/* 1672/*
1673 * a completion generator for usernames; returns _first_ username 1673 * a completion generator for usernames; returns _first_ username
1674 * which starts with supplied text 1674 * which starts with supplied text
1675 * text contains a partial username preceded by random character 1675 * text contains a partial username preceded by random character
1676 * (usually '~'); state is ignored 1676 * (usually '~'); state resets search from start (??? should we do that anyway)
1677 * it's callers responsibility to free returned value 1677 * it's callers responsibility to free returned value
1678 */ 1678 */
1679char * 1679char *
1680username_completion_function(const char *text, int state) 1680username_completion_function(const char *text, int state)
1681{ 1681{
1682 struct passwd *pwd, pwres; 1682#if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
 1683 struct passwd pwres;
1683 char pwbuf[1024]; 1684 char pwbuf[1024];
 1685#endif
 1686 struct passwd *pass = NULL;
1684 1687
1685 if (text[0] == '\0') 1688 if (text[0] == '\0')
1686 return (NULL); 1689 return (NULL);
1687 1690
1688 if (*text == '~') 1691 if (*text == '~')
1689 text++; 1692 text++;
1690 1693
1691 if (state == 0) 1694 if (state == 0)
1692 setpwent(); 1695 setpwent();
1693 1696
1694 while (getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pwd) == 0 1697 while (
1695 && pwd != NULL && text[0] == pwd->pw_name[0] 1698#if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
1696 && strcmp(text, pwd->pw_name) == 0); 1699 getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pass) == 0 && pass != NULL
 1700#else
 1701 (pass = getpwent()) != NULL
 1702#endif
 1703 && text[0] == pass->pw_name[0]
 1704 && strcmp(text, pass->pw_name) == 0)
 1705 continue;
1697 1706
1698 if (pwd == NULL) { 1707 if (pass == NULL) {
1699 endpwent(); 1708 endpwent();
1700 return NULL; 1709 return NULL;
1701 } 1710 }
1702 return strdup(pwd->pw_name); 1711 return strdup(pass->pw_name);
1703} 1712}
1704 1713
1705 1714
1706/* 1715/*
1707 * el-compatible wrapper to send TSTP on ^Z 1716 * el-compatible wrapper to send TSTP on ^Z
1708 */ 1717 */
1709/* ARGSUSED */ 1718/* ARGSUSED */
1710static unsigned char 1719static unsigned char
1711_el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__))) 1720_el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__)))
1712{ 1721{
1713 (void)kill(0, SIGTSTP); 1722 (void)kill(0, SIGTSTP);
1714 return CC_NORM; 1723 return CC_NORM;
1715} 1724}