GNU Octave  4.4.1
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-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 <sys/types.h>
28 
29 #if defined (HAVE_GRP_H)
30 # include <grp.h>
31 #endif
32 
33 #include "lo-error.h"
34 #include "oct-group.h"
35 #include "str-vec.h"
36 
37 #define NOT_SUPPORTED(nm) \
38  nm ": not supported on this system"
39 
40 OCTAVE_NORETURN static
41 void
43 {
44  (*current_liboctave_error_handler) ("invalid group object");
45 }
46 
47 namespace octave
48 {
49  namespace sys
50  {
52  group::name (void) const
53  {
54  if (! ok ())
55  err_invalid ();
56 
57  return m_name;
58  }
59 
61  group::passwd (void) const
62  {
63  if (! ok ())
64  err_invalid ();
65 
66  return m_passwd;
67  }
68 
69  gid_t
70  group::gid (void) const
71  {
72  if (! ok ())
73  err_invalid ();
74 
75  return m_gid;
76  }
77 
79  group::mem (void) const
80  {
81  if (! ok ())
82  err_invalid ();
83 
84  return m_mem;
85  }
86 
87  group
89  {
90  std::string msg;
91  return getgrent (msg);
92  }
93 
94  group
96  {
97 #if defined (HAVE_GETGRENT)
98  msg = "";
99  return group (::getgrent (), msg);
100 #else
101  msg = NOT_SUPPORTED ("getgrent");
102  return group ();
103 #endif
104  }
105 
106  group
107  group::getgrgid (gid_t gid)
108  {
109  std::string msg;
110  return getgrgid (gid, msg);
111  }
112 
113  group
114  group::getgrgid (gid_t gid, std::string& msg)
115  {
116 #if defined (HAVE_GETGRGID)
117  msg = "";
118  return group (::getgrgid (gid), msg);
119 #else
120  octave_unused_parameter (gid);
121 
122  msg = NOT_SUPPORTED ("getgruid");
123  return group ();
124 #endif
125  }
126 
127  group
129  {
130  std::string msg;
131  return getgrnam (nm, msg);
132  }
133 
134  group
136  {
137 #if defined (HAVE_GETGRNAM)
138  msg = "";
139  return group (::getgrnam (nm.c_str ()), msg);
140 #else
141  octave_unused_parameter (nm);
142 
143  msg = NOT_SUPPORTED ("getgrnam");
144  return group ();
145 #endif
146  }
147 
148  int
150  {
151  std::string msg;
152  return setgrent (msg);
153  }
154 
155  int
157  {
158 #if defined (HAVE_SETGRENT)
159  msg = "";
160  ::setgrent ();
161  return 0;
162 #else
163  msg = NOT_SUPPORTED ("setgrent");
164  return -1;
165 #endif
166  }
167 
168  int
170  {
171  std::string msg;
172  return endgrent (msg);
173  }
174 
175  int
177  {
178 #if defined (HAVE_ENDGRENT)
179  msg = "";
180  ::endgrent ();
181  return 0;
182 #else
183  msg = NOT_SUPPORTED ("endgrent");
184  return -1;
185 #endif
186  }
187 
188  group::group (void *p, std::string& msg)
189  : m_name (), m_passwd (), m_gid (0), m_mem (), valid (false)
190  {
191 #if defined (HAVE_GRP_H)
192  msg = "";
193 
194  if (p)
195  {
196  struct ::group *gr = static_cast<struct ::group *> (p);
197 
198  m_name = gr->gr_name;
199 
200 #if defined (HAVE_GR_PASSWD)
201  m_passwd = gr->gr_passwd;
202 #endif
203 
204  m_gid = gr->gr_gid;
205 
206  // FIXME: Maybe there should be a string_vector constructor
207  // that takes a NUL terminated list of C strings?
208 
209  const char * const *tmp = gr->gr_mem;
210 
211  int k = 0;
212  while (*tmp++)
213  k++;
214 
215  if (k > 0)
216  {
217  tmp = gr->gr_mem;
218 
219  m_mem.resize (k);
220 
221  for (int i = 0; i < k; i++)
222  m_mem[i] = tmp[i];
223  }
224 
225  valid = true;
226  }
227 #else
228  octave_unused_parameter (p);
229 
230  msg = NOT_SUPPORTED ("group functions");
231 #endif
232  }
233  }
234 }
gid_t gid(void) const
Definition: oct-group.cc:70
string_vector m_mem
Definition: oct-group.h:106
create a structure array and initialize its values The dimensions of each cell array of values must match Singleton cells and non cell values are repeated so that they fill the entire array If the cells are create an empty structure array with the specified field names If the argument is an return the underlying struct Observe that the syntax is optimized for struct trong struct("foo", 1) esult
Definition: ov-struct.cc:1736
for large enough k
Definition: lu.cc:617
std::string passwd(void) const
Definition: oct-group.cc:61
void resize(octave_idx_type n, const std::string &rfv="")
Definition: str-vec.h:97
bool ok(void) const
Definition: oct-group.h:75
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
static group getgrnam(const std::string &nm)
Definition: oct-group.cc:128
std::string name(void) const
Definition: oct-group.cc:52
std::string m_passwd
Definition: oct-group.h:100
double tmp
Definition: data.cc:6252
is false
Definition: cellfun.cc:400
#define NOT_SUPPORTED(nm)
Definition: oct-group.cc:37
std::string m_name
Definition: oct-group.h:97
static OCTAVE_NORETURN void err_invalid(void)
Definition: oct-group.cc:42
static group getgrent(void)
Definition: oct-group.cc:88
p
Definition: lu.cc:138
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
for i
Definition: data.cc:5264
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