GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-password.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2026 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <sys/types.h>
31
32#include "oct-error.h"
33#include "oct-password.h"
34#include "pwd-wrappers.h"
35
36#define NOT_SUPPORTED(nm) \
37 nm ": not supported on this system"
38
39OCTAVE_NORETURN static
40void
41err_invalid ()
42{
43 (*current_liboctave_error_handler) ("invalid password object");
44}
45
48
49std::string
51{
52 if (! ok ())
53 err_invalid ();
54
55 return m_name;
56}
57
58std::string
60{
61 if (! ok ())
62 err_invalid ();
63
64 return m_passwd;
65}
66
67uid_t
69{
70 if (! ok ())
71 err_invalid ();
72
73 return m_uid;
74}
75
76gid_t
78{
79 if (! ok ())
80 err_invalid ();
81
82 return m_gid;
83}
84
85std::string
87{
88 if (! ok ())
89 err_invalid ();
90
91 return m_gecos;
92}
93
94std::string
96{
97 if (! ok ())
98 err_invalid ();
99
100 return m_dir;
101}
102
103std::string
105{
106 if (! ok ())
107 err_invalid ();
108
109 return m_shell;
110}
111
114{
115 std::string msg;
116 return getpwent (msg);
117}
118
120password::getpwent (std::string& msg)
121{
122#if defined (HAVE_GETPWENT)
123 msg = "";
124 return password (octave_getpwent_wrapper (), msg);
125#else
126 msg = NOT_SUPPORTED ("getpwent");
127 return password ();
128#endif
129}
130
133{
134 std::string msg;
135 return getpwuid (uid, msg);
136}
137
139password::getpwuid (uid_t uid, std::string& msg)
140{
141#if defined (HAVE_GETPWUID)
142 msg = "";
143 return password (octave_getpwuid_wrapper (uid), msg);
144#else
145 octave_unused_parameter (uid);
146
147 msg = NOT_SUPPORTED ("getpwuid");
148 return password ();
149#endif
150}
151
153password::getpwnam (const std::string& nm)
154{
155 std::string msg;
156 return getpwnam (nm, msg);
157}
158
160password::getpwnam (const std::string& nm, std::string& msg)
161{
162#if defined (HAVE_GETPWNAM)
163 msg = "";
164 return password (octave_getpwnam_wrapper (nm.c_str ()), msg);
165#else
166 octave_unused_parameter (nm);
167
168 msg = NOT_SUPPORTED ("getpwnam");
169 return password ();
170#endif
171}
172
173int
175{
176 std::string msg;
177 return setpwent (msg);
178}
179
180int
181password::setpwent (std::string& msg)
182{
183#if defined (HAVE_SETPWENT)
184 msg = "";
186 return 0;
187#else
188 msg = NOT_SUPPORTED ("setpwent");
189 return -1;
190#endif
191}
192
193int
195{
196 std::string msg;
197 return endpwent (msg);
198}
199
200int
201password::endpwent (std::string& msg)
202{
203#if defined (HAVE_ENDPWENT)
204 msg = "";
206 return 0;
207#else
208 msg = NOT_SUPPORTED ("endpwent");
209 return -1;
210#endif
211}
212
213password::password (void *p, std::string& msg)
214 : m_name (), m_passwd (), m_uid (0), m_gid (0), m_gecos (),
215 m_dir (), m_shell (), m_valid (false)
216{
217#if defined (HAVE_PWD_H)
218 msg = "";
219
220 if (p)
221 {
222 struct octave_passwd_wrapper pw;
223 octave_from_passwd (static_cast<struct ::passwd *> (p), &pw);
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 m_valid = true;
234 }
235#else
236 octave_unused_parameter (p);
237
238 msg = NOT_SUPPORTED ("password functions");
239#endif
240}
241
242OCTAVE_END_NAMESPACE(sys)
243OCTAVE_END_NAMESPACE(octave)
static password getpwnam(const std::string &nm)
static int endpwent()
static password getpwuid(uid_t uid)
std::string gecos() const
static int setpwent()
std::string dir() const
std::string name() const
std::string shell() const
bool ok() const
static password getpwent()
uid_t uid() const
gid_t gid() const
std::string passwd() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define NOT_SUPPORTED(nm)
Definition oct-group.cc:40
void octave_setpwent_wrapper(void)
void octave_endpwent_wrapper(void)
struct passwd * octave_getpwent_wrapper(void)
struct passwd * octave_getpwnam_wrapper(const char *nam)
struct passwd * octave_getpwuid_wrapper(uid_t uid)
void octave_from_passwd(const struct passwd *pw, struct octave_passwd_wrapper *oct_pw)