[repost,.DIR rebuild,sort] transpost.c

看板Maple (BBS架站)作者時間23年前 (2001/10/31 04:17), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/2 (看更多)
/* */ /* National Kaohsiung University of Applied Sciences */ /* Campus Computer Network Association */ /* 國立高雄應用科技大學 校園網路策進會 */ /* */ /* Firebird to Maple 3.0 文章轉換 */ /* */ /* File : transpost.c */ /* Code : Hao-Chine Hung */ /* E-Mail: chine@ccna.kuas.edu.tw */ /* Mobile: +886-958734768 */ /* Tel : +886-7-3814526 Ext.3141 */ /* Ver. : (2001/10/30) */ /* */ #define FBHOME "/home/news/bbs/boards" /* Firebird的boards目錄位置 */ #define BRDHOME "/home/bbs" /* brd要建的位置 */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <time.h> #include <dirent.h> #include <stdlib.h> void archiv32m(time_t chrono, char *fname) { char *str; int n; str=fname+8; *str='\0'; *fname='A'; while(--str>fname) { n=chrono&31; n+='0'; if(n>'9') n+='A'-'9'- 1; *str=n; chrono>>=5; } } typedef struct { time_t chrono; /* timestamp */ int xmode; int xid; /* reserved */ char xname[32]; /* 檔案名稱 */ char owner[80]; /* 作者 (E-mail address) */ char nick[50]; /* 暱稱 */ char date[9]; /* [96/12/01] */ char title[73]; /* 主題 (TTLEN + 1) */ } HDR; int transdate(char *time,char *date) { int i; char year[5],mouth[4],day[3]; for(i=4;i<7;i++) mouth[i-4]=time[i]; mouth[i-4]='\0'; if(!strcmp(mouth,"Jan")) strcpy(mouth,"01"); if(!strcmp(mouth,"Feb")) strcpy(mouth,"02"); if(!strcmp(mouth,"Mar")) strcpy(mouth,"03"); if(!strcmp(mouth,"Apr")) strcpy(mouth,"04"); if(!strcmp(mouth,"May")) strcpy(mouth,"05"); if(!strcmp(mouth,"Jun")) strcpy(mouth,"06"); if(!strcmp(mouth,"Jul")) strcpy(mouth,"07"); if(!strcmp(mouth,"Aug")) strcpy(mouth,"08"); if(!strcmp(mouth,"Sep")) strcpy(mouth,"09"); if(!strcmp(mouth,"Oct")) strcpy(mouth,"10"); if(!strcmp(mouth,"Nov")) strcpy(mouth,"11"); if(!strcmp(mouth,"Dec")) strcpy(mouth,"12"); if(time[8]==' ') day[0]='0'; else day[0]=time[8]; day[1]=time[9]; day[2]='\0'; for(i=22;i<25;i++) year[i-22]=time[i]; year[i-22]='\0'; sprintf(date,"%s/%s/%s",year,mouth,day); if(strlen(date)==8) return 1; else return 0; } int getnums(char *fname,int size) { struct stat st; if(stat(fname,&st)<0) return 0; return(st.st_size/size); } long findymd(char *date) { int i,set=0,y,m,d; char temp[3]; /*處理year*/ switch(date[0]) { case '0':if(date[1]=='1') y=2001; else y=2000;break; case '9':set=1; } if(set) switch(date[1]) { case '0':y=1990;break; case '1':y=1991;break; case '2':y=1992;break; case '3':y=1993;break; case '4':y=1994;break; case '5':y=1995;break; case '6':y=1996;break; case '7':y=1997;break; case '8':y=1998;break; case '9':y=1999;break; } if(date[3]=='0') switch(date[4]) { case '1':m=1;break; case '2':m=2;break; case '3':m=3;break; case '4':m=4;break; case '5':m=5;break; case '6':m=6;break; case '7':m=7;break; case '8':m=8;break; case '9':m=9;break; } else switch(date[4]) { case '0':m=10;break; case '1':m=11;break; case '2':m=12;break; } if(date[6]=='0') switch(date[7]) { case '1':d=1;break; case '2':d=2;break; case '3':d=3;break; case '4':d=4;break; case '5':d=5;break; case '6':d=6;break; case '7':d=7;break; case '8':d=8;break; case '9':d=9;break; } else { temp[0]=date[6]; temp[1]=date[7]; temp[2]='\0'; d=atoi(temp); } return y*10000+m*100+d; } int sortdir(char *board) { int fd0,fd1,i,j,total; HDR buf0,buf1,*newdir; long int date0,date1; char temp[100]; time_t chrono; sprintf(temp,"%s/brd/%s/.DIR",BRDHOME,board); total=getnums(temp,sizeof(HDR)); newdir=(HDR *)malloc(sizeof(HDR)*total); /*把原來的.DIR放置入記憶體裡*/ fd0=open(temp,O_RDWR); for(i=0;i<total;i++) read(fd0,&newdir[i],sizeof(newdir[i])); close(fd0); unlink(temp); for(i=0;i<total;i++) for(j=0;j<total;j++) { if(findymd(newdir[j].date)>findymd(newdir[j+1].date)) { chrono=newdir[j].chrono; newdir[j].chrono=newdir[j+1].chrono; newdir[j+1].chrono=chrono; strcpy(temp,newdir[j].xname); strcpy(newdir[j].xname,newdir[j+1].xname); strcpy(newdir[j+1].xname,temp); strcpy(temp,newdir[j].owner); strcpy(newdir[j].owner,newdir[j+1].owner); strcpy(newdir[j+1].owner,temp); strcpy(temp,newdir[j].nick); strcpy(newdir[j].nick,newdir[j+1].nick); strcpy(newdir[j+1].nick,temp); strcpy(temp,newdir[j].date); strcpy(newdir[j].date,newdir[j+1].date); strcpy(newdir[j+1].date,temp); strcpy(temp,newdir[j].title); strcpy(newdir[j].title,newdir[j+1].title); strcpy(newdir[j+1].title,temp); } } sprintf(temp,"%s/brd/%s/.DIR",BRDHOME,board); fd0=open(temp,O_WRONLY|O_CREAT,0600); for(i=0;i<total;i++) write(fd0,&newdir[i],sizeof(newdir[i])); close(fd0); return 0; } int main(void) { int fd0,fd1,fd2,i,chrono,set=1,xok,xcount; char buf,wrbuf[1024]; char username[100],nickname[100],board[100],title[100],time[100]; DIR *dp1,*dp2; struct dirent *dirp1,*dirp2; char oldfile[128],newfile[128]; HDR fname; /*建立brd目錄*/ sprintf(oldfile,"%s/brd",BRDHOME); mkdir(oldfile,0700); if((dp1=opendir(FBHOME))==NULL) { printf("開啟目錄錯誤: %s",FBHOME); exit(-1); } for(;(dirp1=readdir(dp1))!=NULL;chrono=0x100000) { if(*(dirp1->d_name)!='.') { sprintf(oldfile,"%s/%s",FBHOME,dirp1->d_name); if((dp2=opendir(oldfile))==NULL) { printf("開啟目錄錯誤: %s",oldfile); exit(-1); } /*建立討論區目錄 */ sprintf(oldfile,"%s/brd/%s",BRDHOME,dirp1->d_name); mkdir (oldfile, 0700); /*建立討論區目錄結構 */ for(buf='0';;) { sprintf(oldfile,"%s/brd/%s/%c",BRDHOME,dirp1->d_name,buf); mkdir(oldfile,0700); if(buf=='9') buf='@'; else if(++buf=='W') break; } /*建立.DIR索引檔*/ sprintf(oldfile,"%s/brd/%s/.DIR",BRDHOME,dirp1->d_name); if((fd2=open(oldfile,O_WRONLY|O_CREAT,0600))<0) { printf("建立檔案錯誤: %s",oldfile); exit(-1); } printf("進入 %s 討論區\n\n",dirp1->d_name); xok=0; for(xcount=0;(dirp2=readdir(dp2))!=NULL;xcount++) { if(*(dirp2->d_name)!='.') { chrono++; memset(&fname,0,sizeof(fname)); archiv32m(chrono,fname.xname); sprintf(newfile,"%s/brd/%s/%c/%s",BRDHOME,dirp1->d_name,fname.xname[7],fname.xname); sprintf (oldfile,"%s/%s/%s",FBHOME,dirp1->d_name,dirp2->d_name); if((fd0=open(oldfile,O_RDWR))<0) { printf("開啟檔案錯誤: %s\n",oldfile); exit(-1); } if((fd1=open(newfile,O_WRONLY|O_CREAT,0600))<0) { printf("建立檔案錯誤: %s\n",newfile); exit(-1); } printf("%s => %s\n",oldfile,newfile); /*確認是否符合格式*/ for(i=0;read(fd0,&buf,1);i++) if(buf==':'|buf=='\n'|buf=='\0'|buf==' ') break; else wrbuf[i]=buf; wrbuf[i]='\0'; if(!strcmp(wrbuf,"發信人")) { /*取得username*/ read(fd0,&buf,1); for(i=0;read(fd0,&buf,1);i++) if(buf==' ') break; else username[i]=buf; username[i]='\0'; strcpy(fname.owner,username); /*取得nickname */ for(;read(fd0,&buf,1);) if(buf=='(') break; for(i=0;read(fd0,&buf,1);i++) if(buf==')') break; else nickname[i]=buf; nickname[i]='\0'; strcpy(fname.nick,nickname); /*取得board */ for(;read(fd0,&buf,1);) if(buf==',') break; for(i=0;i<7;i++) read(fd0,&buf,1); for(i=0;read(fd0,&buf,1);i++) if(buf=='\n') break; else board[i]=buf; board[i]='\0'; /*取得title */ for(i=0;i<8;i++) read(fd0,&buf,1); for(i=0;read(fd0,&buf,1);i++) if(buf=='\n') break; else title[i] = buf; title[i]='\0'; strcpy(fname.title,title); /*取得time */ for(;read(fd0,&buf,1);) if(buf=='(') break; for(i=0;read(fd0,&buf,1);i++) if(buf==')') break; else time[i]=buf; time[i]='\0'; if(transdate(time,fname.date)) { fname.chrono=chrono; fname.xmode=1024; fname.xid=0; sprintf(wrbuf,"作者: %s (%s) 看板: %s\n標題: %s\n時間: %s\n",username,nickname,board,title,time); write(fd1,wrbuf,strlen(wrbuf)); while(read(fd0,&buf,1)) if(buf=='\n') break; while(read(fd0,wrbuf,sizeof(wrbuf))) { write(fd1,wrbuf,sizeof(wrbuf)); memset(wrbuf,'\0',sizeof(wrbuf)); } write(fd2,&fname,sizeof(fname)); xok++; } } else { printf("\033[1;32m該檔案無法存取標頭內容: %s\033[0m\n",oldfile); unlink(newfile); } close(fd0); close(fd1); } } sortdir(dirp1->d_name); closedir(dp2); close(fd2); printf("處理總數 %d ==> %d 轉換總數\n\n",xcount,xok); } } closedir(dp1); return 0; } -- ※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ From: 61-217-160-202.HINET-IP.hinet.net
文章代碼(AID): #xtmg-00 (Maple)
文章代碼(AID): #xtmg-00 (Maple)