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 "help.h" |
|
39 #include "oct-map.h" |
|
40 #include "ov.h" |
|
41 #include "oct-obj.h" |
|
42 #include "utils.h" |
|
43 |
|
44 // Password file functions. (Why not?) |
|
45 |
|
46 static octave_value |
2933
|
47 mk_pw_map (const octave_passwd& pw) |
2928
|
48 { |
|
49 octave_value retval; |
|
50 |
|
51 if (pw) |
|
52 { |
|
53 Octave_map m; |
|
54 |
2933
|
55 m ["name"] = pw.name (); |
|
56 m ["passwd"] = pw.passwd (); |
|
57 m ["uid"] = static_cast<double> (pw.uid ()); |
|
58 m ["gid"] = static_cast<double> (pw.gid ()); |
|
59 m ["gecos"] = pw.gecos (); |
|
60 m ["dir"] = pw.dir (); |
|
61 m ["shell"] = pw.shell (); |
2928
|
62 |
|
63 retval = m; |
|
64 } |
|
65 else |
|
66 retval = 0.0; |
|
67 |
|
68 return retval; |
|
69 } |
|
70 |
|
71 DEFUN_DLD (getpwent, args, , |
|
72 "getpwent ()\n\ |
|
73 \n\ |
|
74 Read an entry from the password-file stream, opening it if necessary.") |
|
75 { |
2937
|
76 octave_value_list retval; |
|
77 |
|
78 retval(1) = string (); |
|
79 retval(0) = 0.0; |
2928
|
80 |
|
81 int nargin = args.length (); |
|
82 |
|
83 if (nargin == 0) |
2937
|
84 { |
|
85 string msg; |
|
86 |
|
87 retval(0) = mk_pw_map (octave_passwd::getpwent (msg)); |
|
88 retval(1) = msg; |
|
89 } |
2928
|
90 else |
|
91 print_usage ("getpwent"); |
|
92 |
|
93 return retval; |
|
94 } |
|
95 |
|
96 DEFUN_DLD (getpwuid, args, , |
|
97 "getpwuid (UID)\n\ |
|
98 \n\ |
|
99 Search for a password entry with a matching user ID.") |
|
100 { |
2937
|
101 octave_value_list retval; |
|
102 |
|
103 retval(1) = string (); |
|
104 retval(0) = 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) |
|
113 { |
|
114 if (D_NINT (dval) == dval) |
|
115 { |
|
116 uid_t uid = static_cast<uid_t> (dval); |
|
117 |
2937
|
118 string msg; |
|
119 |
|
120 retval(0) = mk_pw_map (octave_passwd::getpwuid (uid, msg)); |
|
121 retval(1) = msg; |
2928
|
122 } |
|
123 else |
|
124 error ("getpwuid: argument must be an integer"); |
|
125 } |
|
126 } |
|
127 else |
|
128 print_usage ("getpwuid"); |
|
129 |
|
130 return retval; |
|
131 } |
|
132 |
|
133 DEFUN_DLD (getpwnam, args, , |
|
134 "getpwnam (NAME)\n\ |
|
135 \n\ |
|
136 Search for password entry with a matching username.") |
|
137 { |
2937
|
138 octave_value_list retval; |
|
139 |
|
140 retval(1) = string (); |
|
141 retval(0) = 0.0; |
2928
|
142 |
|
143 int nargin = args.length (); |
|
144 |
|
145 if (nargin == 1) |
|
146 { |
|
147 string s = args(0).string_value (); |
|
148 |
|
149 if (! error_state) |
2937
|
150 { |
|
151 string msg; |
|
152 |
|
153 retval(0) = mk_pw_map (octave_passwd::getpwnam (s.c_str (), msg)); |
|
154 retval(1) = msg; |
|
155 } |
2928
|
156 } |
|
157 else |
|
158 print_usage ("getpwnam"); |
|
159 |
|
160 return retval; |
|
161 } |
|
162 |
|
163 DEFUN_DLD (setpwent, args, , |
|
164 "setpwent ()\n\ |
|
165 \n\ |
|
166 Rewind the password-file stream.") |
|
167 { |
2937
|
168 octave_value_list retval; |
|
169 |
|
170 retval(1) = string (); |
|
171 retval(0) = -1.0; |
2928
|
172 |
|
173 int nargin = args.length (); |
|
174 |
|
175 if (nargin == 0) |
2937
|
176 { |
|
177 string msg; |
|
178 |
|
179 retval(0) = static_cast<double> (octave_passwd::setpwent (msg)); |
|
180 retval(1) = msg; |
|
181 } |
2928
|
182 else |
|
183 print_usage ("setpwent"); |
|
184 |
|
185 return retval; |
|
186 } |
|
187 |
|
188 DEFUN_DLD (endpwent, args, , |
|
189 "endpwent ()\n\ |
|
190 \n\ |
|
191 Close the password-file stream.") |
|
192 { |
2937
|
193 octave_value_list retval; |
|
194 |
|
195 retval(1) = string (); |
|
196 retval(0) = -1.0; |
2928
|
197 |
|
198 int nargin = args.length (); |
|
199 |
|
200 if (nargin == 0) |
2937
|
201 { |
|
202 string msg; |
|
203 |
|
204 retval(0) = static_cast<double> (octave_passwd::endpwent (msg)); |
|
205 retval(1) = msg; |
|
206 } |
2928
|
207 else |
|
208 print_usage ("endpwent"); |
|
209 |
|
210 return retval; |
|
211 } |
|
212 |
|
213 /* |
|
214 ;;; Local Variables: *** |
|
215 ;;; mode: C++ *** |
|
216 ;;; End: *** |
|
217 */ |