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
getpwent.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 <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  retval = ovl (m);
61  }
62  else
63  retval = ovl (0);
64 
65  return retval;
66 }
67 
68 DEFUN (getpwent, args, ,
69  doc: /* -*- texinfo -*-
70 @deftypefn {} {@var{pw_struct} =} getpwent ()
71 Return a structure containing an entry from the password database,
72 opening it if necessary.
73 
74 Once the end of the data has been reached, @code{getpwent} returns 0.
75 @seealso{setpwent, endpwent}
76 @end deftypefn */)
77 {
78  if (args.length () != 0)
79  print_usage ();
80 
81  std::string msg;
82 
83  // octave::sys::password::getpwent may set msg.
85 
86  return ovl (val, msg);
87 }
88 
89 DEFUN (getpwuid, args, ,
90  doc: /* -*- texinfo -*-
91 @deftypefn {} {@var{pw_struct} =} getpwuid (@var{uid}).
92 Return a structure containing the first entry from the password database
93 with the user ID @var{uid}.
94 
95 If the user ID does not exist in the database, @code{getpwuid} returns 0.
96 @seealso{getpwnam}
97 @end deftypefn */)
98 {
99  if (args.length () != 1)
100  print_usage ();
101 
102  double dval = args(0).double_value ();
103 
104  if (octave::math::x_nint (dval) != dval)
105  error ("getpwuid: UID must be an integer");
106 
107  uid_t uid = static_cast<uid_t> (dval);
108 
109  std::string msg;
110 
111  // octave::sys::password::getpwuid may set msg.
113 
114  return ovl (val, msg);
115 }
116 
117 DEFUN (getpwnam, args, ,
118  doc: /* -*- texinfo -*-
119 @deftypefn {} {@var{pw_struct} =} getpwnam (@var{name})
120 Return a structure containing the first entry from the password database
121 with the user name @var{name}.
122 
123 If the user name does not exist in the database, @code{getpwname} returns 0.
124 @seealso{getpwuid}
125 @end deftypefn */)
126 {
127  if (args.length () != 1)
128  print_usage ();
129 
130  std::string s = args(0).string_value ();
131 
132  std::string msg;
133 
134  // octave::sys::password::getpwnam may set msg.
136 
137  return ovl (val, msg);
138 }
139 
140 DEFUN (setpwent, args, ,
141  doc: /* -*- texinfo -*-
142 @deftypefn {} {} setpwent ()
143 Return the internal pointer to the beginning of the password database.
144 @seealso{getpwent, endpwent}
145 @end deftypefn */)
146 {
147  if (args.length () != 0)
148  print_usage ();
149 
150  std::string msg;
151 
152  // octave::sys::password::setpwent may set msg.
153  int status = octave::sys::password::setpwent (msg);
154 
155  return ovl (static_cast<double> (status), msg);
156 }
157 
158 DEFUN (endpwent, args, ,
159  doc: /* -*- texinfo -*-
160 @deftypefn {} {} endpwent ()
161 Close the password database.
162 @seealso{getpwent, setpwent}
163 @end deftypefn */)
164 {
165  if (args.length () != 0)
166  print_usage ();
167 
168  std::string msg;
169 
170  // octave::sys::password::endpwent may set msg.
171  int status = octave::sys::password::endpwent (msg);
172 
173  return ovl (static_cast<double> (status), msg);
174 }
static password getpwnam(const std::string &nm)
Definition: oct-passwd.cc:152
std::string gecos(void) const
Definition: oct-passwd.cc:87
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
OCTINTERP_API void print_usage(void)
Definition: defun.cc:52
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
uid_t uid(void) const
Definition: oct-passwd.cc:69
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
static password getpwuid(uid_t uid)
Definition: oct-passwd.cc:133
void error(const char *fmt,...)
Definition: error.cc:570
s
Definition: file-io.cc:2682
JNIEnv void * args
Definition: ov-java.cc:67
static password getpwent(void)
Definition: oct-passwd.cc:114
std::string dir(void) const
Definition: oct-passwd.cc:96
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
static int setpwent(void)
Definition: oct-passwd.cc:171
std::string name(void) const
Definition: oct-passwd.cc:51
static octave_value mk_pw_map(const octave::sys::password &pw)
Definition: getpwent.cc:44
octave_value retval
Definition: data.cc:6294
static int endpwent(void)
Definition: oct-passwd.cc:191
gid_t gid(void) const
Definition: oct-passwd.cc:78
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:223
std::string passwd(void) const
Definition: oct-passwd.cc:60
std::string shell(void) const
Definition: oct-passwd.cc:105
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
T x_nint(T x)
Definition: lo-mappers.h:299