#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#define PTIME 20
#define SQty 1000
#define fname "material.txt"
#define fcode "code.txt"
#define ftemp "temp.txt"
FILE *fp,*fp1,*fp2;
char stoday[11];
int day_today=0;
struct date
{
int da_year;
int da_mon;
int da_day;
};
int main(void)
{
void print(void);
void edit_code(void);
void check_reserve(void);
void record(char type);
char* qtyCalc(char check_no[10],int s_day,int e_day);
{
struct date d;
getdate(&d);
sprintf(stoday,"%d/%d/%d",d.da_year,d.da_mon,d.da_day);
day_today=dayCalc(stoday);
}
for(;;)
{
clrscr();
printf("\nMain Menu\n\(1)Sale.\n\(2)Order.\n\(3)Search and Print.\n\(4)Edit Code list\n\(5)Safety Stock\n\(0)Quit\n");
printf("Please enter choice?");
switch(getche())
{
case'0':exit(0);
case'1':record('+');break;
case'2':record('-');break;
case'3':print();break;
case'4':edit_code();break;
case'5':check_reserve();break;
default:printf("Input error!Please input 0~5\n");break;
}
}
return 0;
}
int dayCalc(char sdate[])
{
long year,month,day;
char temp;
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
int i,total;
sscanf(sdate,"%ld%c%ld%c%ld",&year,&temp,&month,&temp,&day);
total=day+(month>2&&((year%4==0&&year%100!=0)||year%400==0));
for(i=0;i<month-1;total+=days[i++]);
return total;
}
void record(char type)
{
long total=0,m_amount=0,in_amt=0,out_amt=0;
char m_no[10],m_date[11],m_rdate[11];
char *s_qty,temp;
char* qtyCalc(char check_no[10],int s_day,int e_day);
printf("\n\n*****************************************************\n");
fflush(stdin);
printf("No:");
gets(m_no);
s_qty=qtyCalc(m_no,1,day_today+PTIME);
sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
if(total<SQty)
{
printf("The current safety stock qty is %ld\n",total);
}
printf("Date(yyyy/mm/dd):");
gets(m_date);
printf("Quantity:");
scanf("%ld",m_amount);
if((fp=fopen(fname,"at"))==NULL)
printf("file open error!\n"),exit(1);
fprintf(fp,"%10s%15s%10ld%2c%15s\n",m_no,m_date,m_amount,type,stoday);
if(fclose(fp)==-1)
printf("file close error!\n"),exit(1);
}
void print(void)
{
void search1(void);
void search2(void);
for(;;)
{
clrscr();
printf("\nsearch & print database\n\(1)query single material\n\(2)query all materials\n\(0)back to mainmenu\n");
printf("enter choice?");
switch(getche())
{
case'0':main();break;
case'1':search1();break;
case'2':search2();break;
default:printf("input error!please input0~2\n");break;
}
}
}
void edit_code(void)
{
char temp[20],code_no[10],code_name[20];
printf("\n\n***************************************\n");
fflush(stdin);
printf("No:");
gets(temp);
if((fp1=fopen(fcode,"a+t"))==NULL)
printf("file open error!(code file)\n"),exit(1);
while(fscanf(fp1,"%10s%20s",code_no,code_name)!=EOF)
{
if(strcmp(code_no,temp)==0)
{
fclose(fp1);
printf("duplicate code NO!\n");
printf("go back to main menu.please press any key......");
getche();
main();
}
}
fprintf(fp1,"%10s",temp);
printf("Name:");
fprintf(fp1,"%20s",gets(temp));
fprintf(fp1,"\n");
if(fclose(fp1)==-1)
printf("file close error!\n"),exit(1);
}
void check_reserve(void)
{
char code_no[10],code_name[20];
long total=0,in_amt=0,out_amt=0;
char *s_qty,temp;
int e_day=0;
if((fp1=fopen(fcode,"r"))==NULL)
printf("file read error(code file)\n"),exit(1);
printf("\n--------------------------------------------\n");
printf("material no material name safety stock");
printf("\n--------------------------------------------\n");
e_day=day_today+PTIME;
while(fscanf(fp1,"%10s%20s",code_no,code_name)!=EOF)
{
s_qty=qtyCalc(code_no,1,e_day);
sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
if(total<SQty)
printf("%s%22s%18ld\n",code_no,code_name,total);
}
if(fclose(fp1)==-1)
printf("\n\npress any key to continue...!");
getche();
}
char* qtyCalc(char check_no[10],int s_day,int e_day)
{
char m_no[10],m_name[20],m_date[11],m_rdate[11],type;
long m_amount,total=0,in_amt=0,out_amt=0;
int m_day=0;
static char sqty[30];
if((fp=fopen(fname,"r"))==NULL)
printf("file read error(database file)!"),exit(1);
while(fscanf(fp,"%s%s%ld%c%s",m_no,m_date,&m_amount,&type,m_rdate)!=EOF)
{
if((strcmp(m_no,check_no)==0)&&(s_day<=m_day)&&(m_day<=e_day))
{
if(type=='+')
total+=m_amount,in_amt+=m_amount;
if(type=='-')
total-=m_amount,out_amt+=m_amount;
}
}
sprintf(sqty,"%ld%ld%ld",in_amt,out_amt,total);
if(fclose(fp)==-1)
printf("file close error!\n"),exit(1);
return sqty;
}
void search1(void)
{
char search_no[10],m_no[10],m_date[11],type,m_rdate[11],p,*s_qty,temp;
char Q[31][1];
long Q1[31][3];
long m_amount=0,in_amt=0,out_amt=0,total=0,ftotal=0;
int m_day,i,j=0;
for(i=0;i<=30;i++)
{
strcpy(Q[i],"");
Q1[i][0]=0,Q1[i][1]=0,Q1[i][2]=0;
}
printf("\n\n查詢今日前後15天的進出貨狀況\n");
fflush(stdin);
printf("\nPlease input material NO:");
gets(search_no);
s_qty=qtyCalc(search_no,1,day_today-16);
if((fp=fopen(fname,"r"))==NULL)
printf("file read error...!"),exit(1);
sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
printf("\nthe safety stock of no.%s:%ld\n",search_no,total);
ftotal=total;
printf("\n-------------------------------------------------------\n");
printf(" date order sale stock\n");
printf("--------------------------------------------------------\n");
while(fscanf(fp,"%s%s%ld%c%s",m_no,m_date,&m_amount,&type,m_rdate)!=EOF)
{
m_day=dayCalc(m_date);
if((strcmp(m_no,search_no)==0)&&((day_today-15)<=m_day)&&(m_day<=(day_today+15)))
{
if(type=='+')
{
Q1[m_day-day_today+15][0]+=m_amount;
Q1[m_day-day_today+15][2]+=m_amount;
}
if(type=='-')
{
Q1[m_day-day_today+15][1]+=m_amount;
Q1[m_day-day_today+15][2]-=m_amount;
}
if(strcmp(Q[m_day-day_today+15],"")==0)
strcpy(Q[m_day-day_today+15],m_date);
}
}
if(fclose(fp)==-1)
printf("file close error!\n"),exit(1);
for(i=0;i<=30;i++)
{
Q1[i][2]=Q1[i][2]+total;
total=Q1[i][2];
if(strcmp(Q[i],"")!=0)
{
printf("%10s%15ld%15ld%15ld\n",Q[i],Q1[i][0],Q1[i][1],Q1[i][2]),j++;
if(j==15)
printf("--more--\n"),getch();
}
}
fflush(stdin);
printf("do you want to save the result(Y/N)?");
scanf("%c",&p);
if((p=='Y')||(p=='y'))
{
if((fp2=fopen(ftemp,"w"))==NULL)
printf("file open error!\n"),exit(1);
fprintf(fp2,"\nthe safety stock of no%s:%ld\n",search_no,ftotal);
fprintf(fp2,"\n----------------------------------------------\n");
fprintf(fp2," date order sale stock\n");
fprintf(fp2,"\n----------------------------------------------\n");
for(i=0;i<=30;i++)
{
if(strcmp(Q[i],"")!=0)
fprintf(fp2,"%10s%15ld%15ld%15ld\n",Q[i],Q1[i][0],Q1[i][1],Q1[i][2]);
}
if(fclose(fp2)==-1)
printf("file close error!\n"),exit(1);
printf("\nfile name:temp.txt\n");
}
printf("\npress any key to continue...!");
getche();
}
void search2(void)
{
char search_date[11];
char code_no[10],code_name[20];
char *s_qty,temp,p;
int day_search=0;
long in_amt=0,out_amt=0,total=0;
fflush(stdin);
printf("\n查詢至輸入的日期截止,所有進出貨的存貨狀況\n");
printf("please input the deadline(yyyy/mm/dd):");
gets(search_date);
day_search=dayCalc(search_date);
printf("do you want to save the result(Y/N)?");
scanf("%c",&p);
printf("\n---------------------------------------------\n");
printf("material no order sale stock\n");
printf("----------------------------------------------\n");
if((p=='Y')||(p=='y'))
{
if((fp2=fopen(ftemp,"w"))==NULL)
printf("file open error!\n"),exit(1);
fprintf(fp2,"\nthe deadline:%s\n",search_date);
fprintf(fp2,"\n----------------------------------------------\n");
fprintf(fp2,"material no order sale stock\n");
fprintf(fp2,"----------------------------------------------\n");
}
if((fp1=fopen(fcode,"r"))==NULL)
printf("file read error...(code file)\n"),exit(1);
while(fscanf(fp1,"%10s%20s",code_no,code_name)!=EOF)
{
s_qty=qtyCalc(code_no,1,day_search);
sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
printf("%10s%15ld%15ld%15ld\n",code_no,in_amt,out_amt,total);
if((p=='Y')||(p=='y'))
fprintf(fp2,"%10s%15ld%15ld%15ld\n",code_no,in_amt,out_amt,total);
total=0,in_amt=0,out_amt=0;
}
if(fclose(fp1)==-1)
printf("file close error!\n"),exit(1);
if((p=='Y')||(p=='y'))
{
if(fclose(fp2)==-1)
printf("file close error!\n"),exit(1);
printf("\nfile name:temp.txt\n");
}
printf("\npress any key to continue...!");getche();
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPGRvcy5oPgojZGVmaW5lIFBUSU1FIDIwCiNkZWZpbmUgU1F0eSAxMDAwCiNkZWZpbmUgZm5hbWUgIm1hdGVyaWFsLnR4dCIKI2RlZmluZSBmY29kZSAiY29kZS50eHQiCiNkZWZpbmUgZnRlbXAgInRlbXAudHh0IgoKRklMRSAqZnAsKmZwMSwqZnAyOwpjaGFyIHN0b2RheVsxMV07CmludCBkYXlfdG9kYXk9MDsKCnN0cnVjdCBkYXRlCnsKICAgIGludCBkYV95ZWFyOwogICAgaW50IGRhX21vbjsKICAgIGludCBkYV9kYXk7ICAgICAgIAp9OwoKaW50IG1haW4odm9pZCkKewogICAgdm9pZCBwcmludCh2b2lkKTsKICAgIHZvaWQgZWRpdF9jb2RlKHZvaWQpOwogICAgdm9pZCBjaGVja19yZXNlcnZlKHZvaWQpOwogICAgdm9pZCByZWNvcmQoY2hhciB0eXBlKTsKICAgIGNoYXIqIHF0eUNhbGMoY2hhciBjaGVja19ub1sxMF0saW50IHNfZGF5LGludCBlX2RheSk7CiAgICAKICAgIHsKICAgICAgICBzdHJ1Y3QgZGF0ZSBkOwogICAgICAgIGdldGRhdGUoJmQpOwogICAgICAgIHNwcmludGYoc3RvZGF5LCIlZC8lZC8lZCIsZC5kYV95ZWFyLGQuZGFfbW9uLGQuZGFfZGF5KTsKICAgICAgICBkYXlfdG9kYXk9ZGF5Q2FsYyhzdG9kYXkpOyAgICAgIAogICAgfSAgICAgCiAgICAKICAgIGZvcig7OykKICAgIHsKICAgICAgICBjbHJzY3IoKTsKICAgICAgICBwcmludGYoIlxuTWFpbiBNZW51XG5cKDEpU2FsZS5cblwoMilPcmRlci5cblwoMylTZWFyY2ggYW5kIFByaW50LlxuXCg0KUVkaXQgQ29kZSBsaXN0XG5cKDUpU2FmZXR5IFN0b2NrXG5cKDApUXVpdFxuIik7CiAgICAgICAgcHJpbnRmKCJQbGVhc2UgZW50ZXIgY2hvaWNlPyIpOwogICAgICAgIHN3aXRjaChnZXRjaGUoKSkKICAgICAgICB7CiAgICAgICAgICAgIGNhc2UnMCc6ZXhpdCgwKTsKICAgICAgICAgICAgY2FzZScxJzpyZWNvcmQoJysnKTticmVhazsKICAgICAgICAgICAgY2FzZScyJzpyZWNvcmQoJy0nKTticmVhazsKICAgICAgICAgICAgY2FzZSczJzpwcmludCgpO2JyZWFrOwogICAgICAgICAgICBjYXNlJzQnOmVkaXRfY29kZSgpO2JyZWFrOwogICAgICAgICAgICBjYXNlJzUnOmNoZWNrX3Jlc2VydmUoKTticmVhazsKICAgICAgICAgICAgZGVmYXVsdDpwcmludGYoIklucHV0IGVycm9yIVBsZWFzZSBpbnB1dCAwfjVcbiIpO2JyZWFrOyAgICAgICAgICAgICAgICAKICAgICAgICB9ICAgICAgIAogICAgfQogICAgcmV0dXJuIDA7Cn0KCmludCBkYXlDYWxjKGNoYXIgc2RhdGVbXSkKewogICAgbG9uZyB5ZWFyLG1vbnRoLGRheTsKICAgIGNoYXIgdGVtcDsKICAgIGludCBkYXlzW109ezMxLDI4LDMxLDMwLDMxLDMwLDMxLDMxLDMwLDMxLDMwLDMxfTsKICAgIGludCBpLHRvdGFsOwogICAgc3NjYW5mKHNkYXRlLCIlbGQlYyVsZCVjJWxkIiwmeWVhciwmdGVtcCwmbW9udGgsJnRlbXAsJmRheSk7CiAgICB0b3RhbD1kYXkrKG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biIpLGV4aXQoMSk7CiAgICAKICAgIGZwcmludGYoZnAsIiUxMHMlMTVzJTEwbGQlMmMlMTVzXG4iLG1fbm8sbV9kYXRlLG1fYW1vdW50LHR5cGUsc3RvZGF5KTsKICAgIGlmKGZjbG9zZShmcCk9PS0xKQogICAgICAgIHByaW50ZigiZmlsZSBjbG9zZSBlcnJvciFcbiIpLGV4aXQoMSk7Cn0KCnZvaWQgcHJpbnQodm9pZCkKewogICAgdm9pZCBzZWFyY2gxKHZvaWQpOwogICAgdm9pZCBzZWFyY2gyKHZvaWQpOwogICAgZm9yKDs7KQogICAgewogICAgICAgIGNscnNjcigpOwogICAgICAgIHByaW50ZigiXG5zZWFyY2ggJiBwcmludCBkYXRhYmFzZVxuXCgxKXF1ZXJ5IHNpbmdsZSBtYXRlcmlhbFxuXCgyKXF1ZXJ5IGFsbCBtYXRlcmlhbHNcblwoMCliYWNrIHRvIG1haW5tZW51XG4iKTsKICAgICAgICBwcmludGYoImVudGVyIGNob2ljZT8iKTsKICAgICAgICBzd2l0Y2goZ2V0Y2hlKCkpCiAgICAgICAgewogICAgICAgICAgICBjYXNlJzAnOm1haW4oKTticmVhazsKICAgICAgICAgICAgY2FzZScxJzpzZWFyY2gxKCk7YnJlYWs7CiAgICAgICAgICAgIGNhc2UnMic6c2VhcmNoMigpO2JyZWFrOwogICAgICAgICAgICBkZWZhdWx0OnByaW50ZigiaW5wdXQgZXJyb3IhcGxlYXNlIGlucHV0MH4yXG4iKTticmVhazsKICAgICAgICB9ICAgICAgIAogICAgfSAgICAgCn0KCnZvaWQgZWRpdF9jb2RlKHZvaWQpCnsKICAgIGNoYXIgdGVtcFsyMF0sY29kZV9ub1sxMF0sY29kZV9uYW1lWzIwXTsKICAgIHByaW50ZigiXG5cbioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKlxuIik7CiAgICBmZmx1c2goc3RkaW4pOwogICAgcHJpbnRmKCJObzoiKTsKICAgIGdldHModGVtcCk7CiAgICBpZigoZnAxPWZvcGVuKGZjb2RlLCJhK3QiKSk9PU5VTEwpCiAgICAgICAgcHJpbnRmKCJmaWxlIG9wZW4gZXJyb3IhKGNvZGUgZmlsZSlcbiIpLGV4aXQoMSk7CiAgICAgICAKICAgIHdoaWxlKGZzY2FuZihmcDEsIiUxMHMlMjBzIixjb2RlX25vLGNvZGVfbmFtZSkhPUVPRikKICAgIHsKICAgICAgICBpZihzdHJjbXAoY29kZV9ubyx0ZW1wKT09MCkKICAgICAgICB7CiAgICAgICAgICAgIGZjbG9zZShmcDEpOwogICAgICAgICAgICBwcmludGYoImR1cGxpY2F0ZSBjb2RlIE5PIVxuIik7CiAgICAgICAgICAgIHByaW50ZigiZ28gYmFjayB0byBtYWluIG1lbnUucGxlYXNlIHByZXNzIGFueSBrZXkuLi4uLi4iKTsKICAgICAgICAgICAgZ2V0Y2hlKCk7CiAgICAgICAgICAgIG1haW4oKTsgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICB9ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgfSAgICAgCiAgICBmcHJpbnRmKGZwMSwiJTEwcyIsdGVtcCk7CiAgICBwcmludGYoIk5hbWU6Iik7CiAgICBmcHJpbnRmKGZwMSwiJTIwcyIsZ2V0cyh0ZW1wKSk7CiAgICBmcHJpbnRmKGZwMSwiXG4iKTsKICAgIGlmKGZjbG9zZShmcDEpPT0tMSkKICAgICAgICBwcmludGYoImZpbGUgY2xvc2UgZXJyb3IhXG4iKSxleGl0KDEpOwp9Cgp2b2lkIGNoZWNrX3Jlc2VydmUodm9pZCkKewogICAgY2hhciBjb2RlX25vWzEwXSxjb2RlX25hbWVbMjBdOwogICAgbG9uZyB0b3RhbD0wLGluX2FtdD0wLG91dF9hbXQ9MDsKICAgIGNoYXIgKnNfcXR5LHRlbXA7CiAgICBpbnQgZV9kYXk9MDsKICAgIAogICAgaWYoKGZwMT1mb3BlbihmY29kZSwiciIpKT09TlVM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RvdGFsPTAsaW5fYW10PTAsb3V0X2FtdD0wOwogICAgaW50IG1fZGF5PTA7CiAgICBzdGF0aWMgY2hhciBzcXR5WzMwXTsKICAgIAogICAgaWYoKGZwPWZvcGVuKGZuYW1lLCJyIikpPT1OVUxMKQogICAgICAgIHByaW50ZigiZmlsZSByZWFkIGVycm9yKGRhdGFiYXNlIGZpbGUpISIpLGV4aXQoMSk7CiAgICAgICAgCiAgICB3aGlsZShmc2NhbmYoZnAsIiVzJXMlbGQlYyVzIixtX25vLG1fZGF0ZSwmbV9hbW91bnQsJnR5cGUsbV9yZGF0ZSkhPUVPRikKICAgIHsKICAgICAgICBpZigoc3RyY21wKG1fbm8sY2hlY2tfbm8pPT0wKSYmKHNfZGF5PD1tX2RheSkmJihtX2RheTw9ZV9kYXkpKQogICAgICAgIHsKICAgICAgICAgICAgaWYodHlwZT09JysnKQogICAgICAgICAgICAgICAgdG90YWwrPW1fYW1vdW50LGluX2FtdCs9bV9hbW91bnQ7CiAgICAgICAgICAgIGlmKHR5cGU9PSctJykKICAgICAgICAgICAgICAgIHRvdGFsLT1tX2Ftb3VudCxvdXRfYW10Kz1tX2Ftb3VudDsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgfSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgfSAgICAgIAogICAgc3ByaW50ZihzcXR5LCIlbGQlbGQlbGQiLGluX2FtdCxvdXRfYW10LHRvdGFsKTsKICAgIGlmKGZjbG9zZShmcCk9PS0xKQogICAgICAgIHByaW50ZigiZmlsZSBjbG9zZSBlcnJvciFcbiIpLGV4aXQoMSk7CiAgICAgICAgcmV0dXJuIHNxdHk7Cn0KCnZvaWQgc2VhcmNoMSh2b2lkKQp7CiAgICBjaGFyIHNlYXJjaF9ub1sxMF0sbV9ub1sxMF0sbV9kYXRlWzExXSx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gIGRhdGUgICAgb3JkZXIgICAgc2FsZSAgICBzdG9ja1xuIik7CiAgICBwcmludGYoIi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tXG4iKTsKICAgIAogICAgd2hpbGUoZnNjYW5mKGZwLCIlcyVzJWxkJWMlcyIsbV9ubyxtX2RhdGUsJm1fYW1vdW50LCZ0eXBlLG1fcmRhdGUpIT1FT0YpCiAgICB7CiAgICAgICAgbV9kYXk9ZGF5Q2FsYyhtX2RhdGUpOwogICAgICAgIGlmKChzdHJjbXAobV9ubyxzZWFyY2hfbm8pPT0wKSYmKChkYXlfdG9kYXktMTUpPD1tX2RheSkmJihtX2RheTw9KGRheV90b2RheSsxNSkpKQogICAgICAgIHsKICAgICAgICAgICAgaWYodHlwZT09JysnKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBRMVttX2RheS1kYXlfdG9kYXkrMTVdWzBdKz1tX2Ftb3VudDsKICAgICAgICAgICAgICAgIFExW21fZGF5LWRheV90b2RheSsxNV1bMl0rPW1fYW1vdW50OyAgICAgICAgICAgICAKICAgICAgICAgICAgfSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICBpZih0eXBlPT0nLScpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIFExW21fZGF5LWRheV90b2RheSsxNV1bMV0rPW1fYW1vdW50OwogICAgICAgICAgICAgICAgUTFbbV9kYXktZGF5X3RvZGF5KzE1XVsyXS09bV9hbW91bnQ7ICAgICAgICAgICAgIAogICAgICAgICAgICB9ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgaWYoc3RyY21wKFFbbV9kYXktZGF5X3RvZGF5KzE1XSwiIik9PTApCiAgICAgICAgICAgICAgICBzdHJjcHkoUVttX2RheS1kYXlfdG9kYXkrMTVdLG1fZGF0ZSk7ICAgICAgICAgICAgIAogICAgICAgIH0gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgIH0gICAKICAgIGlmKGZjbG9zZShmcCk9PS0xKQogICAgICAgIHByaW50ZigiZmlsZSBjbG9zZSBlcnJvciFcbiIpLGV4aXQoMSk7CiAgICAgICAgCiAgICBmb3IoaT0wO2k8PTMwO2krKykKICAgIHsKICAgICAgICBRMVtpXVsyXT1RMVtpXVsyXSt0b3RhbDsKICAgICAgICB0b3RhbD1RMVtpXVsyXTsKICAgICAgICBpZihzdHJjbXAoUVtpXSwiIikhPTApCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoIiUxMHMlMTVsZCUxNWxkJTE1bGRcbiIsUVtpXSxRMVtpXVswXSxRMVtpXVsxXSxRMVtpXVsyXSksaisrOwogICAgICAgICAgICBpZihqPT0xNSkKICAgICAgICAgICAgICAgIHByaW50ZigiLS1tb3JlLS1cbiIpLGdldGNoKCk7ICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgIH0gICAgICAgICAgICAgICAgICAKICAgIH0gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgIGZmbHVzaChzdGRpbik7CiAgICBwcmludGYoImRvIHlvdSB3YW50IHRvIHNhdmUgdGhlIHJlc3VsdChZL04pPyIpOwogICAgc2NhbmYoIiVjIiwmcCk7CiAgICBpZigocD09J1knKXx8KHA9PSd5JykpCiAgICB7CiAgICAgICAgaWYoKGZwMj1mb3BlbihmdGVtcCwidyIpKT09TlVMTCkKICAgICAgICAgICAgcHJpbnRmKCJmaWxlIG9wZW4gZXJyb3IhXG4iKSxleGl0KDEpOwogICAgICAgICAgICAKICAgICAgICBmcHJpbnRmKGZwMiwiXG50aGUgc2FmZXR5IHN0b2NrIG9mIG5vJXM6JWxkXG4iLHNlYXJjaF9ubyxmdG90YWwpOwogICAgICAgIGZwcmludGYoZnAyLCJcbi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS1cbiIpOwogICAgICAgIGZwcmludGYoZnAyLCIgICAgZGF0ZSAgICBvcmRlciAgICBzYWxlICAgIHN0b2NrXG4iKTsKICAgICAgICBmcHJpbnRmKGZwMiwiXG4tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tXG4iKTsKICAgICAgICAKICAgICAgICBmb3IoaT0wO2k8PTMwO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKHN0cmNtcChRW2ldLCIiKSE9MCkKICAgICAgICAgICAgICAgIGZwcmludGYoZnAyLCIlMTBzJTE1bGQlMTVsZCUxNWxkXG4iLFFbaV0sUTFbaV1bMF0sUTFbaV1bMV0sUTFbaV1bMl0pOyAgICAgICAgICAgICAgICAgIAogICAgICAgIH0gICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgaWYoZmNsb3NlKGZwMik9PS0xKQogICAgICAgICAgICBwcmludGYoImZpbGUgY2xvc2UgZXJyb3IhXG4iKSxleGl0KDEpOwogICAgICAgICAgICAKICAgICAgICBwcmludGYoIlxuZmlsZSBuYW1lOnRlbXAudHh0XG4iKTsKICAgIH0KICAgIHByaW50ZigiXG5wcmVzcyBhbnkga2V5IHRvIGNvbnRpbnVlLi4uISIpOwogICAgZ2V0Y2hlKCk7Cn0KCnZvaWQgc2VhcmNoMih2b2lkKQp7CiAgICBjaGFyIHNlYXJjaF9kYXRlWzExXTsKICAgIGNoYXIgY29kZV9ub1sxMF0sY29kZV9uYW1lWzIwXTsKICAgIGNoYXIgKnNfcXR5LHRlbXAscDsKICAgIGludCBkYXlfc2VhcmNoPTA7CiAgICBsb25nIGluX2FtdD0wLG91dF9hbXQ9MCx0b3RhbD0wOwogICAgZmZsdXNoKHN0ZGluKTsKICAgIHByaW50ZigiXG7mn6XoqaLoh7PovLjlhaXnmoTml6XmnJ/miKrmraLvvIzmiYDmnInpgLLlh7rosqjnmoTlrZjosqjni4Dms4Fcb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ZW4oZmNvZGUsInIiKSk9PU5VTEwpCiAgICAgICAgcHJpbnRmKCJmaWxlIHJlYWQgZXJyb3IuLi4oY29kZSBmaWxlKVxuIiksZXhpdCgxKTsKICAgICAgICAKICAgIHdoaWxlKGZzY2FuZihmcDEsIiUxMHMlMjBzIixjb2RlX25vLGNvZGVfbmFtZSkhPUVPRikKICAgIHsKICAgICAgICBzX3F0eT1xdHlDYWxjKGNvZGVfbm8sMSxkYXlfc2VhcmNoKTsKICAgICAgICBzc2NhbmYoc19xdHksIiVsZCVjJWxkJWMlbGQiLCZpbl9hbXQsJnRlbXAsJm91dF9hbXQsJnRlbXAsJnRvdGFsKTsKICAgICAgICBwcmludGYoIiUxMHMlMTVsZCUxNWxkJTE1bGRcbiIsY29kZV9ubyxpbl9hbXQsb3V0X2FtdCx0b3RhbCk7CiAgICAgICAgaWYoKHA9PSdZJyl8fChwPT0neScpKQogICAgICAgICAgICBmcHJpbnRmKGZwMiwiJTEwcyUxNWxkJTE1bGQlMTVsZFxuIixjb2RlX25vLGluX2FtdCxvdXRfYW10LHRvdGFsKTsKICAgICAgICAgICAgdG90YWw9MCxpbl9hbXQ9MCxvdXRfYW10PTA7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgfQogICAgaWYoZmNsb3NlKGZwMSk9PS0xKQogICAgICAgIHByaW50ZigiZmlsZSBjbG9zZSBlcnJvciFcbiIpLGV4aXQoMSk7CiAgICAgICAgCiAgICBpZigocD09J1knKXx8KHA9PSd5JykpCiAgICB7CiAgICAgICAgaWYoZmNsb3NlKGZwMik9PS0xKQogICAgICAgICAgICBwcmludGYoImZpbGUgY2xvc2UgZXJyb3IhXG4iKSxleGl0KDEpOwogICAgICAgICAgICAKICAgICAgICBwcmludGYoIlxuZmlsZSBuYW1lOnRlbXAudHh0XG4iKTsgICAgICAgICAgICAgICAgICAgICAKICAgIH0KICAgIHByaW50ZigiXG5wcmVzcyBhbnkga2V5IHRvIGNvbnRpbnVlLi4uISIpO2dldGNoZSgpOwp9Cg==