ulimit.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <sys/syscall.h>
  7. #include <stdarg.h>
  8. #include <unistd.h>
  9. #include <ulimit.h>
  10. #include <sys/resource.h>
  11. long int ulimit(int cmd, ...)
  12. {
  13. va_list va;
  14. struct rlimit limit;
  15. long int result = -1;
  16. va_start (va, cmd);
  17. switch (cmd) {
  18. /* Get limit on file size. */
  19. case UL_GETFSIZE:
  20. if (getrlimit(RLIMIT_FSIZE, &limit) == 0)
  21. result = limit.rlim_cur / 512; /* bytes to 512 byte blocksize */
  22. break;
  23. /* Set limit on file size. */
  24. case UL_SETFSIZE:
  25. result = va_arg (va, long int);
  26. if ((rlim_t) result > RLIM_INFINITY / 512) {
  27. limit.rlim_cur = RLIM_INFINITY;
  28. limit.rlim_max = RLIM_INFINITY;
  29. } else {
  30. limit.rlim_cur = result * 512;
  31. limit.rlim_max = result * 512;
  32. }
  33. result = setrlimit(RLIMIT_FSIZE, &limit);
  34. break;
  35. case __UL_GETOPENMAX:
  36. result = sysconf(_SC_OPEN_MAX);
  37. break;
  38. default:
  39. __set_errno(EINVAL);
  40. }
  41. va_end (va);
  42. return result;
  43. }