(mh_err_memory,mh_file_copy): New functions.
Showing
2 changed files
with
77 additions
and
1 deletions
... | @@ -231,6 +231,7 @@ void mh_format_free __P((mh_format_t *fmt)); | ... | @@ -231,6 +231,7 @@ void mh_format_free __P((mh_format_t *fmt)); |
231 | mh_builtin_t *mh_lookup_builtin __P((char *name, int *rest)); | 231 | mh_builtin_t *mh_lookup_builtin __P((char *name, int *rest)); |
232 | 232 | ||
233 | void mh_error __P((const char *fmt, ...)); | 233 | void mh_error __P((const char *fmt, ...)); |
234 | void mh_err_memory __P((int fatal)); | ||
234 | 235 | ||
235 | FILE *mh_audit_open __P((char *name, mailbox_t mbox)); | 236 | FILE *mh_audit_open __P((char *name, mailbox_t mbox)); |
236 | void mh_audit_close __P((FILE *fp)); | 237 | void mh_audit_close __P((FILE *fp)); |
... | @@ -274,3 +275,4 @@ void *xrealloc __P((void *, size_t)); | ... | @@ -274,3 +275,4 @@ void *xrealloc __P((void *, size_t)); |
274 | int mh_spawnp __P((const char *prog, const char *file)); | 275 | int mh_spawnp __P((const char *prog, const char *file)); |
275 | int mh_whatnow __P((struct mh_whatnow_env *wh, int initial_edit)); | 276 | int mh_whatnow __P((struct mh_whatnow_env *wh, int initial_edit)); |
276 | int mh_disposition __P((const char *filename)); | 277 | int mh_disposition __P((const char *filename)); |
278 | int mh_file_copy __P((const char *from, const char *to)); | ... | ... |
... | @@ -109,6 +109,14 @@ mh_read_formfile (char *name, char **pformat) | ... | @@ -109,6 +109,14 @@ mh_read_formfile (char *name, char **pformat) |
109 | return 0; | 109 | return 0; |
110 | } | 110 | } |
111 | 111 | ||
112 | void | ||
113 | mh_err_memory (int fatal) | ||
114 | { | ||
115 | mh_error (_("not enough memory")); | ||
116 | if (fatal) | ||
117 | abort (); | ||
118 | } | ||
119 | |||
112 | static char *my_name; | 120 | static char *my_name; |
113 | static char *my_email; | 121 | static char *my_email; |
114 | 122 | ||
... | @@ -412,7 +420,7 @@ mh_spawnp (const char *prog, const char *file) | ... | @@ -412,7 +420,7 @@ mh_spawnp (const char *prog, const char *file) |
412 | xargv = calloc (argc + 2, sizeof (*xargv)); | 420 | xargv = calloc (argc + 2, sizeof (*xargv)); |
413 | if (!xargv) | 421 | if (!xargv) |
414 | { | 422 | { |
415 | mh_error (_("not enough memory")); | 423 | mh_err_memory (0); |
416 | argcv_free (argc, argv); | 424 | argcv_free (argc, argv); |
417 | return 1; | 425 | return 1; |
418 | } | 426 | } |
... | @@ -429,3 +437,69 @@ mh_spawnp (const char *prog, const char *file) | ... | @@ -429,3 +437,69 @@ mh_spawnp (const char *prog, const char *file) |
429 | 437 | ||
430 | return rc; | 438 | return rc; |
431 | } | 439 | } |
440 | |||
441 | int | ||
442 | mh_file_copy (const char *from, const char *to) | ||
443 | { | ||
444 | char *buffer; | ||
445 | size_t bufsize, rdsize; | ||
446 | struct stat st; | ||
447 | stream_t in; | ||
448 | stream_t out; | ||
449 | int rc; | ||
450 | |||
451 | if (stat (from, &st)) | ||
452 | { | ||
453 | mh_error ("mh_copy: %s", mu_errstring (errno)); | ||
454 | return -1; | ||
455 | } | ||
456 | |||
457 | for (bufsize = st.st_size; bufsize > 0 && (buffer = malloc (bufsize)) == 0; | ||
458 | bufsize /= 2) | ||
459 | ; | ||
460 | |||
461 | if (!bufsize) | ||
462 | mh_err_memory (1); | ||
463 | |||
464 | if ((rc = file_stream_create (&in, from, MU_STREAM_READ)) != 0 | ||
465 | || (rc = stream_open (in))) | ||
466 | { | ||
467 | mh_error (_("cannot open input file \"%s\": %s"), | ||
468 | from, mu_errstring (rc)); | ||
469 | free (buffer); | ||
470 | return 1; | ||
471 | } | ||
472 | |||
473 | if ((rc = file_stream_create (&out, to, MU_STREAM_RDWR|MU_STREAM_CREAT)) != 0 | ||
474 | || (rc = stream_open (out))) | ||
475 | { | ||
476 | mh_error (_("cannot open output file \"%s\": %s"), | ||
477 | to, mu_errstring (rc)); | ||
478 | free (buffer); | ||
479 | stream_close (in); | ||
480 | stream_destroy (&in, stream_get_owner (in)); | ||
481 | return 1; | ||
482 | } | ||
483 | |||
484 | while (st.st_size > 0 | ||
485 | && (rc = stream_sequential_read (in, buffer, bufsize, &rdsize)) == 0 | ||
486 | && rdsize > 0) | ||
487 | { | ||
488 | if ((rc = stream_sequential_write (out, buffer, rdsize)) != 0) | ||
489 | { | ||
490 | mh_error (_("write error on \"%s\": %s"), | ||
491 | to, mu_errstring (rc)); | ||
492 | break; | ||
493 | } | ||
494 | st.st_size -= rdsize; | ||
495 | } | ||
496 | |||
497 | free (buffer); | ||
498 | |||
499 | stream_close (in); | ||
500 | stream_close (out); | ||
501 | stream_destroy (&in, stream_get_owner (in)); | ||
502 | stream_destroy (&out, stream_get_owner (out)); | ||
503 | |||
504 | return rc; | ||
505 | } | ... | ... |
-
Please register or sign in to post a comment