Next: FileStream Management
Up: Some Useful Programs
Previous: From rm to RM
  Contents
Another common Unix command line tool is ls. The program ls lists the
contents of a given directory. In addition the program ls can be used to
examine various file properties. For example the following command line call to the
program ls used with the -l option
% ls -l GATErrors.h
will print out the following information about the file GATErrors.h
-rw-r--r-- 1 leonardo masters 14477 15 Apr 11:13 GATErrors.h
The first bit of this printout -rw-r--r-- indicates who can read and write the
file GATErrors.h; the portion 14477 indicates the length of the file
GATErrors.h in bytes, and the portion 15 Apr 11:13 indicates the
last write time of the file GATErrors.h. As has become our habit, we will
create a ``grid enabled'' version of ls -l. The full program is as follows
#include <stdio.h>
#include "GAT.h"
int main( int argc, char *argv[] )
{
char writeChar;
char readChar;
GATResult result;
GATFile sourceFile;
GATContext context;
unsigned long length;
GATTime lastWriteTime;
GATLocation sourceLocation;
/* Check command line syntax */
if( 2 != argc )
{
printf("usage: %s file\n", argv[0]);
return 1;
}
/* Set result to a memory failure */
result = GAT_MEMORYFAILURE;
/* Create GATLocation sourceLocation */
sourceLocation = GATLocation_Create( argv[1] );
/* Check previous GATLocation creation */
if( NULL != sourceLocation )
{
/* Create GATContext context */
context = GATContext_Create();
/* Check previous GATContext creation */
if( NULL != context )
{
/* Create GATFile sourceFile */
sourceFile = GATFile_Create( context, sourceLocation, NULL );
/* Check GATFile creation */
if( NULL != sourceFile )
{
/* Obtain GATFile length */
result = GATFile_GetLength( sourceFile, &length );
/* Check call to GATFile_GetLength
if( GAT_SUCCEEDED( result ) )
{
/* Obtain GATFile lastWriteTime */
result = GATFile_LastWriteTime( sourceFile, &lastWriteTime );
/* Check call to GATFile_LastWriteTime */
if( GAT_SUCCEEDED( result ) )
{
/* Determine readability */
result = GATFile_IsReadable( sourceFile );
/* Check call to GATFile_IsReadable */
if( GAT_SUCCEEDED( result ) )
{
/* Set readChar */
readChar = '-';
if( GAT_SUCCESS == result )
{
readChar = 'r';
}
/* Determine writability */
result = GATFile_IsWritable( sourceFile );
/* Check call to GATFile_IsWritable */
if( GAT_SUCCEEDED( result ) )
{
/* Set writeChar */
writeChar = '-';
if( GAT_SUCCESS == result )
{
writeChar = 'w';
}
/* Print results in the form: rw length lastWriteTime file */
printf( "%c%c %d %d %s\n",
readChar, writeChar, length,
GATTime_GetTime(lastWriteTime), argv[1] );
}
}
/* Destroy GATTime */
GATTime_Destroy( &lastWriteTime );
}
}
/* Destroy GATFile sourceFile */
GATFile_Destroy( &sourceFile );
}
/* Destroy GATContext context */
GATContext_Destroy( &context );
}
/* Destroy GATLocation sourceLocation */
GATLocation_Destroy( &sourceLocation );
}
/* Check result for success and print error */
if( GAT_FAILED( result) )
{
printf( "An error has occurred during the delete operation\n");
return 1;
}
return 0;
}
Again this entire program contains no novel code; so, we will not examine it line by line.
Andre Merzky
2004-05-13
|