📄 sshow.c
字号:
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<fcntl.h>#include<signal.h>#include<curses.h>#include<malloc.h>#include<time.h>#include<pwd.h>#define BEGINYEAR 1970#define DSEC 86400#define HSEC 3600#define MSEC 60#define ROWS 15#define ROW 5#define INROW 22#define MSGROW 23#define NITEM 9struct time{ int second; /* 0--59 */ int minute; /* 0--59 */ int hour; /* 0--23 */ int mday; /* 1--31 */ int month; /* 1--12 */ int year; /* 1970-- */ int wday; /* 0--6(0=Sunday)*/ int yday; /* 1--365(366) */ long tseconds; /* seconds from 1970.1.1,0:0:0 */};struct ticket{ int user; int date; int bus; char flag; int time; int seat; int sta; int price; int fee; int rprice; unsigned number;}*tic;int ntic;/*int *order;*/int col[NITEM]={2,9,18,27,39,45,57,63,71};struct station{ int no; char code[5]; char name[10]; unsigned int howfar;}*sta;int nsta;char *tab[]={ "***** 人 员 账 目 查 询 *****", "----------------------------------------" "---------------------------------------", "Q-退出 D-日期 U-工作人员 N-指定 P-打印日报 M-打印明细账"};char *title[]={"班次","到站","售票员","日期","时间","票号","座号", "票价","状态"};enum Key{ NONE,Up,Down,Right,Left,Center,End,PgDn,Home, PgUp,Ins,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12};char *RP;int count=0,lift=0,fee=0,rcount=0,startnum,endnum;int uno=-1,sno=-1,umodel=-1;char uname[80],sname[80];int btime=0,etime=2359;main(int argc,char *argv[]){ char c; int flag=0; int quit=0; int stic=0,ptic=0; int n,date; long tn,minute; struct time t; struct passwd *upw; RP=getenv("ROOTPATH"); if(RP==NULL) return -1; uno=getuid(); upw=getpwuid(uno); if(upw==NULL) { printf("\n工作人员数据有错误, 编号为%d!\n",uno); return; } strcpy(uname,upw->pw_comment); init(); tn=time(0); gettime(tn,&t); date=t.year*10000+t.month*100+t.mday; minute=t.minute; if((nsta=readsta())==-1) nsta=0; if((ntic=readtic(umodel,date))==-1) ntic=0; show(1,stic,ptic); while(quit==0) { move(21,41+strlen(tab[2])/2); refresh(); c=getkey(); clrin(); clrerr(); if(c>='A'&&c<='Z') c=c-'A'+'a'; switch(c) { case -1: break; case Down: if(ptic<ntic-1) ptic++; if(ptic-stic>=ROWS) { stic++; show(1,stic,ptic); } else show(0,stic,ptic); break; case Up: if(ptic>0) ptic--; if(ptic<stic&&stic>0) { stic--; show(1,stic,ptic); } else show(0,stic,ptic); break; case PgUp: if(ptic>=ROWS) ptic-=ROWS; else ptic=0; if(stic>=ROWS) { stic-=ROWS; show(1,stic,ptic); } else if(stic>0) { stic=0; show(1,stic,ptic); } else show(0,stic,ptic); break; case PgDn: if(ptic<ntic-ROWS) ptic+=ROWS; else ptic=ntic-1; if(stic<ntic-ROWS) { stic+=ROWS; show(1,stic,ptic); } else show(0,stic,ptic); break; case Home: if(stic>0) { stic=0; ptic=0; show(1,stic,ptic); } else { ptic=0; show(0,stic,ptic); } break; case End: ptic=ntic-1; if(ptic-stic>=ROWS) { stic=ntic-ROWS; show(1,stic,ptic); } else show(0,stic,ptic); break; case 'u': getuser(&sno,sname); if(sno!=-1) { umodel=checkuser(sno,date); ntic=readtic(umodel,date); if(ntic==-1) ntic=0; show(1,stic,ptic); } else printf("\7\7"); break; case 'd': n=indate(date); if(n==-1||checkdate(n)==0) { date=n; ntic=readtic(umodel,date); if(ntic==-1) ntic=0; show(1,stic,ptic); } else printf("\7\7"); break; case 'n': n=appoint(ptic); if(n!=-1) if(n-stic>=ROWS) { stic=n; ptic=n; show(1,stic,ptic); } else { ptic=n; show(0,stic,ptic); } break; case 'p': print(date); break; case 'm': printmx(date); break; case 'q': quit=1; break; default: printf("\07"); break; } } erase(); refresh(); endwin();}init(){ int i; void showtime(); signal(SIGINT,SIG_IGN); initscr(); raw(); nonl(); noecho(); erase(); mvaddstr(ROW-4,40-strlen(tab[0])/2,tab[0]); for(i=0;i<NITEM;i++) mvaddstr(ROW-2,col[i],title[i]); mvaddstr(ROW-1,0,tab[1]); mvaddstr(ROW+ROWS,0,tab[1]); mvaddstr(ROW+ROWS+1,40-strlen(tab[2])/2,tab[2]); refresh();}int readsta(){ char fn[80]; char errs[128]; int fd; long size; sprintf(fn,"%s/bas/stas.dat",RP); fd=open(fn,O_RDONLY); if(fd==-1) { sprintf(errs,"文件%s打开失败!",fn); showmsg(errs); return fd; } size=lseek(fd,0L,2); sta=(struct station *)malloc(size); if(sta==NULL) { showmsg("为站点数据分配内存出错!"); return -1; } else { lseek(fd,0L,0); read(fd,(char *)sta,size); close(fd); return size/sizeof(struct station); }}int getuser(int *uno,char uname[]){ struct passwd *psw; char ucode[80]=""; if(*uno!=-1) { psw=getpwuid(*uno); if(psw!=NULL) strcpy(ucode,psw->pw_name); } if(get_s(INROW,30,"工作人员代码: ",ucode,20)==-1) return -1; psw=getpwnam(ucode); if(psw==NULL) { *uno=-1; return -1; } else { *uno=psw->pw_uid; strcpy(uname,psw->pw_comment); return 0; }}int checkuser(int uno,int date){ char fn[80]; int fd; struct ticket t; long size,ss; sprintf(fn,"%s/data/user/%d",RP,date); fd=open(fn,O_RDONLY); if(fd==-1) { showmsg("打开用户数据文件失败!"); return -1; } size=sizeof(struct ticket); while(1) { ss=read(fd,(char *)&t,size); if(ss!=size) { close(fd); return -1; } if(t.user==uno) break; } close(fd); switch(t.flag) { case -2: case 2: return 2; case -1: return 1; case 0: case 1: return 0; }} int readtic(int umodel,int date){ char fn[80]; char errs[128]; int fd; long size; int ntic; if(umodel==0) { while(1) { clrin(); if(get_n(INROW,30,"起始时间: ",&btime,10)==-1) return -1; if(btime>=0&&btime<2400) break; } while(1) { clrin(); if(get_n(INROW,30,"截止时间: ",&etime,10)==-1) return -1; if(etime>=btime&&etime<2400) break; } } sprintf(fn,"%s/data/user/%d",RP,date); fd=open(fn,O_RDONLY); if(fd==-1) { sprintf(errs,"文件%s打开失败!",fn); showmsg(errs); return fd; } size=lseek(fd,0L,2); if(tic!=NULL) free(tic); tic=(struct ticket *)malloc(size); /*order=(int *)malloc(size/sizeof(struct ticket)*sizeof(int));*/ if(tic==NULL/*||order==NULL*/) { showmsg("分配内存出错!"); return -1; } else { lseek(fd,0L,0); size=sizeof(struct ticket); count=0; rcount=0; fee=0; lift=0; for(ntic=0;;) { if(read(fd,(char *)(tic+ntic),size)!=size) break; if(tic[ntic].user!=sno) continue; if(tic[ntic].time<btime||tic[ntic].time>etime) continue; if(tic[ntic].flag==-2) { count-=tic[ntic].price; lift-=tic[ntic].fee; fee+=tic[ntic].price+tic[ntic].fee -tic[ntic].rprice; } else { count+=tic[ntic].price; if(tic[ntic].flag==2) lift+=tic[ntic].fee; else fee+=tic[ntic].fee; } rcount+=tic[ntic].rprice; if(ntic==0) { startnum=tic[ntic].number; endnum=tic[ntic].number; } if(startnum>tic[ntic].number) startnum=tic[ntic].number; if(endnum<tic[ntic].number) endnum=tic[ntic].number; ntic++; } close(fd); return ntic; }}int get_s(int y,int x,char prompt[],char s[],int n) /* x,y location of cursor; s the string */ /* n length of s; */{ char c,ss[128]; int i; for(i=0;i<n-1;i++) ss[i]=' '; ss[n-1]='\0'; mvaddstr(y,x,prompt); x+=strlen(prompt); mvaddstr(y,x,ss); mvaddstr(y,x,s); refresh(); i=0; while(1) { move(y,x+i); refresh(); if(i>=n-1) c=0x0d; else c=getkey(); switch(c) { case -1: continue; case 0x1b: /* <ESC> pressed */ return -1; case 0x0d: /* <ENTER> pressed */ if(i) { ss[i]='\0'; strcpy(s,ss); } return 0; case 0x08: /* <BS> pressed */ if(i<=0) continue; else ss[--i]=' '; break; default: ss[i++]=c; break; } mvaddstr(y,x,ss); refresh();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -