Beispiel #1
0
////
// scan_opts
//
//   Uses getopt() to scan options from the command line.  Sets
//   necessary flags.  Prints an error message for incorrect options.
//
void scan_opts (int argc, char **argv, struct options *options) {
   int option;
   opterr = 0;
   for(;;) {
      option = getopt (argc, argv, "s:v:r");
      if (option == EOF) break;
      switch (option) {
         case 's': options->server = TRUE;
                   options->serverIP = optarg;                break;
         case 'v': options->vote = TRUE;
                   options->candidateName = optarg;
                   options->voterID = argv[argc - 1];         break;
         case 'r': options->results = TRUE;                   break;
         default:  errprintf ("%:bad option (%c)\n", optopt);
                   options->badopt = TRUE;                    break;
      }
   }
   // Print error for bad options.
   if (optind > argc || options->badopt || (argc > optind + 1)) {
      errprintf ("%s: %s %s %s %s %s\n", "Usage", get_execname(),
                 "-s[serverIP]", "-v[candidateName]", "-r",
                 "voterIDnumber");
      error ("bad usage");
   }
   // there must be a voter ID number with a vote
   if (options->vote && (argc != (optind + 1))) {
      errprintf ("%s: %s %s %s %s %s\n", "Usage", get_execname(),
                 "-s[serverIP]", "-v[candidateName]", "-r",
                 "voterIDnumber");
      error ("need voter ID number to vote");
   }
   // If required option -s is not present, then end program.
   if (!options->server) {
      errprintf ("%s: %s %s %s %s %s\n", "Usage", get_execname(),
                 "-s[serverIP]", "-v[candidateName]", "-r",
                 "voterIDnumber");
      error ("no server IP");
   }
   // If any argument exceeds a length of 255
   if ((options->serverIP != NULL && strlen (options->serverIP) >  255)
       || (options->candidateName != NULL &&
           strlen (options->candidateName) >  255) ||
       (options->voterID != NULL && strlen (options->voterID) >  255 ))
      error ("no argument should exceed a length of 255 characters");
}
Beispiel #2
0
void veprintf (const char* format, va_list args) {
   assert (execname != NULL);
   assert (format != NULL);
   fflush (NULL);
   if (strstr (format, "%:") == format) {
      fprintf (stderr, "%s: ", get_execname ());
      format += 2;
   }
   vfprintf (stderr, format, args);
   fflush (NULL);
}
Beispiel #3
0
void scanner_userinit (void) {
   if (basefilename == NULL)
      errprintf ("%s: %s: %s: %s: %s\n", get_execname(), "Error",
                 "lyutils.c", "scanner_userinit()",
                 "null file pointer");
   char *tokname = malloc (strlen (basefilename) + strlen (".tok") + 1);
   xstrcpy (tokname, basefilename);
   xstrcat (tokname, ".tok");
   tokfile = fopen (tokname, "w+");
   free (tokname);
}