#include "bbs.h"
#include <utility/tagitem.h>
#include <dos/dostags.h>

extern char *SkipdFiles;
extern char WorkStr[];
int AEGoodFile;
int rzmsg=0;
extern char prototitle[];
static int Test_File(char *str,char *path);
static void DisplayOutPutofTest(void)
{
 sprintf(GSTR3,"%sWORK/OutPut_Of_Test",NodeStr);
 (void)MenuSend(GSTR3);
}
int checksym;
//FILE *fr = NL ,*fw = NL;
BOOL LocalUpload=0;
extern int XIMPort;
/* temp = extension */
struct TagItem filetags[]={ {SYS_Input,0},{SYS_Output,0},{NP_StackSize,0},{NP_Priority,0},{TAG_DONE,0}};

static int CheckFileExternal(int *stat,char *temp,char *CheckFile)
{
   BPTR fi;
   FILE *fi1;
   struct DiskObject *dobj;
   char **tooltypes;
   char *s;
   int i;
   char FileName[256],Options[256], image[256];

   /* what happens if we look for this file but the dir is non-existant ? */
   sprintf(FileName,"%sFCheck/%s",Cmds->BBSLoc,temp);
   
   dobj=GetDiskObject(FileName);
   if(dobj==NULL) return(0);
   *(stat)=SUCCESS;
   tooltypes=dobj->do_ToolTypes;
   if(s=FindToolType(tooltypes,"CHECKER"))
   {
      strcpy(Options,s);
   }
   else return(0);
   if(s=FindToolType(tooltypes,"OPTIONS"))
   {
     strcat(Options," "); strcat(Options,s);
   }
   if(s=FindToolType(tooltypes,"STACK"))
   {
     filetags[2].ti_Data=(ULONG)atol(s);
   }else filetags[2].ti_Data=4096L;
   if(s=FindToolType(tooltypes,"PRIORITY"))
   {
     filetags[3].ti_Data=(ULONG)atoi(s);
   }
//(RTS)
   strcat(Options," ");strcat(Options,CheckFile);
   sprintf(FileName,"%sOutPut_Of_Test",WorkStr);
   if((fi=Open(FileName,MODE_NEWFILE))!=NULL)
   {
     filetags[1].ti_Data=fi;
     SystemTagList(Options,filetags);
     Close(fi);
     DisplayOutPutofTest();
     sprintf(Options,"%sOutPut_Of_Test",WorkStr);
     i=1; sprintf(FileName,"ERROR.%d",i++);
     while(s=FindToolType(tooltypes,FileName))
     {
        fi1=fopen(Options,"r");
        while(fgets(image,80,fi1)!=NULL)
        {
          if(strstr(image,s)){ *(stat)=FAILURE; break; }
        }
        if(*(stat)==FAILURE){ fclose(fi1); break;}
        fclose(fi1);
        sprintf(FileName,"ERROR.%d",i++);
     }
     DeleteFile(Options);
   }else { FreeDiskObject(dobj); return(0); }
   FreeDiskObject(dobj);
   return(1);
}

static int Test_File(char *str,char *path)
{
 int x,x2,stat;
 char temp[100],temp2[100];
 char temp3[100],temp4[100];
 char Options[200];
 strcpy(temp,str);
 x=0;

sprintf(temp3,"%scommands/syscmd/",Cmds->BBSLoc);
 do  {
 if(temp[x]=='.') {
     x2=0;
     do {
         temp2[x2]=temp[x+x2+1];
         x2+=1;
     }  while(temp[x+x2+1]!='\0');
     temp2[x2]='\0';
     (void)strupr(temp2);
     if(strlen(temp2)==3) {
         strcpy(Options,path);
         stat=NOTALLOWED;AEGoodFile=NOTALLOWED;
          sprintf(temp4,"FILECHECK %s",temp);
          if(!CustomCommand(temp3,temp4,1))
          {
            AEPutStr("Testing ...");
            AEPutStr(FilePart(str));
            AEPutStr("\r\n");

          strcat(Options,temp);
          CheckFileExternal(&stat,temp2,Options);
          }
          else
          {
             stat=AEGoodFile;
             if(stat==NOTTESTED)
             { 
               stat=NOTALLOWED;
              strcat(Options,temp);
             CheckFileExternal(&stat,temp2,Options);
             } 
         }
         return(stat);
     }             /* end if strlen */
 }                 /* end if temp */
 x+=1;
 } while(x<strlen(temp));        /* end do */
 return(NOTALLOWED);
}

extern int NumSkipd,zresume;
extern BYTE BeenUDd;
extern char RecFileNames[];
extern long Time_limit;

int PGoodbye(void);


extern struct myst my_struct;

static long RFreeSpace(char *path)   /* get free space on drives */
{
 BPTR FLock;
 struct InfoData *i_data;

 long stat=0;

 if(i_data=(struct InfoData *)AllocMem((long)sizeof(struct InfoData),MEMF_CHIP)) {
     if(FLock = Lock(path,ACCESS_READ)) {
         if(stat=Info(FLock,i_data))
             stat=(i_data->id_NumBlocks*i_data->id_BytesPerBlock)-(i_data->id_NumBlocksUsed*i_data->id_BytesPerBlock);
         else {
             sprintf(GSTR3,"\r\nCan not get info from %s for free space\r\n",path);
             AEPutStr(GSTR3);
         }
         UnLock(FLock);
     } else {
         sprintf(GSTR3,"\r\nCan not find free space for %s\r\n",path);
         AEPutStr(GSTR3);
     }
     FreeMem(i_data,sizeof(struct InfoData));
 } else
     MyError(0);

 return(stat);
}

