Ejemplo n.º 1
0
static void read_arg(char **argv,int argc, char **nombre_centro,int *capacidad,int *inventario,int *suministro,int *puerto,int *tiempo) {
  // getopt no parsea argumentos cuyo flag es mas de un caracter
  int cp = FALSE;
  {
   int j;
   for(j = 0 ; j < 6 ; j++)
     if(!strcmp(argv[2*j + 1],"-cp")) {
       cp = TRUE;
       *capacidad = atoi(argv[2*j+2]);
       break;
     }

  }
  
  if(!cp) {
    print_use();
    exit(EXIT_FAILURE);
  }
  else 
    while(TRUE){
      int options = getopt(argc,argv,"n:i:t:s:p:c:");
      if (options == -1)
        break;
      switch(options) {
        case 'n':
          *nombre_centro = optarg;
          break;      
        case 'i':
          *inventario = atoi(optarg);
          break;
        case 't':
          *tiempo = atoi(optarg);
          break;
        case 's':
          *suministro = atoi(optarg);
          break;
        case 'p':
          // Para hacer simple el programa, el numero de puerto se valida de una vez aca.
          if(strlen(optarg) != 5) {
            printf("Error: El numero de puerto debe tener 5 digitos.\n");
            exit(EXIT_FAILURE);
          }
          *puerto = atoi(optarg);
          break;
        case 'c':
          break;
        default:
          print_use();
          exit(EXIT_FAILURE);
      }
    }
}
Ejemplo n.º 2
0
static void print_use(struct TestModule *mod)
{
    if (mod->next)
        print_use(mod->next);

    fputs("  use ", fout);
    fwrite(mod->name, mod->len, 1, fout);
    fputs("\n", fout);
}
Ejemplo n.º 3
0
static int generate_set(struct TestSet *set, int *set_i)
{
    int test_i;

    if (set->next)
        generate_set(set->next, set_i);

    tolerance = (set->tolerance > 0.0) ? set->tolerance : DEFAULT_TOLERANCE;

    (*set_i)++;
    fprintf(fout, "subroutine funit_set%i\n", *set_i);
    fputs("  use funit\n", fout);
    
    if (set->mods)
        print_use(set->mods);
    
    fputs("\n", fout);
    fputs("  implicit none\n\n", fout);
    fputs("  character*1024 :: funit_message_\n", fout);
    fputs("  logical :: funit_passed_\n\n", fout);
    
    if (set->code)
        generate_code(set->code);
    
    if (set->tests) {
        size_t max_name = max_test_name_width(set->tests);
        test_i = 0;
        generate_test_call(set, set->tests, &test_i, max_name);
    }
    
    fputs("contains\n\n", fout);
    
    if (set->setup)
        generate_support(set->setup, "setup");
    if (set->teardown)
        generate_support(set->setup, "teardown");
    if (set->tests) {
        test_i = 0;
        if (generate_test(set->tests, &test_i))
            return -1;
    }

    fprintf(fout, "end subroutine funit_set%i\n", *set_i);

    return 0;
}
Ejemplo n.º 4
0
int			get_arguments(options_t *options, int ac, char **av) {
	char	**rest;
	u_int	cnt;

	options->port = STARCRAFT_PORT;
	
	rest = read_options(options, ac, av, sov_arguments);
	if (!rest)
		return 0;
	
	if (!options->pcap_filter) {
		options->pcap_filter = malloc(SNAP_LEN);
		snprintf(options->pcap_filter, SNAP_LEN, PCAP_FILTER_FORMAT, options->port);
	}
	cnt = count_args(rest);
	if (cnt < 2) {
		fprintf(stderr, "Error: not enough arguments." EOL);
		print_use();
		return 0;
	}
	options->capture_device_name = *rest++;
	options->hosts = rest;
	options->host_count = cnt - 1;

	if (options->inject_device_name)
	{
		if (options->bind_udp_port) {
			fprintf(stderr, "Invalid arguments: cannot bind UDP and use injection" EOL);
			return 0;
		}
#ifdef WIN32
		if (!options->inject_ethernet_source || !options->inject_ethernet_destination) {
			fprintf(stderr, "Invalid arguments: source and destination MAC addresses" EOL
				"are required when using injection under windows." EOL);
			return 0;
		}
#endif
	}

	return 1;
}
Ejemplo n.º 5
0
int main(int argc, char **argv) {

  // Validacion y lectura de argumentos

  if(argc != 13){
    print_use();
    exit(EXIT_FAILURE);
  }

  char * nombre_centro;
  int suministro, puerto;

  read_arg(argv,argc,&nombre_centro,&capacidad,&inventario,&suministro,&puerto,&tiempo);

  if (!valid_arg(nombre_centro,capacidad,inventario,suministro,tiempo))
   exit(EXIT_FAILURE); 

  log_file_name = (char *) malloc(strlen(nombre_centro)+8);
  sprintf(log_file_name,"log_%s.txt",nombre_centro);
  
  log_file = fopen(log_file_name,"w");
  if(!log_file) {
    printf("Error: no se pudo crear el archivo log el archivo\n.");
    exit(EXIT_FAILURE);
  }

  fprintf(log_file,"Inventario inicial: %d litros.\n",inventario);

  pthread_attr_init(&attr1);
  pthread_attr_setdetachstate(&attr1,PTHREAD_CREATE_JOINABLE);
  if (pthread_create(&thread_inv, &attr1, inventario_suministro, (void *) suministro)) {
    printf("Error: no se pudo crear el hilo para controlar el inventario.");
    exit(EXIT_FAILURE);
  }

  pthread_attr_init(&attr2);
  pthread_attr_setdetachstate(&attr2,PTHREAD_CREATE_JOINABLE);
  if (pthread_create(&thread_func, &attr2, tiempo_funcionamiento,NULL)) {
    printf("Error: no se pudo crear el hilo para controlar el funcionamiento.");
    exit(EXIT_FAILURE);
  }

  signal(SIGUSR1,finish);
 
  pthread_attr_init(&attr3);
  pthread_attr_setdetachstate(&attr3,PTHREAD_CREATE_JOINABLE);
  if (pthread_create(&thread_exit, &attr3, tiempo_exit,NULL)) {
    printf("Error: no se pudo crear el hilo para salida.");
    exit(EXIT_FAILURE);
  }

  int newSocketID;
  socklen_t addrlen;
  struct sockaddr_in dirServ, dirCli;
  char buffer[256];
  pthread_t h;

  if (pthread_mutex_init(&mtx, NULL) !=0) {
    error("Error mutex");
  }
  socketID = socket(AF_INET,SOCK_STREAM,0);
  if (socketID == 0) {
    error("Error abriendo el socket");
  }

  bzero((char*)&dirServ,sizeof(dirServ));

  dirServ.sin_family = AF_INET;
  dirServ.sin_port = htons(puerto);
  dirServ.sin_addr.s_addr = htonl(INADDR_ANY);
  
  if (bind(socketID,(struct sockaddr *)&dirServ,sizeof(dirServ)) < 0 ) {
    error("Error en la conexion");
  }
  
  listen(socketID,5);
  addrlen = sizeof(dirCli);

  while (TRUE) {
    newSocketID = accept(socketID,(struct sockaddr *)&dirCli,&addrlen);
    if (newSocketID < 0) {
      error("Error aceptando la conexion");
    }
    pthread_create(&h,NULL,procesarPeticion, (void *)newSocketID);
  }
}