home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / WWIV2.ZIP / XFEROVL.C < prev    next >
C/C++ Source or Header  |  1992-12-31  |  13KB  |  580 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1993 by Wayne Bell
  5.  
  6. Distribution of the source code for WWIV, in any form, modified or unmodified,
  7. without PRIOR, WRITTEN APPROVAL by the author, is expressly prohibited.
  8. Distribution of compiled versions of WWIV is limited to copies compiled BY
  9. THE AUTHOR.  Distribution of any copies of WWIV not compiled by the author
  10. is expressly prohibited.
  11.  
  12.  
  13. *****************************************************************************/
  14.  
  15.  
  16.  
  17. #include "vars.h"
  18.  
  19. #pragma hdrstop
  20.  
  21. #include <time.h>
  22. #include <dir.h>
  23.  
  24.  
  25.  
  26. #define SETREC(i)  lseek(dlf,((long) (i))*((long)sizeof(uploadsrec)),SEEK_SET);
  27.  
  28. void move_file(void)
  29. {
  30.   char sx[81],s[81],s1[81],s2[81],ch,*ss;
  31.   int i,i1,ok,d1,d2,done,cp;
  32.   uploadsrec u,u1,u2;
  33.   char *b;
  34.  
  35.   ok=0;
  36.   nl();
  37.   nl();
  38.   prt(2,get_string(821));
  39.   input(sx,12);
  40.   if (strchr(sx,'.')==NULL)
  41.     strcat(sx,".*");
  42.   align(sx);
  43.   dliscan();
  44.   i=recno(sx);
  45.   if (i<0) {
  46.     nl();
  47.     pl(get_string(89));
  48.     closedl();
  49.     return;
  50.   }
  51.   done=0;
  52.   while ((!hangup) && (i>0) && (!done)) {
  53.     cp=i;
  54.     SETREC(i);
  55.     read(dlf,(void *)&u,sizeof(uploadsrec));
  56.     nl();
  57.     printfileinfo(&u,udir[curdir].subnum);
  58.     nl();
  59.     prt(5,get_string(822));
  60.     ch=ynq();
  61.     if (ch=='Q')
  62.       done=1;
  63.     else if (ch=='Y') {
  64.       sprintf(s1,"%s%s",directories[udir[curdir].subnum].path,u.filename);
  65.       do {
  66.         nl();
  67.         nl();
  68.         prt(2,get_string(823));
  69.         ss=mmkey(1);
  70.         if (ss[0]=='?')
  71.           dirlist();
  72.       } while ((!hangup) && (ss[0]=='?'));
  73.       d1=-1;
  74.       if (ss[0])
  75.         for (i1=0; (i1<num_dirs) && (udir[i1].subnum!=-1); i1++)
  76.           if (strcmp(udir[i1].keys,ss)==0)
  77.             d1=i1;
  78.       if (d1!=-1) {
  79.         ok=1;
  80.         d1=udir[d1].subnum;
  81.         closedl();
  82.         dliscan1(d1);
  83.         if (recno(u.filename)>0) {
  84.           ok=0;
  85.           nl();
  86.           pl(get_string(824));
  87.         }
  88.         if (numf>=directories[d1].maxfiles) {
  89.           ok=0;
  90.           nl();
  91.           pl(get_string(825));
  92.         }
  93.         if (freek1(directories[d1].path)<((double)(u.numbytes/1024L)+3)) {
  94.           ok=0;
  95.           nl();
  96.           pl(get_string(826));
  97.         }
  98.         closedl();
  99.         dliscan();
  100.       } else
  101.         ok=0;
  102.     } else
  103.       ok=0;
  104.     if (ok && !done) {
  105.       --cp;
  106.       for (i1=i; i1<numf; i1++) {
  107.         SETREC(i1+1);
  108.         read(dlf,(void *)&u1,sizeof(uploadsrec));
  109.         SETREC(i1);
  110.         write(dlf,(void *)&u1,sizeof(uploadsrec));
  111.       }
  112.       --numf;
  113.       SETREC(0);
  114.       read(dlf, &u1, sizeof(uploadsrec));
  115.       u1.numbytes=numf;
  116.       SETREC(0);
  117.       write(dlf,(void *)&u1,sizeof(uploadsrec));
  118.       ss=read_extended_description(u.filename);
  119.       if (ss)
  120.         delete_extended_description(u.filename);
  121.       closedl();
  122.  
  123.       sprintf(s2,"%s%s",directories[d1].path,u.filename);
  124.       dliscan1(d1);
  125.       for (i=numf; i>=1; i--) {
  126.         SETREC(i);
  127.         read(dlf,(void *)&u1,sizeof(uploadsrec));
  128.         SETREC(i+1);
  129.         write(dlf,(void *)&u1,sizeof(uploadsrec));
  130.       }
  131.       SETREC(1);
  132.       write(dlf,(void *)&u,sizeof(uploadsrec));
  133.       ++numf;
  134.       SETREC(0);
  135.       read(dlf, &u1, sizeof(uploadsrec));
  136.       u1.numbytes=numf;
  137.       if (u.daten>u1.daten) {
  138.         u1.daten = u.daten;
  139.         dir_dates[d1]=u.daten;
  140.       }
  141.       SETREC(0);
  142.       write(dlf,(void *)&u1,sizeof(uploadsrec));
  143.       if (ss) {
  144.         add_extended_description(u.filename,ss);
  145.         farfree(ss);
  146.       }
  147.       closedl();
  148.  
  149.       if ((strcmp(s1,s2)!=0) && (exist(s1))) {
  150.         d2=0;
  151.         if ((s1[1]!=':') && (s2[1]!=':'))
  152.           d2=1;
  153.         if ((s1[1]==':') && (s2[1]==':') && (s1[0]==s2[0]))
  154.           d2=1;
  155.         if (d2) {
  156.           rename(s1,s2);
  157.           unlink(s1);
  158.         } else {
  159.           if ((b=malloca(16400))==NULL)
  160.             return;
  161.           d1=open(s1,O_RDONLY | O_BINARY);
  162.           d2=open(s2,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  163.           i=read(d1,(void *)b,16384);
  164.           while (i>0) {
  165.             write(d2,(void *)b,i);
  166.             i=read(d1,(void *)b,16384);
  167.           }
  168.           close(d1);
  169.           close(d2);
  170.           unlink(s1);
  171.           farfree(b);
  172.         }
  173.       }
  174.       nl();
  175.       pl(get_string(827));
  176.     } else
  177.       closedl();
  178.     dliscan();
  179.     i=nrecno(sx,cp);
  180.   }
  181.   closedl();
  182. }
  183.  
  184. int comparedl(uploadsrec *x, uploadsrec *y, int type)
  185. {
  186.   switch(type) {
  187.     case 0:
  188.       return(strcmp(x->filename,y->filename));
  189.     case 1:
  190.       if (x->daten < y->daten)
  191.         return(-1);
  192.       else
  193.         if (x->daten > y->daten)
  194.           return(1);
  195.         else
  196.           return(0);
  197.     case 2:
  198.       if (x->daten < y->daten)
  199.         return(1);
  200.       else
  201.         if (x->daten > y->daten)
  202.           return(-1);
  203.         else
  204.           return(0);
  205.   }
  206.   return(0);
  207. }
  208.  
  209.  
  210. void quicksort(int l,int r,int type)
  211. {
  212.   register int i,j;
  213.   uploadsrec a,a2,x;
  214.  
  215.   i=l; j=r;
  216.   SETREC(((l+r)/2));
  217.   read(dlf, (void *)&x,sizeof(uploadsrec));
  218.   do {
  219.     SETREC(i);
  220.     read(dlf, (void *)&a,sizeof(uploadsrec));
  221.     while (comparedl(&a,&x,type)<0) {
  222.       SETREC(++i);
  223.       read(dlf, (void *)&a,sizeof(uploadsrec));
  224.     }
  225.     SETREC(j);
  226.     read(dlf, (void *)&a2,sizeof(uploadsrec));
  227.     while (comparedl(&a2,&x,type)>0) {
  228.       SETREC(--j);
  229.       read(dlf, (void *)&a2,sizeof(uploadsrec));
  230.     }
  231.     if (i<=j) {
  232.       if (i!=j) {
  233.         SETREC(i);
  234.         write(dlf,(void *)&a2,sizeof(uploadsrec));
  235.         SETREC(j);
  236.         write(dlf,(void *)&a,sizeof(uploadsrec));
  237.       }
  238.       i++;
  239.       j--;
  240.     }
  241.   } while (i<j);
  242.   if (l<j)
  243.     quicksort(l,j,type);
  244.   if (i<r)
  245.     quicksort(i,r,type);
  246. }
  247.  
  248.  
  249. void sortdir(int dn, int type)
  250. {
  251.   dliscan1(dn);
  252.   if (numf>1)
  253.     quicksort(1,numf,type);
  254.   closedl();
  255. }
  256.  
  257.  
  258. void sort_all(int type)
  259. {
  260.   int i;
  261.  
  262.   for (i=0; (i<num_dirs) && (udir[i].subnum!=-1) && (!kbhitb()); i++) {
  263.     nl();
  264.     ansic(1);
  265.     outstr(get_string(828));
  266.     pl(directories[udir[i].subnum].name);
  267.     sortdir(i,type);
  268.   }
  269. }
  270.  
  271.  
  272. void rename_file(void)
  273. {
  274.   char s[81],s1[81],s2[81],*ss,s3[81],ch;
  275.   int i,cp;
  276.   uploadsrec u;
  277.  
  278.   nl();
  279.   nl();
  280.   prt(2,get_string(829));
  281.   input(s,12);
  282.   if (s[0]==0)
  283.     return;
  284.   if (strchr(s,'.')==NULL)
  285.     strcat(s,".*");
  286.   align(s);
  287.   dliscan();
  288.   nl();
  289.   strcpy(s3,s);
  290.   i=recno(s);
  291.   while (i>0) {
  292.     cp=i;
  293.     SETREC(i);
  294.     read(dlf,(void *)&u,sizeof(uploadsrec));
  295.     nl();
  296.     printfileinfo(&u,udir[curdir].subnum);
  297.     nl();
  298.     prt(5,get_string(830));
  299.     ch=ynq();
  300.     if (ch=='Q')
  301.       break;
  302.     else if (ch=='N') {
  303.       i=nrecno(s3,cp);
  304.       continue;
  305.     }
  306.     nl();
  307.     prt(2,get_string(72));
  308.     input(s,12);
  309.     if (!okfn(s))
  310.       s[0]=0;
  311.     if (s[0]) {
  312.       align(s);
  313.       if (strcmp(s,"        .   ")) {
  314.         strcpy(s1,directories[udir[curdir].subnum].path);
  315.         strcpy(s2,s1);
  316.         strcat(s1,s);
  317.         if (exist(s1))
  318.           pl(get_string(831));
  319.         else {
  320.           strcat(s2,u.filename);
  321.           rename(s2,s1);
  322.           if (exist(s1)) {
  323.             ss=read_extended_description(u.filename);
  324.             if (ss) {
  325.               delete_extended_description(u.filename);
  326.               add_extended_description(s,ss);
  327.               farfree(ss);
  328.             }
  329.             strcpy(u.filename,s);
  330.           } else
  331.             pl(get_string(832));
  332.         }
  333.       }
  334.     }
  335.     nl();
  336.     pl(get_string(833));
  337.     prt(2,": ");
  338.     inputl(s,58);
  339.     if (s[0]) {
  340.       strcpy(u.description,s);
  341.     }
  342.     ss=read_extended_description(u.filename);
  343.     nl();
  344.     nl();
  345.     prt(5,get_string(834));
  346.     if (yn()) {
  347.       nl();
  348.       if (ss) {
  349.         prt(5,get_string(835));
  350.         if (yn()) {
  351.           farfree(ss);
  352.           delete_extended_description(u.filename);
  353.           u.mask &= ~mask_extended;
  354.         } else {
  355.           u.mask |= mask_extended;
  356.           modify_extended_description(&ss,
  357.             directories[udir[curdir].subnum].name,u.filename);
  358.           if (ss) {
  359.             delete_extended_description(u.filename);
  360.             add_extended_description(u.filename,ss);
  361.             farfree(ss);
  362.           }
  363.         }
  364.       } else {
  365.         modify_extended_description(&ss,
  366.             directories[udir[curdir].subnum].name,u.filename);
  367.         if (ss) {
  368.           add_extended_description(u.filename,ss);
  369.           farfree(ss);
  370.           u.mask |= mask_extended;
  371.         } else
  372.           u.mask &= ~mask_extended;
  373.       }
  374.     } else
  375.       if (ss) {
  376.         farfree(ss);
  377.         u.mask |= mask_extended;
  378.       } else
  379.         u.mask &= ~mask_extended;
  380.     SETREC(i);
  381.     write(dlf,(void *)&u,sizeof(uploadsrec));
  382.     i=nrecno(s3,cp);
  383.   }
  384.   closedl();
  385. }
  386.  
  387.  
  388. int upload_file(char *fn, int dn, char *desc)
  389. {
  390.   directoryrec d;
  391.   uploadsrec u,u1;
  392.   int i,i1,i2,ok,f;
  393.   char s[81],s1[81],ff[81];
  394.   long l;
  395.   double ti;
  396.  
  397.   d=directories[dn];
  398.   strcpy(s,fn);
  399.   align(s);
  400.   strcpy(u.filename,s);
  401.   u.ownerusr=usernum;
  402.   u.ownersys=0;
  403.   u.numdloads=0;
  404.   u.filetype=0;
  405.   u.mask=0;
  406.   if (check_ul_event(dn,&u)) {
  407.     outstr(fn);
  408.     pl(get_string(836));
  409.   } else {
  410.     sprintf(ff,"%s%s",d.path,s);
  411.     f=open(ff,O_RDONLY | O_BINARY);
  412.     if (f<=0) {
  413.       if (desc && (*desc)) {
  414.         outstr(get_string(837));
  415.         outstr(fn);
  416.         outstr(": ");
  417.         pl(desc);
  418.       } else {
  419.         outstr(fn);
  420.         pl(get_string(838));
  421.       }
  422.       return(1);
  423.     }
  424.     l=filelength(f);
  425.     u.numbytes=l;
  426.     close(f);
  427.     strcpy(u.upby, nam(&thisuser,usernum));
  428.     strcpy(u.date,date());
  429.     if (d.mask & mask_PD)
  430.       d.mask=mask_PD;
  431.     npr("%s: %4ldk :",u.filename,((u.numbytes)+1023)/1024);
  432.     if ((desc) && (*desc)) {
  433.       strncpy(u.description,desc,58);
  434.       u.description[58]=0;
  435.       pl(u.description);
  436.     } else
  437.       inputl(u.description,58);
  438.     if (u.description[0]==0)
  439.       return(0);
  440.     ++thisuser.uploaded;
  441.     thisuser.uk += ((l+1023)/1024);
  442.     time(&l);
  443.     u.daten=l;
  444.     for (i=numf; i>=1; i--) {
  445.       SETREC(i);
  446.       read(dlf,(void *)&u1,sizeof(uploadsrec));
  447.       SETREC(i+1);
  448.       write(dlf,(void *)&u1,sizeof(uploadsrec));
  449.     }
  450.     SETREC(1);
  451.     write(dlf,(void *)&u,sizeof(uploadsrec));
  452.     ++numf;
  453.     SETREC(0);
  454.     read(dlf, &u1, sizeof(uploadsrec));
  455.     u1.numbytes=numf;
  456.     u1.daten=l;
  457.     dir_dates[dn]=l;
  458.     SETREC(0);
  459.     write(dlf,(void *)&u1,sizeof(uploadsrec));
  460.     ++status.uptoday;
  461.     save_status();
  462.     sprintf(s,get_stringx(1,42),u.filename,d.name);
  463.     sysoplog(s);
  464.   }
  465.   return(1);
  466. }
  467.  
  468.  
  469. int maybe_upload(char *fn, int dn, char *desc)
  470. {
  471.   char s[81];
  472.   int i,i1=0,ok=1,ocd;
  473.   uploadsrec u;
  474.  
  475.   strcpy(s,fn);
  476.   align(s);
  477.   i=recno(s);
  478.  
  479.   if (i==-1) {
  480.     if (!upload_file(s,udir[dn].subnum,desc))
  481.       ok=0;
  482.   } else {
  483.     SETREC(i);
  484.     read(dlf,(void *)&u, sizeof(uploadsrec));
  485.     ocd=curdir;
  486.     curdir=dn;
  487.     printinfo(&u,&i1);
  488.     curdir=ocd;
  489.     if (i1)
  490.       ok=0;
  491.   }
  492.   return(ok);
  493. }
  494.  
  495. void upload_files(char *fn, int dn, int type)
  496. /* This assumes the file holds listings of files, one per line, to be
  497.  * uploaded.  The first word (delimited by space/tab) must be the filename.
  498.  * after the filename are optional tab/space separated words (such as file
  499.  * size or date/time).  After the optional words is the description, which
  500.  * is from that position to the end of the line.  the "type" parameter gives
  501.  * the number of optional words between the filename and description.
  502.  * the optional words (size, date/time) are ignored completely.
  503.  */
  504. {
  505.   char s[255],*fn1,*desc;
  506.   FILE *f;
  507.   int ok=1,abort=0,next=0,ok1,i;
  508.  
  509.   dliscan1(udir[dn].subnum);
  510.  
  511.   f=fopen(fn,"r");
  512.   if (!f) {
  513.     sprintf(s,"%s%s",directories[udir[dn].subnum].path,fn);
  514.     f=fopen(s,"r");
  515.   }
  516.   if (!f) {
  517.     outstr(fn);
  518.     pl(": not found.");
  519.   } else {
  520.     while (ok && fgets(s,250,f)) {
  521.       if (s[0]<=32)
  522.         continue;
  523.       ok1=0;
  524.       fn1=strtok(s," \t\n");
  525.       if (fn1) {
  526.         ok1=1;
  527.         for (i=0; ok1 && (i<type); i++)
  528.           if (strtok(NULL," \t\n")==NULL)
  529.             ok1=0;
  530.         if (ok1) {
  531.           desc=strtok(NULL,"\n");
  532.           if (!desc)
  533.             ok1=0;
  534.         }
  535.       }
  536.       if (ok1) {
  537.         while ((*desc==' ') || (*desc=='\t'))
  538.           ++desc;
  539.         ok=maybe_upload(fn1,dn,desc);
  540.         checka(&abort,&next);
  541.         if (abort)
  542.           ok=0;
  543.       }
  544.     }
  545.     fclose(f);
  546.   }
  547.  
  548.   closedl();
  549. }
  550.  
  551. int uploadall(int dn)
  552. {
  553.   int i,i1,f1,maxf,ok;
  554.   char s[81],s1[81];
  555.   struct ffblk ff;
  556.  
  557.   dliscan1(udir[dn].subnum);
  558.   nl();
  559.   nl();
  560.   strcpy(s,"*.*");
  561.   strcpy(s1,(directories[udir[dn].subnum].path));
  562.   maxf=directories[udir[dn].subnum].maxfiles;
  563.   strcat(s1,s);
  564.   f1=findfirst(s1,&ff,0);
  565.   ok=1;
  566.   i1=0;
  567.   while ((f1==0) && (!hangup) && (numf<maxf) && (ok) && (!i1)) {
  568.     ok=maybe_upload(ff.ff_name,dn,NULL);
  569.     f1=findnext(&ff);
  570.   }
  571.   closedl();
  572.   if (!ok)
  573.     pl(get_string(14));
  574.   if (numf>=maxf)
  575.     pl(get_string(839));
  576.   return(i1);
  577. }
  578.  
  579.  
  580.