/****************************************************************************
 *
 * RCS ID
 * $Id: futils,v 1.11 2001/04/20 14:38:37 david Exp $
 *
 * HISTORY
 * $Log: futils,v $
 * Revision 1.11  2001/04/20 14:38:37  david
 * Updates for 0.7a
 * Corrected reporting of large disk sizes
 *
 * Revision 1.10  2000/04/17 16:48:33  david
 * modifications for running in RISCOS module
 *
 * Revision 1.9  2000/04/02 16:58:48  david
 * Changes so that can be compiled in a RISCOS module.
 *
 * Revision 1.8  2000/02/13 21:05:13  david
 * Modified following testing with !LanMan98
 *
 * Revision 1.7  2000/02/08 20:17:58  david
 * Added Detect!lanman98 and detect extensions - tidied filetype stuff
 * Changed delimiter for files with duplicate names but same extension
 * from / to ALT+\
 *
 * Revision 1.6  2000/01/29 17:21:24  david
 * Updated to try and fix a NT copying a read only file to RISCOS share bug
 *
 * Revision 1.5  2000/01/11 20:16:44  david
 * New function - riscos_free_space()
 *
 * Revision 1.4  1999/11/23 22:03:48  david
 * Updates file long filename support.
 * Increased filename length to MAXPATHLEN
 * Add stuff to treat an archive as a file and not a directory
 * if a flags is not set (useful for when !sparkfs laaded)
 * os_file - updated to be a C replacement for s._os
 *
 * Revision 1.3  1999/11/07 15:14:54  david
 * Updates to support StripExtension, set filetype
 * utils:new stuff!
 *
 * Revision 1.2  1999/08/08 11:51:41  david
 * Added Boolean to switch off file type creation.
 *
 * Revision 1.1  1999/07/20 21:08:24  david
 * Initial revision
 *
 *
 *
 *****************************************************************************/

#include "includes.h"
#include "swis.h"
#include "my_os.h"
                                


/*#define FTEST 1*/
      
/* going to use OS GBPB 10 */
#ifdef USING_OS_GBPB_12
#undef USING_OS_GBPB_12
#endif
       
#ifdef USING_OS_GBPB_12
#define NAME_OFFSET 24
#else
#define NAME_OFFSET 20
#endif
  
typedef struct sfInfo {
   int  loadAddress;
   int  execAddress;
   int  length;
   int  attrib;
   int  type;  
#ifdef USING_OS_GBPB_12
   int  ftype;         
#endif
   char name[MAXPATHLEN+100];
} tfInfo, *tpfInfo;

typedef struct sDosDiscList {
   char *path;
   struct sDosDiscList *next;
} tDosDiscList, *tpDosDiscList;
                   
BOOL smbDontStripFileExt=False;
BOOL stripExtension=True;
BOOL setFileType=True;
BOOL detectDosParts=True;
extern BOOL detectLanMan98;
extern BOOL hexFileTypes;

extern connection_struct * g_conn;

tpDosDiscList dosDiscList=NULL;

BOOL dirChanged=False;            

int getFileNameType;

char cwDir[MAXPATHLEN+100];
char pwDir[MAXPATHLEN+100];
char cFs[MAXPATHLEN+100];

char theFile[MAX_FILE_NAME_LENGTH];
char lastFileIn[MAX_FILE_NAME_LENGTH];
char lastFileOut[MAX_FILE_NAME_LENGTH];

static char match2[MAX_FILE_NAME_LENGTH];

static int searchDir(char    *dirname, 
                     char    *match, 
                     int     *offset,  
                     char    *buffer,
                     int     bufsize)
{
  int             r[10];
  _kernel_oserror *e;         
#if 0        
  char            match2[MAX_FILE_NAME_LENGTH];
#endif

  strcpy(match2,match);
  strcat(match2,"*");             
      
#ifdef USING_OS_GBPB_12   
  r[0] = 12;
#else
  r[0] = 10;  
#endif
  r[1] = (int) dirname;
  r[2] = (int) buffer;
  r[3] = 25;
  r[4] = *offset;
  r[5] = bufsize;
  r[6] = (int) match2;

  /*printf("searchDir %s, match %s, offset %d\n",
         dirname, match2, *offset);  */

  if (e = myos_swi (OS_GBPB, r))
  {                    
#ifdef FTEST 
     printf("searchDir: error %s\n", e->errmess);
#else
     DEBUG(1,("searchDir: error %s\n", e->errmess)); 
#endif
     *offset=-1;
     return 0;   
  }

#if 0
  if (r[3]!=0)
  {
    
     memcpy((char*)pfInfo, buffer, NAME_OFFSET);
     strcpy(pfInfo->name, (char*)(buffer+NAME_OFFSET));
                                              
/* 
     printf("r3 %d, r4 %d\n", r[3], r[4]); 
     printf("load address 0x%x\n", pfInfo->loadAddress);
     printf("exec address 0x%x\n", pfInfo->execAddress);  
     printf("length       %d\n", pfInfo->length);
     printf("attributes   %d\n", pfInfo->attrib);
     printf("type         %d\n", pfInfo->type);
     printf("ftype        0x%x\n", pfInfo->ftype);
*/
  }
#endif
              
  *offset=r[4];                                 
  return r[3];
}

