Changeset 37 for trunk


Ignore:
Timestamp:
07/30/05 23:25:40 (19 years ago)
Author:
tim
Message:

Added basic command line argument parsing. Only prefix_filter option works right now.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile

    r36 r37  
    1212        $(CC) $(CFLAGS) $(OPTS) -o $@ reglookup.o $(LIBS)
    1313
    14 reglookup.o:
     14reglookup.o: reglookup.c
    1515        $(CC) $(CFLAGS) $(OPTS) -c -o $@ reglookup.c
    1616
  • trunk/src/reglookup.c

    r36 r37  
    2828#include "../include/void_stack.h"
    2929
    30 
     30/* XXX: this needs to be rewritten to malloc each resulting string, instead of
     31 *      altering them in place
     32 */
    3133void_stack* path2Stack(const char* s)
    3234{
    3335  void_stack* ret_val = void_stack_new(1024);
    34   char* cur = strdup(s);
    3536  char* next = NULL;
    36  
     37  char* cur;
     38  if (s == NULL)
     39    return ret_val;
     40  else
     41    cur = strdup(s);
     42
    3743  while((next = strrchr(cur, '/')) != NULL)
    3844  {
     
    5157{
    5258  const REGF_NK_REC* cur;
    53   unsigned int buf_left = 127;
    54   unsigned int buf_len = buf_left+1;
    55   unsigned int name_len = 0;
    56   unsigned int grow_amt;
     59  uint32 buf_left = 127;
     60  uint32 buf_len = buf_left+1;
     61  uint32 name_len = 0;
     62  uint32 grow_amt;
    5763  char* buf;
    5864  char* new_buf;
     
    8187    if(name_len+1 > buf_left)
    8288    {
    83       grow_amt = (unsigned int)(buf_len/2);
     89      grow_amt = (uint32)(buf_len/2);
    8490      buf_len += name_len+1+grow_amt-buf_left;
    8591      if((new_buf = realloc(buf, buf_len)) == NULL)
     
    112118void printValueList(REGF_NK_REC* nk, char* prefix)
    113119{
    114   unsigned int i;
     120  uint32 i;
    115121 
    116122  for(i=0; i < nk->num_values; i++)
    117123    printValue(&nk->values[i], prefix);
    118124}
     125
    119126
    120127/* XXX: this function is god-awful.  Needs to be re-designed. */
     
    179186  char* cur_str = NULL;
    180187  bool found_cur = true;
    181   unsigned int i;
    182   unsigned short path_depth;
     188  uint32 i;
     189  uint16 path_depth;
    183190  if(path_stack == NULL)
    184191    return -1;
     
    210217
    211218    if(!found_cur)
    212       return 0;
     219      return 1;
    213220  }
    214221
     
    242249
    243250
     251static void usage(void)
     252{
     253  fprintf(stderr, "Usage: readreg [-f <PREFIX_FILTER>] [-t <TYPE_FILTER>] "
     254                  "[-v] [-s] <REGISTRY_FILE>\n");
     255  /* XXX: replace version string with Subversion property? */
     256  fprintf(stderr, "Version: 0.2\n");
     257  fprintf(stderr, "\n\t-v\t sets verbose mode.");
     258  fprintf(stderr, "\n\t-f\t a simple prefix filter.");
     259  fprintf(stderr, "\n\t-t\t restrict results to a specific type.");
     260  fprintf(stderr, "\n\t-s\t prints security descriptors.");
     261  fprintf(stderr, "\n");
     262}
     263
     264/* Globals, influenced by command line parameters */
     265bool print_verbose = false;
     266bool print_security = false;
     267bool prefix_filter_enabled = false;
     268bool type_filter_enabled = false;
     269char* prefix_filter = NULL;
     270char* type_filter = NULL;
     271char* registry_file = NULL;
     272
     273
    244274int main(int argc, char** argv)
    245275{
     
    249279  REGF_NK_REC* root;
    250280  int retr_path_ret;
    251   char* path = "/ControlSet002/Services/Eventlog/";
    252 
     281  uint32 argi;
     282
     283  /* Process command line arguments */
    253284  if(argc < 2)
    254285  {
    255286    printf("ERROR: Requires 1 argument.\n");
    256     return 1;
    257   }
    258 
    259   f = regfio_open( argv[1] );
     287    usage();
     288    exit(1);
     289  }
     290 
     291  for(argi = 1; argi < argc; argi++)
     292  {
     293    if (strcmp("-f", argv[argi]) == 0)
     294    {
     295      if(++argi > argc)
     296      {
     297        fprintf(stderr, "ERROR: '-f' option requires parameter.\n");
     298        usage();
     299        exit(1);
     300      }
     301      if((prefix_filter = strdup(argv[argi])) == NULL)
     302      {
     303        fprintf(stderr, "ERROR: Memory allocation problem.\n");
     304        exit(2);
     305      }
     306      prefix_filter_enabled = true;
     307    }
     308    else if (strcmp("-t", argv[argi]) == 0)
     309    {
     310      if(++argi > argc)
     311      {
     312        fprintf(stderr, "ERROR: '-t' option requires parameter.\n");
     313        usage();
     314        exit(1);
     315      }
     316      if((prefix_filter = strdup(argv[argi])) == NULL)
     317      {
     318        fprintf(stderr, "ERROR: Memory allocation problem.\n");
     319        exit(2);
     320      }
     321      type_filter_enabled = true;
     322    }
     323    else if (strcmp("-s", argv[argi]) == 0)
     324      print_security = true;
     325    else if (strcmp("-v", argv[argi]) == 0)
     326      print_verbose = true;
     327    else if (argv[argi][0] == '-')
     328    {
     329      fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
     330      usage();
     331      exit(1);
     332    }
     333    else
     334    {
     335      if((registry_file = strdup(argv[argi])) == NULL)
     336      {
     337        fprintf(stderr, "ERROR: Memory allocation problem.\n");
     338        exit(2);
     339      }     
     340    }
     341  }
     342
     343  f = regfio_open(registry_file);
     344  if(f == NULL)
     345  {
     346    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
     347    exit(1);
     348  }
    260349  root = regfio_rootkey(f);
    261 
    262350  nk_stack = void_stack_new(1024);
     351
    263352  if(void_stack_push(nk_stack, root))
    264353  {
    265     path_stack = path2Stack(path);
     354    path_stack = path2Stack(prefix_filter);
    266355    if(void_stack_size(path_stack) < 1)
    267356      printKeyTree(f, nk_stack, "");
    268357    else
    269358    {
    270       retr_path_ret = retrievePath(f, nk_stack,
    271                                    path_stack);
     359      retr_path_ret = retrievePath(f, nk_stack, path_stack);
    272360      if(retr_path_ret == 1)
    273         printf("WARNING: specified path not found.\n");
     361        fprintf(stderr, "WARNING: specified path not found.\n");
    274362      else if(retr_path_ret != 0)
    275         printf("ERROR\n");
    276     }
     363        fprintf(stderr, "ERROR:\n");
     364    }
     365  }
     366  else
     367  {
     368    fprintf(stderr, "ERROR: Memory allocation problem.\n");
     369    exit(2);
    277370  }
    278371  void_stack_destroy(nk_stack);
Note: See TracChangeset for help on using the changeset viewer.