First (hopefully) working version of caption processing.
[screen-lua.git] / src / fileio.c
blobd15f3462576f325bfb2d7a2a8c8349cbe134884f
1 /* Copyright (c) 2008, 2009
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32 #include <pwd.h>
34 #ifndef SIGINT
35 # include <signal.h>
36 #endif
38 #include "config.h"
39 #include "screen.h"
40 #include "extern.h"
42 extern struct display *display, *displays;
43 extern struct win *fore;
44 extern struct layer *flayer;
45 extern int real_uid, eff_uid;
46 extern int real_gid, eff_gid;
47 extern char *extra_incap, *extra_outcap;
48 extern char *home, *RcFileName;
49 extern char SockPath[], *SockName;
50 #ifdef COPY_PASTE
51 extern char *BufferFile;
52 #endif
53 extern int hardcopy_append;
54 extern char *hardcopydir;
56 static char *CatExtra __P((char *, char *));
57 static char *findrcfile __P((char *));
60 char *rc_name = "";
61 int rc_recursion = 0;
63 static char *
64 CatExtra(str1, str2)
65 register char *str1, *str2;
67 register char *cp;
68 register int len1, len2, add_colon;
70 len1 = strlen(str1);
71 if (len1 == 0)
72 return str2;
73 add_colon = (str1[len1 - 1] != ':');
74 if (str2)
76 len2 = strlen(str2);
77 if ((cp = realloc(str2, (unsigned) len1 + len2 + add_colon + 1)) == NULL)
78 Panic(0, strnomem);
79 bcopy(cp, cp + len1 + add_colon, len2 + 1);
81 else
83 if (len1 == 0)
84 return 0;
85 if ((cp = malloc((unsigned) len1 + add_colon + 1)) == NULL)
86 Panic(0, strnomem);
87 cp[len1 + add_colon] = '\0';
89 bcopy(str1, cp, len1);
90 if (add_colon)
91 cp[len1] = ':';
93 return cp;
96 static char *
97 findrcfile(rcfile)
98 char *rcfile;
100 char buf[256];
101 char *p;
103 /* Tilde prefix support courtesy <hesso@pool.math.tu-berlin.de>,
104 * taken from a Debian patch. */
105 if (rcfile && *rcfile == '~')
107 static char rcfilename_tilde_exp[MAXPATHLEN+1];
108 char *slash_position = strchr(rcfile, '/');
109 if (slash_position == rcfile+1)
111 char *home = getenv("HOME");
112 if (!home)
114 Msg(0, "%s: source: tilde expansion failed", rc_name);
115 return;
117 snprintf(rcfilename_tilde_exp, MAXPATHLEN, "%s/%s", home, rcfile+2);
119 else if (slash_position)
121 struct passwd *p;
122 *slash_position = 0;
123 p = getpwnam(rcfile+1);
124 if (!p)
126 Msg(0, "%s: source: tilde expansion failed for user %s", rc_name, rcfile+1);
127 return;
129 snprintf(rcfilename_tilde_exp, MAXPATHLEN, "%s/%s", p->pw_dir, slash_position+1);
131 else
133 Msg(0, "%s: source: illegal tilde expression.", rc_name);
134 return;
136 rcfile = rcfilename_tilde_exp;
139 if (rcfile)
141 char *rcend = rindex(rc_name, '/');
142 if (*rcfile != '/' && rcend && (rcend - rc_name) + strlen(rcfile) + 2 < sizeof(buf))
144 strncpy(buf, rc_name, rcend - rc_name + 1);
145 strcpy(buf + (rcend - rc_name) + 1, rcfile);
146 if (access(buf, R_OK) == 0)
147 return SaveStr(buf);
149 debug1("findrcfile: you specified '%s'\n", rcfile);
150 return SaveStr(rcfile);
152 debug("findrcfile: you specified nothing...\n");
153 if ((p = getenv("SCREENRC")) != NULL && *p != '\0')
155 debug1(" $SCREENRC has: '%s'\n", p);
156 return SaveStr(p);
158 else
160 debug(" ...nothing in $SCREENRC, defaulting $HOME/.screenrc\n");
161 if (strlen(home) > sizeof(buf) - 12)
162 Panic(0, "Rc: home too large");
163 sprintf(buf, "%s/.screenrc", home);
164 return SaveStr(buf);
169 * this will be called twice:
170 * 1) rcfilename = "/etc/screenrc"
171 * 2) rcfilename = RcFileName
174 StartRc(rcfilename, nopanic)
175 char *rcfilename;
176 int nopanic;
178 register int argc, len;
179 register char *p, *cp;
180 char buf[2048];
181 char *args[MAXARGS];
182 int argl[MAXARGS];
183 FILE *fp;
184 char *oldrc_name = rc_name;
186 /* always fix termcap/info capabilities */
187 extra_incap = CatExtra("TF", extra_incap);
189 /* Special settings for vt100 and others */
190 if (display && (!strncmp(D_termname, "vt", 2) || !strncmp(D_termname, "xterm", 5)))
191 extra_incap = CatExtra("xn:f0=\033Op:f1=\033Oq:f2=\033Or:f3=\033Os:f4=\033Ot:f5=\033Ou:f6=\033Ov:f7=\033Ow:f8=\033Ox:f9=\033Oy:f.=\033On:f,=\033Ol:fe=\033OM:f+=\033Ok:f-=\033Om:f*=\033Oj:f/=\033Oo:fq=\033OX", extra_incap);
193 rc_name = findrcfile(rcfilename);
195 if ((fp = secfopen(rc_name, "r")) == NULL)
197 if (!rc_recursion && RcFileName && !strcmp(RcFileName, rc_name))
200 * User explicitly gave us that name,
201 * this is the only case, where we get angry, if we can't read
202 * the file.
204 debug3("StartRc: '%s','%s', '%s'\n", RcFileName, rc_name, rcfilename);
205 if (!nopanic) Panic(0, "Unable to open \"%s\".", rc_name);
206 /* possibly NOTREACHED */
208 debug1("StartRc: '%s' no good. ignored\n", rc_name);
209 Free(rc_name);
210 rc_name = oldrc_name;
211 return 1;
213 while (fgets(buf, sizeof buf, fp) != NULL)
215 if ((p = rindex(buf, '\n')) != NULL)
216 *p = '\0';
217 if ((argc = Parse(buf, sizeof buf, args, argl)) == 0)
218 continue;
219 if (strcmp(args[0], "echo") == 0)
221 if (!display)
222 continue;
223 if (argc < 2 || (argc == 3 && strcmp(args[1], "-n")) || argc > 3)
225 Msg(0, "%s: 'echo [-n] \"string\"' expected.", rc_name);
226 continue;
228 AddStr(args[argc - 1]);
229 if (argc != 3)
231 AddStr("\r\n");
232 Flush();
235 else if (strcmp(args[0], "sleep") == 0)
237 if (!display)
238 continue;
239 debug("sleeeeeeep\n");
240 if (argc != 2)
242 Msg(0, "%s: sleep: one numeric argument expected.", rc_name);
243 continue;
245 DisplaySleep1000(1000 * atoi(args[1]), 1);
247 #ifdef TERMINFO
248 else if (!strcmp(args[0], "termcapinfo") || !strcmp(args[0], "terminfo"))
249 #else
250 else if (!strcmp(args[0], "termcapinfo") || !strcmp(args[0], "termcap"))
251 #endif
253 if (!display)
254 continue;
255 if (argc < 3 || argc > 4)
257 Msg(0, "%s: %s: incorrect number of arguments.", rc_name, args[0]);
258 continue;
260 for (p = args[1]; p && *p; p = cp)
262 if ((cp = index(p, '|')) != 0)
263 *cp++ = '\0';
264 len = strlen(p);
265 if (p[len - 1] == '*')
267 if (!(len - 1) || !strncmp(p, D_termname, len - 1))
268 break;
270 else if (!strcmp(p, D_termname))
271 break;
273 if (!(p && *p))
274 continue;
275 extra_incap = CatExtra(args[2], extra_incap);
276 if (argc == 4)
277 extra_outcap = CatExtra(args[3], extra_outcap);
279 else if (!strcmp(args[0], "source"))
281 if (rc_recursion <= 10)
283 rc_recursion++;
284 (void)StartRc(args[1], 0);
285 rc_recursion--;
289 fclose(fp);
290 Free(rc_name);
291 rc_name = oldrc_name;
292 return 0;
295 void
296 FinishRc(rcfilename)
297 char *rcfilename;
299 char buf[2048];
300 FILE *fp;
301 char *oldrc_name = rc_name;
303 rc_name = findrcfile(rcfilename);
305 if ((fp = secfopen(rc_name, "r")) == NULL)
307 if (rc_recursion)
308 Msg(errno, "%s: source %s", oldrc_name, rc_name);
309 else if (RcFileName && !strcmp(RcFileName, rc_name))
312 * User explicitly gave us that name,
313 * this is the only case, where we get angry, if we can't read
314 * the file.
316 debug3("FinishRc:'%s','%s','%s'\n", RcFileName, rc_name, rcfilename);
317 Panic(0, "Unable to open \"%s\".", rc_name);
318 /* NOTREACHED */
320 debug1("FinishRc: '%s' no good. ignored\n", rc_name);
321 Free(rc_name);
322 rc_name = oldrc_name;
323 return;
326 debug("finishrc is going...\n");
327 while (fgets(buf, sizeof buf, fp) != NULL)
328 RcLine(buf, sizeof buf);
329 (void) fclose(fp);
330 Free(rc_name);
331 rc_name = oldrc_name;
334 void
335 do_source(rcfilename)
336 char *rcfilename;
338 if (rc_recursion > 10)
340 Msg(0, "%s: source: recursion limit reached", rc_name);
341 return;
343 rc_recursion++;
344 FinishRc(rcfilename);
345 rc_recursion--;
350 * Running a Command Line in the environment determined by the display.
351 * The fore window is taken from the display as well as the user.
352 * This is bad when we run detached.
354 void
355 RcLine(ubuf, ubufl)
356 char *ubuf;
357 int ubufl;
359 char *args[MAXARGS];
360 int argl[MAXARGS];
361 #ifdef MULTIUSER
362 extern struct acluser *EffectiveAclUser; /* acl.c */
363 extern struct acluser *users; /* acl.c */
364 #endif
366 if (display)
368 fore = D_fore;
369 flayer = D_forecv->c_layer;
371 else
372 flayer = fore ? fore->w_savelayer : 0;
373 if (Parse(ubuf, ubufl, args, argl) <= 0)
374 return;
375 #ifdef MULTIUSER
376 if (!display)
378 /* the session owner does it, when there is no display here */
379 EffectiveAclUser = users;
380 debug("RcLine: WARNING, no display no user! Session owner executes command\n");
382 #endif
383 DoCommand(args, argl);
384 #ifdef MULTIUSER
385 EffectiveAclUser = 0;
386 #endif
390 * needs display for copybuffer access and termcap dumping
392 void
393 WriteFile(user, fn, dump)
394 struct acluser *user;
395 char *fn;
396 int dump;
398 /* dump==0: create .termcap,
399 * dump==1: hardcopy,
400 * #ifdef COPY_PASTE
401 * dump==2: BUFFERFILE
402 * #endif COPY_PASTE
403 * dump==1: scrollback,
405 register int i, j, k;
406 register char *p;
407 register FILE *f;
408 char fnbuf[1024];
409 char *mode = "w";
410 #ifdef COPY_PASTE
411 int public = 0;
412 # ifdef _MODE_T
413 mode_t old_umask;
414 # else
415 int old_umask;
416 # endif
417 # ifdef HAVE_LSTAT
418 struct stat stb, stb2;
419 int fd, exists = 0;
420 # endif
421 #endif
423 switch (dump)
425 case DUMP_TERMCAP:
426 if (fn == 0)
428 i = SockName - SockPath;
429 if (i > (int)sizeof(fnbuf) - 9)
430 i = 0;
431 strncpy(fnbuf, SockPath, i);
432 strcpy(fnbuf + i, ".termcap");
433 fn = fnbuf;
435 break;
436 case DUMP_HARDCOPY:
437 case DUMP_SCROLLBACK:
438 if (fn == 0)
440 if (fore == 0)
441 return;
442 if (hardcopydir && *hardcopydir && strlen(hardcopydir) < sizeof(fnbuf) - 21)
443 sprintf(fnbuf, "%s/hardcopy.%d", hardcopydir, fore->w_number);
444 else
445 sprintf(fnbuf, "hardcopy.%d", fore->w_number);
446 fn = fnbuf;
448 if (hardcopy_append && !access(fn, W_OK))
449 mode = "a";
450 break;
451 #ifdef COPY_PASTE
452 case DUMP_EXCHANGE:
453 if (fn == 0)
455 strncpy(fnbuf, BufferFile, sizeof(fnbuf) - 1);
456 fnbuf[sizeof(fnbuf) - 1] = 0;
457 fn = fnbuf;
459 public = !strcmp(fn, DEFAULT_BUFFERFILE);
460 # ifdef HAVE_LSTAT
461 exists = !lstat(fn, &stb);
462 if (public && exists && (S_ISLNK(stb.st_mode) || stb.st_nlink > 1))
464 Msg(0, "No write to links, please.");
465 return;
467 # endif
468 break;
469 #endif
472 debug2("WriteFile(%d) %s\n", dump, fn);
473 if (UserContext() > 0)
475 debug("Writefile: usercontext\n");
476 #ifdef COPY_PASTE
477 if (dump == DUMP_EXCHANGE && public)
479 old_umask = umask(0);
480 # ifdef HAVE_LSTAT
481 if (exists)
483 if ((fd = open(fn, O_WRONLY, 0666)) >= 0)
485 if (fstat(fd, &stb2) == 0 && stb.st_dev == stb2.st_dev && stb.st_ino == stb2.st_ino)
486 ftruncate(fd, 0);
487 else
489 close(fd);
490 fd = -1;
494 else
495 fd = open(fn, O_WRONLY|O_CREAT|O_EXCL, 0666);
496 f = fd >= 0 ? fdopen(fd, mode) : 0;
497 # else
498 f = fopen(fn, mode);
499 # endif
500 umask(old_umask);
502 else
503 #endif /* COPY_PASTE */
504 f = fopen(fn, mode);
505 if (f == NULL)
507 debug2("WriteFile: fopen(%s,\"%s\") failed\n", fn, mode);
508 UserReturn(0);
510 else
512 switch (dump)
514 case DUMP_HARDCOPY:
515 case DUMP_SCROLLBACK:
516 if (!fore)
517 break;
518 if (*mode == 'a')
520 putc('>', f);
521 for (j = fore->w_width - 2; j > 0; j--)
522 putc('=', f);
523 fputs("<\n", f);
525 if (dump == DUMP_SCROLLBACK)
527 #ifdef COPY_PASTE
528 for (i = 0; i < fore->w_histheight; i++)
530 p = (char *)(WIN(i)->image);
531 for (k = fore->w_width - 1; k >= 0 && p[k] == ' '; k--)
533 for (j = 0; j <= k; j++)
534 putc(p[j], f);
535 putc('\n', f);
537 #endif
539 for (i = 0; i < fore->w_height; i++)
541 p = (char *)fore->w_mlines[i].image;
542 for (k = fore->w_width - 1; k >= 0 && p[k] == ' '; k--)
544 for (j = 0; j <= k; j++)
545 putc(p[j], f);
546 putc('\n', f);
548 break;
549 case DUMP_TERMCAP:
550 if ((p = index(MakeTermcap(fore->w_aflag), '=')) != NULL)
552 fputs(++p, f);
553 putc('\n', f);
555 break;
556 #ifdef COPY_PASTE
557 case DUMP_EXCHANGE:
558 p = user->u_plop.buf;
559 for (i = user->u_plop.len; i-- > 0; p++)
560 if (*p == '\r' && (i == 0 || p[1] != '\n'))
561 putc('\n', f);
562 else
563 putc(*p, f);
564 break;
565 #endif
567 (void) fclose(f);
568 UserReturn(1);
571 if (UserStatus() <= 0)
572 Msg(0, "Cannot open \"%s\"", fn);
573 else if (display && !*rc_name)
575 switch (dump)
577 case DUMP_TERMCAP:
578 Msg(0, "Termcap entry written to \"%s\".", fn);
579 break;
580 case DUMP_HARDCOPY:
581 case DUMP_SCROLLBACK:
582 Msg(0, "Screen image %s to \"%s\".",
583 (*mode == 'a') ? "appended" : "written", fn);
584 break;
585 #ifdef COPY_PASTE
586 case DUMP_EXCHANGE:
587 Msg(0, "Copybuffer written to \"%s\".", fn);
588 #endif
593 #ifdef COPY_PASTE
596 * returns an allocated buffer which holds a copy of the file named fn.
597 * lenp (if nonzero) points to a location, where the buffer size should be
598 * stored.
600 char *
601 ReadFile(fn, lenp)
602 char *fn;
603 int *lenp;
605 int i, l, size;
606 char c, *bp, *buf;
607 struct stat stb;
609 ASSERT(lenp);
610 debug1("ReadFile(%s)\n", fn);
611 if ((i = secopen(fn, O_RDONLY, 0)) < 0)
613 Msg(errno, "no %s -- no slurp", fn);
614 return NULL;
616 if (fstat(i, &stb))
618 Msg(errno, "no good %s -- no slurp", fn);
619 close(i);
620 return NULL;
622 size = stb.st_size;
623 if ((buf = malloc(size)) == NULL)
625 close(i);
626 Msg(0, strnomem);
627 return NULL;
629 errno = 0;
630 if ((l = read(i, buf, size)) != size)
632 if (l < 0)
633 l = 0;
634 Msg(errno, "Got only %d bytes from %s", l, fn);
635 close(i);
637 else
639 if (read(i, &c, 1) > 0)
640 Msg(0, "Slurped only %d characters (of %d) into buffer - try again",
641 l, size);
642 else
643 Msg(0, "Slurped %d characters into buffer", l);
645 close(i);
646 *lenp = l;
647 for (bp = buf; l-- > 0; bp++)
648 if (*bp == '\n' && (bp == buf || bp[-1] != '\r'))
649 *bp = '\r';
650 return buf;
653 void
654 KillBuffers()
656 if (UserContext() > 0)
657 UserReturn(unlink(BufferFile) ? errno : 0);
658 errno = UserStatus();
659 Msg(errno, "%s %sremoved", BufferFile, errno ? "not " : "");
661 #endif /* COPY_PASTE */
665 * (Almost) secure open and fopen...
668 FILE *
669 secfopen(name, mode)
670 char *name;
671 char *mode;
673 FILE *fi;
674 #ifndef USE_SETEUID
675 int flags, fd;
676 #endif
678 debug2("secfopen(%s, %s)\n", name, mode);
679 #ifdef USE_SETEUID
680 xseteuid(real_uid);
681 xsetegid(real_gid);
682 fi = fopen(name, mode);
683 xseteuid(eff_uid);
684 xsetegid(eff_gid);
685 return fi;
686 #else
687 if (eff_uid == real_uid)
688 return fopen(name, mode);
689 if (mode[0] && mode[1] == '+')
690 flags = O_RDWR;
691 else
692 flags = (mode[0] == 'r') ? O_RDONLY : O_WRONLY;
693 if (mode[0] == 'w')
694 flags |= O_CREAT | O_TRUNC;
695 else if (mode[0] == 'a')
696 flags |= O_CREAT | O_APPEND;
697 else if (mode[0] != 'r')
699 errno = EINVAL;
700 return 0;
702 if ((fd = secopen(name, flags, 0666)) < 0)
703 return 0;
704 if ((fi = fdopen(fd, mode)) == 0)
706 close(fd);
707 return 0;
709 return fi;
710 #endif
715 secopen(name, flags, mode)
716 char *name;
717 int flags;
718 int mode;
720 int fd;
721 #ifndef USE_SETEUID
722 int q;
723 struct stat stb;
724 #endif
726 debug3("secopen(%s, 0x%x, 0%03o)\n", name, flags, mode);
727 #ifdef USE_SETEUID
728 xseteuid(real_uid);
729 xsetegid(real_gid);
730 fd = open(name, flags, mode);
731 xseteuid(eff_uid);
732 xsetegid(eff_gid);
733 return fd;
734 #else
735 if (eff_uid == real_uid)
736 return open(name, flags, mode);
737 /* Truncation/creation is done in UserContext */
738 if ((flags & O_TRUNC) || ((flags & O_CREAT) && access(name, F_OK)))
740 if (UserContext() > 0)
742 if ((fd = open(name, flags, mode)) >= 0)
744 close(fd);
745 UserReturn(0);
747 if (errno == 0)
748 errno = EACCES;
749 UserReturn(errno);
751 if ((q = UserStatus()))
753 if (q > 0)
754 errno = q;
755 return -1;
758 if (access(name, F_OK))
759 return -1;
760 if ((fd = open(name, flags & ~(O_TRUNC | O_CREAT), 0)) < 0)
761 return -1;
762 debug("open successful\n");
763 if (fstat(fd, &stb))
765 close(fd);
766 return -1;
768 debug("fstat successful\n");
769 if (stb.st_uid != real_uid)
771 switch (flags & (O_RDONLY | O_WRONLY | O_RDWR))
773 case O_RDONLY:
774 q = 0004;
775 break;
776 case O_WRONLY:
777 q = 0002;
778 break;
779 default:
780 q = 0006;
781 break;
783 if ((stb.st_mode & q) != q)
785 debug1("secopen: permission denied (%03o)\n", stb.st_mode & 07777);
786 close(fd);
787 errno = EACCES;
788 return -1;
791 debug1("secopen ok - returning %d\n", fd);
792 return fd;
793 #endif
798 printpipe(p, cmd)
799 struct win *p;
800 char *cmd;
802 int pi[2];
803 if (pipe(pi))
805 WMsg(p, errno, "printing pipe");
806 return -1;
808 switch (fork())
810 case -1:
811 WMsg(p, errno, "printing fork");
812 return -1;
813 case 0:
814 display = p->w_pdisplay;
815 displays = 0;
816 #ifdef DEBUG
817 if (dfp && dfp != stderr)
818 fclose(dfp);
819 #endif
820 close(0);
821 dup(pi[0]);
822 closeallfiles(0);
823 if (setgid(real_gid) || setuid(real_uid))
824 Panic(errno, "printpipe setuid");
825 #ifdef SIGPIPE
826 signal(SIGPIPE, SIG_DFL);
827 #endif
828 execl("/bin/sh", "sh", "-c", cmd, (char *)0);
829 Panic(errno, "/bin/sh");
830 default:
831 break;
833 close(pi[0]);
834 return pi[1];
838 readpipe(cmdv)
839 char **cmdv;
841 int pi[2];
843 if (pipe(pi))
845 Msg(errno, "pipe");
846 return -1;
848 switch (fork())
850 case -1:
851 Msg(errno, "fork");
852 return -1;
853 case 0:
854 displays = 0;
855 #ifdef DEBUG
856 if (dfp && dfp != stderr)
857 fclose(dfp);
858 #endif
859 close(1);
860 if (dup(pi[1]) != 1)
862 close(pi[1]);
863 Panic(0, "dup");
865 closeallfiles(1);
866 if (setgid(real_gid) || setuid(real_uid))
868 close(1);
869 Panic(errno, "setuid/setgid");
871 #ifdef SIGPIPE
872 signal(SIGPIPE, SIG_DFL);
873 #endif
874 execvp(*cmdv, cmdv);
875 close(1);
876 Panic(errno, "%s", *cmdv);
877 default:
878 break;
880 close(pi[1]);
881 return pi[0];