GNU Octave 10.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-2025 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 "lo-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
51
53
54std::string
56{
57 if (! ok ())
58 err_invalid ();
59
60 return m_name;
61}
62
63std::string
65{
66 if (! ok ())
67 err_invalid ();
68
69 return m_passwd;
70}
71
72gid_t
73group::gid () const
74{
75 if (! ok ())
76 err_invalid ();
77
78 return m_gid;
79}
80
82group::mem () const
83{
84 if (! ok ())
85 err_invalid ();
86
87 return m_mem;
88}
89
92{
93 std::string msg;
94 return getgrent (msg);
95}
96
98group::getgrent (std::string& msg)
99{
100#if defined (HAVE_GETGRENT)
101 msg = "";
102 return group (::getgrent (), msg);
103#else
104 msg = NOT_SUPPORTED ("getgrent");
105 return group ();
106#endif
107}
108
109group
111{
112 std::string msg;
113 return getgrgid (gid, msg);
114}
115
116group
117group::getgrgid (gid_t gid, std::string& msg)
118{
119#if defined (HAVE_GETGRGID)
120 msg = "";
121 return group (::getgrgid (gid), msg);
122#else
123 octave_unused_parameter (gid);
124
125 msg = NOT_SUPPORTED ("getgruid");
126 return group ();
127#endif
128}
129
130group
131group::getgrnam (const std::string& nm)
132{
133 std::string msg;
134 return getgrnam (nm, msg);
135}
136
137group
138group::getgrnam (const std::string& nm, std::string& msg)
139{
140#if defined (HAVE_GETGRNAM)
141 msg = "";
142 return group (::getgrnam (nm.c_str ()), msg);
143#else
144 octave_unused_parameter (nm);
145
146 msg = NOT_SUPPORTED ("getgrnam");
147 return group ();
148#endif
149}
150
151int
153{
154 std::string msg;
155 return setgrent (msg);
156}
157
158int
159group::setgrent (std::string& msg)
160{
161#if defined (HAVE_SETGRENT)
162 msg = "";
163 ::setgrent ();
164 return 0;
165#else
166 msg = NOT_SUPPORTED ("setgrent");
167 return -1;
168#endif
169}
170
171int
173{
174 std::string msg;
175 return endgrent (msg);
176}
177
178int
179group::endgrent (std::string& msg)
180{
181#if defined (HAVE_ENDGRENT)
182 msg = "";
183 ::endgrent ();
184 return 0;
185#else
186 msg = NOT_SUPPORTED ("endgrent");
187 return -1;
188#endif
189}
190
191group::group (void *p, std::string& msg)
192 : m_name (), m_passwd (), m_gid (0), m_mem (), m_valid (false)
193{
194#if defined (HAVE_GRP_H)
195 msg = "";
196
197 if (p)
198 {
199 struct ::group *gr = static_cast<struct ::group *> (p);
200
201 m_name = gr->gr_name;
202
203#if defined (HAVE_GR_PASSWD)
204 m_passwd = gr->gr_passwd;
205#endif
206
207 m_gid = gr->gr_gid;
208
209 // FIXME: Maybe there should be a string_vector constructor
210 // that takes a NUL terminated list of C strings?
211
212 const char *const *tmp = gr->gr_mem;
213
214 int k = 0;
215 while (*tmp++)
216 k++;
217
218 if (k > 0)
219 {
220 tmp = gr->gr_mem;
221
222 m_mem.resize (k);
223
224 for (int i = 0; i < k; i++)
225 m_mem[i] = tmp[i];
226 }
227
228 m_valid = true;
229 }
230#else
231 octave_unused_parameter (p);
232
233 msg = NOT_SUPPORTED ("group functions");
234#endif
235}
236
237OCTAVE_END_NAMESPACE(sys)
238OCTAVE_END_NAMESPACE(octave)
std::string name() const
Definition oct-group.cc:55
static int endgrent()
Definition oct-group.cc:172
std::string passwd() const
Definition oct-group.cc:64
static group getgrnam(const std::string &nm)
Definition oct-group.cc:131
gid_t gid() const
Definition oct-group.cc:73
string_vector mem() const
Definition oct-group.cc:82
static group getgrgid(gid_t gid)
Definition oct-group.cc:110
bool ok() const
Definition oct-group.h:59
static group getgrent()
Definition oct-group.cc:91
static int setgrent()
Definition oct-group.cc:152
group()
Definition oct-group.h:45
void resize(octave_idx_type n, const std::string &rfv="")
Definition str-vec.h:93
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define NOT_SUPPORTED(nm)
Definition oct-group.cc:40