Mercurial > hg > octave-kai > gnulib-hg
annotate lib/yesno.c @ 17476:6057744acd2c default tip master
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Fri, 16 Aug 2013 06:32:22 -0700 |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
5 | 1 /* yesno.c -- read a yes/no response from stdin |
5851 | 2 |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
3 Copyright (C) 1990, 1998, 2001, 2003-2013 Free Software Foundation, Inc. |
5 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9161
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
5 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9161
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9161
diff
changeset
|
8 (at your option) any later version. |
5 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9161
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
5
diff
changeset
|
17 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
18 #include <config.h> |
5 | 19 |
5149 | 20 #include "yesno.h" |
21 | |
4684 | 22 #include <stdlib.h> |
5 | 23 #include <stdio.h> |
5318
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5149
diff
changeset
|
24 |
9157
18308c6e516b
Test yesno in combination with closein.
Eric Blake <ebb9@byu.net>
parents:
7726
diff
changeset
|
25 /* Return true if we read an affirmative line from standard input. |
5 | 26 |
9157
18308c6e516b
Test yesno in combination with closein.
Eric Blake <ebb9@byu.net>
parents:
7726
diff
changeset
|
27 Since this function uses stdin, it is suggested that the caller not |
18308c6e516b
Test yesno in combination with closein.
Eric Blake <ebb9@byu.net>
parents:
7726
diff
changeset
|
28 use STDIN_FILENO directly, and also that the line |
18308c6e516b
Test yesno in combination with closein.
Eric Blake <ebb9@byu.net>
parents:
7726
diff
changeset
|
29 atexit(close_stdin) be added to main(). */ |
666
969996bb277b
(yesno) [!HAVE_RPMATCH]: Remove function since we'll
Jim Meyering <jim@meyering.net>
parents:
661
diff
changeset
|
30 |
5149 | 31 bool |
4686
70bf7ce55a75
(main): Define with a prototype.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4684
diff
changeset
|
32 yesno (void) |
661
b76c0d01af51
(yesno) [HAVE_RPMATCH]: New rpmatch-based version of the function.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
33 { |
7726
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
34 bool yes; |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
35 |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
36 #if ENABLE_NLS |
5851 | 37 char *response = NULL; |
38 size_t response_size = 0; | |
39 ssize_t response_len = getline (&response, &response_size, stdin); | |
661
b76c0d01af51
(yesno) [HAVE_RPMATCH]: New rpmatch-based version of the function.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
40 |
5851 | 41 if (response_len <= 0) |
42 yes = false; | |
43 else | |
44 { | |
45 response[response_len - 1] = '\0'; | |
46 yes = (0 < rpmatch (response)); | |
47 } | |
661
b76c0d01af51
(yesno) [HAVE_RPMATCH]: New rpmatch-based version of the function.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
48 |
5851 | 49 free (response); |
7726
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
50 #else |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
51 /* Test against "^[yY]", hardcoded to avoid requiring getline, |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
52 regex, and rpmatch. */ |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
53 int c = getchar (); |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
54 yes = (c == 'y' || c == 'Y'); |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
55 while (c != '\n' && c != EOF) |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
56 c = getchar (); |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
57 #endif |
bf3b4aa1ab09
* lib/yesno.c [!ENABLE_NLS]: Don't include getline.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
58 |
5851 | 59 return yes; |
661
b76c0d01af51
(yesno) [HAVE_RPMATCH]: New rpmatch-based version of the function.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
60 } |