ucmd_cat.c 943 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* cat: ring-3 command. Prints a file's contents. */
  2. #include "ucmd_lib.h"
  3. #define DUMP_BUF_SIZE 4096
  4. static unsigned char buf[DUMP_BUF_SIZE];
  5. void _start(const char *path)
  6. {
  7. int n;
  8. unsigned int i;
  9. if (!path || !path[0]) {
  10. u_puts("cat: usage cat /<device>/<file>\n");
  11. u_exit(1);
  12. }
  13. n = u_syscall3(SYS_READ_FILE, (int)path, (int)buf, DUMP_BUF_SIZE);
  14. if (n == SYS_ENODEV) {
  15. u_puts("cat: no such device\n");
  16. u_exit(1);
  17. }
  18. if (n == SYS_ENOFS) {
  19. u_puts("cat: no filesystem on device\n");
  20. u_exit(1);
  21. }
  22. if (n == SYS_EIO) {
  23. u_puts("cat: disk read error\n");
  24. u_exit(1);
  25. }
  26. if (n < 0) {
  27. u_puts("cat: not found\n");
  28. u_exit(1);
  29. }
  30. for (i = 0; i < (unsigned int)n; i++) {
  31. unsigned char c = buf[i];
  32. if (c >= 0x20 && c < 0x7F)
  33. u_putc((char)c);
  34. else if (c == '\n')
  35. u_putc('\n');
  36. else if (c == '\t')
  37. u_putc('\t');
  38. else
  39. u_putc('.');
  40. }
  41. if (n > 0 && buf[n - 1] != '\n')
  42. u_puts("\n");
  43. u_exit(0);
  44. }