whatnow: call external anno(1) instead of internal annotate().
[mmh] / uip / whatnow.c
1 /*
2 ** whatnow.c -- the WhatNow shell
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 **
8 **  The inclusion of attachments is eased by
9 **  using the header field name mechanism added to anno and send.
10 **  The header field name for attachments is predefined.
11 **
12 **  Several commands have been added at the whatnow prompt:
13 **
14 **        cd [ directory ]        This option works just like the shell's
15 **                                cd command and lets the user change the
16 **                                directory from which attachments are
17 **                                taken so that long path names are not
18 **                                needed with every file.
19 **
20 **        ls [ ls-options ]       This option works just like the normal
21 **                                ls command and exists to allow the user
22 **                                to verify file names in the directory.
23 **
24 **        pwd                     This option works just like the normal
25 **                                pwd command and exists to allow the user
26 **                                to verify the directory.
27 **
28 **        attach files            This option attaches the named files to
29 **                                the draft.
30 **
31 **        alist [-ln]             This option lists the attachments on the
32 **                                draft.  -l gets long listings, -n gets
33 **                                numbered listings.
34 **
35 **        detach files            This option removes attachments from the
36 **        detach -n numbers       draft.  This can be done by file name or
37 **                                by attachment number.
38 */
39
40 #include <h/mh.h>
41 #include <fcntl.h>
42 #include <signal.h>
43 #include <h/mime.h>
44 #include <h/utils.h>
45
46 static struct swit whatnowswitches[] = {
47 #define EDITRSW  0
48         { "editor editor", 0 },
49 #define NEDITSW  1
50         { "noedit", 0 },
51 #define PRMPTSW  2
52         { "prompt string", 4 },
53 #define VERSIONSW  3
54         { "version", 0 },
55 #define HELPSW  4
56         { "help", 0 },
57         { NULL, 0 }
58 };
59
60 /*
61 ** Options at the "whatnow" prompt
62 */
63 static struct swit aleqs[] = {
64 #define EDITSW  0
65         { "edit [<editor> <switches>]", 0 },
66 #define REFILEOPT  1
67         { "refile [<switches>] +folder", 0 },
68 #define DISPSW  2
69         { "display", 0 },
70 #define LISTSW  3
71         { "list", 0 },
72 #define SENDSW  4
73         { "send [<switches>]", 0 },
74 #define PUSHSW  5
75         { "push [<switches>]", 0 },
76 #define QUITSW  6
77         { "quit", 0 },
78 #define DELETESW  7
79         { "delete", 0 },
80 #define CDCMDSW  8
81         { "cd [directory]", 0 },
82 #define PWDCMDSW  9
83         { "pwd", 0 },
84 #define LSCMDSW  10
85         { "ls", 0 },
86 #define ATTACHCMDSW  11
87         { "attach", 0 },
88 #define DETACHCMDSW  12
89         { "detach [-n]", 2 },
90 #define ALISTCMDSW  13
91         { "alist [-ln] ", 2 },
92         { NULL, 0 }
93 };
94
95 static char *myprompt = "\nWhat now? ";
96
97 /*
98 ** static prototypes
99 */
100 static int editfile(char **, char **, char *, int, struct msgs *,
101         char *, char *, int);
102 static int sendfile(char **, char *, int);
103 static int removefile(char *);
104 static void writelscmd(char *, int, char **);
105 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
106 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
107 static int system_in_dir(const char *dir, const char *cmd);
108
109
110 #ifdef HAVE_LSTAT
111 static int copyf(char *, char *);
112 #endif
113
114
115 int
116 main(int argc, char **argv)
117 {
118         int nedit = 0, use = 0;
119         char *cp;
120         char *ed = NULL, *drft = NULL, *msgnam = NULL;
121         char buf[BUFSIZ], prompt[BUFSIZ];
122         char **argp, **arguments;
123         struct stat st;
124         char cwd[MAXPATHLEN + 1];  /* current working directory */
125         char file[MAXPATHLEN + 1];  /* file name buffer */
126         char shell[MAXPATHLEN + 1];  /* shell response buffer */
127         FILE *f;  /* read pointer for bgnd proc */
128         char *l;  /* set on -l to alist  command */
129         int n;  /* set on -n to alist command */
130
131 #ifdef LOCALE
132         setlocale(LC_ALL, "");
133 #endif
134
135         invo_name = mhbasename(argv[0]);
136
137         /* read user profile/context */
138         context_read();
139
140         arguments = getarguments(invo_name, argc, argv, 1);
141         argp = arguments;
142
143         /*
144         ** Get the initial current working directory.
145         */
146
147         if (!getcwd(cwd, sizeof (cwd))) {
148                 adios("getcwd", "could not get working directory");
149         }
150
151         while ((cp = *argp++)) {
152                 if (*cp == '-') {
153                         switch (smatch(++cp, whatnowswitches)) {
154                         case AMBIGSW:
155                                 ambigsw(cp, whatnowswitches);
156                                 done(1);
157                         case UNKWNSW:
158                                 adios(NULL, "-%s unknown", cp);
159
160                         case HELPSW:
161                                 snprintf(buf, sizeof(buf),
162                                                 "%s [switches] [file]",
163                                                 invo_name);
164                                 print_help(buf, whatnowswitches, 1);
165                                 done(1);
166                         case VERSIONSW:
167                                 print_version(invo_name);
168                                 done(1);
169
170                         case EDITRSW:
171                                 if (!(ed = *argp++) || *ed == '-')
172                                         adios(NULL, "missing argument to %s",
173                                                         argp[-2]);
174                                 nedit = 0;
175                                 continue;
176                         case NEDITSW:
177                                 nedit++;
178                                 continue;
179
180                         case PRMPTSW:
181                                 if (!(myprompt = *argp++) || *myprompt == '-')
182                                         adios(NULL, "missing argument to %s",
183                                                         argp[-2]);
184                                 continue;
185
186                         }
187                 }
188                 if (drft)
189                         adios(NULL, "only one draft at a time!");
190                 else
191                         drft = cp;
192         }
193
194         if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
195                 drft = getcpy(m_draft(seq_cur));
196
197         msgnam = (cp = getenv("mhaltmsg")) && *cp ? getcpy(cp) : NULL;
198
199         if ((cp = getenv("mhuse")) && *cp)
200                 use = atoi(cp);
201
202         if (!ed && (!(ed = getenv("mheditor")) || !*ed)) {
203                 ed = NULL;
204                 nedit++;
205         }
206
207         /* start editing the draft, unless -noedit was given */
208         if (!nedit && editfile(&ed, NULL, drft, use, NULL, msgnam, NULL, 1)
209                         < 0)
210                 done(1);
211
212         snprintf(prompt, sizeof(prompt), myprompt, invo_name);
213         for (;;) {
214                 if (!(argp = getans(prompt, aleqs))) {
215                         unlink(altmsglink);
216                         done(1);
217                 }
218                 switch (smatch(*argp, aleqs)) {
219                 case DISPSW:
220                         /* display the msg being replied to or distributed */
221                         if (msgnam) {
222                                 snprintf(buf, sizeof buf, "%s '%s'",
223                                                 listproc, msgnam);
224                                 system(buf);
225                         } else {
226                                 advise(NULL, "no alternate message to display");
227                         }
228                         break;
229
230                 case EDITSW:
231                         /* Call an editor on the draft file */
232                         if (*++argp)
233                                 ed = *argp++;
234                         if (editfile(&ed, argp, drft, NOUSE, NULL,
235                                         msgnam, NULL, 1) == NOTOK)
236                                 done(1);
237                         break;
238
239                 case LISTSW:
240                         /* display the draft file */
241                         snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
242                         system(buf);
243                         break;
244
245                 case QUITSW:
246                         /* quit */
247                         if (stat(drft, &st) != NOTOK) {
248                                 advise(NULL, "draft left on %s", drft);
249                         }
250                         done(1);
251
252                 case DELETESW:
253                         /* Delete draft and exit */
254                         removefile(drft);
255                         done(1);
256
257                 case PUSHSW:
258                         /* Send draft in background */
259                         if (sendfile(++argp, drft, 1))
260                                 done(1);
261                         break;
262
263                 case SENDSW:
264                         /* Send draft */
265                         sendfile(++argp, drft, 0);
266                         break;
267
268                 case REFILEOPT:
269                         /* Refile the draft */
270                         if (refile(++argp, drft) == 0)
271                                 done(0);
272                         break;
273
274                 case CDCMDSW:
275                         /*
276                         ** Change the working directory for attachments
277                         **
278                         ** Run the directory through the user's shell
279                         ** so that we can take advantage of any syntax
280                         ** that the user is accustomed to.  Read back
281                         ** the absolute path.
282                         */
283
284                         if (*(argp+1) == NULL) {
285                                 sprintf(buf, "$SHELL -c \"cd;pwd\"");
286                         } else {
287                                 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
288                         }
289                         if ((f = popen_in_dir(cwd, buf, "r"))) {
290                                 fgets(cwd, sizeof (cwd), f);
291
292                                 if (strchr(cwd, '\n'))
293                                         *strchr(cwd, '\n') = '\0';
294
295                                 pclose(f);
296                         } else {
297                                 advise("popen", "could not get directory");
298                         }
299
300                         break;
301
302                 case PWDCMDSW:
303                         /* Print the working directory for attachments */
304                         printf("%s\n", cwd);
305                         break;
306
307                 case LSCMDSW:
308                         /*
309                         ** List files in the current attachment working
310                         ** directory
311                         **
312                         ** Use the user's shell so that we can take
313                         ** advantage of any syntax that the user is
314                         ** accustomed to.
315                         */
316                         writelscmd(buf, sizeof(buf), argp);
317                         system_in_dir(cwd, buf);
318                         break;
319
320                 case ALISTCMDSW:
321                         /*
322                         ** List attachments on current draft.  Options are:
323                         **
324                         ** -l    long listing (full path names)
325                         ** -n    numbers listing
326                         */
327
328                         l = NULL;
329                         n = 0;
330
331                         while (*++argp) {
332                                 if (strcmp(*argp, "-l") == 0)
333                                         l = "/";
334
335                                 else if (strcmp(*argp, "-n") == 0)
336                                         n = 1;
337
338                                 else if (strcmp(*argp, "-ln") == 0 ||
339                                                 strcmp(*argp, "-nl") == 0) {
340                                         l = "/";
341                                         n = 1;
342                                 } else {
343                                         n = -1;
344                                         break;
345                                 }
346                         }
347
348                         if (n == -1)
349                                 advise(NULL, "usage is alist [-ln].");
350
351                         else
352                                 snprintf(buf, sizeof buf,
353                                                 "anno -list -comp '%s' "
354                                                 "%s%s '%s'",
355                                                 attach_hdr,
356                                                 n ? "-number all" : "",
357                                                 l ? "-text IGNORE " : "",
358                                                 drft);
359                                 if (system(buf) != 0) {
360                                         advise(NULL, "Could not list attachment headers.");
361                                 }
362
363                         break;
364
365                 case ATTACHCMDSW:
366                         /*
367                         ** Attach files to current draft.
368                         */
369
370                         if (*(argp+1) == NULL) {
371                                 advise(NULL, "attach command requires file argument(s).");
372                                 break;
373                         }
374
375                         /*
376                         ** Build a command line that causes the user's
377                         ** shell to list the file name arguments.
378                         ** This handles and wildcard expansion, tilde
379                         ** expansion, etc.
380                         */
381                         writelscmd(buf, sizeof(buf), argp);
382
383                         /*
384                         ** Read back the response from the shell,
385                         ** which contains a number of lines with one
386                         ** file name per line.  Remove off the newline.
387                         ** Determine whether we have an absolute or
388                         ** relative path name.  Prepend the current
389                         ** working directory to relative path names.
390                         ** Add the attachment annotation to the draft.
391                         */
392                         if ((f = popen_in_dir(cwd, buf, "r"))) {
393                                 char buf[BUFSIZ];
394
395                                 while (fgets(shell, sizeof(shell), f)) {
396                                         *(strchr(shell, '\n')) = '\0';
397
398                                         if (*shell == '/')
399                                                 sprintf(file, "%s", shell);
400                                         else {
401                                                 sprintf(file, "%s/%s", cwd,
402                                                                 shell);
403                                         }
404                                         snprintf(buf, sizeof buf,
405                                                         "anno -nodate -append "
406                                                         "-comp '%s' -text '%s'"
407                                                         " '%s'",
408                                                         attach_hdr, file,
409                                                         drft);
410                                         if (system(buf) != 0) {
411                                                 advise(NULL, "Could not add attachment header.");
412                                         }
413                                 }
414
415                                 pclose(f);
416                         } else {
417                                 advise("popen", "could not get file from shell");
418                         }
419
420                         break;
421
422                 case DETACHCMDSW:
423                         /*
424                         ** Detach files from current draft.
425                         **
426                         ** Scan the arguments for a -n.  Mixed file
427                         ** names and numbers aren't allowed, so this
428                         ** catches a -n anywhere in the argument list.
429                         */
430                         for (n = 0, arguments = argp + 1;
431                                         *arguments != NULL;
432                                         arguments++) {
433                                 if (strcmp(*arguments, "-n") == 0) {
434                                                 n = 1;
435                                                 break;
436                                 }
437                         }
438
439                         /*
440                         ** A -n was found so interpret the arguments as
441                         ** attachment numbers.  Decrement any remaining
442                         ** argument number that is greater than the one
443                         ** just processed after processing each one so
444                         ** that the numbering stays correct.
445                         */
446                         if (n == 1) {
447                                 for (arguments=argp+1; *arguments;
448                                                 arguments++) {
449                                         if (strcmp(*arguments, "-n") == 0)
450                                                 continue;
451
452                                         if (**arguments != '\0') {
453                                                 char buf[BUFSIZ];
454
455                                                 n = atoi(*arguments);
456                                                 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
457                                                 if (system(buf) != 0) {
458                                                         advise(NULL, "Could not delete attachment header.");
459                                                 }
460
461                                                 for (argp=arguments+1; *argp;
462                                                                 argp++) {
463                                                         if (atoi(*argp) > n) {
464                                                                 if (atoi(*argp) == 1)
465                                                                         *argp = "";
466                                                                 else
467                                                                         sprintf(*argp, "%d", atoi(*argp) - 1);
468                                                         }
469                                                 }
470                                         }
471                                 }
472                                 break;
473                         }
474                         /* else */
475
476                         /*
477                         ** The arguments are interpreted as file names.
478                         ** Run them through the user's shell for wildcard
479                         ** expansion and other goodies.  Do this from
480                         ** the current working directory if the argument
481                         ** is not an absolute path name (does not begin
482                         ** with a /).
483                         **
484                         ** We feed all the file names to the shell at
485                         ** once, otherwise you can't provide a file name
486                         ** with a space in it.
487                         */
488                         writelscmd(buf, sizeof(buf), argp);
489                         if ((f = popen_in_dir(cwd, buf, "r"))) {
490                                 while (fgets(shell, sizeof (shell), f)) {
491                                         *(strchr(shell, '\n')) = '\0';
492                                         snprintf(buf, sizeof buf,
493                                                         "anno -delete -comp "
494                                                         "'%s' -text '%s' '%s'",
495                                                         attach_hdr, shell,
496                                                         drft);
497                                         if (system(buf) != 0) {
498                                                 advise(NULL, "Could not delete attachment header.");
499                                         }
500                                 }
501                                 pclose(f);
502                         } else {
503                                 advise("popen", "could not get file from shell");
504                         }
505
506                         break;
507
508                 default:
509                         /* Unknown command */
510                         advise(NULL, "say what?");
511                         break;
512                 }
513         }
514         /*NOTREACHED*/
515 }
516
517
518
519 /*
520 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
521 ** trailcmd".
522 */
523 static void
524 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
525 {
526         char *cp;
527         /*
528         ** Note that we do not quote -- the argp from the user
529         ** is assumed to be quoted as they desire. (We can't treat
530         ** it as pure literal as that would prevent them using ~,
531         ** wildcards, etc.) The buffer produced by this function
532         ** should be given to popen_in_dir() or system_in_dir() so
533         ** that the current working directory is set correctly.
534         */
535         int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
536         /*
537         ** NB that some snprintf() return -1 on overflow rather than the
538         ** new C99 mandated 'number of chars that would have been written'
539         */
540         /*
541         ** length checks here and inside the loop allow for the
542         ** trailing ';', trailcmd, '"' and NUL
543         */
544         int trailln = strlen(trailcmd) + 3;
545         if (ln < 0 || ln + trailln > bufsz)
546                 adios(NULL, "arguments too long");
547
548         cp = buf + ln;
549
550         while (*++argp) {
551                 ln = strlen(*argp);
552                 /* +1 for leading space */
553                 if (ln + trailln + 1 > bufsz - (cp-buf))
554                         adios(NULL, "arguments too long");
555                 *cp++ = ' ';
556                 memcpy(cp, *argp, ln+1);
557                 cp += ln;
558         }
559         if (*trailcmd) {
560                 *cp++ = ';';
561                 strcpy(cp, trailcmd);
562                 cp += trailln - 3;
563         }
564         *cp++ = '"';
565         *cp = 0;
566 }
567
568 /*
569 ** Build a command line that causes the user's shell to list the file name
570 ** arguments.  This handles and wildcard expansion, tilde expansion, etc.
571 */
572 static void
573 writelscmd(char *buf, int bufsz, char **argp)
574 {
575         writesomecmd(buf, bufsz, "ls", "", argp);
576 }
577
578 /*
579 ** Like system(), but run the command in directory dir.
580 ** This assumes the program is single-threaded!
581 */
582 static int
583 system_in_dir(const char *dir, const char *cmd)
584 {
585         char olddir[BUFSIZ];
586         int r;
587         if (getcwd(olddir, sizeof(olddir)) == 0)
588                 adios("getcwd", "could not get working directory");
589         if (chdir(dir) != 0)
590                 adios("chdir", "could not change working directory");
591         r = system(cmd);
592         if (chdir(olddir) != 0)
593                 adios("chdir", "could not change working directory");
594         return r;
595 }
596
597 /* ditto for popen() */
598 static FILE*
599 popen_in_dir(const char *dir, const char *cmd, const char *type)
600 {
601         char olddir[BUFSIZ];
602         FILE *f;
603         if (getcwd(olddir, sizeof(olddir)) == 0)
604                 adios("getcwd", "could not get working directory");
605         if (chdir(dir) != 0)
606                 adios("chdir", "could not change working directory");
607         f = popen(cmd, type);
608         if (chdir(olddir) != 0)
609                 adios("chdir", "could not change working directory");
610         return f;
611 }
612
613
614 /*
615 ** EDIT
616 */
617
618 static int  reedit = 0;  /* have we been here before? */
619 static char *edsave = NULL;  /* the editor we used previously */
620
621
622 static int
623 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
624         char *altmsg, char *cwd, int save_editor)
625 {
626         int pid, status, vecp;
627         char altpath[BUFSIZ], linkpath[BUFSIZ];
628         char *cp, *vec[MAXARGS];
629         struct stat st;
630
631 #ifdef HAVE_LSTAT
632         int slinked = 0;
633 #endif /* HAVE_LSTAT */
634
635         /* Was there a previous edit session? */
636         if (reedit) {
637                 if (!*ed) {  /* no explicit editor */
638                         *ed = edsave;  /* so use the previous one */
639                         if (!(cp = mhbasename(*ed)))
640                                 cp = *ed;
641
642                         /* unless we've specified it via "editor-next" */
643                         cp = concat(cp, "-next", NULL);
644                         if ((cp = context_find(cp)))
645                                 *ed = cp;
646                 }
647         } else if (!*ed) {
648                 /* set initial editor */
649                 *ed = defaulteditor;
650         }
651
652         if (altmsg) {
653                 if (!mp || *altmsg == '/' || !cwd)
654                         strncpy(altpath, altmsg, sizeof(altpath));
655                 else
656                         snprintf(altpath, sizeof(altpath), "%s/%s",
657                                         mp->foldpath, altmsg);
658                 if (!cwd)
659                         strncpy(linkpath, altmsglink, sizeof(linkpath));
660                 else
661                         snprintf(linkpath, sizeof(linkpath), "%s/%s",
662                                         cwd, altmsglink);
663         }
664
665         if (altmsg) {
666                 unlink(linkpath);
667 #ifdef HAVE_LSTAT
668                 if (link(altpath, linkpath) == NOTOK) {
669                         symlink(altpath, linkpath);
670                         slinked = 1;
671                 } else {
672                         slinked = 0;
673                 }
674 #else /* not HAVE_LSTAT */
675                 link(altpath, linkpath);
676 #endif /* not HAVE_LSTAT */
677         }
678
679         context_save();  /* save the context file */
680         fflush(stdout);
681
682         switch (pid = fork()) {
683         case NOTOK:
684                 advise("fork", "unable to");
685                 status = NOTOK;
686                 break;
687
688         case OK:
689                 if (cwd)
690                         chdir(cwd);
691                 if (altmsg) {
692                         if (mp)
693                                 m_putenv("mhfolder", mp->foldpath);
694                         m_putenv("editalt", altpath);
695                 }
696
697                 vecp = 0;
698                 vec[vecp++] = mhbasename(*ed);
699                 if (arg)
700                         while (*arg)
701                                 vec[vecp++] = *arg++;
702                 vec[vecp++] = file;
703                 vec[vecp] = NULL;
704
705                 execvp(*ed, vec);
706                 fprintf(stderr, "unable to exec ");
707                 perror(*ed);
708                 _exit(-1);
709
710         default:
711                 if ((status = pidwait(pid, NOTOK))) {
712                         if (((status & 0xff00) != 0xff00)
713                                 && (!reedit || (status & 0x00ff))) {
714                                 if (!use && (status & 0xff00) && (rename(file, cp = m_backup (file)) != NOTOK)) {
715                                         advise(NULL, "problems with edit--draft left in %s", cp);
716                                 } else {
717                                         advise(NULL, "problems with edit--%s preserved", file);
718                                 }
719                         }
720                         status = -2;  /* maybe "reedit ? -2 : -1"? */
721                         break;
722                 }
723
724                 reedit++;
725 #ifdef HAVE_LSTAT
726                 if (altmsg && mp && !is_readonly(mp) && (slinked ?
727                                 lstat (linkpath, &st) != NOTOK &&
728                                 S_ISREG(st.st_mode) &&
729                                 copyf(linkpath, altpath) == NOTOK :
730                                 stat(linkpath, &st) != NOTOK &&
731                                 st.st_nlink == 1 &&
732                                 (unlink(altpath) == NOTOK ||
733                                 link(linkpath, altpath) == NOTOK)))
734                         advise(linkpath, "unable to update %s from", altmsg);
735 #else /* HAVE_LSTAT */
736                 if (altmsg && mp && !is_readonly(mp) &&
737                                 stat(linkpath, &st) != NOTOK &&
738                                 st.st_nlink == 1 &&
739                                 (unlink(altpath) == NOTOK ||
740                                 link(linkpath, altpath) == NOTOK))
741                         advise(linkpath, "unable to update %s from", altmsg);
742 #endif /* HAVE_LSTAT */
743         }
744
745         /* normally, we remember which editor we used */
746         if (save_editor)
747                 edsave = getcpy(*ed);
748
749         *ed = NULL;
750         if (altmsg)
751                 unlink(linkpath);
752
753         return status;
754 }
755
756
757 #ifdef HAVE_LSTAT
758 static int
759 copyf(char *ifile, char *ofile)
760 {
761         int i, in, out;
762         char buffer[BUFSIZ];
763
764         if ((in = open(ifile, O_RDONLY)) == NOTOK)
765                 return NOTOK;
766         if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
767                 admonish(ofile, "unable to open and truncate");
768                 close(in);
769                 return NOTOK;
770         }
771
772         while ((i = read(in, buffer, sizeof(buffer))) > OK)
773                 if (write(out, buffer, i) != i) {
774                         advise(ofile, "may have damaged");
775                         i = NOTOK;
776                         break;
777                 }
778
779         close(in);
780         close(out);
781         return i;
782 }
783 #endif /* HAVE_LSTAT */
784
785
786 /*
787 ** SEND
788 */
789
790 static int
791 sendfile(char **arg, char *file, int pushsw)
792 {
793         pid_t child_id;
794         int vecp;
795         char *vec[MAXARGS];
796
797         context_save();  /* save the context file */
798         fflush(stdout);
799
800         switch (child_id = fork()) {
801         case NOTOK:
802                 advise(NULL, "unable to fork, so sending directly...");
803                 /* fall */
804         case OK:
805                 vecp = 0;
806                 vec[vecp++] = invo_name;
807                 if (pushsw)
808                         vec[vecp++] = "-push";
809                 if (arg)
810                         while (*arg)
811                                 vec[vecp++] = *arg++;
812                 vec[vecp++] = file;
813                 vec[vecp] = NULL;
814
815                 execvp("send", vec);
816                 fprintf(stderr, "unable to exec ");
817                 perror("send");
818                 _exit(-1);
819
820         default:
821                 if (pidwait(child_id, OK) == 0)
822                         done(0);
823                 return 1;
824         }
825 }
826
827
828 /*
829 ** Remove the draft file
830 */
831
832 static int
833 removefile(char *drft)
834 {
835         if (unlink(drft) == NOTOK)
836                 adios(drft, "unable to unlink");
837
838         return OK;
839 }