2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
|
29 #ifdef HAVE_SYS_TYPES_H |
|
30 #include <sys/types.h> |
|
31 #endif |
|
32 |
2933
|
33 #include "oct-passwd.h" |
2928
|
34 |
|
35 #include "defun-dld.h" |
|
36 #include "error.h" |
|
37 #include "gripes.h" |
|
38 #include "oct-map.h" |
|
39 #include "ov.h" |
|
40 #include "oct-obj.h" |
|
41 #include "utils.h" |
|
42 |
|
43 // Password file functions. (Why not?) |
|
44 |
|
45 static octave_value |
2933
|
46 mk_pw_map (const octave_passwd& pw) |
2928
|
47 { |
|
48 octave_value retval; |
|
49 |
|
50 if (pw) |
|
51 { |
|
52 Octave_map m; |
|
53 |
4233
|
54 m ["name"](0) = pw.name (); |
|
55 m ["passwd"](0) = pw.passwd (); |
|
56 m ["uid"](0) = static_cast<double> (pw.uid ()); |
|
57 m ["gid"](0) = static_cast<double> (pw.gid ()); |
|
58 m ["gecos"](0) = pw.gecos (); |
|
59 m ["dir"](0) = pw.dir (); |
|
60 m ["shell"](0) = pw.shell (); |
2928
|
61 |
|
62 retval = m; |
|
63 } |
|
64 else |
4233
|
65 retval = 0; |
2928
|
66 |
|
67 return retval; |
|
68 } |
|
69 |
|
70 DEFUN_DLD (getpwent, args, , |
3301
|
71 "-*- texinfo -*-\n\ |
|
72 @deftypefn {Loadable Function} {@var{pw_struct} = } getpwent ()\n\ |
|
73 Return a structure containing an entry from the password database,\n\ |
|
74 opening it if necessary. Once the end of the data has been reached,\n\ |
|
75 @code{getpwent} returns 0.\n\ |
|
76 @end deftypefn") |
2928
|
77 { |
2937
|
78 octave_value_list retval; |
|
79 |
3523
|
80 retval(1) = std::string (); |
4233
|
81 retval(0) = 0; |
2928
|
82 |
|
83 int nargin = args.length (); |
|
84 |
|
85 if (nargin == 0) |
2937
|
86 { |
3523
|
87 std::string msg; |
2937
|
88 |
|
89 retval(0) = mk_pw_map (octave_passwd::getpwent (msg)); |
|
90 retval(1) = msg; |
|
91 } |
2928
|
92 else |
|
93 print_usage ("getpwent"); |
|
94 |
|
95 return retval; |
|
96 } |
|
97 |
|
98 DEFUN_DLD (getpwuid, args, , |
3301
|
99 "-*- texinfo -*-\n\ |
|
100 @deftypefn {Loadable Function} {@var{pw_struct} = } getpwuid (@var{uid}).\n\ |
|
101 Return a structure containing the first entry from the password database\n\ |
|
102 with the user ID @var{uid}. If the user ID does not exist in the\n\ |
|
103 database, @code{getpwuid} returns 0.\n\ |
|
104 @end deftypefn") |
2928
|
105 { |
2937
|
106 octave_value_list retval; |
|
107 |
3523
|
108 retval(1) = std::string (); |
4233
|
109 retval(0) = 0; |
2928
|
110 |
|
111 int nargin = args.length (); |
|
112 |
|
113 if (nargin == 1) |
|
114 { |
|
115 double dval = args(0).double_value (); |
|
116 |
|
117 if (! error_state) |
|
118 { |
|
119 if (D_NINT (dval) == dval) |
|
120 { |
|
121 uid_t uid = static_cast<uid_t> (dval); |
|
122 |
3523
|
123 std::string msg; |
2937
|
124 |
|
125 retval(0) = mk_pw_map (octave_passwd::getpwuid (uid, msg)); |
|
126 retval(1) = msg; |
2928
|
127 } |
|
128 else |
|
129 error ("getpwuid: argument must be an integer"); |
|
130 } |
|
131 } |
|
132 else |
|
133 print_usage ("getpwuid"); |
|
134 |
|
135 return retval; |
|
136 } |
|
137 |
|
138 DEFUN_DLD (getpwnam, args, , |
3301
|
139 "-*- texinfo -*-\n\ |
|
140 @deftypefn {Loadable Function} {@var{pw_struct} = } getpwnam (@var{name})\n\ |
|
141 Return a structure containing the first entry from the password database\n\ |
|
142 with the user name @var{name}. If the user name does not exist in the\n\ |
|
143 database, @code{getpwname} returns 0.\n\ |
|
144 @end deftypefn") |
2928
|
145 { |
2937
|
146 octave_value_list retval; |
|
147 |
3523
|
148 retval(1) = std::string (); |
2937
|
149 retval(0) = 0.0; |
2928
|
150 |
|
151 int nargin = args.length (); |
|
152 |
|
153 if (nargin == 1) |
|
154 { |
3523
|
155 std::string s = args(0).string_value (); |
2928
|
156 |
|
157 if (! error_state) |
2937
|
158 { |
3523
|
159 std::string msg; |
2937
|
160 |
4391
|
161 retval(0) = mk_pw_map (octave_passwd::getpwnam (s, msg)); |
2937
|
162 retval(1) = msg; |
|
163 } |
2928
|
164 } |
|
165 else |
|
166 print_usage ("getpwnam"); |
|
167 |
|
168 return retval; |
|
169 } |
|
170 |
|
171 DEFUN_DLD (setpwent, args, , |
3301
|
172 "-*- texinfo -*-\n\ |
|
173 @deftypefn {Loadable Function} {} setpwent ()\n\ |
|
174 Return the internal pointer to the beginning of the password database.\n\ |
|
175 @end deftypefn") |
2928
|
176 { |
2937
|
177 octave_value_list retval; |
|
178 |
3523
|
179 retval(1) = std::string (); |
2937
|
180 retval(0) = -1.0; |
2928
|
181 |
|
182 int nargin = args.length (); |
|
183 |
|
184 if (nargin == 0) |
2937
|
185 { |
3523
|
186 std::string msg; |
2937
|
187 |
|
188 retval(0) = static_cast<double> (octave_passwd::setpwent (msg)); |
|
189 retval(1) = msg; |
|
190 } |
2928
|
191 else |
|
192 print_usage ("setpwent"); |
|
193 |
|
194 return retval; |
|
195 } |
|
196 |
|
197 DEFUN_DLD (endpwent, args, , |
3301
|
198 "-*- texinfo -*-\n\ |
|
199 @deftypefn {Loadable Function} {} endpwent ()\n\ |
|
200 Close the password database.\n\ |
|
201 @end deftypefn") |
2928
|
202 { |
2937
|
203 octave_value_list retval; |
|
204 |
3523
|
205 retval(1) = std::string (); |
2937
|
206 retval(0) = -1.0; |
2928
|
207 |
|
208 int nargin = args.length (); |
|
209 |
|
210 if (nargin == 0) |
2937
|
211 { |
3523
|
212 std::string msg; |
2937
|
213 |
|
214 retval(0) = static_cast<double> (octave_passwd::endpwent (msg)); |
|
215 retval(1) = msg; |
|
216 } |
2928
|
217 else |
|
218 print_usage ("endpwent"); |
|
219 |
|
220 return retval; |
|
221 } |
|
222 |
|
223 /* |
|
224 ;;; Local Variables: *** |
|
225 ;;; mode: C++ *** |
|
226 ;;; End: *** |
|
227 */ |