Mercurial > octave
annotate src/DLD-FUNCTIONS/getgrent.cc @ 10182:0522a65bcd56
assume unistd.h and sys/types.h exist
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 21 Jan 2010 15:41:19 -0500 |
parents | d0ce5e973937 |
children | e9966851619b |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
9245 | 3 Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2005, 2006, 2007, 2009 |
7017 | 4 John W. Eaton |
2928 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave 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 | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2928 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2928 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <string> | |
29 | |
30 #include <sys/types.h> | |
31 | |
2937 | 32 #include "oct-group.h" |
2928 | 33 |
34 #include "defun-dld.h" | |
35 #include "error.h" | |
36 #include "gripes.h" | |
37 #include "oct-map.h" | |
38 #include "ov.h" | |
39 #include "oct-obj.h" | |
40 #include "utils.h" | |
41 | |
42 // Group file functions. (Why not?) | |
43 | |
44 static octave_value | |
2937 | 45 mk_gr_map (const octave_group& gr) |
2928 | 46 { |
47 octave_value retval; | |
48 | |
49 if (gr) | |
50 { | |
51 Octave_map m; | |
52 | |
4675 | 53 m.assign ("name", gr.name ()); |
54 m.assign ("passwd", gr.passwd ()); | |
55 m.assign ("gid", static_cast<double> (gr.gid ())); | |
56 m.assign ("mem", octave_value (gr.mem ())); | |
2928 | 57 |
58 retval = m; | |
59 } | |
60 else | |
4233 | 61 retval = 0; |
2928 | 62 |
63 return retval; | |
64 } | |
65 | |
66 DEFUN_DLD (getgrent, args, , | |
3301 | 67 "-*- texinfo -*-\n\ |
68 @deftypefn {Loadable Function} {@var{grp_struct} =} getgrent ()\n\ | |
69 Return an entry from the group database, opening it if necessary.\n\ | |
70 Once the end of the data has been reached, @code{getgrent} returns 0.\n\ | |
71 @end deftypefn") | |
2928 | 72 { |
2937 | 73 octave_value_list retval; |
74 | |
3523 | 75 retval(1) = std::string (); |
4233 | 76 retval(0) = 0; |
2928 | 77 |
78 int nargin = args.length (); | |
79 | |
80 if (nargin == 0) | |
81 { | |
3523 | 82 std::string msg; |
2937 | 83 |
84 retval(0) = mk_gr_map (octave_group::getgrent (msg)); | |
85 retval(1) = msg; | |
2928 | 86 } |
87 else | |
5823 | 88 print_usage (); |
2928 | 89 |
90 return retval; | |
91 } | |
92 | |
93 DEFUN_DLD (getgrgid, args, , | |
3301 | 94 "-*- texinfo -*-\n\ |
95 @deftypefn {Loadable Function} {@var{grp_struct} =} getgrgid (@var{gid}).\n\ | |
96 Return the first entry from the group database with the group ID\n\ | |
97 @var{gid}. If the group ID does not exist in the database,\n\ | |
98 @code{getgrgid} returns 0.\n\ | |
99 @end deftypefn") | |
2928 | 100 { |
2937 | 101 octave_value_list retval; |
102 | |
3523 | 103 retval(1) = std::string (); |
4233 | 104 retval(0) = 0; |
2928 | 105 |
106 int nargin = args.length (); | |
107 | |
108 if (nargin == 1) | |
109 { | |
110 double dval = args(0).double_value (); | |
111 | |
112 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
113 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
114 if (D_NINT (dval) == dval) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
115 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
116 gid_t gid = static_cast<gid_t> (dval); |
2928 | 117 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
118 std::string msg; |
2937 | 119 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
120 retval(0) = mk_gr_map (octave_group::getgrgid (gid, msg)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
121 retval(1) = msg; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
122 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
123 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
124 error ("getgrgid: argument must be an integer"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
125 } |
2928 | 126 } |
127 else | |
5823 | 128 print_usage (); |
2928 | 129 |
130 return retval; | |
131 } | |
132 | |
133 DEFUN_DLD (getgrnam, args, , | |
3301 | 134 "-*- texinfo -*-\n\ |
135 @deftypefn {Loadable Function} {@var{grp_struct} =} getgrnam (@var{name})\n\ | |
136 Return the first entry from the group database with the group name\n\ | |
137 @var{name}. If the group name does not exist in the database,\n\ | |
9079
4d610aba7347
Cleanup documentation for system.texi, package.texi
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
138 @code{getgrnam} returns 0.\n\ |
3301 | 139 @end deftypefn") |
2928 | 140 { |
2937 | 141 octave_value_list retval; |
142 | |
3523 | 143 retval(1) = std::string (); |
4233 | 144 retval(0) = 0; |
2928 | 145 |
146 int nargin = args.length (); | |
147 | |
148 if (nargin == 1) | |
149 { | |
3523 | 150 std::string s = args(0).string_value (); |
2928 | 151 |
152 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
153 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
154 std::string msg; |
2937 | 155 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
156 retval(0) = mk_gr_map (octave_group::getgrnam (s.c_str (), msg)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
157 retval(1) = msg; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
158 } |
2928 | 159 } |
160 else | |
5823 | 161 print_usage (); |
2928 | 162 |
163 return retval; | |
164 } | |
165 | |
166 DEFUN_DLD (setgrent, args, , | |
3301 | 167 "-*- texinfo -*-\n\ |
168 @deftypefn {Loadable Function} {} setgrent ()\n\ | |
169 Return the internal pointer to the beginning of the group database.\n\ | |
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 |
183 retval(0) = static_cast<double> (octave_group::setgrent (msg)); | |
184 retval(1) = msg; | |
2928 | 185 } |
186 else | |
5823 | 187 print_usage (); |
2928 | 188 |
189 return retval; | |
190 } | |
191 | |
192 DEFUN_DLD (endgrent, args, , | |
3301 | 193 "-*- texinfo -*-\n\ |
194 @deftypefn {Loadable Function} {} endgrent ()\n\ | |
195 Close the group database.\n\ | |
196 @end deftypefn") | |
2928 | 197 { |
2937 | 198 octave_value_list retval; |
199 | |
3523 | 200 retval(1) = std::string (); |
2937 | 201 retval(0) = -1.0; |
2928 | 202 |
203 int nargin = args.length (); | |
204 | |
205 if (nargin == 0) | |
206 { | |
3523 | 207 std::string msg; |
2937 | 208 |
209 retval(0) = static_cast<double> (octave_group::endgrent (msg)); | |
210 retval(1) = msg; | |
2928 | 211 } |
212 else | |
5823 | 213 print_usage (); |
2928 | 214 |
215 return retval; | |
216 } |