#!/bin/sh set -e TMP=$(umask 077; mktemp -d) echo >&2 " tmp $TMP" Die() { rm -rf $TMP trap - EXIT exit ${1:-$e} }; trap Die HUP EXIT #INT pause() {( echo >&2 -n "Continue? ..." read -sn1 REPLY echo >&2 )} confirm() {( echo >&2 -n "$@ [y/n] " read -n1 REPLY echo >&2 case $REPLY in y|Y) : ;; n|N) exit 1 ;; *) confirm "again" ;; esac )} I=0 if [ "${1:++}" ]; then echo >&2 " [thanks] for the file (om nom)." I=1 C="$1" shift 1 fi case $I in 0) echo >&2 "[+] Building/testing internal program now; lucky!" echo >&2 " You should really be more cautious." echo >&2 "-wait for the potential virus..." cat >"$TMP/t.c" <<%EOF #include #include #include void input() { struct stat stdin_stat; fstat(0,&stdin_stat); if (S_ISCHR(stdin_stat.st_mode)) return; /*interactive*/ /*if (S_ISFIFO(stdin_stat.st_mode)) return; /*pipe mode*/ char sti[256]; while (1) { if (fgets(sti, sizeof sti, stdin) ==NULL) break; /**/ printf("%s",sti); } } void files() { FILE *f; f=fopen("____file","w"); mkdir("____dir",0755); } void main() { input(); files(); exit(0); } %EOF ;; 1) cat "$C" >"$TMP/t.c" ;; esac cd $TMP ||{ echo '[error: cannot enter directory] '"$TMP"; Die 1; }>&2 cc -o exe t.c ||: case $I in 0) echo 'ABCDEF' |./exe ||: ;; 1) ./exe "$@" ||: ;; esac e=$? trap - EXIT confirm "program removes tmp. (CTRL-C to abort)"||: Die $e