1. Engineering
  2. Computer Science
  3. please help fill in the missing code in include ltunistdhgt...

Question: please help fill in the missing code in include ltunistdhgt...

Question details

Please help. Fill in the missing code in

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>

void usage( char *cmd )
{
printf("Usage: %s fileName " , cmd);
printf(" Options are: ");
printf(" -h display this help screen ");
printf(" -s n start reading at location 'n'. Default n=0 ");
printf(" -c m read 'm' bytes. Default m=1 ");
}

/*-------------------------------------------------------------------------*/

bool parse_CL( int argc, char **argv, bool *sOpt, unsigned *sVal ,
bool *cOpt, unsigned *cVal, char **file )
{
int opt;


// Initialize the call-by-ref agruments
*sOpt = *cOpt = false ;   
*sVal = 0; *cVal=1 ;
*file = NULL ;
  
opterr = 0 ; /* Prevent getopt() from printing error messages */
char *optionStr = "...." ;

....
while ( ( opt = getopt(argc, argv, optionStr ) ) != -1)
{
switch (opt)
{
case 'h':   usage( argv[0] );
return true ;
break ;
  
case 's': ....
break ;

case 'c': ....
break ;

default:   usage( argv[0] ) ; // invalid options found
return false ;
}
}

// Get the mandatory file name
// Exactly ONE FileName must be passed at the end of command line
  
....
  
return true;
}

/*-------------------------------------------------------------------------*/

bool read_bytes (FILE *file, unsigned long start, unsigned long count, void* buffer)
{
if ( fseek ( .... ) != 0 )
return false; // Invalid starting location in the file

if( fread( .... ) != 1 )
return false; // The desired number of bytes could not be read from the file

return true;
}
/*-------------------------------------------------------------------------*/

#define MAXBUF 2000

int main (int argc, char **argv)
{
bool s_opt , c_opt ;
unsigned s_val , c_val ;
char *fname = NULL ;
uint8_t data[MAXBUF] ;
  
// Parse command line and check for failure
if( ! parse_CL ( .... ) )
exit( EXIT_FAILURE );

if( !fname ) // Parsing succeeds, but no file name, it was a '-h' case
exit( EXIT_SUCCESS );

// Open the input file
FILE *input = fopen( .... );
if( !input )
{
puts("Failed to read file");
exit( EXIT_FAILURE );
}

// Read the desired file bytes into the buffer
if( ! read_bytes( .... ) )
{
puts("Failed to read file");
exit( EXIT_FAILURE );
}

// Now, dump those bytes to the stdout in hexadecimal format
int i=0 ;
while ( i < c_val )
{
printf("%10u ", i );
  
....
  
printf(" ") ;
}

exit( EXIT_SUCCESS );
}

the output needs to be like this::~/cs261/toys$ ::/cs261/toys$ gcc -o fileio fileIO.c :/cs261/toys$ ~/cs261/toys$ . /file10 -h Usage: ./fileio <option (s)> fileName Options are: s n -c m display this help screen start reading at location n . Default n=0 read m bytes. Default m=1 /cs261/toys$ /cs261/toys$/fileio ptrs.c-s 16 -c 40 Usage ./fileio <option (s)> fileName Options are display this help screen start reading at location n . Default n=0 n63A m bytes. Default m=1 s n -C m :/cs261/toys$ :/cs261/toys$ ./fileio -s 16-c 40-h ptrs.c Usage: ./fileio <option (s)> fileName Options are: s n -c m display this help screen start reading at location n . Default n=0 read m bytes. Default m=1 /cs261/toys$ :/cs261/toys$ ./fileio -s 16-c 40 ptrs.c 69 6e 74 00000020 65 72 20 74 72 61 63 69 6e 67 20 65 78 65 72 63 00000010 6d 70 6c 65 Oa 2a Oa 20 2a 20 00000030 69 73 3 2e 20 44 65 /cs261/toys /cs261/toys$ hexdump -C -s 16 -n 40 00000010 f6d 70 6c 00000020( 65 72 20 74 72 61 63 69 6e 67 20 65 78 65 72 63 1er tracing exerc ! 0000003069 73 65 73 2e 20 44 65 00000038 0a 20 2a Oa 0 6f 69 6e 74mple. *. * Pointl lises. De l /cs261/toys$

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution