home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / LIBC / LIBC-4.6 / LIBC-4 / libc-linux / sysdeps / linux / ulimit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-20  |  2.5 KB  |  108 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #ifdef __linux__
  21. #include <ulimit.h>
  22. #else
  23. #include <sysdep.h>
  24. #endif
  25. #include <sys/resource.h>
  26. #include <unistd.h>
  27. #include <stdarg.h>
  28. #include <errno.h>
  29.  
  30. #ifndef linux
  31.  
  32. #ifndef     HAVE_GNU_LD
  33. #define     _etext    etext
  34. #endif
  35.  
  36. extern char _etext;
  37.  
  38. #endif
  39.  
  40. /* Function depends on CMD:
  41.    1 = Return the limit on the size of a file, in units of 512 bytes.
  42.    2 = Set the limit on the size of a file to NEWLIMIT.  Only the
  43.        super-user can increase the limit.
  44.    3 = Return the maximum possible address of the data segment.
  45.    4 = Return the maximum number of files that the calling process
  46.        can open.
  47.    Returns -1 on errors.  */
  48. long
  49. DEFUN(ulimit, (cmd), int cmd DOTS)
  50. {
  51.   int status;
  52.  
  53.   switch (cmd)
  54.     {
  55.     case UL_GETFSIZE:
  56.       {
  57.     /* Get limit on file size.  */
  58.     struct rlimit fsize;
  59.  
  60.     status = getrlimit(RLIMIT_FSIZE, &fsize);
  61.     if (status < 0)
  62.       return -1;
  63.  
  64.     /* Convert from bytes to 512 byte units.  */
  65.     return fsize.rlim_cur / 512;
  66.       }
  67.     case UL_SETFSIZE:
  68.       /* Set limit on file size.  */
  69.       {
  70.     va_list arg;
  71.     long newlimit;
  72.     struct rlimit fsize;
  73.  
  74.     va_start (arg, cmd);
  75.     newlimit = va_arg (arg, long);
  76.     va_end (arg);
  77.  
  78.     fsize.rlim_cur = newlimit * 512;
  79.     fsize.rlim_max = newlimit * 512;
  80.     
  81.     return setrlimit(RLIMIT_FSIZE, &fsize);
  82.       }
  83.     case 3:
  84. #ifdef __linux__
  85.       /* Get maximum address for `brk'.  */
  86.       /* Due to the shared library, we cannot do this. */
  87.       errno = EINVAL;
  88.       return -1;
  89. #else
  90.       {
  91.     struct rlimit dsize;
  92.  
  93.     status = getrlimit(RLIMIT_DATA, &dsize);
  94.     if (status < 0)
  95.       return -1;
  96.  
  97.     return ((long int) &_etext) + dsize.rlim_cur;
  98.       }
  99. #endif
  100.     case 4:
  101.       return __sysconf(_SC_OPEN_MAX);
  102.  
  103.     default:
  104.       errno = EINVAL;
  105.       return -1;
  106.     }
  107. }
  108.