GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
getgrent.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://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-group.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 // Group file functions. (Why not?)
42 
43 static octave_value
45 {
46  if (gr)
47  {
49 
50  m.assign ("name", gr.name ());
51  m.assign ("passwd", gr.passwd ());
52  m.assign ("gid", static_cast<double> (gr.gid ()));
53  m.assign ("mem", octave_value (gr.mem ()));
54 
55  return octave_value (m);
56  }
57  else
58  return octave_value (0);
59 }
60 
61 DEFUN (getgrent, args, ,
62  doc: /* -*- texinfo -*-
63 @deftypefn {} {@var{grp_struct} =} getgrent ()
64 Return an entry from the group database, opening it if necessary.
65 
66 Once the end of data has been reached, @code{getgrent} returns 0.
67 @seealso{setgrent, endgrent}
68 @end deftypefn */)
69 {
70  if (args.length () != 0)
71  print_usage ();
72 
73  std::string msg;
74 
75  // octave::sys::group::getgrent may set msg.
77 
78  return ovl (val, msg);
79 }
80 
81 DEFUN (getgrgid, args, ,
82  doc: /* -*- texinfo -*-
83 @deftypefn {} {@var{grp_struct} =} getgrgid (@var{gid}).
84 Return the first entry from the group database with the group ID
85 @var{gid}.
86 
87 If the group ID does not exist in the database, @code{getgrgid} returns 0.
88 @seealso{getgrnam}
89 @end deftypefn */)
90 {
91  if (args.length () != 1)
92  print_usage ();
93 
94  double dval = args(0).double_value ();
95 
96  if (octave::math::x_nint (dval) != dval)
97  error ("getgrgid: GID must be an integer");
98 
99  gid_t gid = static_cast<gid_t> (dval);
100 
101  std::string msg;
102 
103  // octave::sys::group::getgrgid may set msg.
105 
106  return ovl (val, msg);
107 }
108 
109 DEFUN (getgrnam, args, ,
110  doc: /* -*- texinfo -*-
111 @deftypefn {} {@var{grp_struct} =} getgrnam (@var{name})
112 Return the first entry from the group database with the group name
113 @var{name}.
114 
115 If the group name does not exist in the database, @code{getgrnam} returns 0.
116 @seealso{getgrgid}
117 @end deftypefn */)
118 {
119  if (args.length () != 1)
120  print_usage ();
121 
122  std::string s = args(0).string_value ();
123 
124  std::string msg;
125 
126  // octave::sys::group::getgrnam may set msg.
128 
129  return ovl (val, msg);
130 }
131 
132 DEFUN (setgrent, args, ,
133  doc: /* -*- texinfo -*-
134 @deftypefn {} {} setgrent ()
135 Return the internal pointer to the beginning of the group database.
136 @seealso{getgrent, endgrent}
137 @end deftypefn */)
138 {
139  if (args.length () != 0)
140  print_usage ();
141 
142  std::string msg;
143 
144  // octave::sys::group::setgrent may set msg.
145  int status = octave::sys::group::setgrent (msg);
146 
147  return ovl (static_cast<double> (status), msg);
148 }
149 
150 DEFUN (endgrent, args, ,
151  doc: /* -*- texinfo -*-
152 @deftypefn {} {} endgrent ()
153 Close the group database.
154 @seealso{getgrent, setgrent}
155 @end deftypefn */)
156 {
157  if (args.length () != 0)
158  print_usage ();
159 
160  std::string msg;
161 
162  // octave::sys::group::endgrent may set msg.
163  int status = octave::sys::group::endgrent (msg);
164 
165  return ovl (static_cast<double> (status), msg);
166 }
gid_t gid(void) const
Definition: oct-group.cc:70
static octave_value mk_gr_map(const octave::sys::group &gr)
Definition: getgrent.cc:44
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
void error(const char *fmt,...)
Definition: error.cc:578
std::string passwd(void) const
Definition: oct-group.cc:61
s
Definition: file-io.cc:2729
static group getgrnam(const std::string &nm)
Definition: oct-group.cc:128
std::string name(void) const
Definition: oct-group.cc:52
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
static group getgrent(void)
Definition: oct-group.cc:88
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:227
static int endgrent(void)
Definition: oct-group.cc:169
string_vector mem(void) const
Definition: oct-group.cc:79
static group getgrgid(gid_t gid)
Definition: oct-group.cc:107
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:888
static int setgrent(void)
Definition: oct-group.cc:149
T x_nint(T x)
Definition: lo-mappers.h:284