static long FreeDiskSpace(void)
{
 char string[200],path[100];
 struct DiskObject *dobj;
 char **oldtooltypes;
 char *s;
 long TFS,FSU;
 int drivenum=1;

 sprintf(string,"%sDRIVES",Cmds->BBSLoc);

 TFS=0;
 if(dobj=GetDiskObject(string))
 {
    oldtooltypes=dobj->do_ToolTypes;
    sprintf(path,"DRIVE.%d",drivenum++);
    while(s=FindToolType(oldtooltypes,path))
    {
       strcpy(path,s);
       FSU=RFreeSpace(path);
         TFS+=FSU;
       sprintf(path,"DRIVE.%d",drivenum++);
    }
    FreeDiskObject(dobj);
 }
 else
 {   sprintf(GSTR3,"\r\nThe file %s.info is missing!!!\r\n\r\n",string);
     AEPutStr(GSTR3);
     return(FAILURE);
 }

return(TFS);
}

/* Clean up the work Directory */

static void CleanItUp(void)
{
 BPTR FLock;
 struct FileInfoBlock *Fib;
 register int i;                  /* 11w add */
 char *s;
 int cnt = 0;

 strcpy(GSTR3,WorkStr);
 i=strlen(GSTR3)-1;
 while(i) { if(GSTR3[i]<=' ' || GSTR3[i]=='/') GSTR3[i]='\0'; else break;
 i--;}

  if((Fib =(struct FileInfoBlock *) AllocDosObject(DOS_FIB,NULL)) == NL) {
     MyError(0);     return;
 }
   /* lock the directory (Playpen or RamPen */
 if((FLock = Lock(GSTR3,ACCESS_READ)) == 0) {
     MyError(8);     goto ef;
 }

 if((Examine(FLock,Fib)) == 0) {
     MyError(6);     goto ef;
 }

 if(Fib->fib_DirEntryType > 0) {   /* make sure we locked a directory */
     while(ExNext(FLock,Fib)) {
         if(Fib->fib_DirEntryType < 0) {   /* found a file */
/* * * ERROR CHECKING * * */
             /* or we can change to &Fib->fib_FileName[0] */
             if(strlen(Fib->fib_FileName) == 0) {   /* check for filename > 0 length */
                 goto ef;
             }

             cnt = 0;
             s = Fib->fib_FileName;
             while(*s && cnt < 30) {     /* check for valid file name */
                 if(*s == ' ') {     /* check for spaces at beginning of filename */
                     goto ef;
                 }
                 if(*s == '/') {
                     goto ef;
                 }
                 if(!(isascii((int) *s))) {
                     goto ef;
                 }
                 cnt++;
                 s++;
             }

/* * * END ERROR CHECKING * * */


             sprintf(GSTR3,"%s%s",WorkStr,Fib->fib_FileName);   /* 11w */
             DeleteFile(GSTR3);
           

         } /* end if(Fib->fib_DirEntryType < 0) */

     }   /* end while(ExNext(FLock,Fib)) */

  }     /* end  if(Fib->fib_DirEntryType > 0) */

ef: ;
  if(FLock) UnLock(FLock);
  if(Fib) FreeDosObject(DOS_FIB,Fib);
}




static int UploadDesc(void)
{
 int status,x,count;
 char str[255],str2[255],odate[50],str3[200],str4[200],buff[255];
 FILE *udf;

 CleanItUp();
 AEPutStr("Batch UpLoading.....\r\n");
 AEPutStr("\r\nUnlimited files.  Blank Line to start transfer.\r\n");

 count=0;
 FOREVER {
     count+=1;
     sprintf(str,"\r\nFileName %d: ",count);
     AEPutStr(str);
     status=LineInput("",str,12,KEYBOARD_TIMEOUT);
     if(status<0)  return(status);
     if((str[0]=='A'||str[0]=='a')&&strlen(str)==1) {
         AEPutStr("\r\n");
         return(FAILURE);
     }
     if(strlen(str)==0)  {
         AEPutStr("\r\n");
         break;
     }
     status=Check_For_File(str);   /* is file online ?? */
     if(status==FAILURE) {
         AEPutStr("File Exists, or has a symbol (#?*).\r\n");
         count-=1;
         continue;
     }

     sprintf(str4,"%s%s",WorkStr,str);             /* 11w add */

     udf=fopen(str4,"r");
     if(udf!=NULL) {
         AEPutStr("You can't Upload a duplicate.\r\n");
         count-=1;
         fclose(udf);
         continue;
     }
     Time_system=time(NULL);
     sprintf(str2,"%s",ctime(&Time_system));
     Convert_CTime_To_Date(str2,odate,str3);

CINPAgain:
     sprintf(buff,"\r\nPlease enter a description, you only have %d lines.",my_struct.max_desclines);
     AEPutStr(buff);
/* renamed Spazm to sysops *///JOE 26-Jun
     AEPutStr("\r\nPress return alone to end.  Begin  with (/) to make upload 'Private' to Sysop.\r\n");
     AEPutStr("                                [--------------------------------------------]\r\n");
     sprintf(str2,"%-13s                   :",str);
     AEPutStr(str2);
     /*---11 w */
      /*-- end  11w */
     status=LineInput("",str2,44,KEYBOARD_TIMEOUT);
     if(status<0)  return(status);
     if(strlen(str2)==0) { count-=1; continue; }
     udf=fopen(str4,"w");
     if(udf==NULL) {
         MyError(7);
         //AEPutStr("Tell sysop the system can't open a file in the work dirs\r\n\r\n");
         return(FAILURE);
     }
     fprintf(udf,"%s\n",str2);
     x=0;
     do  {
         AEPutStr("                                :");
         status=LineInput("",str2,44,KEYBOARD_TIMEOUT);
         if(status<0) { fclose(udf); DeleteFile(str4); return(status); }
         if(strlen(str2)!=0)         fprintf(udf,"%s\n",str2);
         x+=1;
     } while(strlen(str2)!=0&&x<(my_struct.max_desclines-1));
     fclose(udf);

 }
 AEPutStr("Okay:   (Enter) to Start, (G)oodbye after transfer, (A)bort? ");

 do  {
     status=Check_Online_Status();
     if(status<0) return(status);
     status=ReadChar(KEYBOARD_TIMEOUT);
     if(status<(-1))  return(status);

     if((status==65) || (status==97)) {
         AEPutStr("Abort!\r\n\r\n");
         return(FAILURE); }
     if((status=='L' || status=='l') && XIMPort==CONSOLE_PORT) { 
        status=13; LocalUpload=1; break; }
 } while(((status!=13L)&&(status!=71L)&&(status!=103L)));

 if(status!=13)  {
     AEPutStr("Goodbye!\r\n\r\n");
     return(2);
 } else  AEPutStr("\r\n\r\n");
 return(1);
}