char temps[MAXPATHLEN+100];

BOOL isInDosDisc(char *filename,BOOL fullName)
{
  BOOL result = False;
  tpDosDiscList aDosDiscList;    
#if 0
  char temps[MAXPATHLEN+100];
#endif

  if (fullName==False && g_conn!=NULL)
  {                                                 
    sprintf(temps,"%s.%s",lp_pathname(SNUM(g_conn)),filename);
  }                                   
  else
  {
    strcpy(temps,filename);
  }

  aDosDiscList = dosDiscList;
  while (aDosDiscList!=NULL && result==False)
  {

    DEBUG(2,("isInDosDisc DosDisc %s, file %s\n",
              aDosDiscList->path,temps));

    if (StrnCaseCmp(aDosDiscList->path,
                    temps,
                    strlen(aDosDiscList->path))==0)
    {
      result=True;       
    }                                                   
    aDosDiscList = aDosDiscList->next;
  }     
            

  return result;
}       

static int firstTime=1;

static  char dirname[MAX_FILE_NAME_LENGTH];
static  char filename[MAX_FILE_NAME_LENGTH];
static  char filename2[MAX_FILE_NAME_LENGTH];
static  char filename3[MAX_FILE_NAME_LENGTH];
static  char filename4[MAX_FILE_NAME_LENGTH];
static  char ftype[15]="";  
static  char ftype2[15]="";
static  char fileext[15]="";
static  char fileext2[15]="";
static  char fileext3[15]="";   

