GNU Octave  4.4.1
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-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 <string>
28 
29 #include <sys/types.h>
30 
31 #include "oct-passwd.h"
32 
33 #include "defun.h"
34 #include "error.h"
35 #include "errwarn.h"
36 #include "oct-map.h"
37 #include "ov.h"
38 #include "ovl.h"
39 #include "utils.h"
40 
41 // Password file functions. (Why not?)
42 
43 static octave_value
45 {
47 
48  if (pw)
49  {
51 
52  m.assign ("name", pw.name ());
53  m.assign ("passwd", pw.passwd ());
54  m.assign ("uid", static_cast<double> (pw.uid ()));
55  m.assign ("gid", static_cast<double> (pw.gid ()));
56  m.assign ("gecos", pw.gecos ());
57  m.assign ("dir", pw.dir ());
58  m.assign ("shell", pw.shell ());
59 
60  return octave_value (m);
61  }
62  else
63  return octave_value (0);
64 }
65 
66 DEFUN (getpwent, args, ,
67  doc: /* -*- texinfo -*-
68 @deftypefn {} {@var{pw_struct} =} getpwent ()
69 Return a structure containing an entry from the password database,
70 opening it if necessary.
71 
72 Once the end of the data has been reached, @code{getpwent} returns 0.
73 @seealso{setpwent, endpwent}
74 @end deftypefn */)
75 {
76  if (args.length () != 0)
77  print_usage ();
78 
79  std::string msg;
80 
81  // octave::sys::password::getpwent may set msg.
83 
84  return ovl (val, msg);
85 }
86 
87 DEFUN (getpwuid, args, ,
88  doc: /* -*- texinfo -*-
89 @deftypefn {} {@var{pw_struct} =} getpwuid (@var{uid}).
90 Return a structure containing the first entry from the password database
91 with the user ID @var{uid}.
92 
93 If the user ID does not exist in the database, @code{getpwuid} returns 0.
94 @seealso{getpwnam}
95 @end deftypefn */)
96 {
97  if (args.length () != 1)
98  print_usage ();
99 
100  double dval = args(0).double_value ();
101 
102  if (octave::math::x_nint (dval) != dval)
103  error ("getpwuid: UID must be an integer");
104 
105  uid_t uid = static_cast<uid_t> (dval);
106 
107  std::string msg;
108 
109  // octave::sys::password::getpwuid may set msg.
111 
112  return ovl (val, msg);
113 }
114 
115 DEFUN (getpwnam, args, ,
116  doc: /* -*- texinfo -*-
117 @deftypefn {} {@var{pw_struct} =} getpwnam (@var{name})
118 Return a structure containing the first entry from the password database
119 with the user name @var{name}.
120 
121 If the user name does not exist in the database, @code{getpwname} returns 0.
122 @seealso{getpwuid}
123 @end deftypefn */)
124 {
125  if (args.length () != 1)
126  print_usage ();
127 
128  std::string s = args(0).string_value ();
129 
130  std::string msg;
131 
132  // octave::sys::password::getpwnam may set msg.
134 
135  return ovl (val, msg);
136 }
137 
138 DEFUN (setpwent, args, ,
139  doc: /* -*- texinfo -*-
140 @deftypefn {} {} setpwent ()
141 Return the internal pointer to the beginning of the password database.
142 @seealso{getpwent, endpwent}
143 @end deftypefn */)
144 {
145  if (args.length () != 0)
146  print_usage ();
147 
148  std::string msg;
149 
150  // octave::sys::password::setpwent may set msg.
151  int status = octave::sys::password::setpwent (msg);
152 
153  return ovl (static_cast<double> (status), msg);
154 }
155 
156 DEFUN (endpwent, args, ,
157  doc: /* -*- texinfo -*-
158 @deftypefn {} {} endpwent ()
159 Close the password database.
160 @seealso{getpwent, setpwent}
161 @end deftypefn */)
162 {
163  if (args.length () != 0)
164  print_usage ();
165 
166  std::string msg;
167 
168  // octave::sys::password::endpwent may set msg.
169  int status = octave::sys::password::endpwent (msg);
170 
171  return ovl (static_cast<double> (status), msg);
172 }
static password getpwnam(const std::string &nm)
Definition: oct-passwd.cc:154
std::string dir(void) const
Definition: oct-passwd.cc:96
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
std::string gecos(void) const
Definition: oct-passwd.cc:87
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
static password getpwuid(uid_t uid)
Definition: oct-passwd.cc:133
void error(const char *fmt,...)
Definition: error.cc:578
s
Definition: file-io.cc:2729
static password getpwent(void)
Definition: oct-passwd.cc:114
uid_t uid(void) const
Definition: oct-passwd.cc:69
static int setpwent(void)
Definition: oct-passwd.cc:175
static octave_value mk_pw_map(const octave::sys::password &pw)
Definition: getpwent.cc:44
octave_value retval
Definition: data.cc:6246
static int endpwent(void)
Definition: oct-passwd.cc:195
std::string name(void) const
Definition: oct-passwd.cc:51
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
std::string passwd(void) const
Definition: oct-passwd.cc:60
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:227
std::string shell(void) const
Definition: oct-passwd.cc:105
gid_t gid(void) const
Definition: oct-passwd.cc:78
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
T x_nint(T x)
Definition: lo-mappers.h:284