GNU Octave  4.2.1
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-2017 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 #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 {
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 = ovl (m);
58  }
59  else
60  retval = ovl (0);
61 
62  return retval;
63 }
64 
65 DEFUN (getgrent, args, ,
66  doc: /* -*- texinfo -*-
67 @deftypefn {} {@var{grp_struct} =} getgrent ()
68 Return an entry from the group database, opening it if necessary.
69 
70 Once the end of data has been reached, @code{getgrent} returns 0.
71 @seealso{setgrent, endgrent}
72 @end deftypefn */)
73 {
74  if (args.length () != 0)
75  print_usage ();
76 
77  std::string msg;
78 
79  // octave::sys::group::getgrent may set msg.
81 
82  return ovl (val, msg);
83 }
84 
85 DEFUN (getgrgid, args, ,
86  doc: /* -*- texinfo -*-
87 @deftypefn {} {@var{grp_struct} =} getgrgid (@var{gid}).
88 Return the first entry from the group database with the group ID
89 @var{gid}.
90 
91 If the group ID does not exist in the database, @code{getgrgid} returns 0.
92 @seealso{getgrnam}
93 @end deftypefn */)
94 {
95  if (args.length () != 1)
96  print_usage ();
97 
98  double dval = args(0).double_value ();
99 
100  if (octave::math::x_nint (dval) != dval)
101  error ("getgrgid: GID must be an integer");
102 
103  gid_t gid = static_cast<gid_t> (dval);
104 
105  std::string msg;
106 
107  // octave::sys::group::getgrgid may set msg.
109 
110  return ovl (val, msg);
111 }
112 
113 DEFUN (getgrnam, args, ,
114  doc: /* -*- texinfo -*-
115 @deftypefn {} {@var{grp_struct} =} getgrnam (@var{name})
116 Return the first entry from the group database with the group name
117 @var{name}.
118 
119 If the group name does not exist in the database, @code{getgrnam} returns 0.
120 @seealso{getgrgid}
121 @end deftypefn */)
122 {
123  if (args.length () != 1)
124  print_usage ();
125 
126  std::string s = args(0).string_value ();
127 
128  std::string msg;
129 
130  // octave::sys::group::getgrnam may set msg.
131  octave_value val = mk_gr_map (octave::sys::group::getgrnam (s.c_str (), msg));
132 
133  return ovl (val, msg);
134 }
135 
136 DEFUN (setgrent, args, ,
137  doc: /* -*- texinfo -*-
138 @deftypefn {} {} setgrent ()
139 Return the internal pointer to the beginning of the group database.
140 @seealso{getgrent, endgrent}
141 @end deftypefn */)
142 {
143  if (args.length () != 0)
144  print_usage ();
145 
146  std::string msg;
147 
148  // octave::sys::group::setgrent may set msg.
149  int status = octave::sys::group::setgrent (msg);
150 
151  return ovl (static_cast<double> (status), msg);
152 }
153 
154 DEFUN (endgrent, args, ,
155  doc: /* -*- texinfo -*-
156 @deftypefn {} {} endgrent ()
157 Close the group database.
158 @seealso{getgrent, setgrent}
159 @end deftypefn */)
160 {
161  if (args.length () != 0)
162  print_usage ();
163 
164  std::string msg;
165 
166  // octave::sys::group::endgrent may set msg.
167  int status = octave::sys::group::endgrent (msg);
168 
169  return ovl (static_cast<double> (status), msg);
170 }
static octave_value mk_gr_map(const octave::sys::group &gr)
Definition: getgrent.cc:44
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
OCTINTERP_API void print_usage(void)
Definition: defun.cc:52
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
void error(const char *fmt,...)
Definition: error.cc:570
s
Definition: file-io.cc:2682
JNIEnv void * args
Definition: ov-java.cc:67
static group getgrnam(const std::string &nm)
Definition: oct-group.cc:126
std::string passwd(void) const
Definition: oct-group.cc:61
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
octave_value retval
Definition: data.cc:6294
std::string name(void) const
Definition: oct-group.cc:52
gid_t gid(void) const
Definition: oct-group.cc:70
static group getgrent(void)
Definition: oct-group.cc:88
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:223
static int endgrent(void)
Definition: oct-group.cc:165
static group getgrgid(gid_t gid)
Definition: oct-group.cc:107
string_vector mem(void) const
Definition: oct-group.cc:79
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:854
static int setgrent(void)
Definition: oct-group.cc:145
T x_nint(T x)
Definition: lo-mappers.h:299