oct-group.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <sys/types.h>
00028 
00029 #ifdef HAVE_GRP_H
00030 #include <grp.h>
00031 #endif
00032 
00033 #include "lo-error.h"
00034 #include "oct-group.h"
00035 #include "str-vec.h"
00036 
00037 #define NOT_SUPPORTED(nm) \
00038   nm ": not supported on this system"
00039 
00040 std::string
00041 octave_group::name (void) const
00042 {
00043   if (! ok ())
00044     gripe_invalid ();
00045 
00046   return gr_name;
00047 }
00048 
00049 std::string
00050 octave_group::passwd (void) const
00051 {
00052   if (! ok ())
00053     gripe_invalid ();
00054 
00055   return gr_passwd;
00056 }
00057 
00058 gid_t
00059 octave_group::gid (void) const
00060 {
00061   if (! ok ())
00062     gripe_invalid ();
00063 
00064   return gr_gid;
00065 }
00066 
00067 string_vector
00068 octave_group::mem (void) const
00069 {
00070   if (! ok ())
00071     gripe_invalid ();
00072 
00073   return gr_mem;
00074 }
00075 
00076 octave_group
00077 octave_group::getgrent (void)
00078 {
00079   std::string msg;
00080   return getgrent (msg);
00081 }
00082 
00083 octave_group
00084 octave_group::getgrent (std::string& msg)
00085 {
00086 #if defined (HAVE_GETGRENT)
00087   msg = std::string ();
00088   return octave_group (::getgrent (), msg);
00089 #else
00090   msg = NOT_SUPPORTED ("getgrent");
00091   return octave_group ();
00092 #endif
00093 }
00094 
00095 octave_group
00096 octave_group::getgrgid (gid_t gid)
00097 {
00098   std::string msg;
00099   return getgrgid (gid, msg);
00100 }
00101 
00102 octave_group
00103 octave_group::getgrgid (gid_t gid, std::string& msg)
00104 {
00105 #if defined (HAVE_GETGRGID)
00106   msg = std::string ();
00107   return octave_group (::getgrgid (gid), msg);
00108 #else
00109   msg = NOT_SUPPORTED ("getgruid");
00110   return octave_group ();
00111 #endif
00112 }
00113 
00114 octave_group
00115 octave_group::getgrnam (const std::string& nm)
00116 {
00117   std::string msg;
00118   return getgrnam (nm, msg);
00119 }
00120 
00121 octave_group
00122 octave_group::getgrnam (const std::string& nm, std::string& msg)
00123 {
00124 #if defined (HAVE_GETGRNAM)
00125   msg = std::string ();
00126   return octave_group (::getgrnam (nm.c_str ()), msg);
00127 #else
00128   msg = NOT_SUPPORTED ("getgrnam");
00129   return octave_group ();
00130 #endif
00131 }
00132 
00133 int
00134 octave_group::setgrent (void)
00135 {
00136   std::string msg;
00137   return setgrent (msg);
00138 }
00139 
00140 int
00141 octave_group::setgrent (std::string& msg)
00142 {
00143 #if defined (HAVE_SETGRENT)
00144   msg = std::string ();
00145   ::setgrent ();
00146   return 0;
00147 #else
00148   msg = NOT_SUPPORTED ("setgrent");
00149   return -1;
00150 #endif
00151 }
00152 
00153 int
00154 octave_group::endgrent (void)
00155 {
00156   std::string msg;
00157   return endgrent (msg);
00158 }
00159 
00160 int
00161 octave_group::endgrent (std::string& msg)
00162 {
00163 #if defined (HAVE_ENDGRENT)
00164   msg = std::string ();
00165   ::endgrent ();
00166   return 0;
00167 #else
00168   msg = NOT_SUPPORTED ("endgrent");
00169   return -1;
00170 #endif
00171 }
00172 
00173 octave_group::octave_group (void *p, std::string& msg)
00174   : gr_name (), gr_passwd (), gr_gid (0), gr_mem (), valid (false)
00175 {
00176 #if defined (HAVE_GRP_H)
00177   msg = std::string ();
00178 
00179   if (p)
00180     {
00181       struct group *gr = static_cast<struct group *> (p);
00182 
00183       gr_name = gr->gr_name;
00184 
00185 #if defined (HAVE_GR_PASSWD)
00186       gr_passwd = gr->gr_passwd;
00187 #endif
00188 
00189       gr_gid = gr->gr_gid;
00190 
00191       // FIXME -- maybe there should be a string_vector
00192       // constructor that takes a NUL terminated list of C
00193       // strings.
00194 
00195       const char * const *tmp = gr->gr_mem;
00196 
00197       int k = 0;
00198       while (*tmp++)
00199         k++;
00200 
00201       if (k > 0)
00202         {
00203           tmp = gr->gr_mem;
00204 
00205           gr_mem.resize (k);
00206 
00207           for (int i = 0; i < k; i++)
00208             gr_mem[i] = tmp[i];
00209         }
00210 
00211       valid = true;
00212     }
00213 #else
00214   msg = NOT_SUPPORTED ("group functions");
00215 #endif
00216 }
00217 
00218 void
00219 octave_group::gripe_invalid (void) const
00220 {
00221   (*current_liboctave_error_handler) ("invalid group object");
00222 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines