GNU Octave  4.0.0
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-2015 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 #ifdef 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 "gripes.h"
36 #include "oct-map.h"
37 #include "ov.h"
38 #include "oct-obj.h"
39 #include "utils.h"
40 
41 // Password file functions. (Why not?)
42 
43 static octave_value
45 {
46  octave_value retval;
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 = m;
61  }
62  else
63  retval = 0;
64 
65  return retval;
66 }
67 
68 DEFUN (getpwent, args, ,
69  "-*- texinfo -*-\n\
70 @deftypefn {Built-in Function} {@var{pw_struct} =} getpwent ()\n\
71 Return a structure containing an entry from the password database,\n\
72 opening it if necessary.\n\
73 \n\
74 Once the end of the data has been reached, @code{getpwent} returns 0.\n\
75 @seealso{setpwent, endpwent}\n\
76 @end deftypefn")
77 {
78  octave_value_list retval;
79 
80  retval(1) = std::string ();
81  retval(0) = 0;
82 
83  int nargin = args.length ();
84 
85  if (nargin == 0)
86  {
87  std::string msg;
88 
89  retval(1) = msg;
90  retval(0) = mk_pw_map (octave_passwd::getpwent (msg));
91  }
92  else
93  print_usage ();
94 
95  return retval;
96 }
97 
98 DEFUN (getpwuid, args, ,
99  "-*- texinfo -*-\n\
100 @deftypefn {Built-in Function} {@var{pw_struct} =} getpwuid (@var{uid}).\n\
101 Return a structure containing the first entry from the password database\n\
102 with the user ID @var{uid}.\n\
103 \n\
104 If the user ID does not exist in the database, @code{getpwuid} returns 0.\n\
105 @seealso{getpwnam}\n\
106 @end deftypefn")
107 {
108  octave_value_list retval;
109 
110  retval(1) = std::string ();
111  retval(0) = 0;
112 
113  int nargin = args.length ();
114 
115  if (nargin == 1)
116  {
117  double dval = args(0).double_value ();
118 
119  if (! error_state)
120  {
121  if (D_NINT (dval) == dval)
122  {
123  uid_t uid = static_cast<uid_t> (dval);
124 
125  std::string msg;
126 
127  retval(1) = msg;
128  retval(0) = mk_pw_map (octave_passwd::getpwuid (uid, msg));
129  }
130  else
131  error ("getpwuid: UID must be an integer");
132  }
133  }
134  else
135  print_usage ();
136 
137  return retval;
138 }
139 
140 DEFUN (getpwnam, args, ,
141  "-*- texinfo -*-\n\
142 @deftypefn {Built-in Function} {@var{pw_struct} =} getpwnam (@var{name})\n\
143 Return a structure containing the first entry from the password database\n\
144 with the user name @var{name}.\n\
145 \n\
146 If the user name does not exist in the database, @code{getpwname} returns 0.\n\
147 @seealso{getpwuid}\n\
148 @end deftypefn")
149 {
150  octave_value_list retval;
151 
152  retval(1) = std::string ();
153  retval(0) = 0.0;
154 
155  int nargin = args.length ();
156 
157  if (nargin == 1)
158  {
159  std::string s = args(0).string_value ();
160 
161  if (! error_state)
162  {
163  std::string msg;
164 
165  retval(1) = msg;
166  retval(0) = mk_pw_map (octave_passwd::getpwnam (s, msg));
167  }
168  }
169  else
170  print_usage ();
171 
172  return retval;
173 }
174 
175 DEFUN (setpwent, args, ,
176  "-*- texinfo -*-\n\
177 @deftypefn {Built-in Function} {} setpwent ()\n\
178 Return the internal pointer to the beginning of the password database.\n\
179 @seealso{getpwent, endpwent}\n\
180 @end deftypefn")
181 {
182  octave_value_list retval;
183 
184  retval(1) = std::string ();
185  retval(0) = -1.0;
186 
187  int nargin = args.length ();
188 
189  if (nargin == 0)
190  {
191  std::string msg;
192 
193  retval(1) = msg;
194  retval(0) = static_cast<double> (octave_passwd::setpwent (msg));
195  }
196  else
197  print_usage ();
198 
199  return retval;
200 }
201 
202 DEFUN (endpwent, args, ,
203  "-*- texinfo -*-\n\
204 @deftypefn {Built-in Function} {} endpwent ()\n\
205 Close the password database.\n\
206 @seealso{getpwent, setpwent}\n\
207 @end deftypefn")
208 {
209  octave_value_list retval;
210 
211  retval(1) = std::string ();
212  retval(0) = -1.0;
213 
214  int nargin = args.length ();
215 
216  if (nargin == 0)
217  {
218  std::string msg;
219 
220  retval(1) = msg;
221  retval(0) = static_cast<double> (octave_passwd::endpwent (msg));
222  }
223  else
224  print_usage ();
225 
226  return retval;
227 }
std::string gecos(void) const
Definition: oct-passwd.cc:76
OCTINTERP_API void print_usage(void)
Definition: defun.cc:51
octave_idx_type length(void) const
Definition: oct-obj.h:89
uid_t uid(void) const
Definition: oct-passwd.cc:58
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
void error(const char *fmt,...)
Definition: error.cc:476
static int setpwent(void)
Definition: oct-passwd.cc:160
std::string dir(void) const
Definition: oct-passwd.cc:85
static int endpwent(void)
Definition: oct-passwd.cc:180
std::string shell(void) const
Definition: oct-passwd.cc:94
int error_state
Definition: error.cc:101
OCTAVE_API double D_NINT(double x)
Definition: lo-mappers.h:240
std::string name(void) const
Definition: oct-passwd.cc:40
static octave_value mk_pw_map(const octave_passwd &pw)
Definition: getpwent.cc:44
static octave_passwd getpwent(void)
Definition: oct-passwd.cc:103
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:225
std::string passwd(void) const
Definition: oct-passwd.cc:49
static octave_passwd getpwuid(uid_t uid)
Definition: oct-passwd.cc:122
static octave_passwd getpwnam(const std::string &nm)
Definition: oct-passwd.cc:141
gid_t gid(void) const
Definition: oct-passwd.cc:67