static long MoveFile(char *filename,long filesize)
{
long stat,err;
int pathnum;
int goodtogo=0;
 if(filesize == 0) {
     ErrorLog("(mf 518)\n");
     return(0);
 }

 filesize=filesize+16384;

     pathnum=1;
     sprintf(GSTR3,"ULPATH.%d",pathnum++);
     while(GetFromConf(CN-1,GSTR3,GSTR3)) {
         stat=RFreeSpace(GSTR3);
            if(Sopt->RamPen[0]!=NULL)      sprintf(GSTR1,"%s/%s",Sopt->RamPen,filename);
             else            sprintf(GSTR1,"%sPLAYPEN/%s",NodeStr,filename);
             sprintf(GSTR2,"%s%s",GSTR3,filename);
         if(Rename(GSTR1,GSTR2))
         {
           DeleteFile(GSTR1);
           sprintf(GSTR1,"\tUpload moved to %s\n",GSTR2);
           CallersLog(GSTR1);
           AEPutStr(" File Posted\r\n\r\n");
           return(1);
         }
         if(stat>filesize) {
             goodtogo=1;
             if(stat=FileCopy(GSTR1,GSTR2)) {
                 DeleteFile(GSTR1);
                 sprintf(GSTR1,"\tUpload moved to %s\n",GSTR2);
                 CallersLog(GSTR1);
                 AEPutStr(" File Posted\r\n\r\n");
                 return(1);
             }
         }
       sprintf(GSTR3,"ULPATH.%d",pathnum++);
     } 
    if(!goodtogo)
    {
    AEPutStr("WARNING!\r\nNO FREE SPACE on any path!  Moving to upload dir...");
    ErrorLog("WARNING!NO FREE SPACE on any path!  Moving to upload dir...\n");
    }
 
    AEPutStr("FAILURE!!!  unable to move file!\r\n\r\n");
    sprintf(GSTR1,"\tFAILURE!, unable to move file %s from PlayPen\n",filename);
    CallersLog(GSTR1);
     if(Sopt->RamPen[0]!=NULL)      sprintf(GSTR1,"%s/%s",Sopt->RamPen,filename);
             else            sprintf(GSTR1,"%sPLAYPEN/%s",NodeStr,filename);
    DeleteFile(GSTR1);
    return(0);
}

