GNU Octave  4.2.1
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-2017 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 #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  msg = NOT_SUPPORTED ("getpwuid");
147  return password ();
148 #endif
149  }
150 
151  password
153  {
154  std::string msg;
155  return getpwnam (nm, msg);
156  }
157 
158  password
160  {
161 #if defined (HAVE_GETPWNAM)
162  msg = "";
163  return password (::getpwnam (nm.c_str ()), msg);
164 #else
165  msg = NOT_SUPPORTED ("getpwnam");
166  return password ();
167 #endif
168  }
169 
170  int
172  {
173  std::string msg;
174  return setpwent (msg);
175  }
176 
177  int
179  {
180 #if defined (HAVE_SETPWENT)
181  msg = "";
182  ::setpwent ();
183  return 0;
184 #else
185  msg = NOT_SUPPORTED ("setpwent");
186  return -1;
187 #endif
188  }
189 
190  int
192  {
193  std::string msg;
194  return endpwent (msg);
195  }
196 
197  int
199  {
200 #if defined (HAVE_ENDPWENT)
201  msg = "";
202  ::endpwent ();
203  return 0;
204 #else
205  msg = NOT_SUPPORTED ("endpwent");
206  return -1;
207 #endif
208  }
209 
211  : m_name (), m_passwd (), m_uid (0), m_gid (0), m_gecos (),
212  m_dir (), m_shell (), valid (false)
213  {
214 #if defined (HAVE_PWD_H)
215  msg = "";
216 
217  if (p)
218  {
219  struct ::passwd *pw = static_cast<struct ::passwd *> (p);
220 
221  m_name = pw->pw_name;
222  m_passwd = pw->pw_passwd;
223  m_uid = pw->pw_uid;
224  m_gid = pw->pw_gid;
225  m_gecos = pw->pw_gecos;
226  m_dir = pw->pw_dir;
227  m_shell = pw->pw_shell;
228 
229  valid = true;
230  }
231 #else
232  msg = NOT_SUPPORTED ("password functions");
233 #endif
234  }
235  }
236 }
static password getpwnam(const std::string &nm)
Definition: oct-passwd.cc:152
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
std::string gecos(void) const
Definition: oct-passwd.cc:87
std::string m_passwd
Definition: oct-passwd.h:111
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:1688
uid_t uid(void) const
Definition: oct-passwd.cc:69
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:941
std::string dir(void) const
Definition: oct-passwd.cc:96
static int setpwent(void)
Definition: oct-passwd.cc:171
std::string name(void) const
Definition: oct-passwd.cc:51
is false
Definition: cellfun.cc:398
bool ok(void) const
Definition: oct-passwd.h:86
static int endpwent(void)
Definition: oct-passwd.cc:191
static OCTAVE_NORETURN void err_invalid(void)
Definition: oct-passwd.cc:41
gid_t gid(void) const
Definition: oct-passwd.cc:78
p
Definition: lu.cc:138
std::string m_shell
Definition: oct-passwd.h:126
std::string passwd(void) const
Definition: oct-passwd.cc:60
std::string shell(void) const
Definition: oct-passwd.cc:105
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:854