kfscmd.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. void
  12. main(int argc, char *argv[])
  13. {
  14. char *name, buf[4*1024];
  15. int fd, n, i, errs;
  16. name = 0;
  17. ARGBEGIN{
  18. case 'n':
  19. name = ARGF();
  20. break;
  21. default:
  22. fprint(2, "usage: kfscmd [-n server] commands\n");
  23. exits("usage");
  24. }ARGEND
  25. if(name)
  26. snprint(buf, sizeof buf, "/srv/kfs.%s.cmd", name);
  27. else
  28. strcpy(buf, "/srv/kfs.cmd");
  29. fd = open(buf, ORDWR);
  30. if(fd < 0){
  31. fprint(2, "kfscmd: can't open commands file\n");
  32. exits("commands file");
  33. }
  34. errs = 0;
  35. for(i = 0; i < argc; i++){
  36. if(write(fd, argv[i], strlen(argv[i])) != strlen(argv[i])){
  37. fprint(2, "%s: error writing %s: %r", argv0, argv[i]);
  38. errs++;
  39. continue;
  40. }
  41. for(;;){
  42. n = read(fd, buf, sizeof buf - 1);
  43. if(n < 0){
  44. fprint(2, "%s: error executing %s: %r", argv0, argv[i]);
  45. errs++;
  46. break;
  47. }
  48. buf[n] = '\0';
  49. if(strcmp(buf, "done") == 0 || strcmp(buf, "success") == 0)
  50. break;
  51. if(strcmp(buf, "unknown command") == 0){
  52. errs++;
  53. print("kfscmd: command %s not recognized\n", argv[i]);
  54. break;
  55. }
  56. write(1, buf, n);
  57. }
  58. }
  59. exits(errs ? "errors" : 0);
  60. }