Mercurial > hg > octave-nkf
annotate src/DLD-FUNCTIONS/getpwent.cc @ 11043:e9966851619b
getpwent.cc, getgrent.cc, getrusage.cc: use octave_scalar_map instead of Octave_map
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 29 Sep 2010 03:19:37 -0400 |
parents | 0522a65bcd56 |
children | fd0a3ac60b0e |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2005, 2006, 2007, 2008 |
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 | |
2933 | 32 #include "oct-passwd.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 // Password file functions. (Why not?) | |
43 | |
44 static octave_value | |
2933 | 45 mk_pw_map (const octave_passwd& pw) |
2928 | 46 { |
47 octave_value retval; | |
48 | |
49 if (pw) | |
50 { | |
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
|
51 octave_scalar_map m; |
2928 | 52 |
4675 | 53 m.assign ("name", pw.name ()); |
54 m.assign ("passwd", pw.passwd ()); | |
55 m.assign ("uid", static_cast<double> (pw.uid ())); | |
56 m.assign ("gid", static_cast<double> (pw.gid ())); | |
57 m.assign ("gecos", pw.gecos ()); | |
58 m.assign ("dir", pw.dir ()); | |
59 m.assign ("shell", pw.shell ()); | |
2928 | 60 |
61 retval = m; | |
62 } | |
63 else | |
4233 | 64 retval = 0; |
2928 | 65 |
66 return retval; | |
67 } | |
68 | |
69 DEFUN_DLD (getpwent, args, , | |
3301 | 70 "-*- texinfo -*-\n\ |
7650 | 71 @deftypefn {Loadable Function} {@var{pw_struct} =} getpwent ()\n\ |
3301 | 72 Return a structure containing an entry from the password database,\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
73 opening it if necessary. Once the end of the data has been reached,\n\ |
3301 | 74 @code{getpwent} returns 0.\n\ |
75 @end deftypefn") | |
2928 | 76 { |
2937 | 77 octave_value_list retval; |
78 | |
3523 | 79 retval(1) = std::string (); |
4233 | 80 retval(0) = 0; |
2928 | 81 |
82 int nargin = args.length (); | |
83 | |
84 if (nargin == 0) | |
2937 | 85 { |
3523 | 86 std::string msg; |
2937 | 87 |
88 retval(0) = mk_pw_map (octave_passwd::getpwent (msg)); | |
89 retval(1) = msg; | |
90 } | |
2928 | 91 else |
5823 | 92 print_usage (); |
2928 | 93 |
94 return retval; | |
95 } | |
96 | |
97 DEFUN_DLD (getpwuid, args, , | |
3301 | 98 "-*- texinfo -*-\n\ |
7650 | 99 @deftypefn {Loadable Function} {@var{pw_struct} =} getpwuid (@var{uid}).\n\ |
3301 | 100 Return a structure containing the first entry from the password database\n\ |
101 with the user ID @var{uid}. If the user ID does not exist in the\n\ | |
102 database, @code{getpwuid} returns 0.\n\ | |
103 @end deftypefn") | |
2928 | 104 { |
2937 | 105 octave_value_list retval; |
106 | |
3523 | 107 retval(1) = std::string (); |
4233 | 108 retval(0) = 0; |
2928 | 109 |
110 int nargin = args.length (); | |
111 | |
112 if (nargin == 1) | |
113 { | |
114 double dval = args(0).double_value (); | |
115 | |
116 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
117 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
118 if (D_NINT (dval) == dval) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
119 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
120 uid_t uid = static_cast<uid_t> (dval); |
2928 | 121 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
122 std::string msg; |
2937 | 123 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
124 retval(0) = mk_pw_map (octave_passwd::getpwuid (uid, msg)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
125 retval(1) = msg; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
126 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
127 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
128 error ("getpwuid: argument must be an integer"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
129 } |
2928 | 130 } |
131 else | |
5823 | 132 print_usage (); |
2928 | 133 |
134 return retval; | |
135 } | |
136 | |
137 DEFUN_DLD (getpwnam, args, , | |
3301 | 138 "-*- texinfo -*-\n\ |
7650 | 139 @deftypefn {Loadable Function} {@var{pw_struct} =} getpwnam (@var{name})\n\ |
3301 | 140 Return a structure containing the first entry from the password database\n\ |
141 with the user name @var{name}. If the user name does not exist in the\n\ | |
142 database, @code{getpwname} returns 0.\n\ | |
143 @end deftypefn") | |
2928 | 144 { |
2937 | 145 octave_value_list retval; |
146 | |
3523 | 147 retval(1) = std::string (); |
2937 | 148 retval(0) = 0.0; |
2928 | 149 |
150 int nargin = args.length (); | |
151 | |
152 if (nargin == 1) | |
153 { | |
3523 | 154 std::string s = args(0).string_value (); |
2928 | 155 |
156 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
157 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
158 std::string msg; |
2937 | 159 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
160 retval(0) = mk_pw_map (octave_passwd::getpwnam (s, msg)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
161 retval(1) = msg; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
162 } |
2928 | 163 } |
164 else | |
5823 | 165 print_usage (); |
2928 | 166 |
167 return retval; | |
168 } | |
169 | |
170 DEFUN_DLD (setpwent, args, , | |
3301 | 171 "-*- texinfo -*-\n\ |
172 @deftypefn {Loadable Function} {} setpwent ()\n\ | |
173 Return the internal pointer to the beginning of the password database.\n\ | |
174 @end deftypefn") | |
2928 | 175 { |
2937 | 176 octave_value_list retval; |
177 | |
3523 | 178 retval(1) = std::string (); |
2937 | 179 retval(0) = -1.0; |
2928 | 180 |
181 int nargin = args.length (); | |
182 | |
183 if (nargin == 0) | |
2937 | 184 { |
3523 | 185 std::string msg; |
2937 | 186 |
187 retval(0) = static_cast<double> (octave_passwd::setpwent (msg)); | |
188 retval(1) = msg; | |
189 } | |
2928 | 190 else |
5823 | 191 print_usage (); |
2928 | 192 |
193 return retval; | |
194 } | |
195 | |
196 DEFUN_DLD (endpwent, args, , | |
3301 | 197 "-*- texinfo -*-\n\ |
198 @deftypefn {Loadable Function} {} endpwent ()\n\ | |
199 Close the password database.\n\ | |
200 @end deftypefn") | |
2928 | 201 { |
2937 | 202 octave_value_list retval; |
203 | |
3523 | 204 retval(1) = std::string (); |
2937 | 205 retval(0) = -1.0; |
2928 | 206 |
207 int nargin = args.length (); | |
208 | |
209 if (nargin == 0) | |
2937 | 210 { |
3523 | 211 std::string msg; |
2937 | 212 |
213 retval(0) = static_cast<double> (octave_passwd::endpwent (msg)); | |
214 retval(1) = msg; | |
215 } | |
2928 | 216 else |
5823 | 217 print_usage (); |
2928 | 218 |
219 return retval; | |
220 } |