GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-passwd.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <sys/types.h>
28 
29 #ifdef HAVE_PWD_H
30 #include <pwd.h>
31 #endif
32 
33 #include "lo-error.h"
34 #include "oct-passwd.h"
35 
36 #define NOT_SUPPORTED(nm) \
37  nm ": not supported on this system"
38 
39 std::string
40 octave_passwd::name (void) const
41 {
42  if (! ok ())
43  gripe_invalid ();
44 
45  return pw_name;
46 }
47 
48 std::string
50 {
51  if (! ok ())
52  gripe_invalid ();
53 
54  return pw_passwd;
55 }
56 
57 uid_t
58 octave_passwd::uid (void) const
59 {
60  if (! ok ())
61  gripe_invalid ();
62 
63  return pw_uid;
64 }
65 
66 gid_t
67 octave_passwd::gid (void) const
68 {
69  if (! ok ())
70  gripe_invalid ();
71 
72  return pw_gid;
73 }
74 
75 std::string
77 {
78  if (! ok ())
79  gripe_invalid ();
80 
81  return pw_gecos;
82 }
83 
84 std::string
85 octave_passwd::dir (void) const
86 {
87  if (! ok ())
88  gripe_invalid ();
89 
90  return pw_dir;
91 }
92 
93 std::string
95 {
96  if (! ok ())
97  gripe_invalid ();
98 
99  return pw_shell;
100 }
101 
104 {
105  std::string msg;
106  return getpwent (msg);
107 }
108 
110 octave_passwd::getpwent (std::string& msg)
111 {
112 #if defined HAVE_GETPWENT
113  msg = std::string ();
114  return octave_passwd (::getpwent (), msg);
115 #else
116  msg = NOT_SUPPORTED ("getpwent");
117  return octave_passwd ();
118 #endif
119 }
120 
123 {
124  std::string msg;
125  return getpwuid (uid, msg);
126 }
127 
129 octave_passwd::getpwuid (uid_t uid, std::string& msg)
130 {
131 #if defined (HAVE_GETPWUID)
132  msg = std::string ();
133  return octave_passwd (::getpwuid (uid), msg);
134 #else
135  msg = NOT_SUPPORTED ("getpwuid");
136  return octave_passwd ();
137 #endif
138 }
139 
141 octave_passwd::getpwnam (const std::string& nm)
142 {
143  std::string msg;
144  return getpwnam (nm, msg);
145 }
146 
148 octave_passwd::getpwnam (const std::string& nm, std::string& msg)
149 {
150 #if defined (HAVE_GETPWNAM)
151  msg = std::string ();
152  return octave_passwd (::getpwnam (nm.c_str ()), msg);
153 #else
154  msg = NOT_SUPPORTED ("getpwnam");
155  return octave_passwd ();
156 #endif
157 }
158 
159 int
161 {
162  std::string msg;
163  return setpwent (msg);
164 }
165 
166 int
167 octave_passwd::setpwent (std::string& msg)
168 {
169 #if defined (HAVE_SETPWENT)
170  msg = std::string ();
171  ::setpwent ();
172  return 0;
173 #else
174  msg = NOT_SUPPORTED ("setpwent");
175  return -1;
176 #endif
177 }
178 
179 int
181 {
182  std::string msg;
183  return endpwent (msg);
184 }
185 
186 int
187 octave_passwd::endpwent (std::string& msg)
188 {
189 #if defined (HAVE_ENDPWENT)
190  msg = std::string ();
191  ::endpwent ();
192  return 0;
193 #else
194  msg = NOT_SUPPORTED ("endpwent");
195  return -1;
196 #endif
197 }
198 
199 octave_passwd::octave_passwd (void *p, std::string& msg)
200  : pw_name (), pw_passwd (), pw_uid (0), pw_gid (0), pw_gecos (),
201  pw_dir (), pw_shell (), valid (false)
202 {
203 #if defined (HAVE_PWD_H)
204  msg = std::string ();
205 
206  if (p)
207  {
208  struct passwd *pw = static_cast<struct passwd *> (p);
209 
210  pw_name = pw->pw_name;
211  pw_passwd = pw->pw_passwd;
212  pw_uid = pw->pw_uid;
213  pw_gid = pw->pw_gid;
214  pw_gecos = pw->pw_gecos;
215  pw_dir = pw->pw_dir;
216  pw_shell = pw->pw_shell;
217 
218  valid = true;
219  }
220 #else
221  msg = NOT_SUPPORTED ("password functions");
222 #endif
223 }
224 
225 void
227 {
228  (*current_liboctave_error_handler) ("invalid password object");
229 }