getgrent.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 <string>
00028 
00029 #include <sys/types.h>
00030 
00031 #include "oct-group.h"
00032 
00033 #include "defun-dld.h"
00034 #include "error.h"
00035 #include "gripes.h"
00036 #include "oct-map.h"
00037 #include "ov.h"
00038 #include "oct-obj.h"
00039 #include "utils.h"
00040 
00041 // Group file functions.  (Why not?)
00042 
00043 static octave_value
00044 mk_gr_map (const octave_group& gr)
00045 {
00046   octave_value retval;
00047 
00048   if (gr)
00049     {
00050       octave_scalar_map m;
00051 
00052       m.assign ("name", gr.name ());
00053       m.assign ("passwd", gr.passwd ());
00054       m.assign ("gid", static_cast<double> (gr.gid ()));
00055       m.assign ("mem", octave_value (gr.mem ()));
00056 
00057       retval = m;
00058     }
00059   else
00060     retval = 0;
00061 
00062   return retval;
00063 }
00064 
00065 DEFUN_DLD (getgrent, args, ,
00066  "-*- texinfo -*-\n\
00067 @deftypefn {Loadable Function} {@var{grp_struct} =} getgrent ()\n\
00068 Return an entry from the group database, opening it if necessary.\n\
00069 Once the end of data has been reached, @code{getgrent} returns 0.\n\
00070 @end deftypefn")
00071 {
00072   octave_value_list retval;
00073 
00074   retval(1) = std::string ();
00075   retval(0) = 0;
00076 
00077   int nargin = args.length ();
00078 
00079   if (nargin == 0)
00080     {
00081       std::string msg;
00082 
00083       retval(1) = msg;
00084       retval(0) = mk_gr_map (octave_group::getgrent (msg));
00085     }
00086   else
00087     print_usage ();
00088 
00089   return retval;
00090 }
00091 
00092 DEFUN_DLD (getgrgid, args, ,
00093   "-*- texinfo -*-\n\
00094 @deftypefn {Loadable Function} {@var{grp_struct} =} getgrgid (@var{gid}).\n\
00095 Return the first entry from the group database with the group ID\n\
00096 @var{gid}.  If the group ID does not exist in the database,\n\
00097 @code{getgrgid} returns 0.\n\
00098 @end deftypefn")
00099 {
00100   octave_value_list retval;
00101 
00102   retval(1) = std::string ();
00103   retval(0) = 0;
00104 
00105   int nargin = args.length ();
00106 
00107   if (nargin == 1)
00108     {
00109       double dval = args(0).double_value ();
00110 
00111       if (! error_state)
00112         {
00113           if (D_NINT (dval) == dval)
00114             {
00115               gid_t gid = static_cast<gid_t> (dval);
00116 
00117               std::string msg;
00118 
00119               retval(1) = msg;
00120               retval(0) = mk_gr_map (octave_group::getgrgid (gid, msg));
00121             }
00122           else
00123             error ("getgrgid: GID must be an integer");
00124         }
00125     }
00126   else
00127     print_usage ();
00128 
00129   return retval;
00130 }
00131 
00132 DEFUN_DLD (getgrnam, args, ,
00133   "-*- texinfo -*-\n\
00134 @deftypefn {Loadable Function} {@var{grp_struct} =} getgrnam (@var{name})\n\
00135 Return the first entry from the group database with the group name\n\
00136 @var{name}.  If the group name does not exist in the database,\n\
00137 @code{getgrnam} returns 0.\n\
00138 @end deftypefn")
00139 {
00140   octave_value_list retval;
00141 
00142   retval(1) = std::string ();
00143   retval(0) = 0;
00144 
00145   int nargin = args.length ();
00146 
00147   if (nargin == 1)
00148     {
00149       std::string s = args(0).string_value ();
00150 
00151       if (! error_state)
00152         {
00153           std::string msg;
00154 
00155           retval(1) = msg;
00156           retval(0) = mk_gr_map (octave_group::getgrnam (s.c_str (), msg));
00157         }
00158     }
00159   else
00160     print_usage ();
00161 
00162   return retval;
00163 }
00164 
00165 DEFUN_DLD (setgrent, args, ,
00166   "-*- texinfo -*-\n\
00167 @deftypefn {Loadable Function} {} setgrent ()\n\
00168 Return the internal pointer to the beginning of the group database.\n\
00169 @end deftypefn")
00170 {
00171   octave_value_list retval;
00172 
00173   retval(1) = std::string ();
00174   retval(0) = -1.0;
00175 
00176   int nargin = args.length ();
00177 
00178   if (nargin == 0)
00179     {
00180       std::string msg;
00181 
00182       retval(1) = msg;
00183       retval(0) = static_cast<double> (octave_group::setgrent (msg));
00184     }
00185   else
00186     print_usage ();
00187 
00188   return retval;
00189 }
00190 
00191 DEFUN_DLD (endgrent, args, ,
00192   "-*- texinfo -*-\n\
00193 @deftypefn {Loadable Function} {} endgrent ()\n\
00194 Close the group database.\n\
00195 @end deftypefn")
00196 {
00197   octave_value_list retval;
00198 
00199   retval(1) = std::string ();
00200   retval(0) = -1.0;
00201 
00202   int nargin = args.length ();
00203 
00204   if (nargin == 0)
00205     {
00206       std::string msg;
00207 
00208       retval(1) = msg;
00209       retval(0) = static_cast<double> (octave_group::endgrent (msg));
00210     }
00211   else
00212     print_usage ();
00213 
00214   return retval;
00215 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines