Mercurial > octave-nkf
annotate libinterp/corefcn/getgrent.cc @ 20616:fd0efcdb3718
use new string_value method to handle value extraction errors
* dirfns.cc, file-io.cc, gammainc.cc, help.cc, load-path.cc,
octave-link.cc, qz.cc, regexp.cc, strfns.cc, syscalls.cc, time.cc,
variables.cc: Use new string_value method.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 08 Oct 2015 19:00:51 -0400 |
parents | f90c8372b7ba |
children |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3 Copyright (C) 1996-2015 John W. Eaton |
2928 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
2928 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
2928 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <string> | |
28 | |
29 #include <sys/types.h> | |
30 | |
2937 | 31 #include "oct-group.h" |
2928 | 32 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
33 #include "defun.h" |
2928 | 34 #include "error.h" |
35 #include "gripes.h" | |
36 #include "oct-map.h" | |
37 #include "ov.h" | |
38 #include "oct-obj.h" | |
39 #include "utils.h" | |
40 | |
41 // Group file functions. (Why not?) | |
42 | |
43 static octave_value | |
2937 | 44 mk_gr_map (const octave_group& gr) |
2928 | 45 { |
46 octave_value retval; | |
47 | |
48 if (gr) | |
49 { | |
11043
e9966851619b
getpwent.cc, getgrent.cc, getrusage.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
50 octave_scalar_map m; |
2928 | 51 |
4675 | 52 m.assign ("name", gr.name ()); |
53 m.assign ("passwd", gr.passwd ()); | |
54 m.assign ("gid", static_cast<double> (gr.gid ())); | |
55 m.assign ("mem", octave_value (gr.mem ())); | |
2928 | 56 |
57 retval = m; | |
58 } | |
59 else | |
4233 | 60 retval = 0; |
2928 | 61 |
62 return retval; | |
63 } | |
64 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
65 DEFUN (getgrent, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
66 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
67 @deftypefn {Built-in Function} {@var{grp_struct} =} getgrent ()\n\ |
3301 | 68 Return an entry from the group database, opening it if necessary.\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
69 \n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
70 Once the end of data has been reached, @code{getgrent} returns 0.\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
71 @seealso{setgrent, endgrent}\n\ |
3301 | 72 @end deftypefn") |
2928 | 73 { |
2937 | 74 octave_value_list retval; |
75 | |
3523 | 76 retval(1) = std::string (); |
4233 | 77 retval(0) = 0; |
2928 | 78 |
79 int nargin = args.length (); | |
80 | |
81 if (nargin == 0) | |
82 { | |
3523 | 83 std::string msg; |
2937 | 84 |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
11553
diff
changeset
|
85 retval(1) = msg; |
2937 | 86 retval(0) = mk_gr_map (octave_group::getgrent (msg)); |
2928 | 87 } |
88 else | |
5823 | 89 print_usage (); |
2928 | 90 |
91 return retval; | |
92 } | |
93 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
94 DEFUN (getgrgid, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
95 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
96 @deftypefn {Built-in Function} {@var{grp_struct} =} getgrgid (@var{gid}).\n\ |
3301 | 97 Return the first entry from the group database with the group ID\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
98 @var{gid}.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
99 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
100 If the group ID does not exist in the database, @code{getgrgid} returns 0.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
101 @seealso{getgrnam}\n\ |
3301 | 102 @end deftypefn") |
2928 | 103 { |
2937 | 104 octave_value_list retval; |
105 | |
3523 | 106 retval(1) = std::string (); |
4233 | 107 retval(0) = 0; |
2928 | 108 |
109 int nargin = args.length (); | |
110 | |
111 if (nargin == 1) | |
112 { | |
113 double dval = args(0).double_value (); | |
114 | |
20587
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
115 if (D_NINT (dval) == dval) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
116 { |
20587
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
117 gid_t gid = static_cast<gid_t> (dval); |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
118 |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
119 std::string msg; |
2928 | 120 |
20587
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
121 retval(1) = msg; |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
122 retval(0) = mk_gr_map (octave_group::getgrgid (gid, msg)); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
123 } |
20587
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
124 else |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
125 error ("getgrgid: GID must be an integer"); |
2928 | 126 } |
127 else | |
5823 | 128 print_usage (); |
2928 | 129 |
130 return retval; | |
131 } | |
132 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
133 DEFUN (getgrnam, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
134 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
135 @deftypefn {Built-in Function} {@var{grp_struct} =} getgrnam (@var{name})\n\ |
3301 | 136 Return the first entry from the group database with the group name\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
137 @var{name}.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
138 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
139 If the group name does not exist in the database, @code{getgrnam} returns 0.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
140 @seealso{getgrgid}\n\ |
3301 | 141 @end deftypefn") |
2928 | 142 { |
2937 | 143 octave_value_list retval; |
144 | |
3523 | 145 retval(1) = std::string (); |
4233 | 146 retval(0) = 0; |
2928 | 147 |
148 int nargin = args.length (); | |
149 | |
150 if (nargin == 1) | |
151 { | |
3523 | 152 std::string s = args(0).string_value (); |
2928 | 153 |
20587
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
154 std::string msg; |
2937 | 155 |
20587
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
156 retval(1) = msg; |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20207
diff
changeset
|
157 retval(0) = mk_gr_map (octave_group::getgrnam (s.c_str (), msg)); |
2928 | 158 } |
159 else | |
5823 | 160 print_usage (); |
2928 | 161 |
162 return retval; | |
163 } | |
164 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
165 DEFUN (setgrent, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
166 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
167 @deftypefn {Built-in Function} {} setgrent ()\n\ |
3301 | 168 Return the internal pointer to the beginning of the group database.\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
169 @seealso{getgrent, endgrent}\n\ |
3301 | 170 @end deftypefn") |
2928 | 171 { |
2937 | 172 octave_value_list retval; |
173 | |
3523 | 174 retval(1) = std::string (); |
2937 | 175 retval(0) = -1.0; |
2928 | 176 |
177 int nargin = args.length (); | |
178 | |
179 if (nargin == 0) | |
180 { | |
3523 | 181 std::string msg; |
2937 | 182 |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
11553
diff
changeset
|
183 retval(1) = msg; |
2937 | 184 retval(0) = static_cast<double> (octave_group::setgrent (msg)); |
2928 | 185 } |
186 else | |
5823 | 187 print_usage (); |
2928 | 188 |
189 return retval; | |
190 } | |
191 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
192 DEFUN (endgrent, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
193 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
194 @deftypefn {Built-in Function} {} endgrent ()\n\ |
3301 | 195 Close the group database.\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
196 @seealso{getgrent, setgrent}\n\ |
3301 | 197 @end deftypefn") |
2928 | 198 { |
2937 | 199 octave_value_list retval; |
200 | |
3523 | 201 retval(1) = std::string (); |
2937 | 202 retval(0) = -1.0; |
2928 | 203 |
204 int nargin = args.length (); | |
205 | |
206 if (nargin == 0) | |
207 { | |
3523 | 208 std::string msg; |
2937 | 209 |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
11553
diff
changeset
|
210 retval(1) = msg; |
2937 | 211 retval(0) = static_cast<double> (octave_group::endgrent (msg)); |
2928 | 212 } |
213 else | |
5823 | 214 print_usage (); |
2928 | 215 |
216 return retval; | |
217 } |