home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / amiexpress / source / utils / user / acpuser.c next >
Encoding:
C/C++ Source or Header  |  1992-12-26  |  8.1 KB  |  389 lines

  1. #include <exec/types.h>
  2. #include <exec/exec.h>
  3. #include <functions.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "dh0:code/code20/defines.h"
  7. struct User U;
  8. struct UserKeys K;
  9. struct User P;
  10. char FileName[200];
  11. char Keys[200];
  12. char temp[200];
  13. char Ktemp[200];
  14. int TLock(char *str);
  15. void sr(char *str);
  16. void end(void);
  17. int GetUN(char *SA);
  18. int strnicmp(char *str1,char *str2,int len);
  19. static struct FileLock      *pdir = NULL;
  20. static struct FileInfoBlock *dir_info = NULL;
  21. struct Commands cfg;
  22. struct ConfList
  23. {
  24.   struct ConfList *Next;
  25.   char image[80];
  26. } *CList=NULL,*CFirst=NULL;
  27. struct PSTR
  28. {
  29.   int s1;
  30.   int s2;
  31.   int ns;
  32.   int ne;
  33. };
  34. #define ACCESS_READ -2
  35. char BBSloc[200];
  36. void free_pdir();
  37. void LookFor(char *Part,int old,int new);
  38. void CheckPartUpload(int old,int new);
  39. void AcpError();
  40. void FreeConf();
  41. struct ConfList *AllocConf();
  42. main(int argc,char *argv[])
  43. {
  44.    int i,j;
  45.    struct PSTR t;
  46.    FILE *fi,*fo,*fo2;
  47.    char image[200];
  48.    char ConfName[200];
  49.    j=0;
  50.    dir_info=(struct FileInfoBlock *)AllocMem((long)sizeof(struct FileInfoBlock),MEMF_PUBLIC|MEMF_CLEAR);
  51.    if(dir_info==NULL)
  52.    {
  53.      printf("Can't allocate memory for directory tree searches\n");
  54.      end();
  55.    } 
  56.    if(argc!=2)
  57.    {
  58.      AcpError();
  59.    }
  60.    printf("\n\n");
  61.    printf("SalvUser version 1.2, written by Joseph Hodge\n");
  62.    strcpy(FileName,argv[1]); sr(FileName);
  63.    strupr(FileName);
  64.    if(!strcmp(FileName,"ACP"))
  65.    {
  66.      strcpy(FileName,"s:acp.startup");
  67.      if(!TLock(FileName))
  68.      {
  69.        AcpError();
  70.      }
  71.      strcpy(BBSloc,"");
  72.      fi=fopen(FileName,"r");
  73.        while(fgets(image,90,fi)!=NULL)
  74.        {
  75.          sr(image);
  76.          if(ParseImage(image,&t))
  77.          {
  78.            if(!strnicmp(&image[t.s1],"BBS_LOCATION",12))
  79.            {
  80.              strcpy(BBSloc,&image[t.s2]); continue;
  81.            }
  82.            if(!strnicmp(&image[t.s1],"CONF_LOCAL",10))
  83.            {
  84.               strcpy(ConfName,&image[t.s2+3]);
  85.               if(ConfName[strlen(ConfName)-1]=='/')
  86.                  ConfName[strlen(ConfName)-1]='\0';
  87.               if(TLock(ConfName))
  88.               {
  89.                 CList=(struct ConfList *)AllocConf();
  90.                 if(CList==NULL)
  91.                 {
  92.                   printf("Can't Allocate memory for Conference List\n");
  93.                   FreeConf();
  94.                   end();
  95.                 }
  96.                 strcpy(CList->image,ConfName);
  97.               }
  98.               continue;
  99.            }
  100.          }
  101.        }
  102.        fclose(fi);
  103.    }
  104.    else
  105.    {
  106.      if(!TLock(FileName))
  107.      {
  108.        printf("Can't locate %s\n",FileName);
  109.        AcpError();
  110.      }
  111.      fi=fopen(FileName,"rb");
  112.      fread((APTR)&cfg,sizeof(struct Commands),1,fi);
  113.      fclose(fi);
  114.      for(i=0;i<9;i++)
  115.      {
  116.         strcpy(ConfName,cfg.ConfLoc[i]);
  117.         if(ConfName[0]!='\0')
  118.         {
  119.           if(ConfName[strlen(ConfName)-1]=='/')
  120.              ConfName[strlen(ConfName)-1]='\0';
  121.           if(TLock(ConfName))
  122.           {
  123.             CList=(struct ConfList *)AllocConf();
  124.             if(CList==NULL)
  125.             {
  126.               printf("Can't Allocate memory for Conference List\n");
  127.               FreeConf();
  128.               end();
  129.             }
  130.             strcpy(CList->image,ConfName);
  131.           }
  132.         }
  133.       }
  134.       strcpy(BBSloc,cfg.BBSLoc);
  135.    }
  136.    if(BBSloc[0]=='\0')
  137.    {
  138.      printf("Can't find BBS Location\n");
  139.      end();
  140.    }
  141.    if(CFirst==NULL)
  142.    {
  143.      printf("Can't find Conference locations\n");
  144.      end();
  145.    }
  146.    sprintf(FileName,"%sUser.Data",BBSloc);
  147.    sprintf(Keys,"%sUser.Keys",BBSloc);
  148.    sr(FileName);
  149.    if(!TLock(FileName))
  150.    {
  151.      printf("Can't locate %s\n",FileName);
  152.      printf("\n\n");
  153.      end();
  154.    } 
  155.    sprintf(temp,"%s.old",FileName);
  156.    if(TLock(temp))
  157.    {
  158.      DeleteFile(temp);
  159.    }
  160.    Rename(FileName,temp);
  161.    if(TLock(Keys))
  162.    {
  163.      sprintf(Ktemp,"%s.old",Keys);
  164.      if(TLock(Ktemp))
  165.      {
  166.        DeleteFile(Ktemp);
  167.      }
  168.      Rename(Keys,Ktemp);
  169.    }
  170.    printf("\n");
  171.    printf("Original file %s has been backed up as %s\n",FileName,temp);
  172.    printf("Original file %s has been backed up as %s\n",Keys,Ktemp);
  173.    printf("\n");
  174.  
  175.    i=0;
  176.    fi=fopen(temp,"rb");
  177.    fo2=fopen(Keys,"ab");
  178.    fo=fopen(FileName,"wb");
  179.    printf("\n");
  180.    while(fread((APTR)&U,sizeof(struct User),1,fi)!=NULL)
  181.    {
  182.       
  183.        if(U.Slot_Number>0)
  184.        { j +=1;
  185.          if(U.Slot_Number!=j)
  186.          {
  187.            CheckPartUpload(U.Slot_Number,j);
  188.          }
  189.          U.Slot_Number=j;
  190.         
  191.        fwrite((APTR)&U,sizeof(struct User),1,fo); 
  192.        printf("ASalvaging: %3d->%3d >%-35.35s \n",i+1,j,U.Name);
  193.        
  194.        
  195.      strcpy(K.UserName,U.Name);
  196.       K.Number=(long)(U.Slot_Number);
  197.      K.New_User=U.New_User;
  198.      strcpy(K.Pad,"");
  199.      fwrite((APTR)&K,sizeof(struct UserKeys),1,fo2);
  200.      }
  201.   else printf("APacked   : %3d      >%-35.35s\n",i+1,U.Name);
  202.      i +=1;
  203.    } 
  204.    printf("\n");
  205.    fclose(fi);
  206.    fclose(fo);
  207.    fclose(fo2);
  208.    printf("Removed %d Inactive Accounts out of %d Accounts\n",i-j,i);
  209.    printf("Packed %5.2f%% of User DataBase\n",(float)(((float)(i-j)/(float)i))*100.00);
  210.    end();
  211. }
  212. void sr(char *str)
  213. {
  214.   register int i;
  215.   i=strlen(str)-1;
  216.   while(i>-1)
  217.   {
  218.     if(*(str+i)<=32) *(str+i)='\0'; else break;
  219.     i--; 
  220.   }
  221. }
  222. int TLock(char *str)
  223. {
  224.   long lock;
  225.   if(lock=Lock(str,ACCESS_READ))
  226.   {
  227.     UnLock(lock); return(1);
  228.   }
  229.   
  230.   return(0);
  231. }
  232.  
  233. void end(void)
  234. {
  235.   char temp[255];
  236.   free_pdir();
  237.   if(dir_info)
  238.   {
  239.      FreeMem(dir_info,(long)sizeof(struct FileInfoBlock));
  240.   }
  241.   if(CFirst) FreeConf();
  242. printf("Press [return] to continue\n");
  243. gets(temp);
  244.   exit(0);
  245. }
  246. void free_pdir()
  247. {
  248.    if ( pdir )
  249.       {
  250.       UnLock((BPTR)pdir);
  251.       pdir=NULL;
  252.        }
  253.    return;   
  254. }
  255. struct ConfList *AllocConf()
  256. {
  257.   struct ConfList *p;
  258.   
  259.   p=(struct ConfList *)AllocMem(sizeof(struct ConfList),MEMF_PUBLIC|MEMF_CLEAR);
  260.   if(p)
  261.   {
  262.     if(CList)CList->Next=(struct ConfList *)p;
  263.     p->Next=NULL;
  264.     if(!CFirst) CFirst=(struct ConfList *)p;
  265.   }
  266.   return((struct ConfList *)p);
  267. }
  268.  
  269. void FreeConf()
  270. {
  271.   struct ConfList *p,*t;
  272.   for(p=CFirst;p!=NULL;p=t)
  273.   {
  274.     t=p->Next;
  275.     FreeMem((struct ConfList *)p,sizeof(struct ConfList));
  276.     p=t;
  277.   }
  278.   CFirst=NULL;
  279.   CList=NULL;
  280. }
  281. void AcpError()
  282.    {
  283.       printf("SalvUser version 1.2, written by Joseph Hodge\n");
  284.       printf("compatible with AmiExpress 1.1x+\n");
  285.       printf("Usage: SalvUser <ACP|configpath>\n");
  286.       printf("   ie: SalvUser ACP\n");
  287.       printf("       SalvUser BBS:Config1\n");
  288.       printf("\n");
  289.       end();
  290.    }
  291.  
  292. void CheckPartUpload(int old,int new)
  293. {
  294.    char key[20];
  295.    char image[200];
  296.    struct ConfList *p;
  297.    sprintf(key,"@%d",old);
  298.    p=CFirst;
  299.    for(p=CFirst;p!=NULL;p=p->Next)
  300.    {
  301.     
  302.       LookFor(p->image,old,new);
  303.    }
  304. }
  305. void LookFor(char *Part,int old,int new)
  306. {
  307.   REGISTER int i=0;
  308.   REGISTER int j=0;
  309.   char ch;
  310.   char image[200];
  311.   char t[200];
  312.   char name[200];
  313.   char p[200];
  314.   char PartUpload[200];
  315.   int breakhard=1;
  316.   sprintf(PartUpload,"%s/PartUpload",Part);
  317.   while(breakhard)
  318.   {
  319.     free_pdir();
  320.     if(!(pdir=(struct FileLock *)Lock(PartUpload,(ULONG)ACCESS_READ)))
  321.     {
  322.       free_pdir();
  323.       return;
  324.     }
  325.     if(!Examine((BPTR)pdir,dir_info))
  326.     { 
  327.       free_pdir();
  328.       return;
  329.     }
  330.     breakhard=0;
  331.     while(ExNext((BPTR)pdir,dir_info))
  332.     {
  333.       strcpy(name,dir_info->fib_FileName);
  334.       if(GetUN(name)==old)
  335.       {
  336.         sprintf(image,"%s/PartUpload/%s",Part,name);
  337.         i=0;
  338.         while(1)
  339.         {
  340.           if(image[i]==NULL) break;
  341.           if(image[i]=='@') {image[i]='\0'; break; }
  342.           i++;
  343.         }
  344.         free_pdir();
  345.         sprintf(t,"%s@%d",image,new);
  346.         sprintf(p,"%s@%d",image,old);
  347.         if(TLock(t)) DeleteFile(t);
  348.         Rename(p,t);
  349.         breakhard=1;
  350.         printf("Renaming %s\n",p);
  351.         printf("         %s\n\n",t);
  352.         break;
  353.       }
  354.         
  355.       
  356.     }
  357.   }
  358. }
  359.  
  360. int strnicmp(char *str1,char *str2,int len)
  361. {
  362.   register int i;
  363.   strupr(str1);
  364.   strupr(str2);
  365.   i=0;
  366.   while(i<len)
  367.   {
  368.     if(*(str1+i)!=*(str2+i)) return(1);
  369.     i++;
  370.   }
  371.   return(0);
  372. }
  373. int GetUN(char *SA)
  374. {
  375. int x,N;
  376.  
  377. for(x=0; x<strlen(SA); x++)
  378.     {
  379.     if(SA[x]=='@')
  380.         {
  381.         strcat(SA," ");
  382.         N=(int)atol(SA+x+1);
  383.         SA[x]='\00';
  384.         return(N);
  385.         }
  386.     }
  387. return(0);
  388. }
  389.