cmd_uclinux.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "sash.h"
  2. #include <fcntl.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <pwd.h>
  7. #include <grp.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #if 0
  11. char psbuf[256];
  12. char name[40];
  13. int pid, state;
  14. char statec;
  15. void
  16. do_ps(argc, argv)
  17. int argc;
  18. char **argv;
  19. {
  20. int i;
  21. int h;
  22. int max;
  23. FILE * f;
  24. DIR * d;
  25. struct dirent * de;
  26. int l;
  27. printf(" PID TTY STAT TIME COMMAND\n");
  28. d = opendir("/proc");
  29. if (!d)
  30. return;
  31. while (de = readdir(d)) {
  32. for(i=0;i<strlen(de->d_name);i++)
  33. if (!isdigit(de->d_name[i]))
  34. goto next;
  35. sprintf(psbuf, "/proc/%s/stat", de->d_name);
  36. h = open(psbuf, O_RDONLY);
  37. if (h==-1)
  38. continue;
  39. l = read(h, psbuf, 255);
  40. if (l<=0) {
  41. perror("Unable to read status");
  42. close(h);
  43. continue;
  44. }
  45. psbuf[l] = '\0';
  46. psbuf[255] = '\0';
  47. if (sscanf(psbuf,
  48. "%d %s %c",
  49. &pid, name, &statec)<3)
  50. {
  51. perror("Unable to parse status");
  52. close(h);
  53. continue;
  54. }
  55. state = statec;
  56. close(h);
  57. sprintf(psbuf, "/proc/%s/cmdline", de->d_name);
  58. h = open(psbuf, O_RDONLY);
  59. if (h == -1) {
  60. perror("Unable to open cmdline");
  61. continue;
  62. }
  63. l = read(h, psbuf, 255);
  64. if (l < 0) {
  65. perror("Unable to read cmdline");
  66. close(h);
  67. continue;
  68. }
  69. close(h);
  70. psbuf[255] = psbuf[l] = '\0';
  71. printf("%5d %3s %c --:-- %s\n", pid, "", state, psbuf);
  72. next:
  73. }
  74. closedir(d);
  75. }
  76. #endif
  77. void
  78. do_cat(argc, argv)
  79. int argc;
  80. char **argv;
  81. {
  82. int fd;
  83. char *name;
  84. size_t l;
  85. char buf[256];
  86. while (argc-- > 1) {
  87. if (intflag) {
  88. return;
  89. }
  90. name = *(++argv);
  91. fd = open(name, O_RDONLY);
  92. if (fd < 0) {
  93. perror(name);
  94. return;
  95. }
  96. while ((l = read(fd, buf, sizeof(buf))) > 0) {
  97. fwrite(buf, 1, l, stdout);
  98. }
  99. close(fd);
  100. }
  101. }