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
oct-group.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 <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  msg = NOT_SUPPORTED ("getgruid");
121  return group ();
122 #endif
123  }
124 
125  group
127  {
128  std::string msg;
129  return getgrnam (nm, msg);
130  }
131 
132  group
134  {
135 #if defined (HAVE_GETGRNAM)
136  msg = "";
137  return group (::getgrnam (nm.c_str ()), msg);
138 #else
139  msg = NOT_SUPPORTED ("getgrnam");
140  return group ();
141 #endif
142  }
143 
144  int
146  {
147  std::string msg;
148  return setgrent (msg);
149  }
150 
151  int
153  {
154 #if defined (HAVE_SETGRENT)
155  msg = "";
156  ::setgrent ();
157  return 0;
158 #else
159  msg = NOT_SUPPORTED ("setgrent");
160  return -1;
161 #endif
162  }
163 
164  int
166  {
167  std::string msg;
168  return endgrent (msg);
169  }
170 
171  int
173  {
174 #if defined (HAVE_ENDGRENT)
175  msg = "";
176  ::endgrent ();
177  return 0;
178 #else
179  msg = NOT_SUPPORTED ("endgrent");
180  return -1;
181 #endif
182  }
183 
184  group::group (void *p, std::string& msg)
185  : m_name (), m_passwd (), m_gid (0), m_mem (), valid (false)
186  {
187 #if defined (HAVE_GRP_H)
188  msg = "";
189 
190  if (p)
191  {
192  struct ::group *gr = static_cast<struct ::group *> (p);
193 
194  m_name = gr->gr_name;
195 
196 #if defined (HAVE_GR_PASSWD)
197  m_passwd = gr->gr_passwd;
198 #endif
199 
200  m_gid = gr->gr_gid;
201 
202  // FIXME: Maybe there should be a string_vector constructor
203  // that takes a NUL terminated list of C strings?
204 
205  const char * const *tmp = gr->gr_mem;
206 
207  int k = 0;
208  while (*tmp++)
209  k++;
210 
211  if (k > 0)
212  {
213  tmp = gr->gr_mem;
214 
215  m_mem.resize (k);
216 
217  for (int i = 0; i < k; i++)
218  m_mem[i] = tmp[i];
219  }
220 
221  valid = true;
222  }
223 #else
224  msg = NOT_SUPPORTED ("group functions");
225 #endif
226  }
227  }
228 }
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
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:1688
for large enough k
Definition: lu.cc:606
bool ok(void) const
Definition: oct-group.h:75
void resize(octave_idx_type n, const std::string &rfv="")
Definition: str-vec.h:97
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:941
static group getgrnam(const std::string &nm)
Definition: oct-group.cc:126
std::string passwd(void) const
Definition: oct-group.cc:61
std::string m_passwd
Definition: oct-group.h:100
double tmp
Definition: data.cc:6300
is false
Definition: cellfun.cc:398
std::string name(void) const
Definition: oct-group.cc:52
#define NOT_SUPPORTED(nm)
Definition: oct-group.cc:37
std::string m_name
Definition: oct-group.h:97
gid_t gid(void) const
Definition: oct-group.cc:70
static OCTAVE_NORETURN void err_invalid(void)
Definition: oct-group.cc:42
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
static group getgrent(void)
Definition: oct-group.cc:88
p
Definition: lu.cc:138
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