Commit 5c470f14 5c470f1488e05c87bdaef2936eb74670d49ee5ab by Sergey Poznyakoff

Implemented Write. Fixed memory leaks.

1 parent 2400d34e
...@@ -45,44 +45,27 @@ mail_write (int argc, char **argv) ...@@ -45,44 +45,27 @@ mail_write (int argc, char **argv)
45 if (isupper (argv[0][0])) 45 if (isupper (argv[0][0]))
46 sender = 1; 46 sender = 1;
47 else if (argc >= 2) 47 else if (argc >= 2)
48 filename = argv[--argc]; 48 filename = strdup (argv[--argc]);
49 else 49 else
50 { 50 filename = strdup ("mbox");
51 filename = strdup ("mbox");
52 }
53 51
54 num = util_expand_msglist (argc, argv, &msglist); 52 num = util_expand_msglist (argc, argv, &msglist);
55 53
56 if (sender) 54 if (sender)
57 { 55 {
58 mailbox_get_message (mbox, num > 0 ? msglist[0] : cursor, 0); 56 filename = util_get_sender(msglist[0], 1);
59 /* get from */ 57 if (!filename)
60 /* filename = login name part */ 58 {
59 free (msglist);
60 return 1;
61 }
61 } 62 }
62 63
63 output = fopen (filename, "a"); 64 output = fopen (filename, "a");
64 65
65 if (num > 0) 66 for (i = 0; i < num; i++)
66 {
67 for (i=0; i < num; i++)
68 {
69 mailbox_get_message (mbox, msglist[i], &msg);
70 message_get_body (msg, &bod);
71 body_get_stream (bod, &stream);
72 /* should there be a separator? */
73 while (stream_read(stream, buffer, sizeof (buffer) - 1, off, &n)
74 == 0 && n != 0)
75 {
76 buffer[n] = '\0';
77 fprintf (output, "%s", buffer);
78 off += n;
79 }
80 /* mark as saved */
81 }
82 }
83 else
84 { 67 {
85 mailbox_get_message (mbox, cursor, &msg); 68 mailbox_get_message (mbox, msglist[i], &msg);
86 message_get_body (msg, &bod); 69 message_get_body (msg, &bod);
87 body_get_stream (bod, &stream); 70 body_get_stream (bod, &stream);
88 /* should there be a separator? */ 71 /* should there be a separator? */
...@@ -93,7 +76,7 @@ mail_write (int argc, char **argv) ...@@ -93,7 +76,7 @@ mail_write (int argc, char **argv)
93 fprintf (output, "%s", buffer); 76 fprintf (output, "%s", buffer);
94 off += n; 77 off += n;
95 } 78 }
96 /* mark as save */ 79 /* mark as saved */
97 } 80 }
98 81
99 fclose (output); 82 fclose (output);
......