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

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