GNU Octave  3.8.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-2013 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 Once the end of data has been reached, @code{getgrent} returns 0.\n\
70 @end deftypefn")
71 {
72  octave_value_list retval;
73 
74  retval(1) = std::string ();
75  retval(0) = 0;
76 
77  int nargin = args.length ();
78 
79  if (nargin == 0)
80  {
81  std::string msg;
82 
83  retval(1) = msg;
84  retval(0) = mk_gr_map (octave_group::getgrent (msg));
85  }
86  else
87  print_usage ();
88 
89  return retval;
90 }
91 
92 DEFUN (getgrgid, args, ,
93  "-*- texinfo -*-\n\
94 @deftypefn {Built-in Function} {@var{grp_struct} =} getgrgid (@var{gid}).\n\
95 Return the first entry from the group database with the group ID\n\
96 @var{gid}. If the group ID does not exist in the database,\n\
97 @code{getgrgid} returns 0.\n\
98 @end deftypefn")
99 {
100  octave_value_list retval;
101 
102  retval(1) = std::string ();
103  retval(0) = 0;
104 
105  int nargin = args.length ();
106 
107  if (nargin == 1)
108  {
109  double dval = args(0).double_value ();
110 
111  if (! error_state)
112  {
113  if (D_NINT (dval) == dval)
114  {
115  gid_t gid = static_cast<gid_t> (dval);
116 
117  std::string msg;
118 
119  retval(1) = msg;
120  retval(0) = mk_gr_map (octave_group::getgrgid (gid, msg));
121  }
122  else
123  error ("getgrgid: GID must be an integer");
124  }
125  }
126  else
127  print_usage ();
128 
129  return retval;
130 }
131 
132 DEFUN (getgrnam, args, ,
133  "-*- texinfo -*-\n\
134 @deftypefn {Built-in Function} {@var{grp_struct} =} getgrnam (@var{name})\n\
135 Return the first entry from the group database with the group name\n\
136 @var{name}. If the group name does not exist in the database,\n\
137 @code{getgrnam} returns 0.\n\
138 @end deftypefn")
139 {
140  octave_value_list retval;
141 
142  retval(1) = std::string ();
143  retval(0) = 0;
144 
145  int nargin = args.length ();
146 
147  if (nargin == 1)
148  {
149  std::string s = args(0).string_value ();
150 
151  if (! error_state)
152  {
153  std::string msg;
154 
155  retval(1) = msg;
156  retval(0) = mk_gr_map (octave_group::getgrnam (s.c_str (), msg));
157  }
158  }
159  else
160  print_usage ();
161 
162  return retval;
163 }
164 
165 DEFUN (setgrent, args, ,
166  "-*- texinfo -*-\n\
167 @deftypefn {Built-in Function} {} setgrent ()\n\
168 Return the internal pointer to the beginning of the group database.\n\
169 @end deftypefn")
170 {
171  octave_value_list retval;
172 
173  retval(1) = std::string ();
174  retval(0) = -1.0;
175 
176  int nargin = args.length ();
177 
178  if (nargin == 0)
179  {
180  std::string msg;
181 
182  retval(1) = msg;
183  retval(0) = static_cast<double> (octave_group::setgrent (msg));
184  }
185  else
186  print_usage ();
187 
188  return retval;
189 }
190 
191 DEFUN (endgrent, args, ,
192  "-*- texinfo -*-\n\
193 @deftypefn {Built-in Function} {} endgrent ()\n\
194 Close the group database.\n\
195 @end deftypefn")
196 {
197  octave_value_list retval;
198 
199  retval(1) = std::string ();
200  retval(0) = -1.0;
201 
202  int nargin = args.length ();
203 
204  if (nargin == 0)
205  {
206  std::string msg;
207 
208  retval(1) = msg;
209  retval(0) = static_cast<double> (octave_group::endgrent (msg));
210  }
211  else
212  print_usage ();
213 
214  return retval;
215 }