rdwr.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. int wrrd;
  12. void
  13. usage(void)
  14. {
  15. fprint(2, "usage: rdwr [-w] file\n");
  16. exits("usage");
  17. }
  18. void
  19. main(int argc, char **argv)
  20. {
  21. int fd;
  22. char buf[8192];
  23. int n;
  24. ARGBEGIN{
  25. case 'w':
  26. wrrd = 1;
  27. break;
  28. default:
  29. usage();
  30. }ARGEND;
  31. if(argc != 1)
  32. usage();
  33. if((fd = open(argv[0], ORDWR)) < 0)
  34. sysfatal("open: %r");
  35. if(wrrd){
  36. n = read(fd, buf, sizeof buf);
  37. if(n < 0)
  38. fprint(2, "read error: %r\n");
  39. else{
  40. write(1, buf, n);
  41. print("\n");
  42. }
  43. }
  44. while(print("> "), (n = read(0, buf, 1000)) > 0) {
  45. seek(fd, 0, 0);
  46. if(write(fd, buf, n-1) != n-1) /* n-1: no newline */
  47. fprint(2, "write error: %r\n");
  48. seek(fd, 0, 0);
  49. n = read(fd, buf, sizeof buf);
  50. if(n < 0)
  51. fprint(2, "read error: %r\n");
  52. else{
  53. write(1, buf, n);
  54. print("\n");
  55. }
  56. }
  57. }