cmd_uclinux.c 1.7 KB

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