static void CleanPlayPen(void)
{
 int stat,err;
 BPTR FLock;
 struct FileInfoBlock *Fib;
 char *s;
 int cnt = 0;

 if(Sopt->RamPen[0]!=NULL) strcpy(GSTR1,Sopt->RamPen);
 else  sprintf(GSTR1,"%sPlaypen",NodeStr);

 if((Fib = (struct FileInfoBlock *)AllocDosObject(DOS_FIB,NULL)) == NL) {
     MyError(0);     return;
 }
   /* lock the directory (Playpen or RamPen */
 if((FLock = Lock(GSTR1,ACCESS_READ)) == 0) {
     MyError(8);     goto fx;
 }

 if((Examine(FLock,Fib)) == 0) {
     MyError(6);     goto fx;
 }

 if(Fib->fib_DirEntryType > 0) {   /* make sure we locked a directory */
     while(ExNext(FLock,Fib)) {
         if(Fib->fib_DirEntryType < 0) {   /* found a file */
             /* or we can change to &Fib->fib_FileName[0] */
             if(strlen(Fib->fib_FileName) == 0) {   /* check for filename > 0 length */
                 goto fx;
             }

             cnt = 0;
             s = Fib->fib_FileName;
             while(*s && cnt < 30) {     /* check for valid file name */
                 if(*s == ' ') {     /* check for spaces at beginning of filename */
                     goto fx;
                 }
                 if(*s == '/') {
                     goto fx;
                 }
                 if(!(isascii((int) *s))) {
                     goto fx;
                 }
                 cnt++;
                 s++;
             }

             /* get our copy to Filename */
             sprintf(GSTR2,"%sPartUpload/%s@%d",Conference_Location,Fib->fib_FileName,User.Slot_Number);
             if(Sopt->RamPen[0]!=NULL) {
                 sprintf(GSTR1,"%s/%s",Sopt->RamPen,Fib->fib_FileName);
                 do {
                     if(GSTR1[strlen(GSTR1)-1] == '/') {
                         goto fx;
                     }

                     /* again not error, only for testing */
                     /* check for valid copy */
                     stat=FileCopy(GSTR1,GSTR2);
                     if(!stat)   strcat(GSTR2,"_");

                 } while(!stat&&(strlen(GSTR2)<40));

             }          /* end if RamPen */
              else {    /* uploading to hdrive */
                 sprintf(GSTR1,"%sPlaypen/%s",NodeStr,Fib->fib_FileName);
                 stat=Rename(GSTR1,GSTR2);
                 err=IoErr();
                 if(!stat&&err==213) {
/* sx:  */           do {
                         if(GSTR1[strlen(GSTR1)-1] == '/') {
                             goto fx;
                         }
                         /* again not error, only for testing */
                         /* check for valid filename */
                         stat=FileCopy(GSTR1,GSTR2);
                         if(!stat) strcat(GSTR2,"_");
                         else  {
#ifdef RTS
//(RTS) Sat Jun  6 21:23:40 1992  .. set cause we never knew we lost carrier with a parcial
// upload, there for when we went back from upload_a_file we didnt return goodbye or lost_carrier

//                         part_upload = TRUE;
#endif
                         }
                     } while(!stat&&(strlen(GSTR2)<40));
                  }          /* end if(!stat&&err==213 */
             }              /* end else */
             if(stat)  DeleteFile(GSTR1);

         }                       /* end if(Fib->fib_DirEntryType < 0)  */
     }                           /* end while(ExNext(FLock,Fib))       */
 }                               /* end if(Fib->fib_DirEntryType > 0)  */

fx: ;
  if(FLock) UnLock(FLock);
  if(Fib) FreeDosObject(DOS_FIB,Fib);
}

static void SysopULStats(int holdflag)
{
 int num;
 char str[256],str2[256];
 FILE *ff;

 sprintf(str,"%sSysopStats/NumULs_%d",Cmds->BBSLoc,CN);
 if(holdflag)
     strcat(str,"HOLD");

 num=0;
 if((ff= fopen(str,"r"))) {
     fgets(str2,20,ff);
     num=(int)atol(str2);
     fclose(ff);
 }
 num+=1;
 ff=fopen(str,"w");
 if(ff==NULL)    return;
 fprintf(ff,"%d\n",num);
 fclose(ff);
}

static int ResumeStuff(long TFS,long FSUploading)
{
 int status,ch;
 char string[256],path[256],ray2[256],ray[256],str[256];
 struct FileInfoBlock *FBlock;
 BPTR FLock;
 long stat,err;

 strcpy(path,Conference_Location);
 strcat(path,"PartUpload/");

 if(!(FLock = Lock(path,ACCESS_READ))) {    //(RTS) replaced no err cking version below
    MyError(8);
    return(-1);
 }                                                     // was MEMF_CHIP
 if((FBlock=(struct FileInfoBlock *)AllocDosObject(DOS_FIB,NULL)) == NULL) {
     MyError(0); //(RTS)
     UnLock(FLock);
     return(-1);
 }

 if(!(Examine(FLock,FBlock))) {
      FreeDosObject(DOS_FIB,FBlock);
      UnLock(FLock);
      MyError(6);
      return(-1);
 }

//ADD THIS !!

 while ((ExNext(FLock,FBlock))!=0) {  //(RTS)
     strcpy(str,FBlock->fib_FileName);
     strcpy(ray2,str);
     status=GetUN(str);  /* returns user number from end of file name */
     if(status==User.Slot_Number) {
         sprintf(string,"Resume %-12s [%6ld] (Y/N)? ",str,FBlock->fib_Size);
         AEPutStr(string);
         FOREVER {
             ch=Check_Online_Status();
             if(ch<0) {
                 FreeDosObject(DOS_FIB,FBlock);
                 if(FLock) UnLock(FLock); //(RTS) Lock held
                 return(ch);
             }
             ch=ReadChar(KEYBOARD_TIMEOUT);
             if(ch==TIMEOUT) {
                 FreeDosObject(DOS_FIB,FBlock);
                 if(FLock) UnLock(FLock); //(RTS) Lock held
                 return(NO_CARRIER);
             }
             if(ch=='N' || ch=='n')  {
                 AEPutStr("No!   Delete (Y/N)? ");
                 PurgeLine();
                 FOREVER {
                     ch=ReadChar(KEYBOARD_TIMEOUT);
                     if(ch==TIMEOUT) {
                         FreeDosObject(DOS_FIB,FBlock);
                         if(FLock) UnLock(FLock); //(RTS) Lock held
                         return(NO_CARRIER);
                     }
                     if(Whence_The_Logon>=REMOTE_LOGON) {
                         status=CheckCarrier();
                         if(status==FALSE) {
                             FreeDosObject(DOS_FIB,FBlock);
                             if(FLock) UnLock(FLock); //(RTS) Lock held
                             return(NO_CARRIER);
                         }
                     }
                     if(ch=='N' || ch=='n')  {
                         AEPutStr("No!\r\n");
                         goto DirCont;
                     }
                     if(ch=='Y' || ch=='y')  break;
                     else  continue;
                 }        /* end first forever */
                 AEPutStr("Yes.. Deleted!\r\n");
                 strcpy(string,path);
                 strcat(string,ray2);
                 DeleteFile(string);
                 goto DirCont;
             }  /* end if ch == 'n */
             if(ch=='Y' || ch=='y')  break;
             else  continue;
         }   /* end 2nd forever */
         AEPutStr("Yes..\r\n");
         sprintf(string,"\r\nResuming upload: %s\r\n\r\n",str);
         AEPutStr(string);      /* filename */
         strcat(path,ray2);     /* conf_loc:filename/ */

         if(Sopt->RamPen[0]!=NULL) {
           strcpy(ray,Sopt->RamPen);  /* should be filename without @name */
           strcat(ray,"/");
           strcat(ray,str);    /* should be old partial file with @num on end */

           stat=FileCopy(path,ray);  /* path = filename +@num */

         } else {
             strcpy(ray,NodeStr);   /* Node0: etc. */
             strcat(ray,"Playpen/");
             strcat(ray,str);       /* filename */
             stat = FileCopy(path,ray);      /* rename file */

         }
         if(stat==1) DeleteFile(path);

         if(FBlock) FreeDosObject(DOS_FIB,FBlock);
         if(FLock) UnLock(FLock);
         return(1);
DirCont:
         ;
    }            /* if status & user name */
 }
 if(FBlock) FreeDosObject(DOS_FIB,FBlock);
 if(FLock)  UnLock(FLock);
 return(0);
}


