#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
char name[16];
char *command[10] = { "cat",
"ls",
"id",
"ps",
"file ./oob" };
void alarm_handler()
{
puts("TIME OUT");
exit(-1);
}
void initialize()
{
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
signal(SIGALRM, alarm_handler);
alarm(30);
}
int main()
{
int idx;
initialize();
printf("Admin name: ");
read(0, name, sizeof(name));
printf("What do you want?: ");
scanf("%d", &idx);
system(command[idx]);
return 0;
}
문제의 소스코드다.
위와 같이 입력을 받고 맞는 커맨드를 실행시킨다.
char name[16];
char *command[10] = { "cat",
"ls",
"id",
"ps",
"file ./oob" };
위 코드를 통해 전역변수들이 선언되어 있음을 확인할 수 있다.
0번째 인덱스 부터 해서 4번째 인덱스 까지 각각 {”cat”, ”ls”, “id”, “ps”, “file ./oob”}
커맨드들이 있다.
printf("Admin name: ");
read(0, name, sizeof(name));
printf("What do you want?: ");
scanf("%d", &idx);
system(command[idx]);
return 0;