コード例 #1
0
ファイル: mathprep.c プロジェクト: jlinnosa/mathprep
int main(int argc, char *argv[])
{
	int x, y, op, prev_x, prev_y, prev_op, result;
	char operator, *str;

	printf("mathprep\n\n");
	init_randomness();

	x = y = op = 0;
	if ((str = malloc(BUFFSIZE)) == NULL)
		exit(EXIT_FAILURE);

	for (;;) {
		prev_x = x;
		prev_y = y;
		prev_op = op;

		do {
			x = random_int(1, 8);
			y = random_int(1, 3);
			op = random_int(1, 2);
		} while (x == prev_x && y == prev_y && op == prev_op);

		switch (op) {
		case OP_ADD:
			operator = '+';
			result = x + y;
			break;
		case OP_SUB:
			operator = '-';
			result = x - y;
			break;
		default:
			/* this should not happen :) */
			assert(0);
		};

		snprintf(str, BUFFSIZE, "%i %c %i", x, operator, y);
		if (!ask(str, result))
			break;
	}

	free(str);
	exit(EXIT_SUCCESS);
}
コード例 #2
0
ファイル: genuuid.c プロジェクト: sunny256/suuid
struct uuid_result create_and_log_uuids(const struct Options *opt)
{
	struct uuid_result retval;
	char *rcfile = NULL, *logfile = NULL;
	unsigned int i, count;
	struct Rc rc;
	struct Entry entry;
	struct Logs logs;

	assert(opt);

	logs.logfp = NULL;
	count = opt->count;
	retval.count = 0;
	memset(retval.lastuuid, 0, UUID_LENGTH + 1);
	retval.success = TRUE;
	init_xml_entry(&entry);

	/*
	 * Get information about the environment; hostname, current directory, 
	 * tty, location of rc file and log directory, etc.
	 */

	if (init_randomness() == EXIT_FAILURE) {
		retval.success = FALSE;
		goto cleanup;
	}

	rcfile = get_rcfilename(opt);
	if (read_rcfile(rcfile, &rc) == EXIT_FAILURE) {
		retval.success = FALSE;
		goto cleanup;
	}

	if (fill_entry_struct(&entry, &rc, opt) == EXIT_FAILURE) {
		retval.success = FALSE;
		goto cleanup;
	}

	logfile = get_log_prefix(&rc, opt, ".xml");
	if (!logfile) {
		retval.success = FALSE;
		goto cleanup;
	}

	signal(SIGHUP, sighandler);
	signal(SIGINT, sighandler);
	signal(SIGQUIT, sighandler);
	signal(SIGPIPE, sighandler);
	signal(SIGTERM, sighandler);

	/*
	 * Open the log file. If it's missing, create it.
	 */

	logs.logfp = open_logfile(logfile);
	if (!logs.logfp) {
		retval.success = FALSE;
		goto cleanup;
	}

	/*
	 * Generate the UUIDs and write them to the log file.
	 */

	if (opt->uuid)
		count = 1;
	for (i = 0; i < count; i++) {
		if (!process_uuid(&logs, &rc, opt, &entry)) {
			retval.success = FALSE;
			goto cleanup;
		}
		retval.count++;
		if (should_terminate)
			break;
	}
	if (valid_uuid(entry.uuid, TRUE))
		strncpy(retval.lastuuid, entry.uuid, UUID_LENGTH + 1);

	/*
	 * Check that the correct amount of UUIDs were created.
	 */

	if (retval.count < opt->count)
		fprintf(stderr, "%s: Generated only %u of %u UUIDs\n",
		                progname, retval.count, opt->count);

	/*
	 * Close up the shop and go home.
	 */

cleanup:
	if (logs.logfp && (close_logfile(logs.logfp) == EXIT_FAILURE))
		retval.success = FALSE;

	free(logfile);
	free_sess(&entry);
	free_tags(&entry);
	free(rc.uuidcmd);
	free(rc.macaddr);
	free(rc.hostname);
	free(entry.txt);
	free(entry.date);
	free(entry.cwd);
	free(rcfile);

	return retval;
}