148110Sbostic /*-
2*62185Sbostic * Copyright (c) 1980, 1993
3*62185Sbostic * The Regents of the University of California. All rights reserved.
448110Sbostic *
548110Sbostic * %sccs.include.redist.c%
622264Sdist */
722264Sdist
822264Sdist #ifndef lint
9*62185Sbostic static char copyright[] =
10*62185Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*62185Sbostic The Regents of the University of California. All rights reserved.\n";
1248110Sbostic #endif /* not lint */
1322264Sdist
1422264Sdist #ifndef lint
15*62185Sbostic static char sccsid[] = "@(#)px_header.c 8.1 (Berkeley) 06/06/93";
1648110Sbostic #endif /* not lint */
1722264Sdist
1822264Sdist /*
192841Swnj * pxheader - program to sit in front of interpreter code to make shell mods
202841Swnj * unnecessary to make Pascal obj's look like real programs.
212841Swnj *
222841Swnj * Bill Joy UCB February 6, 1978
232841Swnj */
242841Swnj
252948Smckusic #include <stdio.h>
262948Smckusic #include <sys/types.h>
272948Smckusic #include <a.out.h>
2810571Smckusick #include "config.h"
292948Smckusic #include "whoami.h"
302948Smckusic #include "objfmt.h"
312948Smckusic
322841Swnj #define ETXTBSY 26
3311879Smckusick #define ADDR_LC \
3411879Smckusick (START + HEADER_BYTES - sizeof (struct exec) - sizeof (struct pxhdr))
355639Smckusic #define MAXARGS 512
362841Swnj
372948Smckusic extern errno;
382948Smckusic
main(argc,argv)392841Swnj main(argc, argv)
402841Swnj register int argc;
412841Swnj register char *argv[];
422841Swnj {
435639Smckusic register int i;
445639Smckusic int codesiz, symtabsiz;
455639Smckusic register char *cp;
465639Smckusic char *largv[MAXARGS];
475639Smckusic int fd, pv[2], pid;
482841Swnj
495639Smckusic cp = (char *)(ADDR_LC);
505639Smckusic codesiz = ((struct pxhdr *)(cp))->objsize + sizeof(struct pxhdr);
515639Smckusic symtabsiz = ((struct pxhdr *)(cp))->symtabsize;
525639Smckusic if (argc > MAXARGS - 3)
535639Smckusic error(2, "Too many arguments.\n");
545639Smckusic if (symtabsiz != 0) {
555639Smckusic largv[0] = "pxhdr";
565639Smckusic largv[1] = "/tmp/px00000";
575639Smckusic cp = &largv[1][11];
585639Smckusic for (i = getpid(); i > 0; i /= 10)
595639Smckusic *cp-- = '0' + i % 10;
605639Smckusic fd = creat(largv[1], 0444);
615639Smckusic if (fd < 0)
625639Smckusic error(3, "Cannot create /tmp file\n");
635639Smckusic for (i = 0; i < argc; i++)
645639Smckusic largv[i + 2] = argv[i];
655639Smckusic largv[argc + 2] = 0;
665639Smckusic writeobj(fd, codesiz, symtabsiz);
6710571Smckusick run(px_debug, largv);
685639Smckusic /* no return */
692841Swnj }
705639Smckusic largv[0] = "pipe";
715639Smckusic for (i = 0; i < argc; i++)
722841Swnj largv[i + 1] = argv[i];
732841Swnj largv[argc + 1] = 0;
742841Swnj pipe(pv);
755639Smckusic pid = fork();
765639Smckusic if (pid != 0) {
775639Smckusic if (pv[0] != 3) {
785639Smckusic close(3);
795639Smckusic dup(pv[0]);
805639Smckusic close(pv[0]);
812841Swnj }
825639Smckusic close(pv[1]);
8310571Smckusick run(px_intrp, largv);
845639Smckusic /* no return */
852841Swnj }
865639Smckusic writeobj(pv[1], codesiz, symtabsiz);
875639Smckusic exit(0);
885639Smckusic }
895639Smckusic
writeobj(fd,codesiz,symtabsiz)905639Smckusic writeobj(fd, codesiz, symtabsiz)
915639Smckusic int fd;
925639Smckusic int codesiz, symtabsiz;
935639Smckusic {
945639Smckusic int i;
955639Smckusic register char *cp;
965639Smckusic
975639Smckusic cp = (char *)(ADDR_LC);
985639Smckusic while (codesiz != 0) {
995639Smckusic i = (codesiz < BUFSIZ) ? codesiz : BUFSIZ;
1005639Smckusic write(fd, cp, i);
1015639Smckusic cp += i;
1025639Smckusic codesiz -= i;
1032841Swnj }
1045639Smckusic while (symtabsiz != 0) {
1055639Smckusic i = (symtabsiz < BUFSIZ) ? symtabsiz : BUFSIZ;
1065639Smckusic write(fd, cp, i);
1075639Smckusic cp += i;
1085639Smckusic symtabsiz -= i;
1095639Smckusic }
1105639Smckusic close(fd);
1115639Smckusic }
1125639Smckusic
run(prog,args)1135639Smckusic run(prog, args)
1145639Smckusic char *prog;
1155639Smckusic char **args;
1165639Smckusic {
1172841Swnj for (;;) {
1185639Smckusic execv(prog, args);
1192841Swnj if (errno != ETXTBSY)
1202841Swnj break;
1212841Swnj sleep(2);
1222841Swnj }
1235639Smckusic error(0, prog);
1245639Smckusic error(1, " not found.\n");
1252841Swnj }
1262841Swnj
error(errcode,cp)1275639Smckusic error(errcode, cp)
1285639Smckusic int errcode;
1292841Swnj register char *cp;
1302841Swnj {
1312841Swnj register int i;
1322841Swnj register char *dp;
1332841Swnj
1342841Swnj dp = cp;
1352841Swnj i = 0;
1362841Swnj while (*dp++)
1372841Swnj i++;
1382841Swnj write(2, cp, i);
1395639Smckusic if (errcode)
1405639Smckusic exit(errcode);
1412841Swnj }
1422841Swnj
exit(i)1432841Swnj exit(i)
1442841Swnj {
1452841Swnj _exit(i);
1462841Swnj }
147