char *getFileName(char *fname)
{

#if 0
  char dirname[MAX_FILE_NAME_LENGTH];
  char filename[MAX_FILE_NAME_LENGTH];
  char filename2[MAX_FILE_NAME_LENGTH];
  char filename3[MAX_FILE_NAME_LENGTH];
  char filename4[MAX_FILE_NAME_LENGTH];
  char ftype[15]="";  
  char ftype2[15]="";
  char fileext[15]="";
  char fileext2[15]="";
  char fileext3[15]="";       
#endif
  char buffer[sizeof(tfInfo)];
  char *p,*p2;
  char *c;
  int  r[10];
  _kernel_oserror *e;
  int  found=0;
  int  offset,iftype; 
  int  j,i,num;
  tfInfo fInfo;                                              
  char slashprime = 0xb7; /* ALT + '\' = '' */;

  /* fname is already in RISCOS format - __uname has already been called */
                           


  if (firstTime==0 && strcmp(fname,lastFileIn)==0)
  {
    strcpy (theFile, lastFileOut);   
    DEBUG(5,("getFileName - same file as last call %s, %s\n",fname,theFile));
    return theFile;
  }

  /* if we can't find it then will return it as is */
  strcpy(theFile, fname);

  /* does filename exist as is ? */
  r[0]=5 /*20*/;
  r[1]=(int)fname;
  if (e = myos_swi (OS_File, r))
  {

  }
  else
  {             
     iftype = ((r[2] & 0xfff00)>>8);       
/*              
     if (r[0]==1 && r[6]!=-1 && r[6]!=0x1000 && r[6]!=0x2000)
     {
       
       printf("fileEXT %s ext %s, %d, %x\n",filename,fileext,r[0],r[6]);
       found=1;
     }  
*/
     if (r[0]!=0)
     {

        /* does the file have an extension and does it match what we expect? */
        if (r[0]==1)
        {
           /*get_ftype(r[6],fileext); */

           /* get the file extension */
           c=strrchr(fname,'/');    
           if (c==NULL)
           {            
  
              if (/*r[6]*/iftype==0)
              {
                 found=1;
              }

           }
#if 0
           else
           {
            
             if (iftype==0xfe4)
             {
               found = 1;
             }
           }
#endif 

        }
        else
        {

         found=1;
        }

        if (found==1)
        {     
#ifdef FTEST
         printf("getFileName: %s found\n",fname);
#else
         DEBUG(4,("getFileName: %s found\n",fname));
#endif   
        }
     }
  }                        

  /* the file as given doesn't exist - need to work out the real name */               
  if (found==0)
  {
    /* strip off directory name */
    c=strrchr(fname,'.');
    if (c!=NULL)
    {                     
      /* copy filename */
      strcpy(filename,(char*)(c+1)); 

      /* copy directory */
      strncpy(dirname,fname,c-fname);
      dirname[c-fname]=0;
    }                     
    else
    {
      strcpy(dirname,"@");                                       
      strcpy(filename,fname);
    }         

    /* 
     * should really put a check in here that the dirname exists -
     * it might not do if the real dirname has the "slashprime" character in!
     */
               
    /* if it is a wild card don't bother doing a search */
    if (strcmp(filename,"*/*")==0)
    {
      found=1;
    }         
  }

  if (found==0)
  {
                           
    if (detectLanMan98==True || hexFileTypes==True)
    {
       /* get the file extension */
       c=strrchr(filename,',');    
       if (c!=NULL)
       {            
         /* copy extension */
         strcpy(ftype,(char*)(c+1));

         /* copy filename without extension */
         strncpy(filename2,filename,c-filename);
         filename2[c-filename]=0;
                      
#if 1
         /* see if there's a special character there */
         c=strrchr(filename2,slashprime);    
         if (c!=NULL)
         {     
            /* copy filename without extension */
            strncpy(filename4,filename2,c-filename2);
            filename4[c-filename2]=0;       
            strcpy(filename2,filename4);
         }
#endif
       }
       else
       {
          /* get the file extension */
          c=strrchr(filename,'/');    
          if (c!=NULL)
          {            
            /* copy extension */
            strcpy(ftype,(char*)(c+1));

            /* copy filename without extension */
            strncpy(filename2,filename,c-filename);
            filename2[c-filename]=0;
          }
          else
          {
            ftype[0]='\0';
            strcpy(filename2,filename);
          }

       }

    }
    else
    {
       /* get the file extension */
       c=strrchr(filename,'/');    
       if (c!=NULL)
       {            
         /* copy extension */
         strcpy(ftype,(char*)(c+1));

         /* copy filename without extension */
         strncpy(filename2,filename,c-filename);
         filename2[c-filename]=0;
       }
       else
       {
         ftype[0]='\0';
         strcpy(filename2,filename);
       }
    }    

#ifdef FTEST
    printf("getFileName: %s\n", fname);
    printf("getFileName: dir %s, file %s\n", dirname, filename);
    printf("search for %s\n",filename2);
#else
    DEBUG(5,("getFileName: dir %s, file %s\n", dirname, filename));
    DEBUG(5,("search for %s\n",filename2));
#endif   
                      

    
    offset=0;  
    while (offset!=-1 && found==0)
    {
      p=buffer;
      num=searchDir(dirname,filename2,&offset,buffer,1024);     
      if (num!=0)
      {
   
                               
        for (i=0; (i<num) && (found==0) ;i++)
        {
#ifdef FTEST
          printf("p=%x\n",p); 
          for (j=0;j<NAME_OFFSET;j+=4)
          {
             printf("0x%.2x%.2x%.2x%.2x ", *(p+j+3), *(p+2+j), *(p+1+j), *(p+j));
             printf("0x%.8lx ",*((int*)(p+j)));   
          }
          printf("\n");
#endif
          memcpy((char*)&fInfo, p, NAME_OFFSET);
          strcpy(fInfo.name, (char*)(p+NAME_OFFSET));
#ifdef FTEST
          printf("num %d,match: %s, 0x%x\n", i, 
                 fInfo.name, fInfo.ftype);
          for (p2=(p+24);*p2!='\0';p2++) 
          {
             printf("%c,%.2x ",*p2,(*p2)&0xff);
          }
          for (j=0;j<4;j++) 
          {
             printf("%c,%.2x ",*p2,(*p2)&0xff);  
             p2++;
          }
          printf("\n");
#endif   
#ifdef USING_OS_GBPB_12 
          iftype=fInfo.ftype;
#else                        
          iftype=(fInfo.loadAddress & 0xfff00)>>8;
#endif        
          if (fInfo.type!=2)
          {
            get_ftype(iftype,fileext2);
          } 
          else
          {
            fileext2[0]='\0';
          }

#ifdef FTEST  
          printf("num %d,match: %s(%d), 0x%x, %s, %s\n", i, 
                 fInfo.name, strlen(fInfo.name), iftype, fileext2, ftype ); 
          printf("new p=%x\n",p);  
#else
          DEBUG(8,("num %d,match: %s(%d), 0x%x, %s, %s, objType %d\n", i, 
                 fInfo.name, strlen(fInfo.name), iftype, fileext2, ftype,
                 fInfo.type )); 
#endif  
          p+= NAME_OFFSET + strlen(fInfo.name) +1;
#ifdef FTEST
          printf("new p=%x,%x,%x\n",p,(int)p%4,(char*)((int)p+(4-((int)p%4))  ));
#endif   

          if ( ((int)p%4) != 0 )
              p=(char*)((int)p+(4-((int)p%4))  );
#ifdef FTEST
          printf("new p=%x\n",p);
#endif 
                  

          /* get the file extension */
          c=strrchr(fInfo.name,slashprime);    
          if (c!=NULL)
          {            
            /* copy extension */
            strcpy(ftype2,(char*)(c+1));

            /* copy filename without extension */
            strncpy(filename3,fInfo.name,c-fInfo.name);
            filename3[c-fInfo.name]=0;
          }
          else
          {
            c=strrchr(fInfo.name,'/'); 
            if (c!=NULL)
            {
              if (tolower(*(c+1)) == tolower(ftype[0]))   
              {
                /* copy extension */
                strcpy(ftype2,(char*)(c+1));

                /* copy filename without extension */
                strncpy(filename3,fInfo.name,c-fInfo.name);
                filename3[c-fInfo.name]=0;
              }
              else
              {
                ftype2[0]='\0';
                strcpy(filename3,fInfo.name);
              }
            }
            else
            {
              ftype2[0]='\0';
              strcpy(filename3,fInfo.name);
            }
          }
        
          sprintf(fileext3,"%.3x",iftype);
          DEBUG(10,("filename2 %s, filename3 %s, ftype %s, ftype2 %s, fileext2 %s, fileext3 %s\n",
                    filename2, filename3, ftype, ftype2, fileext2, fileext3));     
          if (StrCaseCmp(filename2, filename3)==0 && 
              (StrCaseCmp(ftype,fileext2)==0 ||
               StrCaseCmp(ftype,fileext3)==0))
          {   

#ifdef FTEST
             printf("num %d, match: %s\n",num, fInfo.name);
#else
             DEBUG(5,("match: %s\n", fInfo.name));
#endif
             /* this is the name to return */
             strcpy(theFile,dirname);
             strcat(theFile,".");
             strcat(theFile,fInfo.name);   
             found=1;                  
             
          }    
        }
      }
    }
  }
  
#ifdef FTEST
  printf("getFileName is %s\n", theFile);
#else
  DEBUG(5,("getFileName is %s\n", theFile));
#endif
                    
  strcpy(lastFileIn,fname);
  strcpy(lastFileOut,theFile);

  getFileNameType = iftype; 
  firstTime=0;
  return theFile; 
}

