home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 019.lha / Aterm / testc.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  3KB  |  146 lines

  1. main( argc, arcv )
  2. int argc;
  3. char *argv[]
  4. {
  5. #include <stdio.h>
  6. #include <exec/types.h>
  7. #include <exec/libraries/dos.h>
  8. #include <exec/memory.h>
  9. #include <functions.h>
  10.  
  11. #define CHAR unsigned char
  12.  
  13. /*  Z C H K I  --  Check if input file exists and is readable  */
  14.  
  15. /*
  16.   Returns:
  17.    >= 0 if the file can be read (returns the size).
  18.      -1 if file doesn't exist or can't be accessed,
  19.      -2 if file exists but is not readable (e.g. a directory file).
  20.      -3 if file exists but protected against read access.
  21. */
  22. /*
  23.  Directory files, special files, and symbolic links are not readable.
  24. */
  25. long zchki(name) 
  26. CHAR *name; 
  27. {
  28.    struct FileInfoBlock *FBlock;
  29.    long         *FLock;
  30.    long         result;
  31.  
  32.    if ( (FLock = (struct FileLock *) Lock(name, ACCESS_READ)) == NULL)
  33.       return -1;
  34.  
  35.    if ( (FBlock = (struct FileInfoBlock *)
  36.      AllocMem( (long)sizeof(struct FileInfoBlock), (long)(MEMF_CHIP))) != NULL)
  37.    if ( FBlock == NULL )
  38.       result = -1;
  39.    else
  40.      {
  41.     if ( !Examine( FLock, FBlock) )
  42.        result = -1;
  43.     else
  44.       {
  45.          if ( FBlock->fib_DirEntryType > 0 )
  46.         result = -2; /* It's a directory */
  47.          else
  48.         result = FBlock->fib_Size;
  49.       }
  50.     FreeMem( FBlock, (long)sizeof(struct FileInfoBlock) );
  51.       }
  52.  
  53.    UnLock( FLock );
  54.    return result;
  55.  
  56. }
  57.  
  58.  
  59. /*  Z L T O R  --  Convert filename from local format to common form.    */
  60.  
  61. zltor(name,name2) 
  62. CHAR *name; 
  63. CHAR *name2; 
  64. {
  65.     CHAR work[100]; 
  66.     register CHAR *cp;
  67.     register CHAR *pp;
  68.     register int dc = 0;
  69.  
  70.     strcpy(work,name);
  71.     for (cp = pp = work; *cp != '\0'; cp++) 
  72.       {   /* strip path name */
  73.     if (*cp == '/' || *cp == ':') 
  74.       {
  75.          pp = cp;
  76.          pp++;
  77.        }
  78.     else if (islower(*cp)) *cp = toupper(*cp); /* Uppercase letters */
  79.     else if (*cp == '~') *cp = 'X'; /* Change tilde to 'X' */
  80.     else if ((*cp == '.') && (++dc > 1)) *cp = 'X'; /* & extra dots */
  81.       }
  82.     cp = name2;             /* If nothing before dot, */
  83.     if (*pp == '.') *cp++ = 'X';        /* insert 'X' */
  84.     strcpy(cp,pp);
  85. }
  86.  
  87. /*  Z F C D A T  --  Put file creation date/time in str.     */
  88. /*             returns 1 if able to get date, 0 otherwise. */
  89.  
  90. zfcdat(fname,str) 
  91. CHAR *fname,
  92. *str; 
  93. {
  94.    strcpy( str, "<zfcdat: Not Implemented>" );
  95.    return 0;
  96. }
  97.  
  98.  
  99. /* Z F R E E -- Return total number of free bytes on drive specified */
  100.  
  101. long zfree(drive)
  102. CHAR *drive;
  103. {
  104.    register struct InfoData *id;
  105.    long         FLock;
  106.    LONG            result;
  107.  
  108.    if ( (FLock = (struct InfoData *) Lock(drive, ACCESS_READ)) == NULL)
  109.       return 0;
  110.  
  111.    if ( (id = (struct InfoData *)
  112.      AllocMem( (long)sizeof(struct InfoData), (long)(MEMF_CHIP))) != NULL)
  113.    if ( id == NULL )
  114.       result = 0;
  115.    else
  116.      {
  117.     if ( !Info( FLock, id) )
  118.        result = 0;
  119.     else
  120.        result = (id->id_NumBlocks - id->id_NumBlocksUsed )
  121.           * id->id_BytesPerBlock;
  122.     FreeMem( id, (long)sizeof(struct InfoData) );
  123.       }
  124.  
  125.    UnLock( FLock );
  126.    return result;
  127.  
  128. }
  129.  
  130. /*-----------------Start of Main--------------------*/
  131.  
  132. char *filename;
  133.  
  134. if ( argc != 2 )
  135.    {    
  136.       printf("Woops, argc= %d\n", argc );
  137.       exit( 2 );
  138.    }
  139.  
  140. filename = argv[ 1 ];
  141. printf( "Examining '%s'\n", filename );
  142. printf( "File size is %ld\n", zchki( filename ) );
  143. printf( "Free Bytes are %ld\n", zfree( filename );
  144. printf( "Th Th That's all Folks!\n" );
  145. exit( 0 );
  146. }