Commit 48442c8f 48442c8f89b9de078fe91ac9d0a7495bcb7882f1 by Sergey Poznyakoff

Updated by gnulib-sync

1 parent 91cfb588
1 /* Declaration for error-reporting function 1 /* Declaration for error-reporting function
2 Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc. 2 Copyright (C) 1995, 1996, 1997, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
3 4
4 5 This program is free software; you can redistribute it and/or modify
5 NOTE: The canonical source of this file is maintained with the GNU C Library. 6 it under the terms of the GNU General Public License as published by
6 Bugs can be reported to bug-glibc@prep.ai.mit.edu. 7 the Free Software Foundation; either version 2, or (at your option)
7 8 any later version.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12 9
13 This program is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details. 13 GNU General Public License for more details.
17 14
18 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License along
19 along with this program; if not, write to the Free Software 16 with this program; if not, write to the Free Software Foundation,
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 USA. */
22 18
23 #ifndef _ERROR_H 19 #ifndef _ERROR_H
24 #define _ERROR_H 1 20 #define _ERROR_H 1
25 21
26 #ifndef __attribute__ 22 #ifndef __attribute__
27 /* This feature is available in gcc versions 2.5 and later. */ 23 /* This feature is available in gcc versions 2.5 and later. */
28 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__ 24 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
29 # define __attribute__(Spec) /* empty */ 25 # define __attribute__(Spec) /* empty */
30 # endif 26 # endif
31 /* The __-protected variants of `format' and `printf' attributes 27 /* The __-protected variants of `format' and `printf' attributes
...@@ -40,17 +36,15 @@ ...@@ -40,17 +36,15 @@
40 extern "C" { 36 extern "C" {
41 #endif 37 #endif
42 38
43 #if defined (__STDC__) && __STDC__
44
45 /* Print a message with `fprintf (stderr, FORMAT, ...)'; 39 /* Print a message with `fprintf (stderr, FORMAT, ...)';
46 if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM). 40 if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
47 If STATUS is nonzero, terminate the program with `exit (STATUS)'. */ 41 If STATUS is nonzero, terminate the program with `exit (STATUS)'. */
48 42
49 extern void error (int status, int errnum, const char *format, ...) 43 extern void error (int __status, int __errnum, const char *__format, ...)
50 __attribute__ ((__format__ (__printf__, 3, 4))); 44 __attribute__ ((__format__ (__printf__, 3, 4)));
51 45
52 extern void error_at_line (int status, int errnum, const char *fname, 46 extern void error_at_line (int __status, int __errnum, const char *__fname,
53 unsigned int lineno, const char *format, ...) 47 unsigned int __lineno, const char *__format, ...)
54 __attribute__ ((__format__ (__printf__, 5, 6))); 48 __attribute__ ((__format__ (__printf__, 5, 6)));
55 49
56 /* If NULL, error will flush stdout, then print on stderr the program 50 /* If NULL, error will flush stdout, then print on stderr the program
...@@ -58,12 +52,6 @@ extern void error_at_line (int status, int errnum, const char *fname, ...@@ -58,12 +52,6 @@ extern void error_at_line (int status, int errnum, const char *fname,
58 function without parameters instead. */ 52 function without parameters instead. */
59 extern void (*error_print_progname) (void); 53 extern void (*error_print_progname) (void);
60 54
61 #else
62 void error ();
63 void error_at_line ();
64 extern void (*error_print_progname) ();
65 #endif
66
67 /* This variable is incremented each time `error' is called. */ 55 /* This variable is incremented each time `error' is called. */
68 extern unsigned int error_message_count; 56 extern unsigned int error_message_count;
69 57
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* Copyright (C) 1992-2001, 2003, 2004 Free Software Foundation, Inc.
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. 2 This file is part of the GNU C Library.
3 3
4 This library is free software; you can redistribute it and/or 4 This program is free software; you can redistribute it and/or modify
5 modify it under the terms of the GNU Lesser General Public 5 it under the terms of the GNU General Public License as published by
6 License as published by the Free Software Foundation; either 6 the Free Software Foundation; either version 2, or (at your option)
7 version 2 of the License, or (at your option) any later version. 7 any later version.
8 8
9 This library is distributed in the hope that it will be useful, 9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 Lesser General Public License for more details. 12 GNU General Public License for more details.
13 13
14 You should have received a copy of the GNU Lesser General Public 14 You should have received a copy of the GNU General Public License along
15 License along with this library; if not, write to the Free Software 15 with this program; if not, write to the Free Software Foundation,
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 17
18 #include <stdio.h> 18 #if HAVE_CONFIG_H
19 #include <string.h> 19 # include <config.h>
20 #include <stdlib.h> 20 #endif
21 #include <termios.h>
22 21
23 /* Alain: Parts originally from GNU Lib C. */ 22 #if !_LIBC
23 # include "getpass.h"
24 #endif
24 25
25 static void 26 #if _LIBC
26 echo_off(int fd, struct termios *stored_settings) 27 # define HAVE_STDIO_EXT_H 1
27 { 28 #endif
28 struct termios new_settings; 29
29 tcgetattr (fd, stored_settings); 30 #include <stdbool.h>
30 new_settings = *stored_settings; 31
31 new_settings.c_lflag &= (~ECHO); 32 #include <stdio.h>
32 tcsetattr (fd, TCSANOW, &new_settings); 33 #if HAVE_STDIO_EXT_H
33 } 34 # include <stdio_ext.h>
35 #else
36 # define __fsetlocking(stream, type) /* empty */
37 #endif
38 #if !_LIBC
39 # include "getline.h"
40 #endif
41
42 #include <termios.h>
43 #include <unistd.h>
44
45 #if _LIBC
46 # include <wchar.h>
47 #endif
48
49 #if _LIBC
50 # define NOTCANCEL_MODE "c"
51 #else
52 # define NOTCANCEL_MODE
53 #endif
54
55 #if _LIBC
56 # define flockfile(s) _IO_flockfile (s)
57 # define funlockfile(s) _IO_funlockfile (s)
58 #elif USE_UNLOCKED_IO
59 # include "unlocked-io.h"
60 #else
61 # if !HAVE_DECL_FFLUSH_UNLOCKED
62 # undef fflush_unlocked
63 # define fflush_unlocked(x) fflush (x)
64 # endif
65 # if !HAVE_DECL_FLOCKFILE
66 # undef flockfile
67 # define flockfile(x) ((void) 0)
68 # endif
69 # if !HAVE_DECL_FUNLOCKFILE
70 # undef funlockfile
71 # define funlockfile(x) ((void) 0)
72 # endif
73 # if !HAVE_DECL_FPUTS_UNLOCKED
74 # undef fputs_unlocked
75 # define fputs_unlocked(str,stream) fputs (str, stream)
76 # endif
77 # if !HAVE_DECL_PUTC_UNLOCKED
78 # undef putc_unlocked
79 # define putc_unlocked(c,stream) putc (c, stream)
80 # endif
81 #endif
82
83 #if _LIBC
84 # include <bits/libc-lock.h>
85 #else
86 # define __libc_cleanup_push(function, arg) /* empty */
87 # define __libc_cleanup_pop(execute) /* empty */
88 #endif
89
90 #if !_LIBC
91 # define __getline getline
92 # define __tcgetattr tcgetattr
93 #endif
94
95 /* It is desirable to use this bit on systems that have it.
96 The only bit of terminal state we want to twiddle is echoing, which is
97 done in software; there is no need to change the state of the terminal
98 hardware. */
99
100 #ifndef TCSASOFT
101 # define TCSASOFT 0
102 #endif
34 103
35 static void 104 static void
36 echo_on(int fd, struct termios *stored_settings) 105 call_fclose (void *arg)
37 { 106 {
38 tcsetattr (fd, TCSANOW, stored_settings); 107 if (arg != NULL)
108 fclose (arg);
39 } 109 }
40 110
41 char * 111 char *
42 getpass (const char * prompt) 112 getpass (const char *prompt)
43 { 113 {
114 FILE *tty;
44 FILE *in, *out; 115 FILE *in, *out;
45 struct termios stored_settings; 116 struct termios s, t;
117 bool tty_changed;
46 static char *buf; 118 static char *buf;
47 static size_t buf_size; 119 static size_t bufsize;
48 char *pbuf; 120 ssize_t nread;
121
122 /* Try to write to and read from the terminal if we can.
123 If we can't open the terminal, use stderr and stdin. */
49 124
50 /* First pass initialize the buffer. */ 125 tty = fopen ("/dev/tty", "w+" NOTCANCEL_MODE);
51 if (buf_size == 0) 126 if (tty == NULL)
52 { 127 {
53 buf_size = 256; 128 in = stdin;
54 buf = calloc (1, buf_size); 129 out = stderr;
55 if (buf == NULL)
56 return NULL;
57 } 130 }
58 else 131 else
59 memset (buf, '\0', buf_size); 132 {
133 /* We do the locking ourselves. */
134 __fsetlocking (tty, FSETLOCKING_BYCALLER);
135
136 out = in = tty;
137 }
138
139 /* Make sure the stream we opened is closed even if the thread is
140 canceled. */
141 __libc_cleanup_push (call_fclose, tty);
142
143 flockfile (out);
60 144
61 /* Turn echoing off if it is on now. */ 145 /* Turn echoing off if it is on now. */
62 echo_off (fileno (stdin), &stored_settings); 146
147 if (__tcgetattr (fileno (in), &t) == 0)
148 {
149 /* Save the old one. */
150 s = t;
151 /* Tricky, tricky. */
152 t.c_lflag &= ~(ECHO|ISIG);
153 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
154 }
155 else
156 tty_changed = false;
63 157
64 /* Write the prompt. */ 158 /* Write the prompt. */
65 fputs (prompt, stdout); 159 #ifdef USE_IN_LIBIO
66 fflush (stdout); 160 if (_IO_fwide (out, 0) > 0)
161 __fwprintf (out, L"%s", prompt);
162 else
163 #endif
164 fputs_unlocked (prompt, out);
165 fflush_unlocked (out);
67 166
68 /* Read the password. */ 167 /* Read the password. */
69 pbuf = fgets (buf, buf_size, stdin); 168 nread = __getline (&buf, &bufsize, in);
70 if (pbuf) 169
170 #if !_LIBC
171 /* As far as is known, glibc doesn't need this no-op fseek. */
172
173 /* According to the C standard, input may not be followed by output
174 on the same stream without an intervening call to a file
175 positioning function. Suppose in == out; then without this fseek
176 call, on Solaris, HP-UX, AIX, OSF/1, the previous input gets
177 echoed, whereas on IRIX, the following newline is not output as
178 it should be. POSIX imposes similar restrictions if fileno (in)
179 == fileno (out). The POSIX restrictions are tricky and change
180 from POSIX version to POSIX version, so play it safe and invoke
181 fseek even if in != out. */
182 fseek (out, 0, SEEK_CUR);
183 #endif
184
185 if (buf != NULL)
71 { 186 {
72 size_t nread = strlen (pbuf); 187 if (nread < 0)
73 if (nread && pbuf[nread - 1] == '\n') 188 buf[0] = '\0';
74 { 189 else if (buf[nread - 1] == '\n')
75 /* Remove the newline. */ 190 {
76 pbuf[nread - 1] = '\0'; 191 /* Remove the newline. */
77 /* Write the newline that was not echoed. */ 192 buf[nread - 1] = '\0';
78 putc ('\n', stdout); 193 if (tty_changed)
79 } 194 {
195 /* Write the newline that was not echoed. */
196 #ifdef USE_IN_LIBIO
197 if (_IO_fwide (out, 0) > 0)
198 putwc_unlocked (L'\n', out);
199 else
200 #endif
201 putc_unlocked ('\n', out);
202 }
203 }
80 } 204 }
81 205
82 /* Restore the original setting. */ 206 /* Restore the original setting. */
83 echo_on (fileno (stdin), &stored_settings); 207 if (tty_changed)
208 (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
84 209
85 return pbuf; 210 funlockfile (out);
86 }
87 211
88 #ifdef _GETPASS_STANDALONE_TEST 212 __libc_cleanup_pop (0);
89 213
90 int 214 call_fclose (tty);
91 main () 215
92 { 216 return buf;
93 char *p;
94 p = getpass ("my prompt: ");
95 if (p)
96 printf ("Passwd: %s\n", p);
97 return 0;
98 } 217 }
99 #endif
......
1 /* rpl_malloc.c -- a replacement for malloc that don't accept 0 size 1 /* malloc() function that is glibc compatible.
2 Copyright (C) 2001 Free Software Foundation, Inc. 2 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 5 it under the terms of the GNU General Public License as published by
...@@ -15,12 +15,22 @@ ...@@ -15,12 +15,22 @@
15 along with this program; if not, write to the Free Software Foundation, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 17
18 /* written by Jim Meyering */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 #undef malloc
24
18 #include <stdlib.h> 25 #include <stdlib.h>
19 26
27 /* Allocate an N-byte block of memory from the heap.
28 If N is zero, allocate a 1-byte block. */
29
20 void * 30 void *
21 rpl_malloc(size_t size) 31 rpl_malloc (size_t n)
22 { 32 {
23 if (!size) 33 if (n == 0)
24 size++; 34 n = 1;
25 return malloc(size); 35 return malloc (n);
26 } 36 }
......
1 /* rpl_realloc.c -- a replacement for broken realloc implementations 1 /* realloc() function that is glibc compatible.
2 Copyright (C) 2001 Free Software Foundation, Inc. 2 Copyright (C) 1997, 2003, 2004 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 5 it under the terms of the GNU General Public License as published by
...@@ -15,18 +15,32 @@ ...@@ -15,18 +15,32 @@
15 along with this program; if not, write to the Free Software Foundation, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 17
18 /* written by Jim Meyering */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 #undef realloc
24
18 #include <stdlib.h> 25 #include <stdlib.h>
19 26
27 /* Change the size of an allocated block of memory P to N bytes,
28 with error checking. If N is zero, change it to 1. If P is NULL,
29 use malloc. */
30
20 void * 31 void *
21 rpl_realloc(void *ptr, size_t size) 32 rpl_realloc (void *p, size_t n)
22 { 33 {
23 if (!ptr) 34 if (n == 0)
24 return malloc(size);
25 if (!size)
26 { 35 {
27 if (ptr) 36 n = 1;
28 free(ptr); 37
29 return malloc(size); 38 /* In theory realloc might fail, so don't rely on it to free. */
39 free (p);
40 p = NULL;
30 } 41 }
31 return realloc(ptr, size); 42
43 if (p == NULL)
44 return malloc (n);
45 return realloc (p, n);
32 } 46 }
......