#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATA_FILE "data.txt"
struct STUDENT
{
int id;
char name[80];
int take_num;
int takes[6];
struct STUDENT* left_next;/*link list*/
struct STUDENT* right_next;
};
struct STUDENT* head;
struct STUDENT* current;
struct STUDENT* previous;/*link list*/
/*void init(struct STUDENT students[]);*/
void load(FILE* fp,int select_num);
void make_tree(struct STUDENT* student_ptr,struct STUDENT** head,int select_num);
void traverse_tree(struct STUDENT** head);
struct STUDENT* b_search(struct STUDENT* head,int id,char* name,int select_num);
void show(struct STUDENT* student);
int add(struct STUDENT* student,int select_course);
void save(FILE* fp,struct STUDENT* student);
int main (void)
{
FILE *fp;
struct STUDENT students[256];
int id=-1;//search by id
char name[80]={0};//search by name
int index;
int course;
int result;
int select_num;//save the number selected
struct STUDENT* consequence;
//init(students);//對id,name做初始化
fp=fopen(DATA_FILE,"r");
//load(fp,students);
//fclose(fp);
printf("please select enter name or enter id:\n");
printf("enter name 按1,enter id 按2\n");
scanf("%d",&select_num);
//select_num=1;
if(select_num==1)
{
load(fp,select_num);
fclose(fp);
printf("please enter name:");
scanf("%s",name);
consequence=b_search(head,-1,name,1);
if(consequence==NULL)
{
printf("name doesn`t exist,program ends\n");
exit(1);
}
//id=name_to_id(students,name);
printf("%d ",consequence->id);
printf("%s",consequence->name);
}
else if(select_num==2)
{
load(fp,select_num);
fclose(fp);
printf("please enter id:");
scanf("%d",&id);
consequence=b_search(head,id,0,2);
if(consequence==NULL)
{
printf("id doesn`t exist,program ends\n");
exit(1);
}
printf("%d ",consequence->id);
printf("%s",consequence->name);
}
while(1)
{
show(consequence);
if(consequence->take_num==5)
{
printf("all courses are selected,program ends");
break;
}
printf("請輸入課程代號,-1結束選課");
scanf("%d",&course);
if(course==-1)
{
printf("程式結束\n");
break;
}
result=add(consequence,course);
if(result==1)
{
printf("選課成功\n");
}
else if(result==2)
{
printf("重複選課\n");
}
else if(result==3)
{
printf("無此課程\n");
}
}
fp=fopen(DATA_FILE,"w");
save(fp,head);
fprintf(fp,"\b");
fclose(fp);
system("pause");
return 0;
}
/*void init(struct STUDENT students[])
{
int i,j;
for(i=1;i<256;i++)
{
students[i].id=-1;
//students[i].name[80]={0};
memset(students[i].name,0,sizeof(students[i].name));
}
}*/
void load(FILE* fp,int select_num)
{
int i,j,k;
i=0;
//if(select_num==2)/*enter id*/
{
while(!feof(fp))
{
i=i+1;
struct STUDENT* student_ptr=(struct STUDENT*)malloc(sizeof(struct STUDENT));
// printf("%d\n",i);
fscanf(fp,"%d",&(student_ptr->id));
fscanf(fp,"%s",student_ptr->name);
fscanf(fp,"%d",&(student_ptr->take_num));
// printf("%d ",student_ptr->id);
// printf("%d\n",student_ptr->take_num);
for(j=1;j<=(student_ptr->take_num);j++)
{
fscanf(fp,"%d",&(student_ptr->takes[j]));
}
student_ptr->left_next=NULL;
student_ptr->right_next=NULL;
make_tree(student_ptr,&head,select_num);
printf("%d\n",i);
}
traverse_tree(&head);
}
}
void show(struct STUDENT* student)
{
int i;
printf("您已選以下課程:\n");
for(i=1;i<=student->take_num;i++)
{
printf("%d",student->takes[i]);
printf(" ");
}
printf("\n");
}
int add(struct STUDENT* student,int course)
{
int i;
if(course>=101&&course<=105)
{
for(i=1;i<=student->take_num;i++)
{
if(student->takes[i]==course)
return 2;
}
student->takes[i]=course;
student->take_num=(student->take_num)+1;
return 1;
}
return 3;
}
void save(FILE* fp,struct STUDENT* head)
{
int i;
if(head==NULL)
{
return;
}
else
{
fprintf(fp,"%d ",head->id);
fprintf(fp,"%s ",head->name);
fprintf(fp,"%d ",head->take_num);
for(i=1;i<=head->take_num;i++)
{
fprintf(fp,"%d",head->takes[i]);
if(i<head->take_num)
fprintf(fp," ");
}
fprintf(fp,"\n");
//printf("%d",head->id);
save(fp,head->left_next);
save(fp,head->right_next);
}
}
struct STUDENT* b_search(struct STUDENT* head,int id,char* name,int select_num)
{
//printf("%d ",select_num);
struct STUDENT* result;
if(select_num==1)
{
if(head==NULL)
{
result=NULL;
return result;
}
else if(head!=NULL)
{
if(strcmp((head)->name,name)>0)
{
result=b_search(head->left_next,-1,name,1);
return result;
}
else if(strcmp((head)->name,name)<0)
{
result=b_search(head->right_next,-1,name,1);
return result;
}
else if(strcmp((head)->name,name)==0)
{
return head;
}
}
}
else if(select_num==2)
{
if(head==NULL)
{
return NULL;
}
else
{
if(id==head->id)
{
result=head;
return result;
}
else if(id<head->id)
{
result=b_search(head->left_next,id,0,2);
return result;
}
else if(id>head->id)
{
result=b_search(head->right_next,id,0,2);
return result;
}
}
}
}
void make_tree(struct STUDENT* student_ptr,struct STUDENT** head,int select_num)
{
if(select_num==1)
{
if(*head==NULL)
{
*head=student_ptr;
}
else if(*head!=NULL)
{
if(strcmp((*head)->name,student_ptr->name)>0)
{
make_tree(student_ptr,&((*head)->left_next),select_num);
}
else if(strcmp((*head)->name,student_ptr->name)<=0)
{
make_tree(student_ptr,&((*head)->right_next),select_num);
}
}
}
else if(select_num==2)
{
if(*head==NULL)
{
*head=student_ptr;
// printf("%d\n",(*head)->id);
/*current=student_ptr;
previous=student_ptr;*/
}
else if(*head!=NULL)
{
if((*head)->id>=student_ptr->id)
{
/*previous=current;
current=head->left_next;*/
if((*head)->left_next==NULL)
{
(*head)->left_next=student_ptr;
}
else if((*head)->left_next!=NULL)
{
make_tree(student_ptr,&((*head)->left_next),select_num);
}
}
else if((*head)->id<student_ptr->id)
{
if((*head)->right_next==NULL)
{
(*head)->right_next=student_ptr;
}
else if((*head)->right_next!=NULL)
{
make_tree(student_ptr,&((*head)->right_next),select_num);
}
}
}
}
//printf("%s %d\n",head->name,head->id);
}
void traverse_tree(struct STUDENT** head)
{
if(*head==NULL)
{
return;
}
else
{
//printf("%s %d\n",(*head)->name,(*head)->id);
traverse_tree(&((*head)->left_next));
traverse_tree(&((*head)->right_next));
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSBEQVRBX0ZJTEUgImRhdGEudHh0IgoKc3RydWN0IFNUVURFTlQKewogICAgaW50IGlkOwogICAgY2hhciBuYW1lWzgwXTsKICAgIGludCB0YWtlX251bTsKICAgIGludCB0YWtlc1s2XTsKICAgIHN0cnVjdCBTVFVERU5UKiBsZWZ0X25leHQ7LypsaW5rIGxpc3QqLwogICAgc3RydWN0IFNUVURFTlQqIHJpZ2h0X25leHQ7ICAgICAgIAp9OwoKc3RydWN0IFNUVURFTlQqIGhlYWQ7CnN0cnVjdCBTVFVERU5UKiBjdXJyZW50OwpzdHJ1Y3QgU1RVREVOVCogcHJldmlvdXM7LypsaW5rIGxpc3QqLwoKLyp2b2lkIGluaXQoc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSk7Ki8Kdm9pZCBsb2FkKEZJTEUqIGZwLGludCBzZWxlY3RfbnVtKTsKdm9pZCBtYWtlX3RyZWUoc3RydWN0IFNUVURFTlQqIHN0dWRlbnRfcHRyLHN0cnVjdCBTVFVERU5UKiogaGVhZCxpbnQgc2VsZWN0X251bSk7CnZvaWQgdHJhdmVyc2VfdHJlZShzdHJ1Y3QgU1RVREVOVCoqIGhlYWQpOwoKc3RydWN0IFNUVURFTlQqIGJfc2VhcmNoKHN0cnVjdCBTVFVERU5UKiBoZWFkLGludCBpZCxjaGFyKiBuYW1lLGludCBzZWxlY3RfbnVtKTsKCnZvaWQgc2hvdyhzdHJ1Y3QgU1RVREVOVCogc3R1ZGVudCk7CmludCBhZGQoc3RydWN0IFNUVURFTlQqIHN0dWRlbnQsaW50IHNlbGVjdF9jb3Vyc2UpOwp2b2lkIHNhdmUoRklMRSogZnAsc3RydWN0IFNUVURFTlQqIHN0dWRlbnQpOwoKaW50IG1haW4gKHZvaWQpCnsKICAgIEZJTEUgKmZwOwogICAgc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbMjU2XTsKICAgIGludCBpZD0tMTsvL3NlYXJjaCBieSBpZAogICAgY2hhciBuYW1lWzgwXT17MH07Ly9zZWFyY2ggYnkgbmFtZQogICAgaW50IGluZGV4OwogICAgaW50IGNvdXJzZTsKICAgIGludCByZXN1bHQ7CiAgICBpbnQgc2VsZWN0X251bTsvL3NhdmUgdGhlIG51bWJlciBzZWxlY3RlZAogICAgc3RydWN0IFNUVURFTlQqIGNvbnNlcXVlbmNlOwogICAgCiAgICAvL2luaXQoc3R1ZGVudHMpOy8v5bCNaWTvvIxuYW1l5YGa5Yid5aeL5YyWIAogICAgZnA9Zm9wZW4oREFUQV9GSUxFLCJyIik7CiAgICAKICAgIC8vbG9hZChmcCxzdHVkZW50cyk7CiAgICAvL2ZjbG9zZShmcCk7CiAgICAKICAgIHByaW50ZigicGxlYXNlIHNlbGVjdCBlbnRlciBuYW1lIG9yIGVudGVyIGlkOlxuIik7CiAgICBwcmludGYoImVudGVyIG5hbWUg5oyJMe+8jGVudGVyIGlkIOaMiTJcbiIpOwogICAgc2NhbmYoIiVkIiwmc2VsZWN0X251bSk7CiAgICAvL3NlbGVjdF9udW09MTsKICAgIGlmKHNlbGVjdF9udW09PTEpCiAgICB7CiAgICAgICAgbG9hZChmcCxzZWxlY3RfbnVtKTsKICAgICAgICBmY2xvc2UoZnApOwogICAgICAgIHByaW50ZigicGxlYXNlIGVudGVyIG5hbWU6Iik7CiAgICAgICAgc2NhbmYoIiVzIixuYW1lKTsgCiAgICAgICAgY29uc2VxdWVuY2U9Yl9zZWFyY2goaGVhZCwtMSxuYW1lLDEpOwogICAgICAgICAgCiAgICAgICAgaWYoY29uc2VxdWVuY2U9PU5VTEwpCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoIm5hbWUgZG9lc25gdCBleGlzdCxwcm9ncmFtIGVuZHNcbiIpOwogICAgICAgICAgICBleGl0KDEpOyAgICAgICAgICAgIAogICAgICAgIH0gCiAgICAgICAgLy9pZD1uYW1lX3RvX2lkKHN0dWRlbnRzLG5hbWUpOyAgCiAgICAgICAgcHJpbnRmKCIlZCAiLGNvbnNlcXVlbmNlLT5pZCk7CiAgICAgICAgcHJpbnRmKCIlcyIsY29uc2VxdWVuY2UtPm5hbWUpOyAgICAgICAgICAKICAgIH0KICAgIGVsc2UgaWYoc2VsZWN0X251bT09MikKICAgIHsKICAgICAgICBsb2FkKGZwLHNlbGVjdF9udW0pOwogICAgICAgIGZjbG9zZShmcCk7CiAgICAgICAgcHJpbnRmKCJwbGVhc2UgZW50ZXIgaWQ6Iik7CiAgICAgICAgc2NhbmYoIiVkIiwmaWQpOwogICAgICAgIGNvbnNlcXVlbmNlPWJfc2VhcmNoKGhlYWQsaWQsMCwyKTsgICAgICAgIAogICAgICAgIAogICAgICAgIAogICAgICAgIGlmKGNvbnNlcXVlbmNlPT1OVUxMKQogICAgICAgIHsKICAgICAgICAgICAgcHJpbnRmKCJpZCBkb2VzbmB0IGV4aXN0LHByb2dyYW0gZW5kc1xuIik7CiAgICAgICAgICAgIGV4aXQoMSk7ICAgICAgICAgICAgCiAgICAgICAgfQogICAgICAgIHByaW50ZigiJWQgIixjb25zZXF1ZW5jZS0+aWQpOwogICAgICAgIHByaW50ZigiJXMiLGNvbnNlcXVlbmNlLT5uYW1lKTsKICAgIH0KICAgIAogICAgd2hpbGUoMSkKICAgIHsKICAgICAgICBzaG93KGNvbnNlcXVlbmNlKTsKICAgICAgICBpZihjb25zZXF1ZW5jZS0+dGFrZV9udW09PTUpCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoImFsbCBjb3Vyc2VzIGFyZSBzZWxlY3RlZCxwcm9ncmFtIGVuZHMiKTsKICAgICAgICAgICAgYnJlYWs7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgIH0gICAgICAgIAogICAgICAgIHByaW50Zigi6KuL6Ly45YWl6Kqy56iL5Luj6Jmf77yMLTHntZDmnZ/pgbjoqrIiKTsKICAgICAgICBzY2FuZigiJWQiLCZjb3Vyc2UpOwogICAgICAgIGlmKGNvdXJzZT09LTEpCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoIueoi+W8j+e1kOadn1xuIik7IAogICAgICAgICAgICBicmVhazsgICAgICAgICAgICAgCiAgICAgICAgfQogICAgICAgIAogICAgICAgIHJlc3VsdD1hZGQoY29uc2VxdWVuY2UsY291cnNlKTsKICAgICAgICBpZihyZXN1bHQ9PTEpCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoIumBuOiqsuaIkOWKn1xuIik7ICAgICAgICAgICAgIAogICAgICAgIH0KICAgICAgICBlbHNlIGlmKHJlc3VsdD09MikKICAgICAgICB7CiAgICAgICAgICAgIHByaW50Zigi6YeN6KSH6YG46KqyXG4iKTsgICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihyZXN1bHQ9PTMpCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoIueEoeatpOiqsueoi1xuIik7ICAgICAKICAgICAgICB9CiAgICB9CiAgICAKICAgIGZwPWZvcGVuKERBVEFfRklMRSwidyIpOwogICAgc2F2ZShmcCxoZWFkKTsKICAgIGZwcmludGYoZnAsIlxiIik7CiAgICBmY2xvc2UoZnApOwogICAgCiAgICBzeXN0ZW0oInBhdXNlIik7CiAgICByZXR1cm4gMDsKfQoKLyp2b2lkIGluaXQoc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSkKewogICAgaW50IGksajsKICAgIGZvcihpPTE7aTwyNTY7aSsrKQogICAgewogICAgICAgIHN0dWRlbnRzW2ldLmlkPS0xOyAKICAgICAgICAvL3N0dWRlbnRzW2ldLm5hbWVbODBdPXswfTsKICAgICAgICBtZW1zZXQoc3R1ZGVudHNbaV0ubmFtZSwwLHNpemVvZihzdHVkZW50c1tpXS5uYW1lKSk7ICAgICAgICAgICAgICAgIAogICAgfSAgICAgCn0qLwoKdm9pZCBsb2FkKEZJTEUqIGZwLGludCBzZWxlY3RfbnVtKQp7CiAgICBpbnQgaSxqLGs7CiAgICBpPTA7CiAgICAvL2lmKHNlbGVjdF9udW09PTIpLyplbnRlciBpZCovCiAgICB7CiAgICAgICAgd2hpbGUoIWZlb2YoZnApKQogICAgICAgIHsKICAgICAgICAgICAgaT1pKzE7CiAgICAgICAgICAgIHN0cnVjdCBTVFVERU5UKiBzdHVkZW50X3B0cj0oc3RydWN0IFNUVURFTlQqKW1hbGxvYyhzaXplb2Yoc3RydWN0IFNUVURFTlQpKTsKICAgICAgICAgICAvLyBwcmludGYoIiVkXG4iLGkpOwogICAgICAgICAgICBmc2NhbmYoZnAsIiVkIiwmKHN0dWRlbnRfcHRyLT5pZCkpOwogICAgICAgICAgICBmc2NhbmYoZnAsIiVzIixzdHVkZW50X3B0ci0+bmFtZSk7CiAgICAgICAgICAgIGZzY2FuZihmcCwiJWQiLCYoc3R1ZGVudF9wdHItPnRha2VfbnVtKSk7CiAgICAgICAgICAgLy8gcHJpbnRmKCIlZCAiLHN0dWRlbnRfcHRyLT5pZCk7CiAgICAgICAgICAgLy8gcHJpbnRmKCIlZFxuIixzdHVkZW50X3B0ci0+dGFrZV9udW0pOwogICAgICAgICAgICBmb3Ioaj0xO2o8PShzdHVkZW50X3B0ci0+dGFrZV9udW0pO2orKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZnNjYW5mKGZwLCIlZCIsJihzdHVkZW50X3B0ci0+dGFrZXNbal0pKTsgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICBzdHVkZW50X3B0ci0+bGVmdF9uZXh0PU5VTEw7CiAgICAgICAgICAgIHN0dWRlbnRfcHRyLT5yaWdodF9uZXh0PU5VTEw7CiAgICAgICAgICAgIAogICAgICAgICAgICBtYWtlX3RyZWUoc3R1ZGVudF9wdHIsJmhlYWQsc2VsZWN0X251bSk7CiAgICAgICAgICAgIHByaW50ZigiJWRcbiIsaSk7ICAgICAgIAogICAgICAgIH0gCiAgICAgICAgdHJhdmVyc2VfdHJlZSgmaGVhZCk7CiAgICB9CiAgICAKfQoKCnZvaWQgc2hvdyhzdHJ1Y3QgU1RVREVOVCogc3R1ZGVudCkKewogICAgaW50IGk7CiAgICBwcmludGYoIuaCqOW3sumBuOS7peS4i+iqsueoizpcbiIpOwogICAgZm9yKGk9MTtpPD1zdHVkZW50LT50YWtlX251bTtpKyspCiAgICB7CiAgICAgICAgcHJpbnRmKCIlZCIsc3R1ZGVudC0+dGFrZXNbaV0pOwogICAgICAgIHByaW50ZigiICIpOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICB9ICAgICAKICAgIHByaW50ZigiXG4iKTsKfQoKaW50IGFkZChzdHJ1Y3QgU1RVREVOVCogc3R1ZGVudCxpbnQgY291cnNlKQp7CiAgICBpbnQgaTsKICAgIGlmKGNvdXJzZT49MTAxJiZjb3Vyc2U8PTEwNSkKICAgIHsKICAgICAgICBmb3IoaT0xO2k8PXN0dWRlbnQtPnRha2VfbnVtO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKHN0dWRlbnQtPnRha2VzW2ldPT1jb3Vyc2UpCiAgICAgICAgICAgICAgICByZXR1cm4gMjsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgfSAgCiAgICAgICAgc3R1ZGVudC0+dGFrZXNbaV09Y291cnNlOwogICAgICAgIHN0dWRlbnQtPnRha2VfbnVtPShzdHVkZW50LT50YWtlX251bSkrMTsKICAgICAgICByZXR1cm4gMTsgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgfSAgICAKICAgIHJldHVybiAzOwp9Cgp2b2lkIHNhdmUoRklMRSogZnAsc3RydWN0IFNUVURFTlQqIGhlYWQpCnsKICAgIGludCBpOwogICAgaWYoaGVhZD09TlVMTCkKICAgIHsKICAgICAgICByZXR1cm47ICAgICAgICAgICAgICAKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBmcHJpbnRmKGZwLCIlZCAiLGhlYWQtPmlkKTsKICAgICAgICBmcHJpbnRmKGZwLCIlcyAiLGhlYWQtPm5hbWUpOwogICAgICAgIGZwcmludGYoZnAsIiVkICIsaGVhZC0+dGFrZV9udW0pOwogICAgICAgIGZvcihpPTE7aTw9aGVhZC0+dGFrZV9udW07aSsrKQogICAgICAgIHsKICAgICAgICAgICAgZnByaW50ZihmcCwiJWQiLGhlYWQtPnRha2VzW2ldKTsKICAgICAgICAgICAgaWYoaTxoZWFkLT50YWtlX251bSkKICAgICAgICAgICAgICAgIGZwcmludGYoZnAsIiAiKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgZnByaW50ZihmcCwiXG4iKTsgCiAgICAgICAgLy9wcmludGYoIiVkIixoZWFkLT5pZCk7CiAgICAgICAgc2F2ZShmcCxoZWFkLT5sZWZ0X25leHQpOwogICAgICAgIHNhdmUoZnAsaGVhZC0+cmlnaHRfbmV4dCk7IAogICAgfQp9CgpzdHJ1Y3QgU1RVREVOVCogYl9zZWFyY2goc3RydWN0IFNUVURFTlQqIGhlYWQsaW50IGlkLGNoYXIqIG5hbWUsaW50IHNlbGVjdF9udW0pCnsKICAgIC8vcHJpbnRmKCIlZCAiLHNlbGVjdF9udW0pOwogICAgc3RydWN0IFNUVURFTlQqIHJlc3VsdDsKICAgIGlmKHNlbGVjdF9udW09PTEpCiAgICB7ICAgIAogICAgICAgIGlmKGhlYWQ9PU5VTEwpCiAgICAgICAgewogICAgICAgICAgICByZXN1bHQ9TlVMTDsKICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsgICAgICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihoZWFkIT1OVUxMKQogICAgICAgIHsKICAgICAgICAgICAgaWYoc3RyY21wKChoZWFkKS0+bmFtZSxuYW1lKT4wKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByZXN1bHQ9Yl9zZWFyY2goaGVhZC0+bGVmdF9uZXh0LC0xLG5hbWUsMSk7CiAgICAgICAgICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgICAgICAgICB9ICAgICAgICAgCiAgICAgICAgICAgIGVsc2UgaWYoc3RyY21wKChoZWFkKS0+bmFtZSxuYW1lKTwwKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByZXN1bHQ9Yl9zZWFyY2goaGVhZC0+cmlnaHRfbmV4dCwtMSxuYW1lLDEpOwogICAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKHN0cmNtcCgoaGVhZCktPm5hbWUsbmFtZSk9PTApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgcmV0dXJuIGhlYWQ7ICAgICAKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGVsc2UgaWYoc2VsZWN0X251bT09MikKICAgIHsKICAgICAgICBpZihoZWFkPT1OVUxMKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIE5VTEw7ICAgICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsgICAgCiAgICAgICAgICAgIGlmKGlkPT1oZWFkLT5pZCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmVzdWx0PWhlYWQ7CiAgICAgICAgICAgICAgICByZXR1cm4gcmVzdWx0OyAgICAgICAgICAgICAgICAKICAgICAgICAgICAgfSAKICAgICAgICAgICAgZWxzZSBpZihpZDxoZWFkLT5pZCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmVzdWx0PWJfc2VhcmNoKGhlYWQtPmxlZnRfbmV4dCxpZCwwLDIpOyAKICAgICAgICAgICAgICAgIHJldHVybiByZXN1bHQ7ICAgIAogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYoaWQ+aGVhZC0+aWQpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHJlc3VsdD1iX3NlYXJjaChoZWFkLT5yaWdodF9uZXh0LGlkLDAsMik7IAogICAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsgICAgCiAgICAgICAgICAgIH0KICAgICAgICB9ICAgICAgICAKICAgIH0KfQoKCnZvaWQgbWFrZV90cmVlKHN0cnVjdCBTVFVERU5UKiBzdHVkZW50X3B0cixzdHJ1Y3QgU1RVREVOVCoqIGhlYWQsaW50IHNlbGVjdF9udW0pCnsKICAgICBpZihzZWxlY3RfbnVtPT0xKQogICAgIHsKICAgICAgICAgaWYoKmhlYWQ9PU5VTEwpICAgICAgICAgICAgICAgICAKICAgICAgICAgewogICAgICAgICAgICAgKmhlYWQ9c3R1ZGVudF9wdHI7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgfQogICAgICAgICBlbHNlIGlmKCpoZWFkIT1OVUxMKQogICAgICAgICB7CiAgICAgICAgICAgICBpZihzdHJjbXAoKCpoZWFkKS0+bmFtZSxzdHVkZW50X3B0ci0+bmFtZSk+MCkKICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICBtYWtlX3RyZWUoc3R1ZGVudF9wdHIsJigoKmhlYWQpLT5sZWZ0X25leHQpLHNlbGVjdF9udW0pOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgIH0gICAgIAogICAgICAgICAgICAgZWxzZSBpZihzdHJjbXAoKCpoZWFkKS0+bmFtZSxzdHVkZW50X3B0ci0+bmFtZSk8PTApCiAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgbWFrZV90cmVlKHN0dWRlbnRfcHRyLCYoKCpoZWFkKS0+cmlnaHRfbmV4dCksc2VsZWN0X251bSk7ICAgICAKICAgICAgICAgICAgIH0KICAgICAgICAgfQogICAgIH0KICAgICBlbHNlIGlmKHNlbGVjdF9udW09PTIpCiAgICAgeyAgIAogICAgICAgIGlmKCpoZWFkPT1OVUxMKQogICAgICAgIHsKICAgICAgICAgICAgKmhlYWQ9c3R1ZGVudF9wdHI7CiAgICAgICAgICAgLy8gcHJpbnRmKCIlZFxuIiwoKmhlYWQpLT5pZCk7CiAgICAgICAgICAgIC8qY3VycmVudD1zdHVkZW50X3B0cjsgCiAgICAgICAgICAgIHByZXZpb3VzPXN0dWRlbnRfcHRyOyovICAgICAgICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgZWxzZSBpZigqaGVhZCE9TlVMTCkKICAgICAgICB7CiAgICAgICAgICAgIGlmKCgqaGVhZCktPmlkPj1zdHVkZW50X3B0ci0+aWQpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8qcHJldmlvdXM9Y3VycmVudDsKICAgICAgICAgICAgICAgIGN1cnJlbnQ9aGVhZC0+bGVmdF9uZXh0OyovCiAgICAgICAgICAgICAgICBpZigoKmhlYWQpLT5sZWZ0X25leHQ9PU5VTEwpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgKCpoZWFkKS0+bGVmdF9uZXh0PXN0dWRlbnRfcHRyOyAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlIGlmKCgqaGVhZCktPmxlZnRfbmV4dCE9TlVMTCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBtYWtlX3RyZWUoc3R1ZGVudF9wdHIsJigoKmhlYWQpLT5sZWZ0X25leHQpLHNlbGVjdF9udW0pOwogICAgICAgICAgICAgICAgfSAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKCgqaGVhZCktPmlkPHN0dWRlbnRfcHRyLT5pZCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYoKCpoZWFkKS0+cmlnaHRfbmV4dD09TlVMTCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAoKmhlYWQpLT5yaWdodF9uZXh0PXN0dWRlbnRfcHRyOyAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICB9ICAgICAKICAgICAgICAgICAgICAgIGVsc2UgaWYoKCpoZWFkKS0+cmlnaHRfbmV4dCE9TlVMTCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBtYWtlX3RyZWUoc3R1ZGVudF9wdHIsJigoKmhlYWQpLT5yaWdodF9uZXh0KSxzZWxlY3RfbnVtKTsgICAgIAogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9ICAgICAKICAgICAgICB9ICAKICAgICB9IAogICAgLy9wcmludGYoIiVzICVkXG4iLGhlYWQtPm5hbWUsaGVhZC0+aWQpOwp9Cgp2b2lkIHRyYXZlcnNlX3RyZWUoc3RydWN0IFNUVURFTlQqKiBoZWFkKQp7CiAgICBpZigqaGVhZD09TlVMTCkKICAgIHsKICAgICAgICByZXR1cm47ICAgICAgICAgICAgICAKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICAvL3ByaW50ZigiJXMgJWRcbiIsKCpoZWFkKS0+bmFtZSwoKmhlYWQpLT5pZCk7CiAgICAgICAgdHJhdmVyc2VfdHJlZSgmKCgqaGVhZCktPmxlZnRfbmV4dCkpOwogICAgICAgIHRyYXZlcnNlX3RyZWUoJigoKmhlYWQpLT5yaWdodF9uZXh0KSk7ICAKICAgIH0gICAKfQo=