GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
getpwent.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 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 <string>
31 
32 #include <sys/types.h>
33 
34 #include "oct-password.h"
35 
36 #include "defun.h"
37 #include "error.h"
38 #include "errwarn.h"
39 #include "oct-map.h"
40 #include "ov.h"
41 #include "ovl.h"
42 #include "utils.h"
43 
45 
46 // Password file functions. (Why not?)
47 
48 static octave_value
49 mk_pw_map (const sys::password& pw)
50 {
51  octave_value retval;
52 
53  if (pw)
54  {
56 
57  m.assign ("name", pw.name ());
58  m.assign ("passwd", pw.passwd ());
59  m.assign ("uid", static_cast<double> (pw.uid ()));
60  m.assign ("gid", static_cast<double> (pw.gid ()));
61  m.assign ("gecos", pw.gecos ());
62  m.assign ("dir", pw.dir ());
63  m.assign ("shell", pw.shell ());
64 
65  return octave_value (m);
66  }
67  else
68  return octave_value (0);
69 }
70 
71 DEFUN (getpwent, args, ,
72  doc: /* -*- texinfo -*-
73 @deftypefn {} {@var{pw_struct} =} getpwent ()
74 Return a structure containing an entry from the password database,
75 opening it if necessary.
76 
77 Once the end of the data has been reached, @code{getpwent} returns 0.
78 @seealso{setpwent, endpwent}
79 @end deftypefn */)
80 {
81  if (args.length () != 0)
82  print_usage ();
83 
84  std::string msg;
85 
86  // sys::password::getpwent may set msg.
87  octave_value val = mk_pw_map (sys::password::getpwent (msg));
88 
89  return ovl (val, msg);
90 }
91 
92 DEFUN (getpwuid, args, ,
93  doc: /* -*- texinfo -*-
94 @deftypefn {} {@var{pw_struct} =} getpwuid (@var{uid}).
95 Return a structure containing the first entry from the password database
96 with the user ID @var{uid}.
97 
98 If the user ID does not exist in the database, @code{getpwuid} returns 0.
99 @seealso{getpwnam}
100 @end deftypefn */)
101 {
102  if (args.length () != 1)
103  print_usage ();
104 
105  double dval = args(0).double_value ();
106 
107  if (math::x_nint (dval) != dval)
108  error ("getpwuid: UID must be an integer");
109 
110  uid_t uid = static_cast<uid_t> (dval);
111 
112  std::string msg;
113 
114  // sys::password::getpwuid may set msg.
115  octave_value val = mk_pw_map (sys::password::getpwuid (uid, msg));
116 
117  return ovl (val, msg);
118 }
119 
120 DEFUN (getpwnam, args, ,
121  doc: /* -*- texinfo -*-
122 @deftypefn {} {@var{pw_struct} =} getpwnam (@var{name})
123 Return a structure containing the first entry from the password database
124 with the user name @var{name}.
125 
126 If the user name does not exist in the database, @code{getpwname} returns 0.
127 @seealso{getpwuid}
128 @end deftypefn */)
129 {
130  if (args.length () != 1)
131  print_usage ();
132 
133  std::string s = args(0).string_value ();
134 
135  std::string msg;
136 
137  // sys::password::getpwnam may set msg.
138  octave_value val = mk_pw_map (sys::password::getpwnam (s, msg));
139 
140  return ovl (val, msg);
141 }
142 
143 DEFUN (setpwent, args, ,
144  doc: /* -*- texinfo -*-
145 @deftypefn {} {[@var{status}, @var{msg}] =} setpwent ()
146 Return the internal pointer to the beginning of the password database.
147 @seealso{getpwent, endpwent}
148 @end deftypefn */)
149 {
150  if (args.length () != 0)
151  print_usage ();
152 
153  std::string msg;
154 
155  // sys::password::setpwent may set msg.
156  int status = sys::password::setpwent (msg);
157 
158  return ovl (static_cast<double> (status), msg);
159 }
160 
161 DEFUN (endpwent, args, ,
162  doc: /* -*- texinfo -*-
163 @deftypefn {} {[@var{status}, @var{msg}] =} endpwent ()
164 Close the password database.
165 @seealso{getpwent, setpwent}
166 @end deftypefn */)
167 {
168  if (args.length () != 0)
169  print_usage ();
170 
171  std::string msg;
172 
173  // sys::password::endpwent may set msg.
174  int status = sys::password::endpwent (msg);
175 
176  return ovl (static_cast<double> (status), msg);
177 }
178 
179 OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage(void)
Definition: defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void() error(const char *fmt,...)
Definition: error.cc:988
T x_nint(T x)
Definition: lo-mappers.h:269
T octave_idx_type m
Definition: mx-inlines.cc:781
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219