home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / octave-2.1.23 / liboctave / oct-group.cc < prev    next >
C/C++ Source or Header  |  2000-01-15  |  4KB  |  229 lines

  1. /*
  2.  
  3. Copyright (C) 1996, 1997 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 2, or (at your option) any
  10. 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, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #ifdef HAVE_SYS_TYPES_H
  28. #include <sys/types.h>
  29. #endif
  30.  
  31. #ifdef HAVE_GRP_H
  32. #include <grp.h>
  33. #endif
  34.  
  35. #include "lo-error.h"
  36. #include "oct-group.h"
  37. #include "str-vec.h"
  38.  
  39. #define NOT_SUPPORTED(nm) \
  40.   nm ## ": not supported on this system"
  41.  
  42. string
  43. octave_group::name (void) const
  44. {
  45.   if (! ok ())
  46.     gripe_invalid ();
  47.  
  48.   return gr_name;
  49. }
  50.  
  51. string
  52. octave_group::passwd (void) const
  53. {
  54.   if (! ok ())
  55.     gripe_invalid ();
  56.  
  57.   return gr_passwd;
  58. }
  59.  
  60. gid_t
  61. octave_group::gid (void) const
  62. {
  63.   if (! ok ())
  64.     gripe_invalid ();
  65.  
  66.   return gr_gid;
  67. }
  68.  
  69. string_vector
  70. octave_group::mem (void) const
  71. {
  72.   if (! ok ())
  73.     gripe_invalid ();
  74.  
  75.   return gr_mem;
  76. }
  77.  
  78. octave_group
  79. octave_group::getgrent (void)
  80. {
  81.   string msg;
  82.   return getgrent (msg);
  83. }
  84.  
  85. octave_group
  86. octave_group::getgrent (string& msg)
  87. {
  88. #if defined (HAVE_GETGRENT)
  89.   msg = string ();
  90.   return octave_group (::getgrent (), msg);
  91. #else
  92.   msg = NOT_SUPPORTED ("getgrent");
  93.   return octave_group ();
  94. #endif
  95. }
  96.  
  97. octave_group
  98. octave_group::getgrgid (gid_t gid)
  99. {
  100.   string msg;
  101.   return getgrgid (gid, msg);
  102. }
  103.  
  104. octave_group
  105. octave_group::getgrgid (gid_t gid, string& msg)
  106. {
  107. #if defined (HAVE_GETGRGID)
  108.   msg = string ();
  109.   return octave_group (::getgrgid (gid), msg);
  110. #else
  111.   msg = NOT_SUPPORTED ("getgruid");
  112.   return octave_group ();
  113. #endif
  114. }
  115.  
  116. octave_group
  117. octave_group::getgrnam (const string& nm)
  118. {
  119.   string msg;
  120.   return getgrnam (msg);
  121. }
  122.  
  123. octave_group
  124. octave_group::getgrnam (const string& nm, string& msg)
  125. {
  126. #if defined (HAVE_GETGRNAM)
  127.   msg = string ();
  128.   return octave_group (::getgrnam (nm.c_str ()), msg);
  129. #else
  130.   msg = NOT_SUPPORTED ("getgrnam");
  131.   return octave_group ();
  132. #endif
  133. }
  134.  
  135. int
  136. octave_group::setgrent (void)
  137. {
  138.   string msg;
  139.   return setgrent (msg);
  140. }
  141.  
  142. int
  143. octave_group::setgrent (string& msg)
  144. {
  145. #if defined (HAVE_SETGRENT)
  146.   msg = string ();
  147.   ::setgrent ();
  148.   return 0;
  149. #else
  150.   msg = NOT_SUPPORTED ("setgrent");
  151.   return -1;
  152. #endif
  153. }
  154.  
  155. int
  156. octave_group::endgrent (void)
  157. {
  158.   string msg;
  159.   return endgrent (msg);
  160. }
  161.  
  162. int
  163. octave_group::endgrent (string& msg)
  164. {
  165. #if defined (HAVE_ENDGRENT)
  166.   msg = string ();
  167.   ::endgrent ();
  168.   return 0;
  169. #else
  170.   msg = NOT_SUPPORTED ("endgrent");
  171.   return -1;
  172. #endif
  173. }
  174.  
  175. octave_group::octave_group (void *p, string& msg)
  176.   : gr_name (), gr_passwd (), gr_gid (0), gr_mem (), valid (false)
  177. {
  178. #if defined (HAVE_GRP_H)
  179.   msg = string ();
  180.  
  181.   if (p)
  182.     {
  183.       struct group *gr = static_cast<struct group *> (p);
  184.  
  185.       gr_name = gr->gr_name;
  186.  
  187. #if defined (HAVE_GR_PASSWD)
  188.       gr_passwd = gr->gr_passwd;
  189. #endif
  190.  
  191.       // XXX FIXME XXX -- maybe there should be a string_vector
  192.       // constructor that takes a NULL terminated list of C
  193.       // strings.
  194.  
  195.       const char * const *tmp = gr->gr_mem;
  196.  
  197.       int k = 0;
  198.       while (*tmp++)
  199.     k++;
  200.  
  201.       if (k > 0)
  202.     {
  203.       tmp = gr->gr_mem;
  204.  
  205.       gr_mem.resize (k);
  206.  
  207.       for (int i = 0; i < k; i++)
  208.         gr_mem[i] = tmp[i];
  209.     }
  210.  
  211.       valid = true;
  212.     }
  213. #else
  214.   msg = NOT_SUPPORTED ("group functions");
  215. #endif
  216. }
  217.  
  218. void
  219. octave_group::gripe_invalid (void) const
  220. {
  221.   (*current_liboctave_error_handler) ("invalid group object");
  222. }
  223.  
  224. /*
  225. ;;; Local Variables: ***
  226. ;;; mode: C++ ***
  227. ;;; End: ***
  228. */
  229.