#include #include #include #include #include #include #include #include #include #ifndef _POSIX_ARG_MAX #define _POSIX_ARG_MAX 4096 #endif #ifndef _POSIX_PATH_MAX #define _POSIX_PATH_MAX 255 #endif #ifndef BUFSIZ BUFSIZ=1024 #endif extern int optind; void usage (void); int main(int argc, char **argv, char **envp) { char *hide_prog_base; int len_envs; int len_args; int i, j; int type; int empty=0; int base=0; int err=0; int num_args=0; char *label; char *real_prog = malloc((size_t) _POSIX_PATH_MAX); char **new_argv; char *c; /* hide program can be run three types format, type 2-3 converts to type 1, * and call the hide program recursively. * 1. hide -hide_option prog args * 2. mv prog prog.hide * ln -s hide prog * prog -hide_option args => "hide" -hide_option prog.hide args * 3. ln -s hide prog.hide * prog.hide -hide_option args => "hide" -hide_option prog args * "hide" is not named hide, but a program linked to hide. * for type 2-3, hide no need in path, see below. */ hide_prog_base = basename(*argv); i=strlen(hide_prog_base); if (strcmp(hide_prog_base, "hide") == 0) { type=1; /* hide prog args */ } else if ( i <= 5 ) { type=2; /* prog args */ } else if (strcmp(hide_prog_base + i - 5, ".hide") == 0) { type=3; /* prog.hide args */ } else { type=2; /* prog args */ } if (type != 2) { /* only process arguments for type 1 and 3 */ while ((i = getopt(argc, argv, "eb")) != EOF) { switch (i) { case 'e': if (base) { err++; } else { empty++; num_args++; } break; case 'b': if (empty) { err++; } else { base++; num_args++; } break; case '?': err++; } } if (err != 0) { usage(); exit(-1); } } memset(real_prog, '\0', (size_t) _POSIX_PATH_MAX); i=strlen(*argv); if (type == 1) { if ( optind <= argc-1 ) { strncpy(real_prog, *(argv+optind), strlen(*(argv+optind))); } else { usage(); exit(-1); } } else if (type == 2) { strncpy(real_prog, *argv, i); strncpy(real_prog+i, ".hide", 5); /* append .hide */ } if (type == 3) { strncpy(real_prog, *argv, i); real_prog[i-5]='\0'; /* get rid of .hide */ } if (type != 1) { /* converts type 2-3 to type 1 */ new_argv=(char **)malloc(1 + num_args + 1 + (argc - optind) + 1); i=0; *(new_argv + i++)="hide"; if (base == 1) { *(new_argv + i++)="-b"; } else if (empty == 1) { *(new_argv + i++)="-e"; } *(new_argv + i++)=real_prog; for(j=optind; j 0 ) { write(STDOUT_FILENO, buf, j); } close(i); close(STDOUT_FILENO); }