コード例 #1
0
bool argument_is_valid(int argc, char * const argv[]){
    int opt;
    while((opt=getopt(argc,argv,"hVo:"))!=-1){
	switch (opt){
	    case 'h':
		usage_print(argv[0]);
		exit(0);
	    case 'V':
		verbose=true;
		break;
	    case 'o':
		if ((outFile=fopen(optarg,"w"))==NULL){
		    fprintf(stderr,"Cannot open %s for writing!\n",optarg);
		    exit(EXIT_FAILURE);
		}
		break;
	    default: /* '?' */
		usage_print(argv[0]);
		exit(EXIT_FAILURE);
	}
    }
    if (optind+2-1 > argc) {
	fprintf(stderr, "Not enough arguments.\n");
	usage_print(argv[0]);
	exit(-1);
    }

    tmplFile_name=argv[optind];
    if ((tmplFile=fopen(tmplFile_name,"r"))==NULL){
	fprintf(stderr,"Cannot open %s for reading!\n",tmplFile_name);
	exit(-1);
    }

    cinFile_name=argv[optind+1];
    if ((cinFile=fopen(cinFile_name,"r"))==NULL){
	fprintf(stderr,"Cannot open %s for reading!\n",cinFile_name);
	exit(-1);
    }

    if ((cinTableFile=fdopen(mkstemp(cinTableFile_name),"w+"))==NULL){
	fprintf(stderr,"Cannot create temporary file %s for reading!\n",cinTableFile_name);
	exit(-2);
    }

    if ((cinGouCiFile=fdopen(mkstemp(cinGouCiFile_name),"w+"))==NULL){
	fprintf(stderr,"Cannot create temporary file %s for reading!\n",cinGouCiFile_name);
	exit(-2);
    }

    if (!outFile){
	outFile=stdout;
    }
    return true;
}
コード例 #2
0
int main(int argc, char *argv[])
{
    char *secret = NV_DATA_SECRET;
    size_t size = NV_DATA_SIZE;
    size_t chunk_size = NV_DATA_CHUNK_SIZE;
    char *md5_string = NULL;
    char *path;
    int rc;

    if (argc < 2) {
        usage_print();
        return 1;
    }

    path = argv[1];

    md5_string = ipc_nv_data_md5_calculate(path, secret, size, chunk_size);
    if (md5_string == NULL) {
        fprintf(stderr, "Calculating nv_data backup md5 failed\n");
        return 1;
    }

    printf("%s", md5_string);

    free(md5_string);

    return 0;
}
コード例 #3
0
ファイル: gaussian-add.c プロジェクト: bmallred/gaussian-add
/**
 * Validate user input.
 *
 * @param start The number to start our computations.
 * @param end The number to end our computations.
 */
void validate_input(int start, int end) {
  if (end <= start || end <= 1) {
    usage_print();
    exit(EXIT_FAILURE);
  }

  if ((start + (end - 1)) % 2 != 0) {
    printf("\nThe sum of your list is not an even numbered set\n");
    printf("Example:\n 1 to 100\n 11 to 40\n 200 to 399 \n\n");
    exit(EXIT_FAILURE);
  }
}
コード例 #4
0
int main(int argc, char * const argv[]){
    if (!argument_is_valid(argc,argv)){
	usage_print(argv[0]);
	return -1;
    }

    if (!cinFile_scan()){
	fprintf(stderr,"Failed when reading %s!\n",cinFile_name);
	return 1;
    }

    if (!files_merge()){
	fprintf(stderr,"Failed when merging files!\n");
	return 3;
    }

    fclose(cinFile);
    fclose(tmplFile);
    if (outFile!=stdout){
	fclose(outFile);
    }

    return 0;
}
コード例 #5
0
ファイル: gaussian-add.c プロジェクト: bmallred/gaussian-add
/**
 * Main entry point.
 *
 * @param argc The argument count.
 * @param argv The argument vector.
 * @return The exit code.
 */
