示例#1
0
int main(int argc, char **argv)
{
	int c, opt;
	extern char *optarg;
	struct stat statbuf;
	char passwd_path[PATH_MAX];
	char group_path[PATH_MAX];
	FILE *passwd_file = NULL;
	FILE *group_file = NULL;
	FILE *devtable = NULL;

	umask (0);

	while ((opt = getopt_long(argc, argv, "D:d:r:qhv", 
			long_options, &c)) >= 0) {
		switch (opt) {
		case 'D':
			devtable = xfopen(optarg, "r");
			if (fstat(fileno(devtable), &statbuf) < 0)
				perror_msg_and_die(optarg);
			if (statbuf.st_size < 10)
				error_msg_and_die("%s: not a proper device table file", optarg);
			break;
		case 'h':
			fprintf(stderr, helptext);
			exit(1);
		case 'r':
		case 'd':				/* for compatibility with mkfs.jffs, genext2fs, etc... */
			if (rootdir != default_rootdir) {
				error_msg_and_die("root directory specified more than once");
			}
			rootdir = xstrdup(optarg);
			break;

		case 'v':
			fprintf(stderr, "makedevs revision %.*s\n",
					(int) strlen(revtext) - 13, revtext + 11);
			exit(1);
		}
	}

	// Get name-id mapping
	sprintf(passwd_path, "%s/etc/passwd", rootdir);
	sprintf(group_path, "%s/etc/group", rootdir);
	if ((passwd_file = fopen(passwd_path, "r")) != NULL) {
		get_list_from_file(passwd_file, &usr_list);
		fclose(passwd_file);
	}
	if ((group_file = fopen(group_path, "r")) != NULL) {
		get_list_from_file(group_file, &grp_list);
		fclose(group_file);
	}

	// Parse devtable
	if(devtable) {
		parse_devtable(devtable);
		fclose(devtable);
	}

	// Free list
	free_list(usr_list);
	free_list(grp_list);

	return 0;
}
示例#2
0
int main(int argc, char **argv)
{
	int c, opt;
	extern char *optarg;
	struct stat statbuf;
	char passwd_path[PATH_MAX];
	char group_path[PATH_MAX];
	FILE *passwd_file = NULL;
	FILE *group_file = NULL;
	FILE *devtable = NULL;
	DIR *dir = NULL;

	umask (0);

	if (argc==1) {
		fputs( helptext , stderr );
		exit(1);
	}

	while ((opt = getopt_long(argc, argv, "D:d:r:htv",
			long_options, &c)) >= 0) {
		switch (opt) {
		case 'D':
			devtable = xfopen(optarg, "r");
			if (fstat(fileno(devtable), &statbuf) < 0)
				perror_msg_and_die(optarg);
			if (statbuf.st_size < 10)
				error_msg_and_die("%s: not a proper device table file", optarg);
			break;
		case 'h':
			puts(helptext);
			exit(0);
		case 'r':
		case 'd':				/* for compatibility with mkfs.jffs, genext2fs, etc... */
			if (rootdir != default_rootdir) {
				error_msg_and_die("root directory specified more than once");
			}
			if ((dir = opendir(optarg)) == NULL) {
				perror_msg_and_die(optarg);
			} else {
				closedir(dir);
			}
			/* If "/" is specified, use "" because rootdir is always prepended to a
			 * string that starts with "/" */
			if (0 == strcmp(optarg, "/"))
				rootdir = xstrdup("");
			else
				rootdir = xstrdup(optarg);
			break;

		case 't':
			trace = 1;
			break;

		case 'v':
			printf("%s: %s\n", app_name, VERSION);
			exit(0);
		default:
			fputs(helptext,stderr);
			exit(1);
		}
	}

	if (argv[optind] != NULL) {
		fputs(helptext,stderr);
		exit(1);
	}

	// Get name-id mapping
	sprintf(passwd_path, "%s/etc/passwd", rootdir);
	sprintf(group_path, "%s/etc/group", rootdir);
	if ((passwd_file = fopen(passwd_path, "r")) != NULL) {
		get_list_from_file(passwd_file, &usr_list);
		fclose(passwd_file);
	}
	if ((group_file = fopen(group_path, "r")) != NULL) {
		get_list_from_file(group_file, &grp_list);
		fclose(group_file);
	}

	// Parse devtable
	if(devtable) {
		parse_devtable(devtable);
		fclose(devtable);
	}

	// Free list
	free_list(usr_list);
	free_list(grp_list);

	return 0;
}
示例#3
0
void digest_input(string filename)
{
	// Print basic information and advance read cursor past author line.
	// (Author line must always exist and must be the first line.)
	cout << "Processing started..." << endl << endl;
	cout << "Input: " << filename << endl << endl;
	string filename_input = "Texts/" + filename;
	ifstream file_text(filename_input);
	print_data_size(file_text);
	string author_name;
	getline(file_text, author_name);
	string filename_author = "Authors/" + author_name + ".csv";
	string filename_summary = create_file_and_name("Digests/", filename, "-SUM.txt");
	string filename_words = create_file_and_name("Digests/", filename, "-WORD.csv");
	string filename_sentences = create_file_and_name("Digests/", filename, "-SENT.csv");
	cout << "Author: " << author_name << endl << endl;
	cout << "Output: " << filename + "-SUM.txt" << endl <<
		"        " << filename + "-WORD.csv" << endl <<
		"        " << filename + "-SENT.csv" << endl << endl;

	Memory RAM;
	string raw_input;
	cout << "Parsing text: |";
	const int bar_width = 50;
	for (int i = 0; i<bar_width; ++i) { cout << " "; }
	cout << "|  0.00%";
	for (int i = 0; getline(file_text, raw_input); ++i) {
		add_data_from_line(RAM, raw_input);
		// TODO: Make constants settable via command-line options (i.e. 10000, 3000)
		if (RAM.word_list.size() > 10000) {
			cout << endl << endl << "Flushing buffer..." << endl << endl;
			combine_list_file(	RAM.word_list,
								filename_words,
								RAM.word_list.begin() + 3000,
								RAM.word_list.end()	);
			cout << "Parsing text: |";
			for (int i = 0; i<bar_width; ++i) { cout << " "; }
			cout << "|  0.00%";
		}
		// Progress bar stuff:
		if (i % 120 == 0) {
			cout << "\b\b\b\b\b\b\b\b"; // "| ##.##%"
			for (int i = 0; i < bar_width; ++i) { cout << "\b"; }
			ifstream file_sizer(filename_input);
			file_sizer.seekg(0, ios::end);
			float size = static_cast<float>(file_sizer.tellg());
			file_sizer.close();
			float current = static_cast<float>(file_text.tellg());
			float percentage = current / size * 100;
			int chars_filled = static_cast<int>(floor(percentage/100.0*bar_width));
			for (int i = 0; i < bar_width; ++i) {
				if (i < chars_filled) {
					cout << "#";
				} else {
					cout << " ";
				}
			}
			std::streamsize precision_init = cout.precision();
			int correct_precision = get_precision(5, percentage);
			cout.precision(correct_precision);
			std::streamsize width_init = cout.width();
			cout << "| " << std::setw(5) << percentage << "%";
			cout.precision(precision_init);
			cout << std::setw(width_init);
		}
	}
	cout << "\b\b\b\b\b\b\b\b"; // "| ##.##%"
	for (int i = 0; i < bar_width; ++i) { cout << "\b"; }
	for (int i = 0; i < bar_width; ++i) { cout << "#"; }
	cout << "| -DONE-" << endl << endl;

	// Final write:
	cout << "Writing frequency files..." << endl << endl;
	std::sort(RAM.word_list.begin(), RAM.word_list.end(), word_compare());
	combine_list_file(RAM.word_list, filename_words);
	get_list_from_file(RAM.word_list, filename_words);
	ofstream file_sentences(filename_sentences);
	file_sentences << "WORDS" << endl;
	for (unsigned int i = 0; i < RAM.sentence_len.size(); ++i) {
		file_sentences << RAM.sentence_len[i] << endl;
	}
	file_sentences.close();
	cout << "Writing summary file..." << endl << endl;
	write_summary(filename_summary, &RAM, author_name);

	cout << endl << "Done!" << endl;
}