GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-passwd.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <sys/types.h>
28 
29 #if defined (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 OCTAVE_NORETURN static
40 void
42 {
43  (*current_liboctave_error_handler) ("invalid password object");
44 }
45 
46 namespace octave
47 {
48  namespace sys
49  {
51  password::name (void) const
52  {
53  if (! ok ())
54  err_invalid ();
55 
56  return m_name;
57  }
58 
60  password::passwd (void) const
61  {
62  if (! ok ())
63  err_invalid ();
64 
65  return m_passwd;
66  }
67 
68  uid_t
69  password::uid (void) const
70  {
71  if (! ok ())
72  err_invalid ();
73 
74  return m_uid;
75  }
76 
77  gid_t
78  password::gid (void) const
79  {
80  if (! ok ())
81  err_invalid ();
82 
83  return m_gid;
84  }
85 
87  password::gecos (void) const
88  {
89  if (! ok ())
90  err_invalid ();
91 
92  return m_gecos;
93  }
94 
96  password::dir (void) const
97  {
98  if (! ok ())
99  err_invalid ();
100 
101  return m_dir;
102  }
103 
105  password::shell (void) const
106  {
107  if (! ok ())
108  err_invalid ();
109 
110  return m_shell;
111  }
112 
113  password
115  {
116  std::string msg;
117  return getpwent (msg);
118  }
119 
120  password
122  {
123 #if defined HAVE_GETPWENT
124  msg = "";
125  return password (::getpwent (), msg);
126 #else
127  msg = NOT_SUPPORTED ("getpwent");
128  return password ();
129 #endif
130  }
131 
132  password
133  password::getpwuid (uid_t uid)
134  {
135  std::string msg;
136  return getpwuid (uid, msg);
137  }
138 
139  password
140  password::getpwuid (uid_t uid, std::string& msg)
141  {
142 #if defined (HAVE_GETPWUID)
143  msg = "";
144  return password (::getpwuid (uid), msg);
145 #else
146  octave_unused_parameter (uid);
147 
148  msg = NOT_SUPPORTED ("getpwuid");
149  return password ();
150 #endif
151  }
152 
153  password
155  {
156  std::string msg;
157  return getpwnam (nm, msg);
158  }
159 
160  password
162  {
163 #if defined (HAVE_GETPWNAM)
164  msg = "";
165  return password (::getpwnam (nm.c_str ()), msg);
166 #else
167  octave_unused_parameter (nm);
168 
169  msg = NOT_SUPPORTED ("getpwnam");
170  return password ();
171 #endif
172  }
173 
174  int
176  {
177  std::string msg;
178  return setpwent (msg);
179  }
180 
181  int
183  {
184 #if defined (HAVE_SETPWENT)
185  msg = "";
186  ::setpwent ();
187  return 0;
188 #else
189  msg = NOT_SUPPORTED ("setpwent");
190  return -1;
191 #endif
192  }
193 
194  int
196  {
197  std::string msg;
198  return endpwent (msg);
199  }
200 
201  int
203  {
204 #if defined (HAVE_ENDPWENT)
205  msg = "";
206  ::endpwent ();
207  return 0;
208 #else
209  msg = NOT_SUPPORTED ("endpwent");
210  return -1;
211 #endif
212  }
213 
215  : m_name (), m_passwd (), m_uid (0), m_gid (0), m_gecos (),
216  m_dir (), m_shell (), valid (false)
217  {
218 #if defined (HAVE_PWD_H)
219  msg = "";
220 
221  if (p)
222  {
223  struct ::passwd *pw = static_cast<struct ::passwd *> (p);
224 
225  m_name = pw->pw_name;
226  m_passwd = pw->pw_passwd;
227  m_uid = pw->pw_uid;
228  m_gid = pw->pw_gid;
229  m_gecos = pw->pw_gecos;
230  m_dir = pw->pw_dir;
231  m_shell = pw->pw_shell;
232 
233  valid = true;
234  }
235 #else
236  octave_unused_parameter (p);
237 
238  msg = NOT_SUPPORTED ("password functions");
239 #endif
240  }
241  }
242 }
bool ok(void) const
Definition: oct-passwd.h:86
static password getpwnam(const std::string &nm)
Definition: oct-passwd.cc:154
std::string m_passwd
Definition: oct-passwd.h:111
std::string dir(void) const
Definition: oct-passwd.cc:96
create a structure array and initialize its values The dimensions of each cell array of values must match Singleton cells and non cell values are repeated so that they fill the entire array If the cells are create an empty structure array with the specified field names If the argument is an return the underlying struct Observe that the syntax is optimized for struct trong struct("foo", 1) esult
Definition: ov-struct.cc:1736
std::string gecos(void) const
Definition: oct-passwd.cc:87
static password getpwuid(uid_t uid)
Definition: oct-passwd.cc:133
#define NOT_SUPPORTED(nm)
Definition: oct-passwd.cc:36
std::string m_gecos
Definition: oct-passwd.h:120
static password getpwent(void)
Definition: oct-passwd.cc:114
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
uid_t uid(void) const
Definition: oct-passwd.cc:69
static int setpwent(void)
Definition: oct-passwd.cc:175
is false
Definition: cellfun.cc:400
static int endpwent(void)
Definition: oct-passwd.cc:195
static OCTAVE_NORETURN void err_invalid(void)
Definition: oct-passwd.cc:41
std::string name(void) const
Definition: oct-passwd.cc:51
std::string passwd(void) const
Definition: oct-passwd.cc:60
p
Definition: lu.cc:138
std::string m_shell
Definition: oct-passwd.h:126
std::string shell(void) const
Definition: oct-passwd.cc:105
gid_t gid(void) const
Definition: oct-passwd.cc:78
std::string m_name
Definition: oct-passwd.h:108
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888