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 |
|
33 #ifdef HAVE_PWD_H |
|
34 #include <pwd.h> |
|
35 #endif |
|
36 |
|
37 #include "defun-dld.h" |
|
38 #include "error.h" |
|
39 #include "gripes.h" |
|
40 #include "help.h" |
|
41 #include "oct-map.h" |
|
42 #include "ov.h" |
|
43 #include "oct-obj.h" |
|
44 #include "utils.h" |
|
45 |
|
46 // Password file functions. (Why not?) |
|
47 |
|
48 static octave_value |
|
49 mk_pw_map (struct passwd *pw) |
|
50 { |
|
51 octave_value retval; |
|
52 |
|
53 if (pw) |
|
54 { |
|
55 Octave_map m; |
|
56 |
|
57 m ["name"] = pw->pw_name; |
|
58 m ["passwd"] = pw->pw_passwd; |
|
59 m ["uid"] = static_cast<double> (pw->pw_uid); |
|
60 m ["gid"] = static_cast<double> (pw->pw_gid); |
|
61 m ["gecos"] = pw->pw_gecos; |
|
62 m ["dir"] = pw->pw_dir; |
|
63 m ["shell"] = pw->pw_shell; |
|
64 |
|
65 retval = m; |
|
66 } |
|
67 else |
|
68 retval = 0.0; |
|
69 |
|
70 return retval; |
|
71 } |
|
72 |
|
73 DEFUN_DLD (getpwent, args, , |
|
74 "getpwent ()\n\ |
|
75 \n\ |
|
76 Read an entry from the password-file stream, opening it if necessary.") |
|
77 { |
|
78 octave_value retval; |
|
79 |
|
80 int nargin = args.length (); |
|
81 |
|
82 if (nargin == 0) |
|
83 { |
|
84 #ifdef HAVE_GETPWENT |
|
85 retval = mk_pw_map (getpwent ()); |
|
86 #else |
|
87 gripe_not_supported ("getpwent"); |
|
88 #endif |
|
89 } |
|
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 { |
|
101 octave_value retval; |
|
102 |
|
103 int nargin = args.length (); |
|
104 |
|
105 if (nargin == 1) |
|
106 { |
|
107 #ifdef HAVE_GETPWUID |
|
108 double dval = args(0).double_value (); |
|
109 |
|
110 if (! error_state) |
|
111 { |
|
112 if (D_NINT (dval) == dval) |
|
113 { |
|
114 uid_t uid = static_cast<uid_t> (dval); |
|
115 |
|
116 retval = mk_pw_map (getpwuid (uid)); |
|
117 } |
|
118 else |
|
119 error ("getpwuid: argument must be an integer"); |
|
120 } |
|
121 #else |
|
122 gripe_not_supported ("getpwuid"); |
|
123 #endif |
|
124 } |
|
125 else |
|
126 print_usage ("getpwuid"); |
|
127 |
|
128 return retval; |
|
129 } |
|
130 |
|
131 DEFUN_DLD (getpwnam, args, , |
|
132 "getpwnam (NAME)\n\ |
|
133 \n\ |
|
134 Search for password entry with a matching username.") |
|
135 { |
|
136 octave_value retval; |
|
137 |
|
138 int nargin = args.length (); |
|
139 |
|
140 if (nargin == 1) |
|
141 { |
|
142 #ifdef HAVE_GETPWNAM |
|
143 string s = args(0).string_value (); |
|
144 |
|
145 if (! error_state) |
|
146 retval = mk_pw_map (getpwnam (s.c_str ())); |
|
147 #else |
|
148 gripe_not_supported ("getpwnam"); |
|
149 #endif |
|
150 } |
|
151 else |
|
152 print_usage ("getpwnam"); |
|
153 |
|
154 return retval; |
|
155 } |
|
156 |
|
157 DEFUN_DLD (setpwent, args, , |
|
158 "setpwent ()\n\ |
|
159 \n\ |
|
160 Rewind the password-file stream.") |
|
161 { |
|
162 octave_value retval; |
|
163 |
|
164 int nargin = args.length (); |
|
165 |
|
166 if (nargin == 0) |
|
167 { |
|
168 #ifdef HAVE_SETPWENT |
|
169 setpwent (); |
|
170 #else |
|
171 gripe_not_supported ("setpwent"); |
|
172 #endif |
|
173 } |
|
174 else |
|
175 print_usage ("setpwent"); |
|
176 |
|
177 return retval; |
|
178 } |
|
179 |
|
180 DEFUN_DLD (endpwent, args, , |
|
181 "endpwent ()\n\ |
|
182 \n\ |
|
183 Close the password-file stream.") |
|
184 { |
|
185 octave_value retval; |
|
186 |
|
187 int nargin = args.length (); |
|
188 |
|
189 if (nargin == 0) |
|
190 { |
|
191 #ifdef HAVE_ENDPWENT |
|
192 endpwent (); |
|
193 #else |
|
194 gripe_not_supported ("endpwent"); |
|
195 #endif |
|
196 } |
|
197 else |
|
198 print_usage ("endpwent"); |
|
199 |
|
200 return retval; |
|
201 } |
|
202 |
|
203 /* |
|
204 ;;; Local Variables: *** |
|
205 ;;; mode: C++ *** |
|
206 ;;; End: *** |
|
207 */ |