int Zmodem_Receive(char *, int);

extern char  PurgeScanNM[];
extern long fsize;
static FILE *f;


int Upload_a_File(int ULFType)            // JOE
{
 struct FileInfoBlock *FBlock;
 BPTR FLock;
 int x,x2,x3,cnt,status,MoveToLCFILES,hold,cstat,NoF,lcfile;
 int status2,gstat;
 long peff,pcps,TFS,FSUploading;
 char path[256],str[255],Istr[255],str2[255];
 char list[1024],fmtstr[256];
 char odate[20],fcomment[256],scomment[9][50];
 char ray[256],ray2[256],temp[256],string[256];
 char buff[255];
 FILE *uaf;
 int FoundDupe=0;
 int mstat;      /* check for carrier. trying to stop upload guru from parcial upload */

  /* these two for testing asCII chars */
 int cnt1 = 0;
 char *s;

 Online_NFiles=0;
 NumSkipd=0;


 if(MaxDirs==0) {
     AEPutStr("\r\n");
     MyError(5);             //Sorry no files in conf
     return(FAILURE);
 }

 if(!ULFType)    {
     strcpy(GSTR1,Screen_Location);
     strcat(GSTR1,"UploadMsg");
     ChecktoDisplay(GSTR1,GSTR3,0,1);
 }

 TFS=FreeDiskSpace();                /* check free space */
 if(TFS==FAILURE)    return(0);

 if(Sopt->RamPen[0]!=NULL) sprintf(path,"%s/",Sopt->RamPen);
 else sprintf(path,"%sPlaypen/",NodeStr);

 FSUploading=RFreeSpace(path);

 if((FSUploading)<2000000L) {   /* Do we have 2 megs or free space ?? */
     MyError(9);            /* no free space */
     return(0);
 }
 if(!ULFType) {
     status=CommandSplit();
     if(status>1) {
         strcpy(string,Command[1]);
         SetProtocol(string);
     } else {
         GSTR1[0]='\0';
         SetProtocol(GSTR1);
     }
     switch(User.Protocol) {
         case 'Z':
             if(Sopt->RamPen[0]!=NULL)                     /* are we uploading to a diff device */
                 sprintf(buff,"%s UPLOADING to %s..\r\n",prototitle,Sopt->RamPen);
             else                        /* otherwise normal upload to playpen dir */
                 sprintf(buff,"%s UPLOADING....\r\n",prototitle);

             AEPutStr(buff);                              /* show it to the user */
             break;
     }
     sprintf(string,"%lu free bytes available for uploading.  %lu at one time.\r\n",TFS,FSUploading);
     AEPutStr(string);
     AEPutStr("Filename lengths above 12 are not allowed.\r\n\r\n");

     switch(User.Protocol) {
         case 'Z':
             zresume=ResumeStuff(TFS,FSUploading);
             if(zresume<0) return(zresume);
             if(zresume==0&&MAINMENU_Li[1]=='R'&&MAINMENU_Li[2]=='G') {
                 AEPutStr("\r\nThere are no more files to resume on.\r\n\r\n");
                 return(0);
             }
             if(zresume==0)  {
                 gstat=UploadDesc();
                 if(gstat<0) {
                     CleanItUp();
                     return(gstat);
                 }
             }
             break;
     }
 }


/* if user used 'rz' it never entered the above loop so gstat never got set */

 /* uploading to another device?? */
 if(Sopt->RamPen[0]!=NULL) sprintf(path,"%s/",Sopt->RamPen);
 else sprintf(path,"%sPlaypen/",NodeStr);

 DTBT=0;
 TBT=NULL;
 TTTM=NULL;
 TTEFF=NULL;
 TTCPS=NULL;

 if(!BeenUDd) {
     DisplayUserToCallersLog(1);
     BeenUDd=1;
 }

 switch(User.Protocol) {
     case 'Z':
         Zmodem_Receive(path,ULFType);     /* path of upload */
         break;
 }

 AEPutStr("\r\n\r\nFile Uploading Complete...\r\n");

 peff=NULL;
 pcps=NULL;
 if(Online_NFiles!=NULL) {
     peff=(TTEFF/Online_NFiles);
     pcps=(TTCPS/Online_NFiles);
 }

 if( (pcps > (long) User_keys.UPcps) || (User_keys.UPcps > 5000)) {
        User_keys.UPcps = (UWORD) pcps;

 }
 sprintf(string," %d file(s), %ldk bytes, %ld minute(s). %ld second(s), %ld cps, %ld %% efficiency."
     ,Online_NFiles,(TBT/1024L),(TTTM/60L),(TTTM%60L),pcps,peff);

 AEPutStr(string);
 AEPutStr("\r\n\r\n");

 strcpy(str,"\t");
 strcat(str,string);
 strcat(str,"\n");

 if(Online_NFiles>0) {
     (void)CallersLog(str);
     (void)UDLog(str);
 } else {
     (void)CallersLog("\tUpload Failed..\n");
     (void)UDLog("\tUpload Failed..\n");
 }

 peff=(((TTTM*3L)/2L)+60L);
 if(Online_NFiles<1)  peff=0L;


 if(NumSkipd>0)  {
     AEPutStr("The file(s) :\r\n");
     x=0;
     while(x<NumSkipd) {
         sprintf(str,"\t\t%s \r\n",SkipdFiles+(x*32));
         AEPutStr(str);
         sprintf(str,"\tSkipped %s\n",SkipdFiles+(x*32));
         CallersLog(str);
         UDLog(str);
         x+=1;
     }
     AEPutStr("\r\n\t\tSKIPPED.  They already exist or have symbols.\r\n");
 }

 sprintf(str,"Time increased by %d mins.\r\n\r\n",peff/60L);
 AEPutStr(str);

 Time_limit+=peff;    /* add time to user while online */

 /* dunno why cause we dont return shit */
 Check_Online_Status();    /* can return no carrier */

 if(Cmds->AcLvl[LVL_KEEP_UPLOAD_CREDIT]>0)        User.Time_Total+=(peff/2);

 MoveToLCFILES=0;
 hold=0;
 lcfile=0;
 strcpy(list,RecFileNames);
 if(Cmds->AcLvl[LVL_CAPITOLS_in_FILE]==1)       strupr(list);

 NoF=0;
 cnt=0;

 /* loop thru uploaded (ing) list of files & move to where they belong*/
/* this gets the list of files uploaded */

 for(x=0;x<strlen(list);++x) {
nx:  ;
     if(list[x]!=' ') {
         str[cnt]=list[x];
         ++cnt;
     } else {
         str[cnt]='\0';
         NoF+=1;
         MoveToLCFILES=0;
         hold=0;
         lcfile=0;
         if(NoF>Online_NFiles)    goto Eit;
        
         cnt=0;   /* reset to zero */

         if(strlen(str)>0) {
             if((strlen(str)>12)&&(MoveToLCFILES==NULL)) {
                 /* if we loose carrier here with a +++, it will show up as
                 the file name so check for carrier now */

                 sprintf(fmtstr,"%s is too long a name, please rename.\r\n\r\n",str);
                 AEPutStr(fmtstr);
                 AEPutStr("             [------------]");
INPAgain:
                 if(Whence_The_Logon>=REMOTE_LOGON) {
                     cstat=CheckCarrier();
                     if(cstat==FALSE) {
                         MoveToLCFILES=1;
                         goto CLGO;
                     }
                 }
                 AEPutStr("\r\nNew Filename: ");
                 status=LineInput("",Istr,12,KEYBOARD_TIMEOUT);
                 if(status<0) { MoveToLCFILES=1; ModemOffHook();}
                 if(strlen(Istr)==0)  goto INPAgain;

                 if( (Istr[0]=='R' && Istr[1]=='Z') && strlen(Istr) == 2) {
                      AEPutStr("\r\nRZ is an invalid name for a file\r\n");
                      goto INPAgain;
                 }

                 x2=0;
                 do {          /* CHECK THE STRING */
                     if(Istr[x2]==':' || Istr[x2]=='/' || Istr[x2]=='*' || Istr[x2]==' ' || \
                        Istr[x2]=='#' || Istr[x2] == '+' || Istr[x2] == '?') {
                         MyError(10);  // AEPutStr("\r\nYou may not include any special symbols\r\n");
                         goto INPAgain;
                     }
                     ++x2;
                 } while(x2<strlen(Istr));

                 status=Check_For_File(Istr); /* should include RZ */
                 if(status==FAILURE) {
sprintf(GSTR3,"The name %s is used, please rename.\r\n",Istr);
 AEPutStr(GSTR3);
                     goto INPAgain;
                 }

                 if(Sopt->RamPen[0]!=NULL) {                       /* check Ram dir */
                     sprintf(GSTR2,"%s/%s",Sopt->RamPen,Istr);
                     sprintf(GSTR3,"%s/%s",Sopt->RamPen,str);

                 }  else {
                     sprintf(GSTR2,"%sPlayPen/%s",NodeStr,Istr);
                     sprintf(GSTR3,"%sPlayPen/%s",NodeStr,str);
                 }

                 status=Rename(GSTR3,GSTR2);        /* oldstr, newstr */
                 if(status==NULL) {
                     sprintf(GSTR3,"The name %s is used, please rename.\r\n",Istr);
 AEPutStr(GSTR3);
                     goto INPAgain;
                 }
                 strcpy(str,Istr);
                 if(Cmds->AcLvl[LVL_CAPITOLS_in_FILE]==1)  /* use upper case only */
                     strupr(str);

                 AEPutStr("\r\n");
             }    /* end if str > 12 */

             /*===================== jump here if we also lost carrier ========*/
ax:          Time_system=time(NULL);
             sprintf(fmtstr,"%s",ctime(&Time_system));
             Convert_CTime_To_Date(fmtstr,odate,temp);

             /* add our check for ram playpen */
             if(Sopt->RamPen[0]!=NULL)   sprintf(str2,"%s/%s",Sopt->RamPen,str);
              else        sprintf(str2,"%sPlaypen/%s",NodeStr,str);

             if((FLock = Lock(str2,ACCESS_READ)) == NULL) {
                 MyError(8);
                 goto nx;
             }

             if((FBlock=(struct FileInfoBlock *)AllocDosObject(DOS_FIB,NULL)) == NULL) {
                 MyError(11);
                 UnLock(FLock);
                 return(FAILURE);
             }
             if( Examine(FLock,FBlock) )     fsize = FBlock->fib_Size;
             UnLock(FLock);
             FreeDosObject(DOS_FIB,FBlock);
#ifdef OLDCODE
             if(MoveToLCFILES) goto CLGO;
#endif
CINPAgain:
           if(Whence_The_Logon>=REMOTE_LOGON)
           {
             cstat=CheckCarrier();
             if(cstat==FALSE) {
                 MoveToLCFILES=1;
                 goto CLGO;
             }
           }
             sprintf(fmtstr,"%s%s",WorkStr,str);   /* 11w */
             uaf=fopen(fmtstr,"r");
             if(uaf==NULL) {
#ifdef JOE_AECODE
          AEPutStr("\r\nPlease enter a description, you have eight lines for your description.");
          AEPutStr("\r\nPress return alone to end.  Begin description with (/) to make upload 'Private'.\r\n");
          AEPutStr("                                [--------------------------------------------]\r\n");
#else

                 sprintf(buff,"\r\nEnter a description, you only have %d lines.",my_struct.max_desclines);
                 AEPutStr(buff);

                 AEPutStr("\r\nPress return alone to end.  Begin description with (/) to make upload 'Private'.\r\n");
                 AEPutStr("                                [--------------------------------------------]\r\n");
#endif
                 sprintf(fmtstr,"%-13s %7ld  %s :",str,fsize,odate);
                 AEPutStr(fmtstr);
CLGO:
                 if(MoveToLCFILES==1) {

                     x2=0;

                     strcpy(fcomment,"LOST CARRIER ");
                     strcat(fcomment,User.Name);
                     goto CNext;
                 }
                 status=LineInput("",fcomment,44,KEYBOARD_TIMEOUT);
                 if(status<0) { MoveToLCFILES=1; ModemOffHook(); goto CLGO; }
                 if(strlen(fcomment)==0)  goto CINPAgain;

                 if( (fcomment[0] == 'R' || fcomment[0] == 'r') && \
                       (fcomment[1] == 'Z' || fcomment[1] == 'z') && \
                       (strlen(fcomment) < 4) )
                      goto CINPAgain;

                /* stop that B0 shit */
                if(fcomment[0] == 'B' && fcomment[1] =='0')
                      goto CINPAgain;

                 s = fcomment;
                 cnt1 = 0;
                 while(*s && cnt1 < 20) {
                     if(!(isascii((int) *s))) {
                        goto CINPAgain;
                     }
                     cnt1++;
                     s++;
                 }

                 x2=0;
                 do {
                     AEPutStr("                                :");
                     status=LineInput("",scomment[x2],44,KEYBOARD_TIMEOUT);
                     if(status<0) { MoveToLCFILES=1; ModemOffHook(); goto CLGO; }
                     x2+=1;
                 } while(strlen(scomment[x2-1])!=0&&x2< (my_struct.max_desclines-1));

             } else {
                 fgets(fcomment,80,uaf);
                 fcomment[strlen(fcomment)-1]='\0';
                 x2=0;  while(fgets(scomment[x2],80,uaf)!=NULL)
                 {
                     status=strlen(scomment[x2]);
                     if(status>0)
                         scomment[x2][status-1]='\0';
                     x2+=1;
                   if(x2>=(my_struct.max_desclines-1)) break;
                 } 
                  if(uaf) fclose(uaf);
             }
CNext:
             status2=NOTALLOWED;
             if(!MoveToLCFILES && !rzmsg)
             {
             sprintf(fmtstr,"\r\nTesting... %s...\r\n",str);
             status2=Test_File(str,path);
             if(status2==NOTALLOWED||status2==SUCCESS)
                 AEPutStr("\r\nTested Ok...");

             }
             status=Check_For_File(str);

            if(MoveToLCFILES==1) status=LCFILES;
             else
                 if(fcomment[0]=='/' && rzmsg==NULL){ status=PRIVATE;}

             if(status2==FAILURE) {            /* Move to a Hold AREA */
                 hold=1;
                 sprintf(WORKB,"Requires review, possibly bad format\r\n\t  Moving to %s's private Directory.\r\n\r\n",Cmds->SysopName);
                 AEPutStr(WORKB);
                 goto Move_It;
             }



             if(status==FAILURE) {             /* Move to a Hold AREA */
                 if(FoundDupe)  {              /* 11w */
                     sprintf(WORKB,"\r\nFile already exists, moving to %s's private directory\r\n",Cmds->SysopName);
                     AEPutStr(WORKB);
                     hold=1;
                 }

             }
             if(status==SUCCESS) {            /* Move to Upload AREA */
                 hold=NULL;
                 User.Uploads+=1;
             }
             if(status==LCFILES)  {            /* 11w */
                 lcfile=1; rzmsg=NULL;
                 AEPutStr("\r\nCarrier lost, moving to lost carrier directory.\r\n");

             }
             if(status==PRIVATE) {             /* Private Upload */
                 hold=1; rzmsg=NULL;
                 sprintf(WORKB,"\r\nMoving to %s's private directory.\r\n\r\n",Cmds->SysopName);
                 AEPutStr(WORKB);
             }
             if(rzmsg)
                AEPutStr("\r\nMoving to message base file directory.\r\n");
Move_It:     /* gets here if lostcarrier, and file is complete but not when file is incomplete */
             if(hold || lcfile || rzmsg) {       /* 11w added lcfile */
                if(lcfile)
                    sprintf(GSTR2,"%sLCFILES/%s",Conference_Location,str);
                if(hold) sprintf(GSTR2,"%sHOLD/%s",Conference_Location,str);
                if(rzmsg) sprintf(GSTR2,"%sF%ld/%s",MsgBase_Location,MailHeader.MsgNumb,str);
               
                 if(Sopt->RamPen[0]!=NULL) {
                     sprintf(GSTR1,"%s/%s",Sopt->RamPen,str);
                 }else
                   sprintf(GSTR1,"%sPLAYPEN/%s",NodeStr,str);
                 status=0;
                 while(strlen(FilePart(GSTR2))<35 && !status)
                 {
                   status=Rename(GSTR1,GSTR2);
                   if(!status) {
                            status=FileCopy(GSTR1,GSTR2);
                           if(!status) strcat(GSTR2,"_");
                           if(status) DeleteFile(GSTR1); 
                    }
                 }
                 strcpy(GSTR1,"\tUpload ");
                 if(status==NULL) {
                         sprintf(WORKB,"WARNING!\r\nUnable to move file!\r\n");
                    AEPutStr(WORKB);
                     strcat(GSTR1,"unable to be ");
                 }
                 sprintf(GSTR3,"moved to %s\n",GSTR2);
                 strcat(GSTR1,GSTR3);
                 CallersLog(GSTR1);
             } else
                 MoveFile(str,fsize);

             SysopULStats(hold);
             /* Add Uploaded Bytes to Users Account */
             if(hold==NULL && lcfile==NULL && rzmsg==NULL)  User.Bytes_Upload+=fsize;

             /* Build the first line to send to upload dir */
             if(lcfile && strlen(str) > 12) {
                     sprintf(fmtstr,"%s %7ld  %s  %s\n",str,fsize,odate,fcomment);
              } else
                 sprintf(fmtstr,"%-13s %7ld  %s  %s\n",str,fsize,odate,fcomment);

             if(strlen(str) < 13) {      /* for big file name on lost carrier */
                if(checksym)    fmtstr[13]=checksym;
                else
                {
                
                if(status2==FAILURE)     fmtstr[13]='F';
                if(status2==SUCCESS)     fmtstr[13]='P';
                if(status2==NOTALLOWED)  fmtstr[13]='N';
                }
             }
             if(FoundDupe) { fmtstr[13]='D'; FoundDupe = 0; }
             

             if(hold==NULL && lcfile==NULL) {
                  if(!rzmsg) 
                 {
                 strcpy(ray,Conference_Location);
                 strcat(ray,"DIR");
                 sprintf(ray2,"%d",MaxDirs);
                 strcat(ray,ray2);
                 }
                 else sprintf(ray,"%sF%ld/%s.dis",MsgBase_Location,MailHeader.MsgNumb,str);
             } else {
                 strcpy(ray,Conference_Location);
                 if(lcfile)  {
                    strcat(ray,"LCFILES/");
                    strcat(ray,PurgeScanNM);
                    strcat(ray,".lc");
                 } else
                    strcat(ray,"HOLD/HELD");
             }

             f=fopen(ray,"a");
             fprintf(f,"%s",fmtstr);
             x3=0;
             while(x2) {
                 /* Print the comment lines */
                 if(scomment[x3][0]!='\0')
                 fprintf(f,"                                 %s\n",scomment[x3++]);
                 x2-=1;
             }
             if(GET_BIT(ACS_SENTBY_FILES))   /* Print the Sent by: line */
                 fprintf(f,"                                 Sent by: %s\n",User.Name);
             fclose(f);
         }   /*if strlen > 1 */
       }
     }       /* else */
Eit:
//PurgeLine();


 CleanPlayPen();


 CleanItUp();

/* we get here after lcfile but gugued*/
 if(!lcfile) BytesADL+=TBT;     /* dont add bytes if files moved to LCFILES DIR */

 DisplayULStats(&User);          /* Show User stats.. Num Dnloads, uploads */
 AEPutStr("\r\n");

 mstat=Check_Online_Status();
 if(mstat<0) {
     return(mstat);
 }


 if(!ULFType) {     /* nor a rz upload */
     if(gstat==2) {
      return(PGoodbye());
     }
 }

 return(SUCCESS);
}
