home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / k / ksh48.zip / sh / do_ulimit.c < prev    next >
C/C++ Source or Header  |  1992-08-25  |  3KB  |  149 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.3 1992/08/10 12:02:23 sjg Exp $";
  17. #endif
  18.  
  19. #ifdef _BSDI
  20. #define _BSD    1
  21. #endif
  22.  
  23. #include "stdh.h"
  24. #include <errno.h>
  25. #include <signal.h>
  26. #include <setjmp.h>
  27. #if defined(_BSD) || defined(_BSD_SYSV)
  28. #include <sys/time.h>
  29. #include <sys/resource.h>
  30. #else
  31. #define    RLIMIT_FSIZE    2
  32. #endif
  33. #include "sh.h"
  34.  
  35. extern    long ulimit();
  36.  
  37. int
  38. do_ulimit(a1, a2)
  39.     char    *a1, *a2;
  40. {
  41.     register int    c;
  42.     long        i;
  43. #if defined(_BSD) || defined(_BSD_SYSV)
  44.     struct rlimit    limit;        /* data being gotten/set */
  45.     int        softonly = 0;    /* set => soft limit, clear => hard limit */
  46.     int        factor = 1024;    /* unit scaling (1K or 1) */
  47. #endif
  48.     int    command = RLIMIT_FSIZE;
  49.  
  50.     if (a1 && (*a1 == '-'))        /* DAG -- Gould added first test */
  51.     {    c = *++a1;        /* DAG */
  52. #if defined(_BSD) || defined(_BSD_SYSV)
  53.         if (c >= 'A' && c <= 'Z')
  54.         {
  55.             ++softonly;
  56.             c += 'a' - 'A';    /* DAG -- map to lower-case */
  57.         }
  58. #endif
  59.         switch(c)
  60.         {
  61. #if defined(_BSD) || defined(_BSD_SYSV)
  62.             case 'c':
  63.                 command = RLIMIT_CORE;
  64.                 break;
  65.             case 'd':
  66.                 command = RLIMIT_DATA;
  67.                 break;
  68.             case 'm':
  69.                 command = RLIMIT_RSS;
  70.                 break;
  71.             case 's':
  72.                 command = RLIMIT_STACK;
  73.                 break;
  74.             case 't':
  75.                 factor = 1;
  76.                 command = RLIMIT_CPU;
  77.                 break;
  78. #endif    /* _BSD || _BSD_SYSV */
  79.             case 'f':
  80.                 command = RLIMIT_FSIZE;
  81. #if _BSD_SYSV
  82.                 factor = 512;
  83. #endif
  84.                 break;
  85.             default:
  86. #if _BSD
  87.                 errorf("Usage: %s [-cdmstf] [limit]\n", "ulimit");
  88. #else
  89.                 errorf("Usage: %s [-f] [limit]\n", "ulimit");
  90. #endif
  91.         }
  92.         a1 = a2;
  93.     }
  94.     if (a1)
  95.     {
  96.         i = 0;
  97.         while ((c = *a1++) >= '0' && c <= '9')
  98.         {
  99.             i = (i * 10) + (long)(c - '0');
  100.             if (i < 0)
  101.                 goto Error;
  102.         }
  103.         if (c || i < 0)
  104.             goto Error;
  105.     }
  106. #if !(defined(_BSD) || defined(_BSD_SYSV))
  107.     else
  108.     {
  109.         i = -1;
  110.         command--;
  111.     }
  112.  
  113.     if ((i = ulimit(command, i)) < 0L)
  114.         goto Error;
  115.  
  116.     if (command != RLIMIT_FSIZE)
  117.         shellf("%ld\n", i);
  118. #else                    /* DPK -- generalized for 4.nBSD: */
  119.     if (getrlimit(command, &limit))
  120.         goto Error;    /* errno is already set */
  121.  
  122.     if (a1)
  123.     {
  124.         limit.rlim_cur = i * factor;
  125.  
  126.         if (!softonly)
  127.             limit.rlim_max = limit.rlim_cur;
  128.  
  129.         if (setrlimit(command, &limit))
  130.             goto Error;
  131.     }
  132.     else
  133.     {
  134.         i = softonly ? limit.rlim_cur : limit.rlim_max;
  135. #if _BSD            /* DAG -- System V always prints an integer */
  136.         if (i == RLIM_INFINITY)
  137.             shellf("unlimited\n");
  138.         else
  139. #endif
  140.             shellf("%ld\n", i/factor);
  141.     }
  142. #endif    /* _BSD || _BSD_SYSV */
  143.     return 0;
  144.  
  145.   Error:
  146.     errorf("bad ulimit\n");
  147. }
  148.  
  149.