static char newname[MAX_FILE_NAME_LENGTH];

char *getNewFileName(char *fname, int *aftype)
{                  
  char buffer[sizeof(tfInfo)];
#if 0
  char dirname[MAX_FILE_NAME_LENGTH];
  char filename[MAX_FILE_NAME_LENGTH];
  char filename2[MAX_FILE_NAME_LENGTH];
  char filename3[MAX_FILE_NAME_LENGTH];
  char newname[MAX_FILE_NAME_LENGTH];
  char ftype[15];
#endif

  char newext;
  char *c,*p;        
  BOOL extok=False;
  int  found=0;
  int  offset;
  int  num,i;
  tfInfo fInfo;     
  int  r[10];
  _kernel_oserror *e;

  /* 
   * assume that fname does not exist already - application should
   * have checked this!
   */

   DEBUG(5,("getNewFileName %s\n",fname));
                      
   /* 
    * If we want the file to be as is then don't mess about with
    * file typing.
    */
   if (smbDontStripFileExt==True)
   {               
      DEBUG(5,("getNewFileName - don't strip file ext is True - returning %s\n",
              fname)); 
      *aftype=0xfff;
      return fname;
   }

   if (detectDosParts==True && isInDosDisc(fname,False)==True &&
       detectLanMan98==False)
   {               
      DEBUG(5,("getNewFileName - inside DOS partition %s\n",
              fname)); 
      *aftype=0xfe4;
      return fname;
   }

    /* strip off directory name */
    c=strrchr(fname,'.');
    if (c!=NULL)
    {                     
      /* copy filename */
      strcpy(filename,(char*)(c+1)); 

      /* copy directory */
      strncpy(dirname,fname,c-fname);
      dirname[c-fname]=0;
    }                     
    else
    {
      strcpy(dirname,"@");                                       
      strcpy(filename,fname);
    }         
                      
    if (detectLanMan98==True)
    {
      /* get the file extension */
      c=strrchr(filename,',');    
      if (c!=NULL)
      {            
        /* copy extension */
        strcpy(ftype,(char*)(c+1));
        
        /* copy filename without extension */
        strncpy(filename2,filename,c-filename);
        filename2[c-filename]=0;                 
           
        if (setFileType==True)
        {
          *aftype=(int)strtol((const char*)ftype, (char**)NULL, 16);
        }
        else
        {
          ftype[0]='\0';                    
          *aftype=0;
        }

        extok=True;
      }
    }

     
    if (extok==False)
    {
      /* get the file extension */
      c=strrchr(filename,'/');    
      if (c!=NULL)
      {            
        /* copy extension */
        strcpy(ftype,(char*)(c+1));
                                
        if (stripExtension==True)
        {
          /* copy filename without extension */
          strncpy(filename2,filename,c-filename);
          filename2[c-filename]=0;                 
        }
        else
        {
          strcpy(filename2,filename);
        }
           
        if (setFileType==True)
        {
          get_ext(aftype,ftype);
        }
        else
        {
          ftype[0]='\0';                    
          *aftype=0;
        }
      }
      else
      {
        ftype[0]='\0';                    
        *aftype=0;
        strcpy(filename2,filename);
      }    

      extok=True;
    }  
    /* see if the file exists without an extension first */
    strcpy(filename3,dirname);
    strcat(filename3,".");
    strcat(filename3,filename2);

    DEBUG(8,("trying %s\n",filename3));

    r[0]=20;
    r[1]=(int)filename3;
    if (e = myos_swi (OS_File, r))
    {

    }
    else
    {                    
      /* doesn't exist so we can use it */
      if (r[0]==0)
      {
         DEBUG(8,("%s OK to use \n",filename3));
         found=1;  
         strcpy(theFile,filename3);
      }                
      else
      {                
         DEBUG(8,("%s already exists\n",filename3));

      }
    }
   

  if (found==0 && stripExtension==True)
  {                            
    char slashprime[2];
    /* 
     * append a slash to avoid matching name we have just discovered
     * does exist.
     */     
    slashprime[0]=0xb7; /* ALT + '\' = '' */;  
    slashprime[1]='\0';
    strcat(filename2,slashprime);

    DEBUG(5,("search for %s, %s\n",filename2,ftype));
    offset=0; 
    newext='0';
    sprintf(newname,"%s%c",filename2,newext); 
    while (offset!=-1 && found==0)
    {       
      p=buffer;
      num=searchDir(dirname,filename2,&offset,buffer,sizeof(buffer));
      if (num!=0)
      { 
 
                               
        for (i=0; (i<num) && (found==0) ;i++)
        {
          memcpy((char*)&fInfo, p, NAME_OFFSET);
          strcpy(fInfo.name, (char*)(p+NAME_OFFSET));  
#ifdef FTEST
          printf("num %d,match: %s\n", i, fInfo.name);
#else
          DEBUG(5,("num %d,match: %s,%s\n", i, fInfo.name,newname));  
#endif
          p+= NAME_OFFSET + strlen(fInfo.name)+1;

          if ( ((int)p%4) != 0 )
             p=(char*)((int)p+(4-((int)p%4))  );

          /* found a match, see if we can use the name */
          if (StrCaseCmp(newname,fInfo.name)==0)
          {                                              
             /* name in use, so create the next one */             
             if (newext=='9')
                newext='A';
             else
                newext++;   

             sprintf(newname,"%s%c",filename2,newext); 
          }
          else
          {
             /* name is free! */
             found=1;
          }
        }       
      }
    }
  
     
    strcpy(theFile,dirname);
    strcat(theFile,".");
    strcat(theFile,newname);
  }
                                       
#ifdef FTEST
  printf("getNewFileName is %s, ext %s, 0x%x\n", theFile, ftype, *aftype);
#else  
  DEBUG(5,("getNewFileName is %s, ext %s, 0x%x\n", theFile, ftype, *aftype)); 
#endif                                                                   

  strcpy(lastFileIn,fname);
  strcpy(lastFileOut,theFile);

  return theFile;  
}

                      
void storeDirs(char *dirname)
{
  _kernel_oserror *e;   
  int r[10];  
  char *c;     
  
  if (dirChanged==False)
  {     
     c=strchr(dirname,':');
     if (c!=NULL)
     {
        strncpy(cFs,dirname,(int)(c-dirname)); 
        cFs[(int)(c-dirname)]='\0';
     }
     else
     {
        cFs[0]='\0';
     }                           
                    
     /* read current dir */
     r[0] = 54;
     r[1] = (int)cwDir;
     r[2] = 0;
     r[3] = (int)cFs;
     r[5] = sizeof(cwDir);

     e= myos_swi (OS_FSControl, r);
     DEBUG(2,("cwd %s  ::  %s\n",cFs,cwDir));

     /* read previous dir */
     r[0] = 54;
     r[1] = (int)pwDir;
     r[2] = 1;
     r[3] = (int)cFs;
     r[5] = sizeof(pwDir);

     e = myos_swi (OS_FSControl, r);
     DEBUG(2,("pwd %s  ::  %s\n",cFs,pwDir));

     dirChanged=True;
  }
}

