GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-group.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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 
43 OCTAVE_NORETURN static
44 void
46 {
47  (*current_liboctave_error_handler) ("invalid group object");
48 }
49 
50 namespace octave
51 {
52  namespace sys
53  {
54  std::string
55  group::name (void) const
56  {
57  if (! ok ())
58  err_invalid ();
59 
60  return m_name;
61  }
62 
63  std::string
64  group::passwd (void) const
65  {
66  if (! ok ())
67  err_invalid ();
68 
69  return m_passwd;
70  }
71 
72  gid_t
73  group::gid (void) const
74  {
75  if (! ok ())
76  err_invalid ();
77 
78  return m_gid;
79  }
80 
82  group::mem (void) const
83  {
84  if (! ok ())
85  err_invalid ();
86 
87  return m_mem;
88  }
89 
90  group
92  {
93  std::string msg;
94  return getgrent (msg);
95  }
96 
97  group
98  group::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 
109  group
110  group::getgrgid (gid_t gid)
111  {
112  std::string msg;
113  return getgrgid (gid, msg);
114  }
115 
116  group
117  group::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 
130  group
131  group::getgrnam (const std::string& nm)
132  {
133  std::string msg;
134  return getgrnam (nm, msg);
135  }
136 
137  group
138  group::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 
151  int
153  {
154  std::string msg;
155  return setgrent (msg);
156  }
157 
158  int
159  group::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 
171  int
173  {
174  std::string msg;
175  return endgrent (msg);
176  }
177 
178  int
179  group::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 
191  group::group (void *p, std::string& msg)
192  : m_name (), m_passwd (), m_gid (0), m_mem (), 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  valid = true;
229  }
230 #else
231  octave_unused_parameter (p);
232 
233  msg = NOT_SUPPORTED ("group functions");
234 #endif
235  }
236  }
237 }
string_vector m_mem
Definition: oct-group.h:109
static int setgrent(void)
Definition: oct-group.cc:152
bool ok(void) const
Definition: oct-group.h:78
std::string m_passwd
Definition: oct-group.h:103
static group getgrent(void)
Definition: oct-group.cc:91
string_vector mem(void) const
Definition: oct-group.cc:82
gid_t gid(void) const
Definition: oct-group.cc:73
std::string passwd(void) const
Definition: oct-group.cc:64
static group getgrgid(gid_t gid)
Definition: oct-group.cc:110
static int endgrent(void)
Definition: oct-group.cc:172
static group getgrnam(const std::string &nm)
Definition: oct-group.cc:131
std::string name(void) const
Definition: oct-group.cc:55
std::string m_name
Definition: oct-group.h:100
void resize(octave_idx_type n, const std::string &rfv="")
Definition: str-vec.h:95
static OCTAVE_NORETURN void err_invalid(void)
Definition: oct-group.cc:45
#define NOT_SUPPORTED(nm)
Definition: oct-group.cc:40