Sunday, Nov 18th

Last update12:59:40 PM GMT

Write a C Program Utility

Write e-mail

A simple C command line utility takes a series of command line options. The options are given to the utility like this : <utility_name> options=[no]option1,[no]options2,[no]option3?...


Write C code using bitwise operators to use these flags in the code.


//Each option will have a bit reserved in the global_options_bits integer. The global_options_bits
// integer will have a bit set or not set depending on how the option was specified by the user.
// For example, if the user said nooption1, the bit for OPTION1 in global_options_bits
// will be 0. Likewise, if the user specified option3, the bit for OPTION3 in global_options_bits
// will be set to 1.

#define OPTION1 "option1" // For strcmp() with the passed arguments.
#define OPTION1_BITPOS (0x00000001) // Bit reserved for this option.
#define OPTION2 "option2"
#define OPTION2_BITPOS (0x00000002)
#define OPTION3 "option3"
#define OPTION3_BITPOS (0x00000004)

//Required to do the bit operations.
#define ALL_BITPOS (0x0001ffff)

// Assume you have already parsed the command line option and that
// parsed_argument_without_no has option1 or option2 or option3 (depending on what has
// been provided at the command line) and the variable negate_argument says if the
// option was negated or not (i.e, if it was option1 or nooption1)

if (strcmp((char *) parsed_argument_without_no, (char *) OPTION1) == 0)

 // Copy the global_options_bits to a temporary variable.
 tmp_action= global_options_bits;

 if (negate_argument)

 // Setting the bit for this particular option to 0 as the option has
 // been negated.

 action_mask= ~(OPTION1_BITPOS) & ALL_BITPOS;
 tmp_action= tmp_action & action_mask;



 //Setting the bit for this particular option to 1.

 action_mask= (OPTION1_BITPOS);
 tmp_action= tmp_action | action_mask;


 // Copy back the final bits to the global_options_bits variable

 global_options_bits= tmp_action;


else if (strcmp((char *) parsed_argument_without_no, (char *) OPTION2) == 0)
 //Similar code for OPTION2

else if (strcmp((char *) parsed_argument_without_no, (char *) OPTION3) == 0)
 //Similar code for OPTION3

//Now someone who wishes to check if a particular option was set or not can use the
// following type of code anywhere else in the code.

if(((global_options_bits & OPTION1_BITPOS) == OPTION1_BITPOS)
 //Do processing for the case where OPTION1 was active. 
 //Do processing for the case where OPTION1 was NOT active.

Share this post

Web Hosting