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