[16210] in Athena Bugs
Re: sun4 8.2.8: attachandrun
daemon@ATHENA.MIT.EDU (Jacob Morzinski)
Sun Aug 23 06:56:07 1998
To: "Jacob Morzinski" <jmorzins@MIT.EDU>
Cc: bugs@MIT.EDU
From: Jacob Morzinski <jmorzins@MIT.EDU>
Date: 23 Aug 1998 06:56:03 -0400
In-Reply-To: "Jacob Morzinski"'s message of Thu, 20 Aug 1998 18:25:15 EDT
<daemon@ATHENA.MIT.EDU> (Jacob Morzinski) writes:
> What's wrong:
> athena% attachandrun maple maple maple
> attachandrun: failure to exec maple: Exec format error
> Exit 3
Here's a patch, loosely based on bash and tcsh, which causes
attachandrun to try to pass text files with no interpreter line
to /bin/sh. I've only tested it on Solaris 2.6 running on a
Sparc 5 (mary-kay-commandos.mit.edu) and on Irix 6.3 (oliver.mit.edu).
-Jacob
*** /afs/dev.mit.edu/source/src-8.2/athena/bin/attach/main.c Wed Apr 8 18:05:20 1998
--- ./main.c Sun Aug 23 06:40:04 1998
***************
*** 953,958 ****
--- 953,1005 ----
}
#endif /* ZEPHYR */
+
+ /* Put this prototype somewhere where it makes sense. */
+ void try_shell_exec (char *path, char *argv[], int argc);
+
+ void try_shell_exec (path, argv, argc)
+ char *path;
+ char *argv[];
+ int argc;
+ {
+ register int i;
+ int fd, sample_len;
+ unsigned char sample[80];
+ char **arg;
+
+ /* Before handing mysterious files to the shell, it's nice to
+ * check the first line (up to 80 characters) for binary data.
+ */
+ if ((fd = open(path, O_RDONLY)) != -1) {
+ sample_len = read(fd, (char *) sample, 80);
+ close(fd);
+
+ if (sample_len < 0) {
+ return; /* Our read was interrupted. */
+ }
+ else if (sample_len == 0) {
+ exit(0); /* Empty files return TRUE. */
+ }
+ else {
+ for (i=0; i<sample_len; i++) {
+ if (sample[i] == '\n')
+ break;
+ if (isspace(sample[i]) == 0 && isprint(sample[i]) == 0)
+ return;
+ }
+ /* Build a new argument list. */
+ arg = (char **)malloc((argc+2) * sizeof(argv[0]));
+ arg[0] = "/bin/sh";
+ arg[1] = path;
+ for (i=1; i<=argc; i++) {
+ arg[i+1] = argv[i];
+ }
+ execv(arg[0], arg);
+ free(arg);
+ }
+ }
+ }
+
attachandruncmd(argc, argv)
int argc;
char **argv;
***************
*** 997,1002 ****
--- 1044,1055 ----
}
execv(path, argv + 3);
+
+ switch (errno) {
+ case ENOEXEC:
+ /* Text files must be fed to the shell by hand. */
+ try_shell_exec(path, argv + 3, argc - 3); /* "3" from execv */
+ }
fprintf(stderr, "%s: failure to exec %s: %s\n", progname,
argv[2], strerror(errno));