#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*---*/
#include <signal.h>
#include <unistd.h>
#include <errno.h>
static volatile int flagSignalS = 0 ;
void signal_handler( int sig) {
char mes[ ] = "jumped into the signal handler, aborting.\n " ;
write( 2 , mes, sizeof ( mes) - 1 ) ;
flagSignalS = 1 ;
}
enum sigentry { INIT = 0 , DISABLE, ENABLE } ;
void signal_handling_entry( enum sigentry entry) {
sigset_t set;
static sigset_t defmaskS, restoreS;
struct sigaction act;
switch ( entry) {
case INIT:
sigemptyset( & set) ;
sigaddset( & set, SIGINT) ;
sigaddset( & set, SIGQUIT) ;
sigaddset( & set, SIGTERM) ;
sigaddset( & set, SIGTSTP) ;
act.sa_handler = signal_handler;
act.sa_flags = set;
sigemptyset( & act.sa_mask ) ;
sigaction( SIGINT, & act, 0 ) ;
sigaction( SIGQUIT, & act, 0 ) ;
sigaction( SIGTERM, & act, 0 ) ;
sigaction( SIGTSTP, & act, 0 ) ;
defmaskS = set;
return ;
case DISABLE:
sigprocmask( SIG_BLOCK, & defmaskS, & restoreS) ;
return ;
case ENABLE:
sigprocmask( SIG_SETMASK, & restoreS, 0 ) ;
return ;
default :
return ;
}
}
struct node {
char * data;
struct node * next;
struct node * prev;
} ;
int push_head( struct node ** root, char * data) {
struct node * p;
if ( ( p
= malloc ( sizeof ( struct node
) ) ) != 0 ) { p-> data = data;
if ( * root == 0 ) {
p-> next = p;
p-> prev = p;
} else {
p-> next = * root;
p-> prev = ( * root) -> prev;
p-> prev-> next = p;
p-> next-> prev = p;
}
* root = p;
return 1 ;
}
return 0 ;
}
int push_tail( struct node ** root, char * data) {
struct node * p;
if ( ( p
= malloc ( sizeof ( struct node
) ) ) != 0 ) { p-> data = data;
if ( * root == 0 ) {
p-> next = p;
p-> prev = p;
* root = p;
} else {
p-> next = * root;
p-> prev = ( * root) -> prev;
p-> prev-> next = p;
p-> next-> prev = p;
}
return 1 ;
}
return 0 ;
}
char * pop( struct node ** root) {
char * r;
struct node * p;
if ( * root == 0 )
return 0 ;
p = * root;
r = p-> data;
if ( p-> next == p) {
* root = 0 ;
} else {
p-> next-> prev = p-> prev;
p-> prev-> next = p-> next;
* root = p-> next;
}
return r;
}
#define BUFFSIZE 1024
int main( int argc, char * argv[ ] ) {
struct node * rootS = 0 ;
static char buff[ BUFFSIZE] ;
char * p;
int flag_reverse;
int flagsig;
if ( argc != 2 ) {
usage:
fprintf ( stderr
, "usage: %s -r/f.\n option -r:reverse, -f:forward\n " , argv
[ 0 ] ) ; }
if ( * argv[ 1 ] == '-' ) {
if ( * ( argv[ 1 ] + 1 ) == 'f' )
flag_reverse = 0 ;
else if ( * ( argv[ 1 ] + 1 ) == 'r' )
flag_reverse = 1 ;
else
goto usage;
} else {
goto usage;
}
signal_handling_entry( INIT) ;
rootS = 0 ;
for ( ;; ) {
signal_handling_entry( DISABLE) ;
flagsig = flagSignalS;
signal_handling_entry( ENABLE) ;
if ( flagsig)
break ;
if ( fgets ( buff
, BUFFSIZE
, stdin
) == 0 ) { if ( errno != EINTR)
break ;
else
continue ;
}
if ( flag_reverse) {
if ( ! push_head( & rootS, p) )
} else {
if ( ! push_tail( & rootS, p) )
}
}
}
for ( ;; ) {
signal_handling_entry( DISABLE) ;
flagsig = flagSignalS;
signal_handling_entry( ENABLE) ;
if ( flagsig)
break ;
if ( ( p = pop( & rootS) ) == 0 )
break ;
}
while ( ( p = pop( & rootS) ) != 0 )
return 0 ;
}
/* end */
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgovKi0tLSovCiNpbmNsdWRlIDxzaWduYWwuaD4KI2luY2x1ZGUgPHVuaXN0ZC5oPgojaW5jbHVkZSA8ZXJybm8uaD4KCnN0YXRpYyB2b2xhdGlsZSBpbnQgZmxhZ1NpZ25hbFMgPSAwOwoKdm9pZCBzaWduYWxfaGFuZGxlcihpbnQgc2lnKSB7CiAgY2hhciBtZXNbXSA9ICJqdW1wZWQ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2hhbmRsaW5nX2VudHJ5KElOSVQpOwogIHJvb3RTID0gMDsKICBmb3IgKDs7KSB7CiAgICBzaWduYWxfaGFuZGxpbmdfZW50cnkoRElTQUJMRSk7CiAgICBmbGFnc2lnID0gZmxhZ1NpZ25hbFM7CiAgICBzaWduYWxfaGFuZGxpbmdfZW50cnkoRU5BQkxFKTsKICAgIGlmIChmbGFnc2lnKQogICAgICBicmVhazsKICAgIGlmIChmZ2V0cyhidWZmLCBCVUZGU0laRSwgc3RkaW4pID09IDApIHsKICAgICAgaWYgKGVycm5vICE9IEVJTlRSKQogICAgICAgIGJyZWFrOwogICAgICBlbHNlCiAgICAgICAgY29udGludWU7CiAgICB9CiAgICBpZiAoKHAgPSBtYWxsb2Moc3RybGVuKGJ1ZmYpICsgMSkpICE9IDApIHsKICAgICAgc3RyY3B5KHAsIGJ1ZmYpOwogICAgICBpZiAoZmxhZ19yZXZlcnNlKSB7CiAgICAgICAgaWYgKCFwdXNoX2hlYWQoJnJvb3RTLCBwKSkKICAgICAgICAgIGZyZWUocCk7CiAgICAgIH0gZWxzZSB7CiAgICAgICAgaWYgKCFwdXNoX3RhaWwoJnJvb3RTLCBwKSkKICAgICAgICAgIGZyZWUocCk7CiAgICAgIH0KICAgIH0KICB9CiAgcHV0Y2hhcignXG4nKTsKICBmb3IgKDs7KSB7CiAgICBzaWduYWxfaGFuZGxpbmdfZW50cnkoRElTQUJMRSk7CiAgICBmbGFnc2lnID0gZmxhZ1NpZ25hbFM7CiAgICBzaWduYWxfaGFuZGxpbmdfZW50cnkoRU5BQkxFKTsKICAgIGlmIChmbGFnc2lnKQogICAgICBicmVhazsKICAgIGlmKChwID0gcG9wKCZyb290UykpID09IDApCiAgICAgIGJyZWFrOwogICAgcHJpbnRmKCIlcyIsIHApOwogICAgZnJlZShwKTsKICB9CiAgd2hpbGUgKChwID0gcG9wKCZyb290UykpICE9IDApCiAgICBmcmVlKHApOwogIHJldHVybiAwOwp9Ci8qIGVuZCAqLwo=