cp.myr 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. use std
  2. use bio
  3. use "dispatch"
  4. const cp = {params
  5. std.put("cp called\n")
  6. var src,dest,fsrc,fdest
  7. var buf : byte[16*1024]
  8. var b
  9. src=params[0]
  10. dest=params[1]
  11. // if dest is current path
  12. if std.eq(dest,".")
  13. std.put("copy in here \n")
  14. dest=std.strcat("./",std.basename(src))
  15. ;;
  16. match bio.open(src, bio.Rd)
  17. | `std.Ok bio: fsrc = bio
  18. | `std.Err e: std.fatal("Unable to open src file: {}\n", e)
  19. ;;
  20. match bio.create(dest, bio.Wr, 0o644)
  21. | `std.Ok bio: fdest = bio
  22. | `std.Err m: std.fatal("Failed to create dest file: {}\n", m)
  23. ;;
  24. /*
  25. match bio.open(dest, bio.Rd)
  26. | `std.Ok bio2: fdest = bio2
  27. | `std.Err e: std.fatal("Unable to open dest file: {}\n", e)
  28. ;;
  29. */
  30. while true
  31. b= r(fsrc, buf[:])
  32. match b
  33. | "": break;
  34. | _: bio.write(fdest, b)
  35. ;;
  36. ;;
  37. bio.flush(fsrc)
  38. bio.flush(fdest)
  39. bio.close(fsrc)
  40. bio.close(fdest)
  41. std.slfree(dest)
  42. std.put("copied {} to {}\n",src,dest)
  43. }
  44. const __init__ = {
  45. dispatch.register("cp", cp)
  46. }
  47. const r = {f, buf
  48. match bio.read(f, buf)
  49. | `std.Ok b:
  50. -> b
  51. | `std.Err `bio.Eof:
  52. std.put("eof\n")
  53. -> ""
  54. | `std.Err e:
  55. std.put("err\n")
  56. -> ""
  57. ;;
  58. }