| 1 |
|
| /****************************** |
| 2 |
|
| * pdl.c - perldl spawner |
| 3 |
|
| * Works around a problem with many unices that you can't use an interpreter |
| 4 |
|
| * to run an interpreter -- so "#!/usr/bin/perldl" won't work. |
| 5 |
|
| * This is a compiled piece of code that launches perldl "directly", |
| 6 |
|
| * so that the poor kernel's mind isn't blown. |
| 7 |
|
| * |
| 8 |
|
| * If you feed in a single non-switch argument it gets prepended with a |
| 9 |
|
| * "-" to let perldl know that it's an input file. That way you can be lazy |
| 10 |
|
| * and say "#!/usr/bin/pdl" at the top of your script. |
| 11 |
|
| * |
| 12 |
|
| * CED 21-Jul-2004 |
| 13 |
|
| */ |
| 14 |
|
| |
| 15 |
|
| #include <unistd.h> |
| 16 |
|
| #include <stdlib.h> |
| 17 |
|
| #include <stdio.h> |
| 18 |
|
| #include <sys/types.h> |
| 19 |
|
| #include <sys/wait.h> |
| 20 |
|
| |
| 21 |
0 |
| int main(int argc, char **argv) { |
| 22 |
|
| char perldl[BUFSIZ]; |
| 23 |
|
| int pipes[2]; |
| 24 |
|
| int pid,i; |
| 25 |
|
| int status; |
| 26 |
|
| |
| 27 |
0 |
▶ | if(pipe(pipes)) {perror("pdl (perldl spawn wrapper)"); exit(1);} |
Branch: 1 branch
| Branch | True | False | Total |
| if(pipe(pipes)) {perror("pdl (perldl spawn wrapper)"); exit(1);} |
0 |
0 |
0 |
|
| 28 |
0 |
| pid = fork(); |
| 29 |
0 |
▶ | if(pid==0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(pid==0) { |
0 |
0 |
0 |
|
| 30 |
0 |
| dup2(pipes[1],1); |
| 31 |
0 |
| dup2(pipes[1],2); |
| 32 |
0 |
| exit(system("which perldl")); |
| 33 |
|
| } |
| 34 |
0 |
| pid = wait(&status); |
| 35 |
0 |
▶ | if(! WIFEXITED(status) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(! WIFEXITED(status) ) { |
0 |
0 |
0 |
|
| 36 |
0 |
| fprintf(stderr,"Hmmm... couldn't seem to find perldl anywhere. Quitting.\n"); |
| 37 |
0 |
| goto exit; |
| 38 |
|
| } |
| 39 |
0 |
▶ | if( read(pipes[0],perldl,BUFSIZ) <= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( read(pipes[0],perldl,BUFSIZ) <= 0 ) { |
0 |
0 |
0 |
|
| 40 |
0 |
| fprintf(stderr, "Read error - quitting.\n"); |
| 41 |
0 |
| goto exit; |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
|
| /* Remove trailing newline */ |
| 45 |
0 |
▶ | for(i=0;i<BUFSIZ && perldl[i]; i++) |
Branch: 2 branches
| Branch | True | False | Total |
| for(i=0;i<BUFSIZ && perldl[i]; i++) |
0 |
0 |
0 |
| for(i=0;i<BUFSIZ && perldl[i]; i++) |
0 |
0 |
0 |
|
| 46 |
0 |
▶ | if(perldl[i]=='\n' || perldl[i]=='\r') |
Branch: 2 branches
| Branch | True | False | Total |
| if(perldl[i]=='\n' || perldl[i]=='\r') |
0 |
0 |
0 |
| if(perldl[i]=='\n' || perldl[i]=='\r') |
0 |
0 |
0 |
|
| 47 |
0 |
| perldl[i]='\0'; |
| 48 |
|
| |
| 49 |
0 |
▶ | if(argc==2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(argc==2) { |
0 |
0 |
0 |
|
| 50 |
0 |
▶ | if(argv[1][0]!='-') { |
Branch: 1 branch
| Branch | True | False | Total |
| if(argv[1][0]!='-') { |
0 |
0 |
0 |
|
| 51 |
0 |
| char **argv2 = malloc((argc+2)*sizeof(char *)); |
| 52 |
|
| int i; |
| 53 |
|
| |
| 54 |
0 |
▶ | if(!argv2) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!argv2) |
0 |
0 |
0 |
|
| 55 |
0 |
| goto exit; |
| 56 |
|
| |
| 57 |
0 |
▶ | for(i=0;i<argc;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<argc;i++) |
0 |
0 |
0 |
|
| 58 |
0 |
| argv2[i+1]=argv[i]; |
| 59 |
|
| |
| 60 |
0 |
| argv2[1]="-"; |
| 61 |
0 |
| argv2[0]="perldl"; |
| 62 |
0 |
| argv2[argc+1]=0; |
| 63 |
|
| |
| 64 |
0 |
| execv(perldl,argv2); |
| 65 |
0 |
| fprintf(stderr,"couldn't execv %s\n",perldl); |
| 66 |
0 |
| goto exit; |
| 67 |
|
| } |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
0 |
| argv[0]="perldl"; |
| 71 |
0 |
| execv(perldl,argv); |
| 72 |
0 |
| fprintf(stderr,"couldn't execv %s (%d args)\n",perldl,argc); |
| 73 |
0 |
| goto exit; |
| 74 |
|
| |
| 75 |
0 |
| exit: |
| 76 |
0 |
| perror("pdl (perldl trampoline)"); |
| 77 |
0 |
| exit(-1); |
| 78 |
|
| } |