void restoreDirs(void)
{
  _kernel_oserror *e;   
  int r[10];  

  if (dirChanged==True)
  {                   
     /* current dir */
     r[0] = 53;
     r[1] = (int)cwDir;
     r[2] = 0;
     r[3] = (int)cFs;
     r[4] = 0;
     r[5] = 0;
     r[6] = 0;

     e= myos_swi (OS_FSControl, r);
    
     r[0] = 53;
     r[1] = (int)pwDir;
     r[2] = 1;
     r[3] = (int)cFs;
     r[4] = 0;
     r[5] = 0;
     r[6] = 0;

     e= myos_swi (OS_FSControl, r);
     dirChanged=False;
  }
}
                               
void addDosDisk(char *filename)
{
  tpDosDiscList aDosDiscList;
  BOOL          found=False;

  aDosDiscList = dosDiscList;
  while (aDosDiscList!=NULL)
  {
    if (strcmp(aDosDiscList->path,filename)==0)
    {  
      /*DEBUG(10,("dosDiscList %s already in list\n",filename));*/ 
      found=True;
    }                                                   
    aDosDiscList = aDosDiscList->next;
  }              

  if (found==False)
  {
    aDosDiscList=(tpDosDiscList) calloc (1, sizeof(tDosDiscList));
    if (aDosDiscList!=NULL)
    {
      aDosDiscList->path = (char*) calloc(1, strlen(filename)+1);
      if (aDosDiscList->path!=NULL)
      {
        strcpy(aDosDiscList->path, filename);
        
        aDosDiscList->next = dosDiscList;
        dosDiscList = aDosDiscList;
      }
    }  

#if 1
    aDosDiscList = dosDiscList;
    while (aDosDiscList!=NULL)
    {
      DEBUG(1,("DosDiscList %s\n",aDosDiscList->path));
      aDosDiscList = aDosDiscList->next;
    } 
#endif

  }                 

}

