home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / pdksh-src.lha / src / amiga / pdksh / sh / do_ulimit.c < prev    next >
C/C++ Source or Header  |  1993-12-01  |  3KB  |  145 lines

  1. /*
  2.     ulimit -- handle "ulimit" builtin
  3.  
  4.     Eric Gisin, September 1988
  5.     Adapted to PD KornShell. Removed AT&T code.
  6.  
  7.     last edit:    06-Jun-1987    D A Gwyn
  8.  
  9.     This started out as the BRL UNIX System V system call emulation
  10.     for 4.nBSD, and was later extended by Doug Kingston to handle
  11.     the extended 4.nBSD resource limits.  It now includes the code
  12.     that was originally under case SYSULIMIT in source file "xec.c".
  13. */
  14.  
  15. #ifndef lint
  16. static char *RCSid = "$Id: do_ulimit.c,v 1.2 1992/04/25 08:33:28 sjg Exp $";
  17. #endif
  18.  
  19. #include "stdh.h"
  20. #include <errno.h>
  21. #include <signal.h>
  22. #include <setjmp.h>
  23. #if defined(_BSD) || defined(_BSD_SYSV)
  24. #include <sys/time.h>
  25. #include <sys/resource.h>
  26. #else
  27. #define    RLIMIT_FSIZE    2
  28. #endif
  29. #include "sh.h"
  30.  
  31. extern    long ulimit();
  32.  
  33. int
  34. do_ulimit(a1, a2)
  35.     char    *a1, *a2;
  36. {
  37.     register int    c;
  38.     long        i;
  39. #if defined(_BSD) || defined(_BSD_SYSV)
  40.     struct rlimit    limit;        /* data being gotten/set */
  41.     int        softonly = 0;    /* set => soft limit, clear => hard limit */
  42.     int        factor = 1024;    /* unit scaling (1K or 1) */
  43. #endif
  44.     int    command = RLIMIT_FSIZE;
  45.  
  46.     if (a1 && (*a1 == '-'))        /* DAG -- Gould added first test */
  47.     {    c = *++a1;        /* DAG */
  48. #if defined(_BSD) || defined(_BSD_SYSV)
  49.         if (c >= 'A' && c <= 'Z')
  50.         {
  51.             ++softonly;
  52.             c += 'a' - 'A';    /* DAG -- map to lower-case */
  53.         }
  54. #endif
  55.         switch(c)
  56.         {
  57. #if defined(_BSD) || defined(_BSD_SYSV)
  58.             case 'c':
  59.                 command = RLIMIT_CORE;
  60.                 break;
  61.             case 'd':
  62.                 command = RLIMIT_DATA;
  63.                 break;
  64.             case 'm':
  65.                 command = RLIMIT_RSS;
  66.                 break;
  67.             case 's':
  68.                 command = RLIMIT_STACK;
  69.                 break;
  70.             case 't':
  71.                 factor = 1;
  72.                 command = RLIMIT_CPU;
  73.                 break;
  74. #endif    /* _BSD || _BSD_SYSV */
  75.             case 'f':
  76.                 command = RLIMIT_FSIZE;
  77. #if _BSD_SYSV
  78.                 factor = 512;
  79. #endif
  80.                 break;
  81.             default:
  82. #if _BSD
  83.                 errorf("Usage: %s [-cdmstf] [limit]\n", "ulimit");
  84. #else
  85.                 errorf("Usage: %s [-f] [limit]\n", "ulimit");
  86. #endif
  87.         }
  88.         a1 = a2;
  89.     }
  90.     if (a1)
  91.     {
  92.         i = 0;
  93.         while ((c = *a1++) >= '0' && c <= '9')
  94.         {
  95.             i = (i * 10) + (long)(c - '0');
  96.             if (i < 0)
  97.                 goto Error;
  98.         }
  99.         if (c || i < 0)
  100.             goto Error;
  101.     }
  102. #if !(defined(_BSD) || defined(_BSD_SYSV))
  103.     else
  104.     {
  105.         i = -1;
  106.         command--;
  107.     }
  108.  
  109.     if ((i = ulimit(command, i)) < 0L)
  110.         goto Error;
  111.  
  112.     if (command != RLIMIT_FSIZE)
  113.         shellf("%ld\n", i);
  114. #else                    /* DPK -- generalized for 4.nBSD: */
  115.     if (getrlimit(command, &limit))
  116.         goto Error;    /* errno is already set */
  117.  
  118.     if (a1)
  119.     {
  120.         limit.rlim_cur = i * factor;
  121.  
  122.         if (!softonly)
  123.             limit.rlim_max = limit.rlim_cur;
  124.  
  125.         if (setrlimit(command, &limit))
  126.             goto Error;
  127.     }
  128.     else
  129.     {
  130.         i = softonly ? limit.rlim_cur : limit.rlim_max;
  131. #if _BSD            /* DAG -- System V always prints an integer */
  132.         if (i == RLIM_INFINITY)
  133.             shellf("unlimited\n");
  134.         else
  135. #endif
  136.             shellf("%ld\n", i/factor);
  137.     }
  138. #endif    /* _BSD || _BSD_SYSV */
  139.     return 0;
  140.  
  141.   Error:
  142.     errorf("bad ulimit\n");
  143. }
  144.  
  145.