#include #include #include #include #define AR″ ″ FILE farj;intlist(); void arx(); main(argc,argv) int argc;char argv[] {charfname1[100],opt[1]; if(argc<4){printf(″\nusage:myar a|x arj—filepathdestpath\n″);exit(1);} strcpy(opt,argv[1]); if(opt[0]==″ a ″){strcpy(fname1,argv[2]); if((farj=fopen(fname1,″ wb ″))==NULL) {printf(″\nerrorin open file%s\n″,fname1);exit(1);} else ftw(argv[3],list,1);} else if(opt[0]==″ x ″arx(argv[2],argv[3]); else{printf(″\nusage:myar a|x arj-filepathdestpath\n″);exit(1);} fclose(farj);} intlist(name,status,type) char name;struct stat status;inttype; {int size,left,bb=80;char buf[80],buf1[80];FILE ft; charfname[100];if(type==FTW-NS)return(0); if(type==FTW-F){ sprintf(buf,″%s%30%d%d\n″,name,status->st—mode&0777,status->st—reserved1,status->st-reserved2,status->st-size); /若UNIX是SCO-UNIX可把″st-reserved1″改成″st-uid″把″st-reserved2″改成″st-gid″/ fwrite(buf,strlen(buf),1,farj);printf(″\nbeing add%s″,name); strcpy(fname,name);ft=fopen(fname,″rb″);size=status->st-size; if(ft==NULL){printf(“\nerrorin open%s",fname); exit(1);} while(fread(buf1,bb,1,ft)>0)fwrite(buf1,bb,1,farj);left=size%bb; if(left>0){fread(buf1,left,1,ft);fwrite(buf1,left,1,farj);} fclose(ft);} else fprintf(farj,″%s%30%d\n″,name,status->st—mode&0777,status->st—reserved1,status->st—reserved2); return(0); } / ==================== / / the follow function for extract / void arx(filepath,destpath) charfilepath[100],destpath[100]; {FILE ft; int acc,owner,grp,size,s1=80,s2,i,len,p,sp,sl; charfname[70],fnamet[70],fname[70],path[70],str[80],buf[80]; strcpy(fname1,filepath); if((farj=fopen(fnam1,″rb″)=NULL){perror(″farj″;exit(1);} while(!feof(farj)){ fgets(buf,s1,farj); acc=0;owner=0;grp=0;size=0; sscanf(buf,″%s%30%d%d%d″,fnamet,&acc,&owner,&grp,&size); if(fnamet[0]==″ ″){len=strlen(fanmet); for(i=1;i path[len-1]=″\0″; strcpy(str,″ mkdir ″); strcat(str,destpath); if(path[0]==″/″); else strcat(str,″/″); strcat(str,path); printf(″\nbeing%s″,str); system(str); continue;} else{printf(″\nbeing extracted from\n%s″,fnamet); if(fnamet[0]==″/″) sprintf(fname,″%s/%s″,destpath,fnamet); else sprintf(fname,″%s/%s,destpath,fnamet); if((ft=fopen(fname,″ab″))==NULL){ perror(″\nopen ft=″); printf(″%s\n″,fname); continue;} sp=0; while(sp if(size-sp >=s1){fread(buf,s1,1,farj);fwrite(buf,s1,1,ft); sp=sp+s1;} else{fread(buf,size-sp,1,farj); fwrite(buf,size-sp,1,ft); sp=size; break;}} fclose(ft); if(chmod(fname,acc)< 0) printf(″\nerrorin chmod:%s″,fname); if(chown(fname,owner,grp)==-1)printf(″\nerror inchown:%s″,fname); } } } |