/* checks to see if a file is a DOSDisc */
void checkForDosDisc(char *filename, BOOL fullPath, int r2)
{
#if 0
  char temps[MAXPATHLEN+100];
#endif
  
    DEBUG(1,("checkForDosDisc %s, r2 %.8x, r2etc, %.8x\n",
          filename, r2, (r2 & 0xfff00) >> 8));

    /* is it a DOSDisc ? */
    if ( ((r2 & 0xfff00) >> 8) == 0xfc8 )
    {                                    
       if (fullPath==False && g_conn!=NULL)
       {
          DEBUG(1,("DOSDisc: %s.%s\n", lp_pathname(SNUM(g_conn)), filename ));
          sprintf(temps,"%s.%s", lp_pathname(SNUM(g_conn)), filename );
          addDosDisk(temps);
       }
       else
       {
          DEBUG(1,("DOSDisc: %s\n", filename ));
          addDosDisk(filename);
       }
    } 

}

void checkImageFile(char *filename, BOOL fullPath)
{
#if 0
  char temps[MAXPATHLEN+100]; 
#endif
  _kernel_oserror *e;
  _kernel_swi_regs rin, rout;

  rin.r[0]=5;
  rin.r[1]=(int)filename;

  /* myos_swi crashed the machine horribly of the disk was not present!! */

  e=_kernel_swi(OS_File, &rin, &rout);
  
  if (e==NULL)
  {
    DEBUG(1,("checkImageFile %s, r2 %.8x, r2etc, %.8x\n",
          filename, rout.r[2], (rout.r[2] & 0xfff00) >> 8));

    /* is it a DOSDisc ? */
    if ( ((rout.r[2] & 0xfff00) >> 8) == 0xfc8 )
    {                                    
       if (fullPath==False && g_conn!=NULL)
       {
          DEBUG(1,("DOSDisc: %s.%s\n", lp_pathname(SNUM(g_conn)), filename ));
          sprintf(temps,"%s.%s", lp_pathname(SNUM(g_conn)), filename );
          addDosDisk(temps);
       }
       else
       {
          DEBUG(1,("DOSDisc: %s\n", filename ));
          addDosDisk(filename);
       }
    }
  }  

}