int main(int argc, char *argv[]) {
  if (argc == 1) {
    printf("\nThis program needs some arguments ...  \n Try add_list -h \n\n");
  }

  int option = 0;
  int start = -1;
  int end = 0;

  // Handle command line arguments.
  while ((option = getopt(argc, argv, "hvs:e:")) != -1) {
    switch (option) {
      case 's':
        start = atoi(optarg);
        break;

      case 'e':
        end = atoi(optarg);
        break;

      case 'v':
        version_print();
        exit(EXIT_FAILURE);

       case 'h':
        help_print();
        exit(EXIT_FAILURE);

       default:
        usage_print();
        exit(EXIT_FAILURE);
    }
  }

  // Validate our input.
  validate_input(start, end);

  // Create an arrays for threading.
  int threadCount = find_number_of_threads(start, end);
  ThreadData data[threadCount];
  pthread_t threadIds[threadCount];
  int i = 0;

  // Spawn threads.
  for (i = 0; i < threadCount; i++) {
    data[i].start = start + (i * CHUNK_SIZE);
    data[i].stop = data[i].start + CHUNK_SIZE - 1;
    data[i].result = 0;

    // If we've exceeded the end amount or reached our maximum threads then set the stop to the actual end.
    if ((data[i].stop > end) || (data[i].stop < end && i + 1 == threadCount)) {
      data[i].stop = end;
    }

    //// NOTE: Uncomment to view what ranges each thread is responsible for.
    //printf("%d: %d to %d\n", i + 1, data[i].start, data[i].stop);

    if (pthread_create(&threadIds[i], NULL, &gaussian_add, (void *)&data[i]) != 0) {
      printf("Could not create thread #%d.", i);
    }
  }

  // Wait for the threads to finish.
  unsigned long long sum = 0;
  for (i = 0; i < threadCount; i++) {
    if (pthread_join(threadIds[i], NULL) != 0) {
      printf("Could not join thread #%d\n", i + 1);
    }

    sum += data[i].result;
  }

  // Inform the user of the results and of the short cut.
  summary_print(threadCount, sum, start, end);

  return 0;
}
コード例 #6
0
ファイル: main.c プロジェクト: clinew/2048
int main(int argc, char* argv[]) {
	struct arguments arguments;
	struct board board;
	char buffer[256];
	int format;
	char input;
	char* message;
	int status; // Game status.
	struct termios term_settings;
	int valid;

	// Parse arguments.
	message = arguments_parse(&arguments, argc, argv);
	if (message) {
		usage_print(message);
	}

	// Apply arguments.
	valid = 1; // Hack; overload to determine whether to play or quit.
	if (arguments.flags & ARGUMENTS_VERSION) {
		printf("%s\n", VERSION);
		valid = 0;
	}
	if (arguments.flags & ARGUMENTS_LEGAL) {
		printf("%s\n", legal);
		valid = 0;
	}
	if (arguments.flags & ARGUMENTS_HELP) {
		usage_print(NULL);
	}
	if (!valid) {
		exit(EXIT_SUCCESS);
	}
	if (arguments.flags & ARGUMENTS_MODE) {
		if (arguments.mode == mode_format) {
			setup_signal_handlers();
			enter_alternate_buffer();
			enter_format_mode(&term_settings);
			format = 1;
		} else if (arguments.mode == mode_plain) {
			format = 0;
		}
	} else if (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO)) {
		setup_signal_handlers();
		enter_alternate_buffer();
		enter_format_mode(&term_settings);
		format = 1;
	}
	if (arguments.flags & ARGUMENTS_SEED) {
		srand(arguments.seed);
	} else {
		srand(time(NULL));
	}

	// Set up the board.
	board_init(&board);
	if (!format) {
		fputs(legal, stdout);
	}

	// Play the game.
play:
	valid = 1;
	while (!(status = board_done(&board))) {
		// Set up screen for next move.
		// Sorry about this ugly call.
		screen_print(&board, valid ? (format ? "\n\n" : "") :
			"\nInvalid move.\n", format);

		// Get the player's move.
		input = yoink(format);

		// Process player's move.
		if (input == 'w' || input == 'k') {
			valid = board_move_up(&board);
		} else if (input == 's' || input == 'j') {
			valid = board_move_down(&board);
		} else if (input == 'a' || input == 'h') {
			valid = board_move_left(&board);
		} else if (input == 'd' || input == 'l') {
			valid = board_move_right(&board);
		} else if (input == 'n') {
			// Start a new game (or not) based on user input.
			printf("Start a new game? [y/N] ");
			input = yoink(format);
			if (input == 'y' || input == 'Y') {
				board_reset(&board);
			}
			continue;
		} else if (input == '?') {
			help_print();
			if (format) {
				printf("\nPress any key to continue.");
				input = yoink(format);
			}
			continue;
		} else {
			valid = 0;
		}

		// End player's move.
		if (valid) {
			board_plop(&board);
		}
	}

	// Print the final board.
	snprintf(buffer, sizeof(buffer),
		"\nGame over, you %s!\n\nPlay again? [y/n]\n",
		(status < 0) ? "LOSE" : "WIN");
	screen_print(&board, buffer, format);

	// Check for new game.
	while ((input = yoink(format)) != 'y' && input != 'Y' &&
		 input != 'n' && input != 'N') {
		 screen_print(&board, buffer, format);
	}
	if (input == 'y' || input == 'Y') {
		board_reset(&board);
		goto play;
	}

	// Restore the terminal.
	if (format) {
		restore_mode();
		leave_alternate_buffer();
	}

	// Free the board.
	board_free(&board);

	// Return success.
	return EXIT_SUCCESS;
}