times.c 730 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <sys/times.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. static
  10. char*
  11. skip(char *p)
  12. {
  13. while(*p == ' ')
  14. p++;
  15. while(*p != ' ' && *p != 0)
  16. p++;
  17. return p;
  18. }
  19. clock_t
  20. times(struct tms *buf)
  21. {
  22. char b[200], *p;
  23. int f;
  24. unsigned long r;
  25. memset(b, 0, sizeof(b));
  26. f = open("/dev/cputime", O_RDONLY);
  27. if(f >= 0) {
  28. lseek(f, SEEK_SET, 0);
  29. read(f, b, sizeof(b));
  30. close(f);
  31. }
  32. p = b;
  33. if(buf)
  34. buf->tms_utime = atol(p);
  35. p = skip(p);
  36. if(buf)
  37. buf->tms_stime = atol(p);
  38. p = skip(p);
  39. r = atol(p);
  40. if(buf){
  41. p = skip(p);
  42. buf->tms_cutime = atol(p);
  43. p = skip(p);
  44. buf->tms_cstime = atol(p);
  45. }
  46. return r;
  47. }