GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
getgrent.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <string>
31 
32 #include <sys/types.h>
33 
34 #include "oct-group.h"
35 
36 #include "defun.h"
37 #include "error.h"
38 #include "errwarn.h"
39 #include "oct-map.h"
40 #include "ov.h"
41 #include "ovl.h"
42 #include "utils.h"
43 
45 
46 // Group file functions. (Why not?)
47 
48 static octave_value
49 mk_gr_map (const sys::group& gr)
50 {
51  if (gr)
52  {
54 
55  m.assign ("name", gr.name ());
56  m.assign ("passwd", gr.passwd ());
57  m.assign ("gid", static_cast<double> (gr.gid ()));
58  m.assign ("mem", octave_value (gr.mem ()));
59 
60  return octave_value (m);
61  }
62  else
63  return octave_value (0);
64 }
65 
66 DEFUN (getgrent, args, ,
67  doc: /* -*- texinfo -*-
68 @deftypefn {} {@var{grp_struct} =} getgrent ()
69 Return an entry from the group database, opening it if necessary.
70 
71 Once the end of data has been reached, @code{getgrent} returns 0.
72 @seealso{setgrent, endgrent}
73 @end deftypefn */)
74 {
75  if (args.length () != 0)
76  print_usage ();
77 
78  std::string msg;
79 
80  // sys::group::getgrent may set msg.
81  octave_value val = mk_gr_map (sys::group::getgrent (msg));
82 
83  return ovl (val, msg);
84 }
85 
86 DEFUN (getgrgid, args, ,
87  doc: /* -*- texinfo -*-
88 @deftypefn {} {@var{grp_struct} =} getgrgid (@var{gid}).
89 Return the first entry from the group database with the group ID
90 @var{gid}.
91 
92 If the group ID does not exist in the database, @code{getgrgid} returns 0.
93 @seealso{getgrnam}
94 @end deftypefn */)
95 {
96  if (args.length () != 1)
97  print_usage ();
98 
99  double dval = args(0).double_value ();
100 
101  if (math::x_nint (dval) != dval)
102  error ("getgrgid: GID must be an integer");
103 
104  gid_t gid = static_cast<gid_t> (dval);
105 
106  std::string msg;
107 
108  // sys::group::getgrgid may set msg.
109  octave_value val = mk_gr_map (sys::group::getgrgid (gid, msg));
110 
111  return ovl (val, msg);
112 }
113 
114 DEFUN (getgrnam, args, ,
115  doc: /* -*- texinfo -*-
116 @deftypefn {} {@var{grp_struct} =} getgrnam (@var{name})
117 Return the first entry from the group database with the group name
118 @var{name}.
119 
120 If the group name does not exist in the database, @code{getgrnam} returns 0.
121 @seealso{getgrgid}
122 @end deftypefn */)
123 {
124  if (args.length () != 1)
125  print_usage ();
126 
127  std::string s = args(0).string_value ();
128 
129  std::string msg;
130 
131  // sys::group::getgrnam may set msg.
132  octave_value val = mk_gr_map (sys::group::getgrnam (s.c_str (), msg));
133 
134  return ovl (val, msg);
135 }
136 
137 DEFUN (setgrent, args, ,
138  doc: /* -*- texinfo -*-
139 @deftypefn {} {[@var{status}, @var{msg}] =} setgrent ()
140 Return the internal pointer to the beginning of the group database.
141 @seealso{getgrent, endgrent}
142 @end deftypefn */)
143 {
144  if (args.length () != 0)
145  print_usage ();
146 
147  std::string msg;
148 
149  // sys::group::setgrent may set msg.
150  int status = sys::group::setgrent (msg);
151 
152  return ovl (static_cast<double> (status), msg);
153 }
154 
155 DEFUN (endgrent, args, ,
156  doc: /* -*- texinfo -*-
157 @deftypefn {} {[@var{status}, @var{msg}] =} endgrent ()
158 Close the group database.
159 @seealso{getgrent, setgrent}
160 @end deftypefn */)
161 {
162  if (args.length () != 0)
163  print_usage ();
164 
165  std::string msg;
166 
167  // sys::group::endgrent may set msg.
168  int status = sys::group::endgrent (msg);
169 
170  return ovl (static_cast<double> (status), msg);
171 }
172 
173 OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage(void)
Definition: defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void() error(const char *fmt,...)
Definition: error.cc:988
T x_nint(T x)
Definition: lo-mappers.h:269
T octave_idx_type m
Definition: mx-inlines.cc:781
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219