webget.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /*
  10. * Sample client.
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. void
  15. xfer(int from, int to)
  16. {
  17. char buf[12*1024];
  18. int n;
  19. while((n = read(from, buf, sizeof buf)) > 0)
  20. if(write(to, buf, n) < 0)
  21. sysfatal("write failed: %r");
  22. if(n < 0)
  23. sysfatal("read failed: %r");
  24. }
  25. void
  26. usage(void)
  27. {
  28. fprint(2, "usage: webget [-b baseurl] [-m mtpt] [-p postbody] url\n");
  29. exits("usage");
  30. }
  31. void
  32. main(int argc, char **argv)
  33. {
  34. int conn, ctlfd, fd, n;
  35. char buf[128], *base, *mtpt, *post, *url;
  36. mtpt = "/mnt/web";
  37. post = nil;
  38. base = nil;
  39. ARGBEGIN{
  40. default:
  41. usage();
  42. case 'b':
  43. base = EARGF(usage());
  44. break;
  45. case 'm':
  46. mtpt = EARGF(usage());
  47. break;
  48. case 'p':
  49. post = EARGF(usage());
  50. break;
  51. }ARGEND;
  52. if (argc != 1)
  53. usage();
  54. url = argv[0];
  55. snprint(buf, sizeof buf, "%s/clone", mtpt);
  56. if((ctlfd = open(buf, ORDWR)) < 0)
  57. sysfatal("couldn't open %s: %r", buf);
  58. if((n = read(ctlfd, buf, sizeof buf-1)) < 0)
  59. sysfatal("reading clone: %r");
  60. if(n == 0)
  61. sysfatal("short read on clone");
  62. buf[n] = '\0';
  63. conn = atoi(buf);
  64. if(base)
  65. if(fprint(ctlfd, "baseurl %s", base) < 0)
  66. sysfatal("baseurl ctl write: %r");
  67. if(fprint(ctlfd, "url %s", url) <= 0)
  68. sysfatal("get ctl write: %r");
  69. if(post){
  70. snprint(buf, sizeof buf, "%s/%d/postbody", mtpt, conn);
  71. if((fd = open(buf, OWRITE)) < 0)
  72. sysfatal("open %s: %r", buf);
  73. if(write(fd, post, strlen(post)) < 0)
  74. sysfatal("post write failed: %r");
  75. close(fd);
  76. }
  77. snprint(buf, sizeof buf, "%s/%d/body", mtpt, conn);
  78. if((fd = open(buf, OREAD)) < 0)
  79. sysfatal("open %s: %r", buf);
  80. xfer(fd, 1);
  81. exits(nil);
  82. }