GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-group.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2026 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 <sys/types.h>
31
32#if defined (HAVE_GRP_H)
33# include <grp.h>
34#endif
35
36#include "oct-error.h"
37#include "oct-group.h"
38#include "str-vec.h"
39
40#define NOT_SUPPORTED(nm) \
41 nm ": not supported on this system"
42
43OCTAVE_NORETURN static
44void
45err_invalid ()
46{
47 (*current_liboctave_error_handler) ("invalid group object");
48}
49
52
53std::string
55{
56 if (! ok ())
57 err_invalid ();
58
59 return m_name;
60}
61
62std::string
64{
65 if (! ok ())
66 err_invalid ();
67
68 return m_passwd;
69}
70
71gid_t
72group::gid () const
73{
74 if (! ok ())
75 err_invalid ();
76
77 return m_gid;
78}
79
81group::mem () const
82{
83 if (! ok ())
84 err_invalid ();
85
86 return m_mem;
87}
88
91{
92 std::string msg;
93 return getgrent (msg);
94}
95
97group::getgrent (std::string& msg)
98{
99#if defined (HAVE_GETGRENT)
100 msg = "";
101 return group (::getgrent (), msg);
102#else
103 msg = NOT_SUPPORTED ("getgrent");
104 return group ();
105#endif
106}
107
108group
110{
111 std::string msg;
112 return getgrgid (gid, msg);
113}
114
115group
116group::getgrgid (gid_t gid, std::string& msg)
117{
118#if defined (HAVE_GETGRGID)
119 msg = "";
120 return group (::getgrgid (gid), msg);
121#else
122 octave_unused_parameter (gid);
123
124 msg = NOT_SUPPORTED ("getgruid");
125 return group ();
126#endif
127}
128
129group
130group::getgrnam (const std::string& nm)
131{
132 std::string msg;
133 return getgrnam (nm, msg);
134}
135
136group
137group::getgrnam (const std::string& nm, std::string& msg)
138{
139#if defined (HAVE_GETGRNAM)
140 msg = "";
141 return group (::getgrnam (nm.c_str ()), msg);
142#else
143 octave_unused_parameter (nm);
144
145 msg = NOT_SUPPORTED ("getgrnam");
146 return group ();
147#endif
148}
149
150int
152{
153 std::string msg;
154 return setgrent (msg);
155}
156
157int
158group::setgrent (std::string& msg)
159{
160#if defined (HAVE_SETGRENT)
161 msg = "";
162 ::setgrent ();
163 return 0;
164#else
165 msg = NOT_SUPPORTED ("setgrent");
166 return -1;
167#endif
168}
169
170int
172{
173 std::string msg;
174 return endgrent (msg);
175}
176
177int
178group::endgrent (std::string& msg)
179{
180#if defined (HAVE_ENDGRENT)
181 msg = "";
182 ::endgrent ();
183 return 0;
184#else
185 msg = NOT_SUPPORTED ("endgrent");
186 return -1;
187#endif
188}
189
190group::group (void *p, std::string& msg)
191 : m_name (), m_passwd (), m_gid (0), m_mem (), m_valid (false)
192{
193#if defined (HAVE_GRP_H)
194 msg = "";
195
196 if (p)
197 {
198 struct ::group *gr = static_cast<struct ::group *> (p);
199
200 m_name = gr->gr_name;
201
202#if defined (HAVE_GR_PASSWD)
203 m_passwd = gr->gr_passwd;
204#endif
205
206 m_gid = gr->gr_gid;
207
208 m_mem = string_vector (gr->gr_mem);
209
210 m_valid = true;
211 }
212#else
213 octave_unused_parameter (p);
214
215 msg = NOT_SUPPORTED ("group functions");
216#endif
217}
218
219OCTAVE_END_NAMESPACE(sys)
220OCTAVE_END_NAMESPACE(octave)
std::string name() const
Definition oct-group.cc:54
static int endgrent()
Definition oct-group.cc:171
std::string passwd() const
Definition oct-group.cc:63
static group getgrnam(const std::string &nm)
Definition oct-group.cc:130
gid_t gid() const
Definition oct-group.cc:72
string_vector mem() const
Definition oct-group.cc:81
static group getgrgid(gid_t gid)
Definition oct-group.cc:109
bool ok() const
Definition oct-group.h:58
static group getgrent()
Definition oct-group.cc:90
static int setgrent()
Definition oct-group.cc:151
group()
Definition oct-group.h:44
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define NOT_SUPPORTED(nm)
Definition oct-group.cc:40