12841Swnj /*
22841Swnj  * pxheader - program to sit in front of interpreter code to make shell mods
32841Swnj  *	      unnecessary to make Pascal obj's look like real programs.
42841Swnj  *
52841Swnj  * This program lives in /usr/lib/px_header
62841Swnj  * Bill Joy UCB February 6, 1978
72841Swnj  */
82841Swnj 
9*11879Smckusick static char sccsid[] = "@(#)px_header.c 1.5 04/08/83";
102841Swnj 
112948Smckusic #include <stdio.h>
122948Smckusic #include <sys/types.h>
132948Smckusic #include <a.out.h>
1410571Smckusick #include "config.h"
152948Smckusic #include "whoami.h"
162948Smckusic #include "objfmt.h"
172948Smckusic 
182841Swnj #define	ETXTBSY	26
19*11879Smckusick #define	ADDR_LC \
20*11879Smckusick 	(START + HEADER_BYTES - sizeof (struct exec) - sizeof (struct pxhdr))
215639Smckusic #define MAXARGS 512
222841Swnj 
232948Smckusic extern	errno;
242948Smckusic 
252841Swnj main(argc, argv)
262841Swnj 	register int argc;
272841Swnj 	register char *argv[];
282841Swnj {
295639Smckusic 	register int i;
305639Smckusic 	int codesiz, symtabsiz;
315639Smckusic 	register char *cp;
325639Smckusic 	char *largv[MAXARGS];
335639Smckusic 	int fd, pv[2], pid;
342841Swnj 
355639Smckusic 	cp = (char *)(ADDR_LC);
365639Smckusic 	codesiz = ((struct pxhdr *)(cp))->objsize + sizeof(struct pxhdr);
375639Smckusic 	symtabsiz = ((struct pxhdr *)(cp))->symtabsize;
385639Smckusic 	if (argc > MAXARGS - 3)
395639Smckusic 		error(2, "Too many arguments.\n");
405639Smckusic 	if (symtabsiz != 0) {
415639Smckusic 		largv[0] = "pxhdr";
425639Smckusic 		largv[1] = "/tmp/px00000";
435639Smckusic 		cp = &largv[1][11];
445639Smckusic 		for (i = getpid(); i > 0; i /= 10)
455639Smckusic 			*cp-- = '0' + i % 10;
465639Smckusic 		fd = creat(largv[1], 0444);
475639Smckusic 		if (fd < 0)
485639Smckusic 			error(3, "Cannot create /tmp file\n");
495639Smckusic 		for (i = 0; i < argc; i++)
505639Smckusic 			largv[i + 2] = argv[i];
515639Smckusic 		largv[argc + 2] = 0;
525639Smckusic 		writeobj(fd, codesiz, symtabsiz);
5310571Smckusick 		run(px_debug, largv);
545639Smckusic 		/* no return */
552841Swnj 	}
565639Smckusic 	largv[0] = "pipe";
575639Smckusic 	for (i = 0; i < argc; i++)
582841Swnj 		largv[i + 1] = argv[i];
592841Swnj 	largv[argc + 1] = 0;
602841Swnj 	pipe(pv);
615639Smckusic 	pid = fork();
625639Smckusic 	if (pid != 0) {
635639Smckusic 		if (pv[0] != 3) {
645639Smckusic 			close(3);
655639Smckusic 			dup(pv[0]);
665639Smckusic 			close(pv[0]);
672841Swnj 		}
685639Smckusic 		close(pv[1]);
6910571Smckusick 		run(px_intrp, largv);
705639Smckusic 		/* no return */
712841Swnj 	}
725639Smckusic 	writeobj(pv[1], codesiz, symtabsiz);
735639Smckusic 	exit(0);
745639Smckusic }
755639Smckusic 
765639Smckusic writeobj(fd, codesiz, symtabsiz)
775639Smckusic 	int fd;
785639Smckusic 	int codesiz, symtabsiz;
795639Smckusic {
805639Smckusic 	int i;
815639Smckusic 	register char *cp;
825639Smckusic 
835639Smckusic 	cp = (char *)(ADDR_LC);
845639Smckusic 	while (codesiz != 0) {
855639Smckusic 		i = (codesiz < BUFSIZ) ? codesiz : BUFSIZ;
865639Smckusic 		write(fd, cp, i);
875639Smckusic 		cp += i;
885639Smckusic 		codesiz -= i;
892841Swnj 	}
905639Smckusic 	while (symtabsiz != 0) {
915639Smckusic 		i = (symtabsiz < BUFSIZ) ? symtabsiz : BUFSIZ;
925639Smckusic 		write(fd, cp, i);
935639Smckusic 		cp += i;
945639Smckusic 		symtabsiz -= i;
955639Smckusic 	}
965639Smckusic 	close(fd);
975639Smckusic }
985639Smckusic 
995639Smckusic run(prog, args)
1005639Smckusic 	char *prog;
1015639Smckusic 	char **args;
1025639Smckusic {
1032841Swnj 	for (;;) {
1045639Smckusic 		execv(prog, args);
1052841Swnj 		if (errno != ETXTBSY)
1062841Swnj 			break;
1072841Swnj 		sleep(2);
1082841Swnj 	}
1095639Smckusic 	error(0, prog);
1105639Smckusic 	error(1, " not found.\n");
1112841Swnj }
1122841Swnj 
1135639Smckusic error(errcode, cp)
1145639Smckusic 	int errcode;
1152841Swnj 	register char *cp;
1162841Swnj {
1172841Swnj 	register int i;
1182841Swnj 	register char *dp;
1192841Swnj 
1202841Swnj 	dp = cp;
1212841Swnj 	i = 0;
1222841Swnj 	while (*dp++)
1232841Swnj 		i++;
1242841Swnj 	write(2, cp, i);
1255639Smckusic 	if (errcode)
1265639Smckusic 		exit(errcode);
1272841Swnj }
1282841Swnj 
1292841Swnj exit(i)
1302841Swnj {
1312841Swnj 	_exit(i);
1322841Swnj }
133