dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / tip / cmds.c
blobbc3c6c59d3af766c60f9f8ffa56182f00f6adb5f
1 /*
2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /*
7 * Copyright (c) 1983 Regents of the University of California.
8 * All rights reserved. The Berkeley software License Agreement
9 * specifies the terms and conditions for redistribution.
12 #pragma ident "%Z%%M% %I% %E% SMI"
14 #include "tip.h"
15 #include <limits.h>
16 #ifdef USG
17 #include <unistd.h>
18 #else
19 #include <vfork.h>
20 #endif
23 * tip
25 * miscellaneous commands
28 int quant[] = { 60, 60, 24 };
30 char null = '\0';
31 char *sep[] = { "second", "minute", "hour" };
32 static char *argv[10]; /* argument vector for take and put */
34 sigjmp_buf intbuf; /* for interrupts and timeouts */
36 void timeout(void); /* timeout function called on alarm */
37 void intcopy(void); /* interrupt routine for file transfers */
38 void transfer(char *, int, char *);
39 void transmit(FILE *, char *, char *);
40 void send(char);
41 void execute(char *);
42 void prtime(char *, time_t);
43 void hardwareflow(char *);
44 void intr(char *);
45 int args(char *, char *[], size_t);
46 int anyof(char *, char *);
49 * FTP - remote ==> local
50 * get a file from the remote host
52 void
53 getfl(int c)
55 char buf[256], *cp;
57 (void) putchar(c);
59 * get the UNIX receiving file's name
61 if (prompt("Local file name? ", copyname, sizeof (copyname)))
62 return;
63 cp = expand(copyname);
64 if (cp == NOSTR)
65 return;
66 if ((sfd = creat(cp, 0666)) < 0) {
67 (void) printf("\r\n%s: cannot creat\r\n", copyname);
68 return;
72 * collect parameters
74 if (prompt("List command for remote system? ", buf, sizeof (buf))) {
75 (void) unlink(copyname);
76 return;
78 transfer(buf, sfd, value(EOFREAD));
82 * Cu-like take command
84 /* ARGSUSED */
85 void
86 cu_take(int cc)
88 int fd, argc;
89 char line[BUFSIZ], *cp;
91 if (prompt("[take] ", copyname, sizeof (copyname)))
92 return;
93 argc = args(copyname, argv, sizeof (argv)/sizeof (char *));
94 if (argc < 1 || argc > 2) {
95 (void) printf("usage: <take> from [to]\r\n");
96 return;
98 if (argc == 1)
99 argv[1] = argv[0];
100 cp = expand(argv[1]);
101 if (cp == NOSTR)
102 return;
103 if ((fd = creat(cp, 0666)) < 0) {
104 (void) printf("\r\n%s: cannot create\r\n", argv[1]);
105 return;
107 (void) snprintf(line, sizeof (line), "cat %s; echo \01", argv[0]);
108 transfer(line, fd, "\01");
112 * Bulk transfer routine --
113 * used by getfl(), cu_take(), and pipefile()
115 void
116 transfer(char *buf, int fd, char *eofchars)
118 int ct;
119 char c, buffer[BUFSIZ];
120 char *p = buffer; /* can't be register because of longjmp */
121 int cnt, eof, bol;
122 time_t start;
123 sig_handler_t f;
125 parwrite(FD, (unsigned char *)buf, strlen(buf));
126 (void) kill(pid, SIGIOT);
127 /* Wait until read process stops */
128 (void) read(repdes[0], (char *)&ccc, 1);
131 * finish command
133 parwrite(FD, (unsigned char *)"\r", 1);
135 (void) read(FD, &c, 1);
136 while ((c&0177) != '\n')
139 if (sigsetjmp(intbuf, 1))
140 goto out;
141 f = signal(SIGINT, (sig_handler_t)intcopy);
142 intr("on");
144 start = time(0);
145 bol = 1;
146 ct = 0;
147 for (;;) {
148 eof = read(FD, &c, 1) <= 0;
149 if (noparity)
150 c &= 0377;
151 else
152 c &= 0177;
153 if (eof || (bol && any(c, eofchars)))
154 break;
155 if (c == 0)
156 continue; /* ignore nulls */
157 if (c == '\r')
158 continue;
159 *p++ = c;
161 if (c == '\n') {
162 bol = 1;
163 if (boolean(value(VERBOSE)))
164 (void) printf("\r%d", ++ct);
165 } else
166 bol = 0;
167 if ((cnt = (p-buffer)) == number(value(FRAMESIZE))) {
168 if (write(fd, buffer, cnt) != cnt) {
169 (void) printf("\r\nwrite error\r\n");
170 goto out;
172 p = buffer;
175 out:
176 if ((cnt = (p-buffer)) != 0)
177 if (write(fd, buffer, cnt) != cnt)
178 (void) printf("\r\nwrite error\r\n");
180 if (boolean(value(VERBOSE)))
181 prtime(" lines transferred in ", time(0)-start);
182 intr("off");
183 (void) write(fildes[1], (char *)&ccc, 1);
184 (void) signal(SIGINT, f);
185 (void) close(fd);
189 * FTP - remote ==> local process
190 * send remote input to local process via pipe
192 /* ARGSUSED */
193 void
194 pipefile(int cc)
196 int cpid, pdes[2];
197 char buf[256];
198 int status, p;
200 if (prompt("Local command? ", buf, sizeof (buf)))
201 return;
203 if (pipe(pdes)) {
204 (void) printf("can't establish pipe\r\n");
205 return;
208 if ((cpid = fork()) < 0) {
209 (void) printf("can't fork!\r\n");
210 return;
211 } else if (cpid) {
212 if (prompt("List command for remote system? ", buf,
213 sizeof (buf))) {
214 (void) close(pdes[0]), (void) close(pdes[1]);
215 (void) kill(cpid, SIGKILL);
216 } else {
217 (void) close(pdes[0]);
218 (void) signal(SIGPIPE, (sig_handler_t)intcopy);
219 transfer(buf, pdes[1], value(EOFREAD));
220 (void) signal(SIGPIPE, SIG_DFL);
221 while ((p = wait(&status)) > 0 && p != cpid)
224 } else {
225 int f;
227 userperm();
228 (void) dup2(pdes[0], 0);
229 (void) close(pdes[0]);
230 for (f = 3; f < 20; f++)
231 (void) close(f);
232 execute(buf);
233 (void) printf("can't execl!\r\n");
234 exit(0);
239 * FTP - local ==> remote
240 * send local file to remote host
241 * terminate transmission with pseudo EOF sequence
243 void
244 tip_sendfile(int cc)
246 FILE *fd;
247 char *fnamex;
249 (void) putchar(cc);
251 * get file name
253 if (prompt("Local file name? ", fname, sizeof (fname)))
254 return;
257 * look up file
259 fnamex = expand(fname);
260 if (fnamex == NOSTR)
261 return;
262 if ((fd = fopen(fnamex, "r")) == NULL) {
263 (void) printf("%s: cannot open\r\n", fname);
264 return;
266 transmit(fd, value(EOFWRITE), NULL);
267 if (!boolean(value(ECHOCHECK))) {
268 struct termios buf;
270 (void) ioctl(FD, TCGETS, (char *)&buf); /* this does a */
271 (void) ioctl(FD, TCSETSF, (char *)&buf); /* wflushtty */
276 * Bulk transfer routine to remote host --
277 * used by tip_sendfile() and cu_put()
279 void
280 transmit(FILE *fd, char *eofchars, char *command)
282 sig_handler_t ointr;
283 char *pc, lastc, rc;
284 int c, ccount, lcount;
285 time_t start_t, stop_t;
287 (void) kill(pid, SIGIOT); /* put TIPOUT into a wait state */
288 timedout = 0;
289 if (sigsetjmp(intbuf, 1)) {
290 if (timedout)
291 (void) printf("\r\ntimed out at eol\r\n");
292 (void) alarm(0);
293 goto out;
295 ointr = signal(SIGINT, (sig_handler_t)intcopy);
296 intr("on");
297 (void) read(repdes[0], (char *)&ccc, 1);
298 if (command != NULL) {
299 for (pc = command; *pc; pc++)
300 send(*pc);
301 if (boolean(value(ECHOCHECK)))
302 (void) read(FD, (char *)&c, 1); /* trailing \n */
303 else {
304 struct termios buf;
305 /* wait for remote stty to take effect */
306 (void) sleep(5);
307 /* this does a */
308 (void) ioctl(FD, TCGETS, (char *)&buf);
309 /* wflushtty */
310 (void) ioctl(FD, TCSETSF, (char *)&buf);
313 lcount = 0;
314 lastc = '\0';
315 start_t = time(0);
316 if (boolean(value(RAWFTP))) {
317 while ((c = getc(fd)) != EOF) {
318 lcount++;
319 send(c);
320 if (boolean(value(VERBOSE)) && lcount%100 == 0)
321 (void) printf("\r%d", lcount);
323 if (boolean(value(VERBOSE)))
324 (void) printf("\r%d", lcount);
325 goto out;
327 for (;;) {
328 ccount = 0;
329 do {
330 c = getc(fd);
331 if (c == EOF)
332 goto out;
333 if (c == 0177)
334 continue;
335 lastc = c;
336 if (c < 040) {
337 if (c == '\n') {
338 c = '\r';
339 } else if (c == '\t') {
340 if (boolean(value(TABEXPAND))) {
341 send(' ');
342 while ((++ccount % 8) != 0)
343 send(' ');
344 continue;
346 } else
347 continue;
349 send(c);
350 } while (c != '\r');
351 if (boolean(value(VERBOSE)))
352 (void) printf("\r%d", ++lcount);
353 if (boolean(value(ECHOCHECK))) {
354 (void) alarm(number(value(ETIMEOUT)));
355 do { /* wait for prompt */
356 (void) read(FD, &rc, 1);
357 } while ((rc&0177) != character(value(PROMPT)));
358 (void) alarm(0);
361 out:
362 if (lastc != '\n' && !boolean(value(RAWFTP)))
363 send('\r');
364 if (eofchars)
365 for (pc = eofchars; *pc; pc++)
366 send(*pc);
367 stop_t = time(0);
368 (void) fclose(fd);
369 if (boolean(value(VERBOSE)))
370 if (boolean(value(RAWFTP)))
371 prtime(" chars transferred in ", stop_t-start_t);
372 else
373 prtime(" lines transferred in ", stop_t-start_t);
374 (void) write(fildes[1], (char *)&ccc, 1);
375 intr("off");
376 (void) signal(SIGINT, ointr);
380 * Cu-like put command
382 /* ARGSUSED */
383 void
384 cu_put(int cc)
386 FILE *fd;
387 char line[BUFSIZ];
388 int argc;
389 char *copynamex;
391 if (prompt("[put] ", copyname, sizeof (copyname)))
392 return;
393 argc = args(copyname, argv, sizeof (argv)/sizeof (char *));
394 if (argc < 1 || argc > 2) {
395 (void) printf("usage: <put> from [to]\r\n");
396 return;
398 if (argc == 1)
399 argv[1] = argv[0];
400 copynamex = expand(argv[0]);
401 if (copynamex == NOSTR)
402 return;
403 if ((fd = fopen(copynamex, "r")) == NULL) {
404 (void) printf("%s: cannot open\r\n", copynamex);
405 return;
407 if (boolean(value(ECHOCHECK)))
408 (void) snprintf(line, sizeof (line), "cat>%s\r", argv[1]);
409 else
410 (void) snprintf(line, sizeof (line),
411 "stty -echo; cat>%s; stty echo\r", argv[1]);
412 transmit(fd, "\04", line);
416 * FTP - send single character
417 * wait for echo & handle timeout
419 void
420 send(char c)
422 char cc;
423 int retry = 0;
425 cc = c;
426 parwrite(FD, (unsigned char *)&cc, 1);
427 #ifdef notdef
428 if (number(value(CDELAY)) > 0 && c != '\r')
429 nap(number(value(CDELAY)));
430 #endif
431 if (!boolean(value(ECHOCHECK))) {
432 #ifdef notdef
433 if (number(value(LDELAY)) > 0 && c == '\r')
434 nap(number(value(LDELAY)));
435 #endif
436 return;
438 tryagain:
439 timedout = 0;
440 if (sigsetjmp(intbuf, 1) && timedout) {
441 (void) printf("\r\ntimeout error (%s)\r\n", ctrl(c));
442 if (retry++ > 3)
443 return;
444 parwrite(FD, (unsigned char *)&null, 1); /* poke it */
445 goto tryagain;
447 (void) alarm(number(value(ETIMEOUT)));
448 (void) read(FD, &cc, 1);
449 (void) alarm(0);
452 void
453 timeout(void)
455 (void) signal(SIGALRM, (sig_handler_t)timeout);
456 timedout = 1;
457 siglongjmp(intbuf, 1);
461 * Stolen from consh() -- puts a remote file on the output of a local command.
462 * Identical to consh() except for where stdout goes.
464 void
465 pipeout(int c)
467 char buf[256];
468 int cpid, status, p;
469 time_t start;
471 (void) putchar(c);
472 if (prompt("Local command? ", buf, sizeof (buf)))
473 return;
474 (void) kill(pid, SIGIOT); /* put TIPOUT into a wait state */
475 (void) signal(SIGINT, SIG_IGN);
476 (void) signal(SIGQUIT, SIG_IGN);
477 intr("on");
478 (void) read(repdes[0], (char *)&ccc, 1);
480 * Set up file descriptors in the child and
481 * let it go...
483 if ((cpid = fork()) < 0)
484 (void) printf("can't fork!\r\n");
485 else if (cpid) {
486 start = time(0);
487 while ((p = wait(&status)) > 0 && p != cpid)
489 } else {
490 int i;
492 userperm();
493 (void) dup2(FD, 1);
494 for (i = 3; i < 20; i++)
495 (void) close(i);
496 (void) signal(SIGINT, SIG_DFL);
497 (void) signal(SIGQUIT, SIG_DFL);
498 execute(buf);
499 (void) printf("can't find `%s'\r\n", buf);
500 exit(0);
502 if (boolean(value(VERBOSE)))
503 prtime("away for ", time(0)-start);
504 (void) write(fildes[1], (char *)&ccc, 1);
505 intr("off");
506 (void) signal(SIGINT, SIG_DFL);
507 (void) signal(SIGQUIT, SIG_DFL);
511 * Fork a program with:
512 * 0 <-> remote tty in
513 * 1 <-> remote tty out
514 * 2 <-> local tty stderr out
516 void
517 consh(int c)
519 char buf[256];
520 int cpid, status, p;
521 sig_handler_t ointr, oquit;
522 time_t start;
524 (void) putchar(c);
525 if (prompt("Local command? ", buf, sizeof (buf)))
526 return;
527 (void) kill(pid, SIGIOT); /* put TIPOUT into a wait state */
528 (void) read(repdes[0], (char *)&ccc, 1);
529 ointr = signal(SIGINT, SIG_IGN);
530 oquit = signal(SIGQUIT, SIG_IGN);
531 unraw();
533 * Set up file descriptors in the child and
534 * let it go...
536 if ((cpid = fork()) < 0)
537 (void) printf("can't fork!\r\n");
538 else if (cpid) {
539 start = time(0);
540 while ((p = wait(&status)) > 0 && p != cpid)
542 raw();
543 (void) signal(SIGINT, ointr);
544 (void) signal(SIGQUIT, oquit);
545 } else {
546 int i;
548 userperm();
549 (void) dup2(FD, 0);
550 (void) dup2(0, 1);
551 for (i = 3; i < 20; i++)
552 (void) close(i);
553 (void) signal(SIGINT, SIG_DFL);
554 (void) signal(SIGQUIT, SIG_DFL);
555 execute(buf);
556 (void) printf("can't find `%s'\r\n", buf);
557 exit(0);
559 if (boolean(value(VERBOSE)))
560 prtime("\r\naway for ", time(0)-start);
561 (void) write(fildes[1], (char *)&ccc, 1);
565 * Escape to local shell
567 /* ARGSUSED */
568 void
569 shell(int cc)
571 int shpid, status;
572 sig_handler_t ointr, oquit;
573 char *cp;
575 (void) printf("[sh]\r\n");
576 ointr = signal(SIGINT, SIG_IGN);
577 oquit = signal(SIGQUIT, SIG_IGN);
578 unraw();
579 if (shpid = fork()) {
580 while (shpid != wait(&status))
582 raw();
583 (void) printf("\r\n!\r\n");
584 (void) signal(SIGINT, ointr);
585 (void) signal(SIGQUIT, oquit);
586 } else {
587 userperm();
588 (void) signal(SIGQUIT, SIG_DFL);
589 (void) signal(SIGINT, SIG_DFL);
590 if ((cp = strrchr(value(SHELL), '/')) == NULL)
591 cp = value(SHELL);
592 else
593 cp++;
594 (void) execl(value(SHELL), cp, 0);
595 (void) printf("\r\ncan't execl!\r\n");
596 exit(1);
601 * TIPIN portion of scripting
602 * initiate the conversation with TIPOUT
604 void
605 setscript(void)
607 char c;
609 if (strlen(value(RECORD)) >= PATH_MAX-1) {
610 (void) fprintf(stderr, "tip: record file name too long\r\n");
611 return;
614 * enable TIPOUT side for dialogue
616 (void) kill(pid, SIGEMT);
617 if (boolean(value(SCRIPT)))
618 (void) write(fildes[1], value(RECORD), strlen(value(RECORD)));
619 (void) write(fildes[1], "\n", 1);
621 * wait for TIPOUT to finish
623 (void) read(repdes[0], &c, 1);
624 if (c == 'n')
625 (void) fprintf(stderr, "tip: can't create record file %s\r\n",
626 value(RECORD));
630 * Change current working directory of
631 * local portion of tip
633 /* ARGSUSED */
634 void
635 chdirectory(int cc)
637 char dirname[80];
638 char *cp = dirname;
640 if (prompt("[cd] ", dirname, sizeof (dirname))) {
641 if (stoprompt)
642 return;
643 cp = value(HOME);
645 if (chdir(cp) < 0)
646 (void) printf("%s: bad directory\r\n", cp);
647 (void) printf("!\r\n");
650 void
651 tip_abort(char *msg)
653 /* don't want to hear about our child */
654 (void) signal(SIGCHLD, SIG_DFL);
655 (void) kill(pid, SIGTERM);
656 myperm();
657 disconnect(msg);
658 if (msg != NOSTR)
659 (void) printf("\r\n%s", msg);
660 (void) printf("\r\n[EOT]\r\n");
661 delock(uucplock);
662 unraw();
663 exit(0);
666 /* ARGSUSED */
667 void
668 finish(int cc)
670 char *dismsg;
672 if ((dismsg = value(DISCONNECT)) != NOSTR) {
673 (void) write(FD, dismsg, strlen(dismsg));
674 (void) sleep(5);
676 tip_abort(NOSTR);
679 void
680 intcopy(void)
683 (void) signal(SIGINT, SIG_IGN);
684 siglongjmp(intbuf, 1);
687 void
688 execute(char *s)
690 char *cp;
692 if ((cp = strrchr(value(SHELL), '/')) == NULL)
693 cp = value(SHELL);
694 else
695 cp++;
696 (void) execl(value(SHELL), cp, "-c", s, 0);
700 args(char *buf, char *a[], size_t na)
702 char *p = buf, *start;
703 char **parg = a;
704 int n = 0;
706 do {
707 while (*p && (*p == ' ' || *p == '\t'))
708 p++;
709 start = p;
710 if (*p)
711 *parg = p;
712 while (*p && (*p != ' ' && *p != '\t'))
713 p++;
714 if (p != start)
715 parg++, n++;
716 if (*p)
717 *p++ = '\0';
718 } while (*p && n < na);
720 return (n);
723 void
724 prtime(char *s, time_t a)
726 int i;
727 int nums[3];
729 for (i = 0; i < 3; i++) {
730 nums[i] = (int)(a % quant[i]);
731 a /= quant[i];
733 (void) printf("%s", s);
734 while (--i >= 0)
735 if (nums[i] || i == 0 && nums[1] == 0 && nums[2] == 0)
736 (void) printf("%d %s%c ", nums[i], sep[i],
737 nums[i] == 1 ? '\0' : 's');
738 (void) printf("\r\n!\r\n");
741 /* ARGSUSED */
742 void
743 variable(int cc)
745 char buf[256];
747 if (prompt("[set] ", buf, sizeof (buf)))
748 return;
749 vlex(buf);
750 if (vtable[BEAUTIFY].v_access&CHANGED) {
751 vtable[BEAUTIFY].v_access &= ~CHANGED;
752 (void) kill(pid, SIGSYS);
754 if (vtable[SCRIPT].v_access&CHANGED) {
755 vtable[SCRIPT].v_access &= ~CHANGED;
756 setscript();
758 * So that "set record=blah script" doesn't
759 * cause two transactions to occur.
761 if (vtable[RECORD].v_access&CHANGED)
762 vtable[RECORD].v_access &= ~CHANGED;
764 if (vtable[RECORD].v_access&CHANGED) {
765 vtable[RECORD].v_access &= ~CHANGED;
766 if (boolean(value(SCRIPT)))
767 setscript();
769 if (vtable[TAND].v_access&CHANGED) {
770 vtable[TAND].v_access &= ~CHANGED;
771 if (boolean(value(TAND)))
772 tandem("on");
773 else
774 tandem("off");
776 if (vtable[LECHO].v_access&CHANGED) {
777 vtable[LECHO].v_access &= ~CHANGED;
778 boolean(value(HALFDUPLEX)) = boolean(value(LECHO));
780 if (vtable[PARITY].v_access&CHANGED) {
781 vtable[PARITY].v_access &= ~CHANGED;
782 setparity(NULL);
784 if (vtable[BAUDRATE].v_access&CHANGED) {
785 vtable[BAUDRATE].v_access &= ~CHANGED;
786 ttysetup(speed(number(value(BAUDRATE))));
788 if (vtable[HARDWAREFLOW].v_access & CHANGED) {
789 vtable[HARDWAREFLOW].v_access &= ~CHANGED;
790 if (boolean(value(HARDWAREFLOW)))
791 hardwareflow("on");
792 else
793 hardwareflow("off");
798 * Turn tandem mode on or off for remote tty.
800 void
801 tandem(char *option)
803 struct termios rmtty;
805 (void) ioctl(FD, TCGETS, (char *)&rmtty);
806 if (equal(option, "on")) {
807 rmtty.c_iflag |= IXOFF|IXON;
808 arg.c_iflag |= IXOFF|IXON;
809 rmtty.c_cc[VSTART] = defarg.c_cc[VSTART];
810 rmtty.c_cc[VSTOP] = defarg.c_cc[VSTOP];
811 } else {
812 rmtty.c_iflag &= ~(IXOFF|IXON);
813 arg.c_iflag &= ~(IXOFF|IXON);
815 (void) ioctl(FD, TCSETSF, (char *)&rmtty);
816 (void) ioctl(0, TCSETSF, (char *)&arg);
820 * Turn hardwareflow mode on or off for remote tty.
822 void
823 hardwareflow(char *option)
825 struct termios rmtty;
827 (void) ioctl(FD, TCGETS, (char *)&rmtty);
828 if (equal(option, "on")) {
829 rmtty.c_cflag |= (CRTSCTS|CRTSXOFF);
830 } else {
831 rmtty.c_cflag &= ~(CRTSCTS|CRTSXOFF);
833 (void) ioctl(FD, TCSETSF, (char *)&rmtty);
837 * Turn interrupts from local tty on or off.
839 void
840 intr(char *option)
843 if (equal(option, "on"))
844 arg.c_lflag |= ISIG;
845 else
846 arg.c_lflag &= ~ISIG;
847 (void) ioctl(0, TCSETSF, (char *)&arg);
851 * Send a break.
853 /* ARGSUSED */
854 void
855 genbrk(int cc)
858 (void) ioctl(FD, TCSBRK, 0);
862 * Suspend tip
864 void
865 suspend(int c)
868 unraw();
869 (void) kill(c == _CTRL('y') ? getpid() : 0, SIGTSTP);
870 raw();
874 * expand a file name if it includes shell meta characters
877 char *
878 expand(char name[])
880 static char xname[BUFSIZ];
881 char cmdbuf[BUFSIZ];
882 int pid, l;
883 char *cp, *Shell;
884 int s, pivec[2];
886 if (!anyof(name, "~{[*?$`'\"\\"))
887 return (name);
888 if (pipe(pivec) < 0) {
889 perror("pipe");
890 return (name);
892 (void) snprintf(cmdbuf, sizeof (cmdbuf), "echo %s", name);
893 if ((pid = vfork()) == 0) {
894 userperm();
895 Shell = value(SHELL);
896 if (Shell == NOSTR)
897 Shell = "/bin/sh";
898 (void) close(pivec[0]);
899 (void) close(1);
900 (void) dup(pivec[1]);
901 (void) close(pivec[1]);
902 (void) close(2);
903 (void) execl(Shell, Shell, "-c", cmdbuf, 0);
904 _exit(1);
906 if (pid == -1) {
907 perror("fork");
908 (void) close(pivec[0]);
909 (void) close(pivec[1]);
910 return (NOSTR);
912 (void) close(pivec[1]);
913 l = read(pivec[0], xname, BUFSIZ);
914 (void) close(pivec[0]);
915 while (wait(&s) != pid)
917 s &= 0377;
918 if (s != 0 && s != SIGPIPE) {
919 (void) fprintf(stderr, "\"Echo\" failed\n");
920 return (NOSTR);
922 if (l < 0) {
923 perror("read");
924 return (NOSTR);
926 if (l == 0) {
927 (void) fprintf(stderr, "\"%s\": No match\n", name);
928 return (NOSTR);
930 if (l == BUFSIZ) {
931 (void) fprintf(stderr, "Buffer overflow expanding \"%s\"\n",
932 name);
933 return (NOSTR);
935 xname[l] = 0;
936 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
938 *++cp = '\0';
939 return (xname);
943 * Are any of the characters in the two strings the same?
947 anyof(char *s1, char *s2)
949 int c;
951 while ((c = *s1++) != 0)
952 if (any(c, s2))
953 return (1);
954 return (0);