home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / UTIL / WWIVE / XFEROVL.C < prev    next >
Text File  |  1991-12-26  |  13KB  |  564 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1991 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()
  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(1,"Filename to move3: ");
  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("6File not found.");
  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(1,"Move this 5(3Y/N/Q5)1? ");
  60.     ch=onek("QNY");
  61.     if (ch=='Q')
  62.       done=1;
  63.     if (ch=='Y') {
  64.       sprintf(s1,"%s%s",directories[udir[curdir].subnum].path,u.filename);
  65.       do {
  66.         nl();
  67.         nl();
  68.     prt(1,"To which directory3? ");
  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<64) && (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("6Filename already in use in that directory.");
  87.     }
  88.     if (numf>=directories[d1].maxfiles) {
  89.       ok=0;
  90.       nl();
  91.       pl("1Too many files in that directory3.");
  92.     }
  93.     if (freek1(directories[d1].path)<((double)(u.numbytes/1024L)+3)) {
  94.       ok=0;
  95.       nl();
  96.       pl("1Not enough disk space to move it3.");
  97.     }
  98.     closedl();
  99.     dliscan();
  100.       } else
  101.         ok=0;
  102.     } else
  103.       ok=0;
  104.     if (ok) {
  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("7File moved1.");
  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<64) && (udir[i].subnum!=-1) && (!kbhitb()); i++) {
  263.     nl();
  264.     ansic(1);
  265.     npr("1Sorting3 %s.\r\n",directories[udir[i].subnum].name);
  266.     sortdir(i,type);
  267.   }
  268. }
  269.  
  270.  
  271. void rename_file()
  272. {
  273.   char s[81],s1[81],s2[81],*ss,s3[81],ch;
  274.   int i,cp;
  275.   uploadsrec u;
  276.  
  277.   nl();
  278.   nl();
  279.   prt(1,"File to rename3: ");
  280.   input(s,12);
  281.   if (s[0]==0)
  282.     return;
  283.   if (strchr(s,'.')==NULL)
  284.     strcat(s,".*");
  285.   align(s);
  286.   dliscan();
  287.   nl();
  288.   strcpy(s3,s);
  289.   i=recno(s);
  290.   while (i>0) {
  291.     cp=i;
  292.     SETREC(i);
  293.     read(dlf,(void *)&u,sizeof(uploadsrec));
  294.     nl();
  295.     printfileinfo(&u,udir[curdir].subnum);
  296.     nl();
  297.     prt(1,"Change info for this file 5(3Y/N/Q5)1? ");
  298.     ch=onek("QNY");
  299.     if (ch=='Q')
  300.       break;
  301.     else if (ch=='N') {
  302.       i=nrecno(s3,cp);
  303.       continue;
  304.     }
  305.     nl();
  306.     prt(1,"New filename3? ");
  307.     input(s,12);
  308.     if (!okfn(s))
  309.       s[0]=0;
  310.     if (s[0]) {
  311.       align(s);
  312.       if (strcmp(s,"        .   ")) {
  313.         strcpy(s1,directories[udir[curdir].subnum].path);
  314.         strcpy(s2,s1);
  315.         strcat(s1,s);
  316.         if (exist(s1))
  317.       pl("1Filename already in use3; 1not changed3.");
  318.     else {
  319.       strcat(s2,u.filename);
  320.       rename(s2,s1);
  321.       if (exist(s1)) {
  322.         ss=read_extended_description(u.filename);
  323.         if (ss) {
  324.           delete_extended_description(u.filename);
  325.           add_extended_description(s,ss);
  326.           farfree(ss);
  327.         }
  328.         strcpy(u.filename,s);
  329.       } else
  330.         pl("Bad filename.");
  331.     }
  332.       }
  333.     }
  334.     nl();
  335.     pl("1New description3:");
  336.     prt(1,": ");
  337.     inputl(s,58);
  338.     if (s[0]) {
  339.       strcpy(u.description,s);
  340.     }
  341.     ss=read_extended_description(u.filename);
  342.     nl();
  343.     nl();
  344.     prt(1,"Modify extended description3? ");
  345.     if (yn()) {
  346.       nl();
  347.       if (ss) {
  348.     prt(1,"Delete it3? ");
  349.     if (yn()) {
  350.       farfree(ss);
  351.       delete_extended_description(u.filename);
  352.       u.mask &= ~mask_extended;
  353.     } else {
  354.       u.mask |= mask_extended;
  355.       modify_extended_description(&ss);
  356.       if (ss) {
  357.         delete_extended_description(u.filename);
  358.         add_extended_description(u.filename,ss);
  359.         farfree(ss);
  360.       }
  361.     }
  362.       } else {
  363.         modify_extended_description(&ss);
  364.         if (ss) {
  365.           add_extended_description(u.filename,ss);
  366.           farfree(ss);
  367.           u.mask |= mask_extended;
  368.         } else
  369.           u.mask &= ~mask_extended;
  370.       }
  371.     } else
  372.       if (ss) {
  373.         farfree(ss);
  374.         u.mask |= mask_extended;
  375.       } else
  376.         u.mask &= ~mask_extended;
  377.     SETREC(i);
  378.     write(dlf,(void *)&u,sizeof(uploadsrec));
  379.     i=nrecno(s3,cp);
  380.   }
  381.   closedl();
  382. }
  383.  
  384.  
  385. int upload_file(char *fn, int dn, char *desc)
  386. {
  387.   directoryrec d;
  388.   uploadsrec u,u1;
  389.   int i,i1,i2,ok,f;
  390.   char s[81],s1[81],ff[81];
  391.   long l;
  392.   double ti;
  393.  
  394.   d=directories[dn];
  395.   strcpy(s,fn);
  396.   align(s);
  397.   strcpy(u.filename,s);
  398.   u.ownerusr=usernum;
  399.   u.ownersys=0;
  400.   u.numdloads=0;
  401.   u.filetype=0;
  402.   u.mask=0;
  403.   if (check_ul_event(dn,&u)) {
  404.     npr("File '%s' deleted by upload event.\r\n",fn);
  405.   } else {
  406.     sprintf(ff,"%s%s",d.path,s);
  407.     f=open(ff,O_RDONLY | O_BINARY);
  408.     if (f<=0) {
  409.       if (desc && (*desc)) {
  410.         npr("ERR: %s: %s\r\n",fn,desc);
  411.       } else {
  412.         npr("File '%s' doesn't exist.\r\n",fn);
  413.       }
  414.       return(1);
  415.     }
  416.     l=filelength(f);
  417.     u.numbytes=l;
  418.     close(f);
  419.     strcpy(u.upby,nam1(&thisuser,usernum,syscfg.systemnumber));
  420.     strcpy(u.date,date());
  421.     if (d.mask & mask_PD)
  422.       d.mask=mask_PD;
  423.     npr("%s: %4ldk :",u.filename,((u.numbytes)+1023)/1024);
  424.     if ((desc) && (*desc)) {
  425.       strncpy(u.description,desc,58);
  426.       u.description[58]=0;
  427.       pl(u.description);
  428.     } else
  429.       inputl(u.description,58);
  430.     if (u.description[0]==0)
  431.       return(0);
  432.     ++thisuser.uploaded;
  433.     thisuser.uk += ((l+1023)/1024);
  434.     time(&l);
  435.     u.daten=l;
  436.     for (i=numf; i>=1; i--) {
  437.       SETREC(i);
  438.       read(dlf,(void *)&u1,sizeof(uploadsrec));
  439.       SETREC(i+1);
  440.       write(dlf,(void *)&u1,sizeof(uploadsrec));
  441.     }
  442.     SETREC(1);
  443.     write(dlf,(void *)&u,sizeof(uploadsrec));
  444.     ++numf;
  445.     SETREC(0);
  446.     read(dlf, &u1, sizeof(uploadsrec));
  447.     u1.numbytes=numf;
  448.     u1.daten=l;
  449.     dir_dates[dn]=l;
  450.     SETREC(0);
  451.     write(dlf,(void *)&u1,sizeof(uploadsrec));
  452.     ++status.uptoday;
  453.     save_status();
  454.     sprintf(s,"+%s uploaded on %s",u.filename,d.name);
  455.     sysoplog(s);
  456.   }
  457.   return(1);
  458. }
  459.  
  460.  
  461. int maybe_upload(char *fn, int dn, char *desc)
  462. {
  463.   char s[81];
  464.   int i,i1=0,ok=1;
  465.   uploadsrec u;
  466.  
  467.   strcpy(s,fn);
  468.   align(s);
  469.   i=recno(s);
  470.  
  471.   if (i==-1) {
  472.     if (!upload_file(s,dn,desc))
  473.       ok=0;
  474.   } else {
  475.     SETREC(i);
  476.     read(dlf,(void *)&u, sizeof(uploadsrec));
  477.     printinfo(&u,&i1);
  478.     if (i1)
  479.       ok=0;
  480.   }
  481.   return(ok);
  482. }
  483.  
  484. void upload_files(char *fn, int dn, int type)
  485. /* This assumes the file holds listings of files, one per line, to be
  486.  * uploaded.  The first word (delimited by space/tab) must be the filename.
  487.  * after the filename are optional tab/space separated words (such as file
  488.  * size or date/time).  After the optional words is the description, which
  489.  * is from that position to the end of the line.  the "type" parameter gives
  490.  * the number of optional words between the filename and description.
  491.  * the optional words (size, date/time) are ignored completely.
  492.  */
  493. {
  494.   char s[255],*fn1,*desc;
  495.   FILE *f;
  496.   int ok=1,abort=0,next=0,ok1,i;
  497.  
  498.   dliscan1(dn);
  499.  
  500.   f=fopen(fn,"r");
  501.   if (!f) {
  502.     npr("\r\n1File3 '%s'1 not found.\r\n\r\n",fn);
  503.   } else {
  504.     while (ok && fgets(s,250,f)) {
  505.       if ((s[0]<=32) || (s[0]>127))
  506.         continue;
  507.       ok1=0;
  508.       fn1=strtok(s," \t\n");
  509.       if (fn1) {
  510.         ok1=1;
  511.         for (i=0; ok1 && (i<type); i++)
  512.           if (strtok(NULL," \t\n")==NULL)
  513.             ok1=0;
  514.         if (ok1) {
  515.           desc=strtok(NULL,"\n");
  516.           if (!desc)
  517.             ok1=0;
  518.         }
  519.       }
  520.       if (ok1) {
  521.         while ((*desc==' ') || (*desc=='\t'))
  522.           ++desc;
  523.         ok=maybe_upload(fn1,dn,desc);
  524.         checka(&abort,&next);
  525.         if (abort)
  526.           ok=0;
  527.       }
  528.     }
  529.     fclose(f);
  530.   }
  531.  
  532.   closedl();
  533. }
  534.  
  535. int uploadall(int dn)
  536. {
  537.   int i,i1,f1,maxf,ok;
  538.   char s[81],s1[81];
  539.   struct ffblk ff;
  540.  
  541.   dliscan1(dn);
  542.   nl();
  543.   nl();
  544.   strcpy(s,"*.*");
  545.   strcpy(s1,(directories[dn].path));
  546.   maxf=directories[dn].maxfiles;
  547.   strcat(s1,s);
  548.   f1=findfirst(s1,&ff,0);
  549.   ok=1;
  550.   i1=0;
  551.   while ((f1==0) && (!hangup) && (numf<maxf) && (ok) && (!i1)) {
  552.     ok=maybe_upload(ff.ff_name,dn,NULL);
  553.     f1=findnext(&ff);
  554.   }
  555.   closedl();
  556.   if (!ok)
  557.     pl("6Aborted.");
  558.   if (numf>=maxf)
  559.     pl("directory full.");
  560.   return(i1);
  561. }
  562.  
  563.  
  564.