void checkImageFile2(char *filename, BOOL fullPath, int ftype)
{
#if 0
  char temps[MAXPATHLEN+100];
#endif
    DEBUG(1,("checkImageFile2 %s, 0x%x\n",
          filename, ftype));

    /* is it a DOSDisc ? */
    if ( ftype == 0xfc8 )
    {                                    
       if (fullPath==False && g_conn!=NULL)
       {
          DEBUG(1,("DOSDisc: %s.%s\n", lp_pathname(SNUM(g_conn)), filename ));
          sprintf(temps,"%s.%s", lp_pathname(SNUM(g_conn)), filename );
          addDosDisk(temps);
       }
       else
       {
          DEBUG(1,("DOSDisc: %s\n", filename ));
          addDosDisk(filename);
       }
    }

}

int riscos_free_space(const char *path, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
{
  int result=-1;                                                
  char *path2;
  _kernel_oserror *e;
  _kernel_swi_regs rin, rout;

  path2 = __uname( (char*)path,0 );

  DEBUG(5,("riscos_free_space: path = %s, path2=%s, cwDir=%s\n",path,path2,cwDir));

  rin.r[0]=55;
  rin.r[1]=(int)path2;

  e=_kernel_swi(OS_FSControl, &rin, &rout);  

  if (e==NULL)
  {                                           
    /* 
     * if (high byte / 512) is not 0 then keep shifting left until high byte
     * is zero, number of bits shifted is then the unit size.
     */
    DEBUG(5,("riscos_free_space 0x%.8lx 0x%.8lx, size 0x%.8lx 0x%.8lx\n",
              rout.r[1], rout.r[0], rout.r[4], rout.r[3]));
    *dfree = ((rout.r[0]>>9)&0x007fffff) | (rout.r[1]<<23);
    *dsize = ((rout.r[3]>>9)&0x007fffff) | (rout.r[4]<<23);
#if 0 
    DEBUG(5,("r[3]>>9 0x%.8lx, r[4]<<23 0x%.8lx\n", 
            (rout.r[3]>>9)&0x007fffff, (rout.r[4]<<23)));
#endif
    result=0;      
  }
  else
  {
    /* 
     * OS_FSControl 55 failed, try OS_FSControl 49 incase we are running on
     * an OS that doesn't support 49 - see PRM 5a-166 
     */
    rin.r[0]=49;
    rin.r[1]=(int)path2;

    e=_kernel_swi(OS_FSControl, &rin, &rout);  

    if (e==NULL)
    {
      *dfree = (rout.r[0]>>9); /* divide by 512 */
      *dsize = (rout.r[2]>>9); /* divide by 512 */
      result=0;      
    }
    else  
    {
      DEBUG(5,("e->errnum %d, e->errmess %s\n",e->errnum, e->errmess));
    }
  }

  DEBUG(5,("riscos_free_space: result=%d, dfree=%d, dsize=%d\n",
        result, *dfree, *dsize));
             
  return result;
}

