[C++] 纯文本查看 复制代码
#define _XOPEN_SOURCE 600
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
static char *str2;
static int handled = 0; //计算信号处理函数的调用次数
static void handler(int sig)
{
crypt(str2, "xx");
handled++;
}
int main(int argc, char **argv)
{
char *cr1;
int call_num, mis_match;
struct sigaction sa;
if (argc != 3) {
printf ("Usage: %s str1 str2\n", argv[0]);
exit (EXIT_FAILURE);
}
str2 = argv[2];
cr1 = strdup(crypt(argv[1], "xx"));
if (cr1 == NULL) {
perror("strdup");
exit (EXIT_FAILURE);
}
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handler;
if (sigaction(SIGINT, &sa, NULL) == -1) {
perror("sigaction");
exit (EXIT_FAILURE);
}
for (call_num = 1, mis_match = 0; ; call_num++) {
if (strcmp(crypt(argv[1], "xx"), cr1) != 0) {
mis_match++;
printf ("Mismatch on call %d (mismatch=%d handled=%d)\n", call_num, mis_match, handled);
}
}
return 0;
}