#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*-------------------*/
#include <signal.h>
#include <unistd.h>
#include <errno.h>
static volatile sig_atomic_t flagSignalS = 0;
void signal_handler(int sig) {
int errsrv = errno;
char mes[] = "\njumped into the signal handler, ignored.\n";
write(2, mes, sizeof(mes) - 1);
errno = errsrv;
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_mask = 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;
}
}
int getSignalS(void) {
int flag_sig;
signal_handling_entry(DISABLE);
flag_sig = flagSignalS;
signal_handling_entry(ENABLE);
return flag_sig;
}
void clearSignalS(void) {
signal_handling_entry(DISABLE);
flagSignalS = 0;
signal_handling_entry(ENABLE);
}
/*-------------------*/
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;
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 (;;) {
if (fgets(buff
, BUFFSIZE
, stdin
) == 0) { if (getSignalS
()) { clearSignalS
(); clearerr(stdin
); continue; } else { fprintf(stderr
, "error occured in fgets(), aborting.\n"); break;
}
} else
break;
}
if (getSignalS()) { clearSignalS(); continue; }
if (p != 0) {
if (flag_reverse) {
while (!push_head(&rootS, p))
if (getSignalS()) { clearSignalS(); continue; } else {
free(p
); goto label_exit
; }
} else {
while (!push_tail(&rootS, p))
if (getSignalS()) { clearSignalS(); continue; } else {
free(p
); goto label_exit
; }
}
}
} /* for(;;) */
while ((p = pop(&rootS)) != 0) {
if (getSignalS()) { clearSignalS(); continue; } else
break;
}
label_exit:
while ((p = pop(&rootS)) != 0)
return 0;
}
/* end */
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgovKi0tLS0tLS0tLS0tLS0tLS0tLS0qLwojaW5jbHVkZSA8c2lnbmFsLmg+CiNpbmNsdWRlIDx1bmlzdGQuaD4KI2luY2x1ZGUgPGVycm5vLmg+CgpzdGF0aWMgdm9sYXRpbGUgc2lnX2F0b21pY190IGZsYWdTaWduYWxTID0gMDsKCnZvaWQgc2lnbmFsX2hh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wOwogICAgfQogICAgKnJvb3QgPSBwOwogICAgcmV0dXJuIDE7CiAgfQogIHJldHVybiAwOwp9CgppbnQgcHVzaF90YWlsKHN0cnVjdCBub2RlICoqcm9vdCwgY2hhciAqZGF0YSkgewogIHN0cnVjdCBub2RlICpwOwogIGlmICgocCA9IG1hbGxvYyhzaXplb2Yoc3RydWN0IG5vZGUpKSkgIT0gMCkgewogICAgcC0+ZGF0YSA9IGRhdGE7CiAgICBpZiAoKnJvb3QgPT0gMCkgewogICAgICBwLT5uZXh0ID0gcDsKICAgICAgcC0+cHJldiA9IHA7CiAgICAgICpyb290ID0gcDsKICAgIH0gZWxzZSB7CiAgICAgIHAtPm5leHQgPSAqcm9vdDsKICAgICAgcC0+cHJldiA9ICgqcm9vdCktPnByZXY7CiAgICAgIHAtPnByZXYtPm5leHQgPSBwOwogICAgICBwLT5uZXh0LT5wcmV2ID0gcDsKICAgIH0KICAgIHJldHVybiAxOwogIH0KICByZXR1cm4gMDsKfQoKY2hhciAqcG9wKHN0cnVjdCBub2RlICoqcm9vdCkgewogIGNoYXIgKnI7CiAgc3RydWN0IG5vZGUgKnA7CiAgaWYgKCpyb290ID09IDApCiAgICByZXR1cm4gMDsKICBwID0gKnJvb3Q7CiAgciA9IHAtPmRhdGE7CiAgaWYgKHAtPm5leHQgPT0gcCkgewogICAgKnJvb3QgPSAwOwogICAgZnJlZShwKTsKICB9IGVsc2UgewogICAgcC0+bmV4dC0+cHJldiA9IHAtPnByZXY7CiAgICBwLT5wcmV2LT5uZXh0ID0gcC0+bmV4dDsKICAgICpyb290ID0gcC0+bmV4dDsKICAgIGZyZWUocCk7CiAgfQogIHJldHVybiByOwp9CgoKI2RlZmluZSBCVUZGU0laRSAxMDI0CmludCBtYWluKGludCBhcmdjLCBjaGFyICphcmd2W10pIHsKICBzdHJ1Y3Qgbm9kZSAqcm9vdFMgPSAwOwogIHN0YXRpYyBjaGFyIGJ1ZmZbQlVGRlNJWkVdOwogIGNoYXIgKnA7CiAgaW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ogICAgICBzdHJjcHkocCwgYnVmZik7CiAgICAgIGlmIChmbGFnX3JldmVyc2UpIHsKICAgICAgICB3aGlsZSAoIXB1c2hfaGVhZCgmcm9vdFMsIHApKQogICAgICAgICAgaWYgKGdldFNpZ25hbFMoKSkgeyBjbGVhclNpZ25hbFMoKTsgY29udGludWU7IH0gZWxzZSB7CiAgICAgICAgICAgIGZyZWUocCk7IGdvdG8gbGFiZWxfZXhpdDsKICAgICAgICAgIH0KICAgICAgfSBlbHNlIHsKICAgICAgICB3aGlsZSAoIXB1c2hfdGFpbCgmcm9vdFMsIHApKQogICAgICAgICAgaWYgKGdldFNpZ25hbFMoKSkgeyBjbGVhclNpZ25hbFMoKTsgY29udGludWU7IH0gZWxzZSB7CiAgICAgICAgICAgIGZyZWUocCk7IGdvdG8gbGFiZWxfZXhpdDsKICAgICAgICAgIH0KICAgICAgfQogICAgfQogIH0gLyogZm9yKDs7KSAqLwogIHB1dGNoYXIoJ1xuJyk7CiAgd2hpbGUgKChwID0gcG9wKCZyb290UykpICE9IDApIHsKICAgIHdoaWxlIChwcmludGYoIiVzIiwgcCkgPCAwKQogICAgICBpZiAoZ2V0U2lnbmFsUygpKSB7IGNsZWFyU2lnbmFsUygpOyBjb250aW51ZTsgfSBlbHNlCiAgICAgICAgYnJlYWs7CiAgICBmcmVlKHApOwogIH0KbGFiZWxfZXhpdDoKICB3aGlsZSAoKHAgPSBwb3AoJnJvb3RTKSkgIT0gMCkKICAgIGZyZWUocCk7CiAgcmV0dXJuIDA7Cn0KLyogZW5kICovCg==