示例#1
0
int o_divr(const char *basemap, const char *covermap, const char *outputmap,
	   int usecats, struct Categories *cats)
{
    struct Popen stats_child, reclass_child;
    FILE *stats_fp, *reclass_fp;
    int first;
    long basecat, covercat, catb, catc;
    double area;

    stats_fp = run_stats(&stats_child, basemap, covermap, "-an");
    reclass_fp = run_reclass(&reclass_child, basemap, outputmap);

    first = 1;
    while (read_stats(stats_fp, &basecat, &covercat, &area)) {
	if (first) {
	    first = 0;
	    catb = basecat;
	    catc = 0;
	}
	if (basecat != catb) {
	  write_reclass(reclass_fp, catb, catc, Rast_get_c_cat((CELL *) &catc, cats),
			  usecats);
	    catb = basecat;
	    catc = 0;
	}
	catc++;
    }
    if (!first)
      write_reclass(reclass_fp, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats);

    G_popen_close(&stats_child);
    G_popen_close(&reclass_child);

    return 0;
}
示例#2
0
void load_data(bool force_reload /*= false*/)
{
	bool isNew = make_tmp_dir();
	read_stats();

	if(isNew || force_reload) {
		load_ratings();
		read_data();
	} else {
		load_ratings();
		char vfile[256];
		strcpy(vfile, tmpdir);
		strcat(vfile,"values");
		FILE *tmp = fopen(vfile, "r");
		fscanf(tmp, "%u %u %u %u %u %lg\n", &nRatings, &nUsers, &nItems, &nValidations, &nTests, &mu);
		int lk = 0;
		for(int i = 0 ; i < nUsers; i++ ) {
			fscanf(tmp, "%d\n", &lk);
			umap[lk] = i;
		}
		for(int i = 0 ; i < nItems; i++ ) {
			fscanf(tmp, "%d\n", &lk);
			imap[lk] = i;
		}
		fclose(tmp);
	}
	read_genres();
	read_artists();
	read_albumData();
	read_trackData();
}
示例#3
0
int main(int argc, char* argv[]) {
	struct dirent **namelist;
	int n;
	struct hashtb* stats = NULL;
	char filename[512];

	stats = hashtb_create(sizeof(struct stats_entry), NULL);

	n = scandir(".", &namelist, &selector, alphasort);
	if (n < 0)
		perror("scandir");
	else {
		while (n--) {
			sprintf(filename, "%s/stats.csv", namelist[n]->d_name);
			read_stats(stats, filename);
			free(namelist[n]);
		}
		free(namelist);
	}

	output_statistics(stats, "combined.csv");

	hashtb_destroy(&stats);
	return 0;
}
示例#4
0
void inc_stats (int a, int b, int c) {
	char data[3][6];
	read_stats(data);
	sprintf(data[0], "%i", atoi(data[0])+a);
	sprintf(data[1], "%i", atoi(data[1])+b);
	sprintf(data[2], "%i", atoi(data[2])+c);
	write_stats(data);
}
示例#5
0
    ReadStreamStat ToBinary(io::ReadStream<Read>& stream, size_t buf_size,
            LibraryOrientation orientation) {

        ReadBinaryWriter<Read> read_writer(orientation);
        size_t buffer_reads = buf_size / (sizeof (Read) * 4);
        size_t reads_to_flush = buffer_reads * file_num_;

        std::vector< std::vector<Read> > buf(file_num_, std::vector<Read>(buffer_reads) );
        std::vector< ReadStreamStat > read_stats(file_num_);
        std::vector< size_t > current_buf_sizes(file_num_, 0);
        size_t read_count = 0;

        for (size_t i = 0; i < file_num_; ++i) {
            file_ds_[i]->seekp(0);
            read_stats[i].write(*file_ds_[i]);
        }

        size_t buf_index;
        while (!stream.eof()) {
            buf_index = read_count % file_num_;

            Read& r = buf[buf_index][current_buf_sizes[buf_index]];
            stream >> r;
            read_stats[buf_index].increase(r);

            ++current_buf_sizes[buf_index];
            VERBOSE_POWER(++read_count, " reads processed");

            if (read_count % reads_to_flush == 0) {
                for (size_t i = 0; i < file_num_; ++i) {
                    FlushBuffer(buf[i], read_writer, *file_ds_[i]);
                    current_buf_sizes[i] = 0;
                }
            }
        }

        ReadStreamStat result;
        for (size_t i = 0; i < file_num_; ++i) {
            buf[i].resize(current_buf_sizes[i]);
            FlushBuffer(buf[i], read_writer, *file_ds_[i]);

            file_ds_[i]->seekp(0);
            read_stats[i].write(*file_ds_[i]);
            result.merge(read_stats[i]);
        }

        INFO(read_count << " reads written");
        return result;
    }
示例#6
0
int
o_mode(const char *basemap, const char *covermap, const char *outputmap, int usecats,
       struct Categories *cats)
{
    struct Popen stats_child, reclass_child;
    FILE *stats, *reclass;
    int first;
    long basecat, covercat, catb, catc;
    double value, max;

    stats = run_stats(&stats_child, basemap, covermap, "-an");
    reclass = run_reclass(&reclass_child, basemap, outputmap);

    first = 1;

    while (read_stats(stats, &basecat, &covercat, &value)) {
	if (first) {
	    first = 0;
	    catb = basecat;
	    catc = covercat;
	    max = value;
	}

	if (basecat != catb) {
	  write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, cats),
			  usecats);
	    catb = basecat;
	    catc = covercat;
	    max = value;
	}

	if (value > max) {
	    catc = covercat;
	    max = value;
	}
    }

    if (first) {
	catb = catc = 0;
    }

    write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats);

    G_popen_close(&stats_child);
    G_popen_close(&reclass_child);

    return 0;
}
int main(int argc, char *argv[]) {
    if (argc < 2) {
        fprintf(stderr,
                "usage: procstatlog poll_interval [procname ...] > procstat.log\n\n"
                "\n"
                "Scans process status every poll_interval seconds (e.g. 0.1)\n"
                "and writes data from /proc/stat, /proc/*/stat files, and\n"
                "other /proc status files every time something changes.\n"
                "\n"
                "Scans all processes by default.  Listing some process name\n"
                "substrings will limit scanning and reduce overhead.\n"
                "\n"
                "Data is logged continuously until the program is killed.\n");
        return 2;
    }

    long poll_usec = (long) (atof(argv[1]) * 1000000l);
    if (poll_usec <= 0) {
        fprintf(stderr, "illegal poll interval: %s\n", argv[1]);
        return 2;
    }

    struct data *old_stats = malloc(sizeof(struct data));
    old_stats->name = NULL;
    old_stats->value = NULL;
    while (1) {
        struct timeval before, after;
        gettimeofday(&before, NULL);
        printf("T + %ld.%06ld\n", before.tv_sec, before.tv_usec);

        struct data *new_stats = read_stats(argv + 2, argc - 2);
        diff_stats(old_stats, new_stats);
        free_stats(old_stats);
        old_stats = new_stats;
        gettimeofday(&after, NULL);
        printf("T - %ld.%06ld\n", after.tv_sec, after.tv_usec);

        long elapsed_usec = (long) after.tv_usec - before.tv_usec;
        elapsed_usec += 1000000l * (after.tv_sec - before.tv_sec);
        if (poll_usec > elapsed_usec) usleep(poll_usec - elapsed_usec);
    }

    return 0;
}
示例#8
0
文件: sysmon.c 项目: RPI-WCL/sysmon
/* "pkill --signal 2 sysmon" will be captured here. */
void signal_callback_handler(int signum)
{
    struct stats st;
    struct ext_stats ext_st;
    unsigned long long interval;

    printf("\nCaught signal %d\n", signum);

    /* Overall stats calculation */
    read_stats(&st);
    interval = get_interval(base_stats.uptime, st.uptime);

    /* Overall stats output */
    fprintf(fp, "Mean:\n");
    #if 1
    subst_stats(&st, &base_stats);      /* st -= base_stats */
    fprint_stats(fp, print_flag | MEM_USED_DBL, &st, &mean);
    #else
    compute_ext_stats(interval, &st, &base_stats, &ext_st);
    ext_st.mem = mean.mem;
    subst_stats(&st, &base_stats);      /* st -= base_stats */
    fprint_stats(fp, print_flag | MEM_USED_DBL, &st, &ext_st);
    #endif

    #if 0
    fprintf(fp, "Minimum:\n");
    fprint_stats(fp, print_flag, &st, &min);
    fprintf(fp, "Maximum:\n");
    fprint_stats(fp, print_flag, &st, &max);
    fprintf(fp, "Variance:\n");
    fprint_stats(fp, print_flag | MEM_USED_DBL, &st, &var);
    #endif

    /* All done! */
    #ifndef DEBUG
    fclose(fp);
    #endif
    exit(signum);
}
示例#9
0
/*
 ***************************************************************************
 * Main entry to the sar program.
 ***************************************************************************
 */
int main(int argc, char **argv)
{
	int i, rc, opt = 1, args_idx = 2;
	int fd[2];
	int day_offset = 0;
	char from_file[MAX_FILE_LEN], to_file[MAX_FILE_LEN];
	char ltemp[20];

	/* Get HZ */
	get_HZ();

	/* Compute page shift in kB */
	get_kb_shift();

	from_file[0] = to_file[0] = '\0';

#ifdef USE_NLS
	/* Init National Language Support */
	init_nls();
#endif

	tm_start.use = tm_end.use = FALSE;

	/* Allocate and init activity bitmaps */
	allocate_bitmaps(act);

	init_structures();

	/* Process options */
	while (opt < argc) {

		if (!strcmp(argv[opt], "-I")) {
			if (argv[++opt]) {
				/* Parse -I option */
				if (parse_sar_I_opt(argv, &opt, act)) {
					usage(argv[0]);
				}
			}
			else {
				usage(argv[0]);
			}
		}

		else if (!strcmp(argv[opt], "-D")) {
			/* Option to tell sar to write to saYYYYMMDD data files */
			flags |= S_F_SA_YYYYMMDD;
			opt++;
		}

		else if (!strcmp(argv[opt], "-P")) {
			/* Parse -P option */
			if (parse_sa_P_opt(argv, &opt, &flags, act)) {
				usage(argv[0]);
			}
		}

		else if (!strcmp(argv[opt], "-o")) {
			if (to_file[0]) {
				/* Output file already specified */
				usage(argv[0]);
			}
			/* Save stats to a file */
			if ((argv[++opt]) && strncmp(argv[opt], "-", 1) &&
			    (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
				strncpy(to_file, argv[opt++], MAX_FILE_LEN);
				to_file[MAX_FILE_LEN - 1] = '\0';
			}
			else {
				strcpy(to_file, "-");
			}
		}

		else if (!strcmp(argv[opt], "-f")) {
			if (from_file[0]) {
				/* Input file already specified */
				usage(argv[0]);
			}
			/* Read stats from a file */
			if ((argv[++opt]) && strncmp(argv[opt], "-", 1) &&
			    (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
				strncpy(from_file, argv[opt++], MAX_FILE_LEN);
				from_file[MAX_FILE_LEN - 1] = '\0';
				/* Check if this is an alternate directory for sa files */
				check_alt_sa_dir(from_file, day_offset, -1);
			}
			else {
				set_default_file(from_file, day_offset, -1);
			}
		}

		else if (!strcmp(argv[opt], "-s")) {
			/* Get time start */
			if (parse_timestamp(argv, &opt, &tm_start, DEF_TMSTART)) {
				usage(argv[0]);
			}
		}

		else if (!strcmp(argv[opt], "-e")) {
			/* Get time end */
			if (parse_timestamp(argv, &opt, &tm_end, DEF_TMEND)) {
				usage(argv[0]);
			}
		}

		else if (!strcmp(argv[opt], "-h")) {
			/* Display help message */
			display_help(argv[0]);
		}

		else if (!strcmp(argv[opt], "-i")) {
			if (!argv[++opt] || (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
				usage(argv[0]);
			}
			interval = atol(argv[opt++]);
			if (interval < 1) {
				usage(argv[0]);
			}
			flags |= S_F_INTERVAL_SET;
		}

		else if (!strcmp(argv[opt], "-m")) {
			if (argv[++opt]) {
				/* Parse option -m */
				if (parse_sar_m_opt(argv, &opt, act)) {
					usage(argv[0]);
				}
			}
			else {
				usage(argv[0]);
			}
		}

		else if (!strcmp(argv[opt], "-n")) {
			if (argv[++opt]) {
				/* Parse option -n */
				if (parse_sar_n_opt(argv, &opt, act)) {
					usage(argv[0]);
				}
			}
			else {
				usage(argv[0]);
			}
		}

		else if ((strlen(argv[opt]) > 1) &&
			 (strlen(argv[opt]) < 4) &&
			 !strncmp(argv[opt], "-", 1) &&
			 (strspn(argv[opt] + 1, DIGITS) == (strlen(argv[opt]) - 1))) {
			day_offset = atoi(argv[opt++] + 1);
		}

		else if (!strncmp(argv[opt], "-", 1)) {
			/* Other options not previously tested */
			if ((rc = parse_sar_opt(argv, &opt, act, &flags, C_SAR)) != 0) {
				if (rc == 1) {
					usage(argv[0]);
				}
				exit(1);
			}
			opt++;
		}

		else if (interval < 0) {
			/* Get interval */
			if (strspn(argv[opt], DIGITS) != strlen(argv[opt])) {
				usage(argv[0]);
			}
			interval = atol(argv[opt++]);
			if (interval < 0) {
				usage(argv[0]);
			}
		}

		else {
			/* Get count value */
			if ((strspn(argv[opt], DIGITS) != strlen(argv[opt])) ||
			    !interval) {
				usage(argv[0]);
			}
			if (count) {
				/* Count parameter already set */
				usage(argv[0]);
			}
			count = atol(argv[opt++]);
			if (count < 1) {
				usage(argv[0]);
			}
		}
	}

	/* 'sar' is equivalent to 'sar -f' */
	if ((argc == 1) ||
	    ((interval < 0) && !from_file[0] && !to_file[0])) {
		set_default_file(from_file, day_offset, -1);
	}

	if (tm_start.use && tm_end.use && (tm_end.tm_hour < tm_start.tm_hour)) {
		tm_end.tm_hour += 24;
	}

	/*
	 * Check option dependencies.
	 */
	/* You read from a file OR you write to it... */
	if (from_file[0] && to_file[0]) {
		fprintf(stderr, _("-f and -o options are mutually exclusive\n"));
		exit(1);
	}
	/* Use time start or option -i only when reading stats from a file */
	if ((tm_start.use || INTERVAL_SET(flags)) && !from_file[0]) {
		fprintf(stderr,
			_("Not reading from a system activity file (use -f option)\n"));
		exit(1);
	}
	/* Don't print stats since boot time if -o or -f options are used */
	if (!interval && (from_file[0] || to_file[0])) {
		usage(argv[0]);
	}

	/* Cannot enter a day shift with -o option */
	if (to_file[0] && day_offset) {
		usage(argv[0]);
	}

	if (USE_PRETTY_OPTION(flags)) {
		dm_major = get_devmap_major();
	}

	if (!count) {
		/*
		 * count parameter not set: Display all the contents of the file
		 * or generate a report continuously.
		 */
		count = -1;
	}

	/* Default is CPU activity... */
	select_default_activity(act);

	/* Reading stats from file: */
	if (from_file[0]) {
		if (interval < 0) {
			interval = 1;
		}

		/* Read stats from file */
		read_stats_from_file(from_file);

		/* Free stuctures and activity bitmaps */
		free_bitmaps(act);
		free_structures(act);

		return 0;
	}

	/* Reading stats from sadc: */

	/* Create anonymous pipe */
	if (pipe(fd) == -1) {
		perror("pipe");
		exit(4);
	}

	switch (fork()) {

	case -1:
		perror("fork");
		exit(4);
		break;

	case 0: /* Child */
		if (dup2(fd[1], STDOUT_FILENO) < 0) {
			perror("dup2");
			exit(4);
		}
		CLOSE_ALL(fd);

		/*
		 * Prepare options for sadc.
		 */
		/* Program name */
		salloc(0, SADC);

		/* Interval value */
		if (interval < 0) {
			usage(argv[0]);
		}
		else if (!interval) {
			strcpy(ltemp, "1");
		}
		else {
			sprintf(ltemp, "%ld", interval);
		}
		salloc(1, ltemp);

		/* Count number */
		if (count >= 0) {
			sprintf(ltemp, "%ld", count + 1);
			salloc(args_idx++, ltemp);
		}

		/* Flags to be passed to sadc */
		salloc(args_idx++, "-z");

		/* Writing data to a file (option -o) */
		if (to_file[0]) {
			/* Set option -D if entered */
			if (USE_SA_YYYYMMDD(flags)) {
				salloc(args_idx++, "-D");
			}
			/* Collect all possible activities (option -S XALL for sadc) */
			salloc(args_idx++, "-S");
			salloc(args_idx++, K_XALL);
			/* Outfile arg */
			salloc(args_idx++, to_file);
		}
		else {
			/*
			 * If option -o hasn't been used, then tell sadc
			 * to collect only activities that will be displayed.
			 */
			int act_id = 0;

			for (i = 0; i < NR_ACT; i++) {
				if (IS_SELECTED(act[i]->options)) {
					act_id |= act[i]->group;
				}
			}
			if (act_id) {
				act_id <<= 8;
				snprintf(ltemp, 19, "%d", act_id);
				ltemp[19] = '\0';
				salloc(args_idx++, "-S");
				salloc(args_idx++, ltemp);
			}
		}

		/* Last arg is NULL */
		args[args_idx] = NULL;

		/* Call now the data collector */
		execv(SADC_PATH, args);
		execvp(SADC, args);
		/*
		 * Note: Don't use execl/execlp since we don't have a fixed number of
		 * args to give to sadc.
		 */
		fprintf(stderr, _("Cannot find the data collector (%s)\n"), SADC);
		perror("exec");
		exit(4);
		break;

	default: /* Parent */
		if (dup2(fd[0], STDIN_FILENO) < 0) {
			perror("dup2");
			exit(4);
		}
		CLOSE_ALL(fd);

		/* Get now the statistics */
		read_stats();

		break;
	}

	/* Free structures and activity bitmaps */
	free_bitmaps(act);
	free_structures(act);

	return 0;
}
/*
 ***************************************************************************
 * Main entry to the sar program
 ***************************************************************************
 */
int main(int argc, char **argv)
{
	int opt = 1, args_idx = 2;
	int fd[2];
	char from_file[MAX_FILE_LEN], to_file[MAX_FILE_LEN];
	char ltemp[20];

	/* Get HZ */
	get_HZ();

	/* Compute page shift in kB */
	get_kb_shift();

	from_file[0] = to_file[0] = '\0';

#ifdef USE_NLS
	/* Init National Language Support */
	init_nls();
#endif

	tm_start.use = tm_end.use = FALSE;
	
	/* Allocate and init activity bitmaps */
	allocate_bitmaps(act);

	init_structures();

	/* Process options */
	while (opt < argc) {

		if (!strcmp(argv[opt], "-I")) {
			if (argv[++opt]) {
				/* Parse -I option */
				if (parse_sar_I_opt(argv, &opt, act)) {
					usage(argv[0]);
				}
			}
			else {
				usage(argv[0]);
			}
		}

		else if (!strcmp(argv[opt], "-P")) {
			/* Parse -P option */
			if (parse_sa_P_opt(argv, &opt, &flags, act)) {
				usage(argv[0]);
			}
		}

		else if (!strcmp(argv[opt], "-o")) {
			/* Save stats to a file */
			if ((argv[++opt]) && strncmp(argv[opt], "-", 1) &&
			    (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
				strncpy(to_file, argv[opt++], MAX_FILE_LEN);
				to_file[MAX_FILE_LEN - 1] = '\0';
			}
			else {
				strcpy(to_file, "-");
			}
		}

		else if (!strcmp(argv[opt], "-f")) {
			/* Read stats from a file */
			if ((argv[++opt]) && strncmp(argv[opt], "-", 1) &&
			    (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
				strncpy(from_file, argv[opt++], MAX_FILE_LEN);
				from_file[MAX_FILE_LEN - 1] = '\0';
			}
			else {
				set_default_file(&rectime, from_file);
			}
		}

		else if (!strcmp(argv[opt], "-s")) {
			/* Get time start */
			if (parse_timestamp(argv, &opt, &tm_start, DEF_TMSTART)) {
				usage(argv[0]);
			}
		}

		else if (!strcmp(argv[opt], "-e")) {
			/* Get time end */
			if (parse_timestamp(argv, &opt, &tm_end, DEF_TMEND)) {
				usage(argv[0]);
			}
		}

		else if (!strcmp(argv[opt], "-i")) {
			if (!argv[++opt] || (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) {
				usage(argv[0]);
			}
			interval = atol(argv[opt++]);
			if (interval < 1) {
				usage(argv[0]);
			}
			flags |= S_F_INTERVAL_SET;
		}

		else if (!strcmp(argv[opt], "-n")) {
			if (argv[++opt]) {
				/* Parse option -n */
				if (parse_sar_n_opt(argv, &opt, act)) {
					usage(argv[0]);
				}
			}
			else {
				usage(argv[0]);
			}
		}

		else if (!strncmp(argv[opt], "-", 1)) {
			/* Other options not previously tested */
			if (parse_sar_opt(argv, &opt, act, &flags, C_SAR)) {
				usage(argv[0]);
			}
			opt++;
		}

		else if (interval < 0) {
			/* Get interval */
			if (strspn(argv[opt], DIGITS) != strlen(argv[opt])) {
				usage(argv[0]);
			}
			interval = atol(argv[opt++]);
			if (interval < 0) {
				usage(argv[0]);
			}
		}

		else {
			/* Get count value */
			if ((strspn(argv[opt], DIGITS) != strlen(argv[opt])) ||
			    !interval) {
				usage(argv[0]);
			}
			if (count) {
				/* Count parameter already set */
				usage(argv[0]);
			}
			count = atol(argv[opt++]);
			if (count < 1) {
				usage(argv[0]);
			}
		}
	}

	/* 'sar' is equivalent to 'sar -f' */
	if ((argc == 1) ||
	    ((interval < 0) && !from_file[0] && !to_file[0])) {
		set_default_file(&rectime, from_file);
	}

	if (tm_start.use && tm_end.use && (tm_end.tm_hour < tm_start.tm_hour)) {
		tm_end.tm_hour += 24;
	}

	/*
	 * Check option dependencies
	 */
	/* You read from a file OR you write to it... */
	if (from_file[0] && to_file[0]) {
		fprintf(stderr, _("-f and -o options are mutually exclusive\n"));
		exit(1);
	}
	/* Use time start or option -i only when reading stats from a file */
	if ((tm_start.use || INTERVAL_SET(flags)) && !from_file[0]) {
		fprintf(stderr,
			_("Not reading from a system activity file (use -f option)\n"));
		exit(1);
	}
	/* Don't print stats since boot time if -o or -f options are used */
	if (!interval && (from_file[0] || to_file[0])) {
		usage(argv[0]);
	}

	if (!count) {
		/*
		 * count parameter not set: Display all the contents of the file
		 * or generate a report continuously.
		 */
		count = -1;
	}

	/* Default is CPU activity... */
	select_default_activity(act);
	
	/* ---Reading stats from file */
	if (from_file[0]) {
		if (interval < 0) {
			interval = 1;
		}

		/* Read stats from file */
		read_stats_from_file(from_file);

		return 0;
	}

	/* ---Reading stats from sadc */

	/* Create anonymous pipe */
	if (pipe(fd) == -1) {
		perror("pipe");
		exit(4);
	}

	switch (fork()) {

	case -1:
		perror("fork");
		exit(4);
		break;

	case 0: /* Child */
		if (dup2(fd[1], STDOUT_FILENO) < 0) {
			perror("dup2");
			exit(4);
		}
		CLOSE_ALL(fd);

		/*
		 * Prepare options for sadc
		 */
		/* Program name */
		salloc(0, SADC);

		/* Interval value */
		if (interval < 0) {
			usage(argv[0]);
		}
		else if (!interval) {
			strcpy(ltemp, "1");
		}
		else {
			sprintf(ltemp, "%ld", interval);
		}
		salloc(1, ltemp);

		/* Count number */
		if (count >= 0) {
			sprintf(ltemp, "%ld", count + 1);
			salloc(args_idx++, ltemp);
		}

		/* Flags to be passed to sadc */
		salloc(args_idx++, "-z");
		salloc(args_idx++, "-S");
		salloc(args_idx++, K_ALL);

		/* Outfile arg */
		if (to_file[0]) {
			salloc(args_idx++, to_file);
		}

		/* Last arg is NULL */
		args[args_idx] = NULL;

		/* Call now the data collector */
		execv(SADC_PATH, args);
		execvp(SADC, args);
		/*
		 * Note: don't use execl/execlp since we don't have a fixed number of
		 * args to give to sadc.
		 */
		fprintf(stderr, _("Cannot find the data collector (%s)\n"), SADC);
		perror("exec");
		exit(4);
		break;

	default: /* Parent */
		if (dup2(fd[0], STDIN_FILENO) < 0) {
			perror("dup2");
			exit(4);
		}
		CLOSE_ALL(fd);

		/* Get now the statistics */
		read_stats();
		break;
	}
	return 0;
}
示例#11
0
int printStats (SDL_Surface *ecran) {
	SDL_Surface *jeu = IMG_Load("img/fond.png");
	SDL_Surface *nb0 = IMG_Load("img/nb0.png");
	SDL_Surface *nb1 = IMG_Load("img/nb1.png");
	SDL_Surface *nb2 = IMG_Load("img/nb2.png");
	SDL_Surface *nb3 = IMG_Load("img/nb3.png");
	SDL_Surface *nb4 = IMG_Load("img/nb4.png");
	SDL_Surface *nb5 = IMG_Load("img/nb5.png");
	SDL_Surface *nb6 = IMG_Load("img/nb6.png");
	SDL_Surface *nb7 = IMG_Load("img/nb7.png");
	SDL_Surface *nb8 = IMG_Load("img/nb8.png");
	SDL_Surface *nb9 = IMG_Load("img/nb9.png");
	SDL_Surface *none = IMG_Load("img/none.png");
	
	SDL_Surface *stats = IMG_Load("img/stats.png");
	SDL_Surface *menu = IMG_Load("img/menu2.png");
	SDL_Surface *menus = IMG_Load("img/menu2s.png");
	SDL_Surface *reset = IMG_Load("img/reset.png");
	SDL_Surface *resets = IMG_Load("img/resets.png");
	
	SDL_Surface *nb[10] = { nb0, nb1, nb2, nb3, nb4,
							nb5, nb6, nb7, nb8, nb9};
	
	SDL_Rect pos = {0, 0};
	SDL_Rect pos_reset = {240, 440};
	SDL_Rect pos_men = {440, 440};
	SDL_Rect pos_stats = {100, 150};
	SDL_Rect pos_s[3][5];
	
	SDL_BlitSurface(jeu, NULL, ecran, &pos);
	SDL_BlitSurface(menu, NULL, ecran, &pos_men);
	SDL_BlitSurface(reset, NULL, ecran, &pos_reset);
	SDL_BlitSurface(stats, NULL, ecran, &pos_stats);
	
	int continuer = 1;
	SDL_Event event;
	int x_m, y_m;
	int i, j;
	char data[3][6];
	
	init_pos_s (pos_s);
	
	read_stats(data);
	for (i = 0; i < 3; i++) {
		for (j = 0; data[i][j] != '\0'; j++) {
			SDL_BlitSurface(nb[data[i][j] - '0'], NULL, ecran, &pos_s[i][j]);
		}
	}
	
	SDL_Flip(ecran);
	
	while (continuer == 1) {
    	SDL_WaitEvent(&event);
        switch (event.type) {
           	case SDL_QUIT:
               	continuer = 0;
               	break;
            case SDL_MOUSEMOTION:
            	x_m = event.motion.x;
	           	y_m = event.motion.y;
           	
           		if (is_over(x_m, y_m, *menu, pos_men)) {
           			SDL_BlitSurface(menus, NULL, ecran, &pos_men);
           		} else if (is_over(x_m, y_m, *reset, pos_reset)) {
           			SDL_BlitSurface(resets, NULL, ecran, &pos_reset);
           		} else {
           			SDL_BlitSurface(menu, NULL, ecran, &pos_men);
           			SDL_BlitSurface(reset, NULL, ecran, &pos_reset);
           		}
           		break;
           	case SDL_MOUSEBUTTONUP:
            	x_m = event.button.x;
	           	y_m = event.button.y;
	           	
            	if (is_over(x_m, y_m, *menu, pos_men)) {
            		continuer = 2;
            	} else if (is_over(x_m, y_m, *reset, pos_reset)) {
            		reset_stats(data);
					for (i = 0; i < 3; i++) {
						for (j = 0; data[i][j] != '\0'; j++) {
							SDL_BlitSurface(none, NULL, ecran, &pos_s[i][j]);
						}
						SDL_BlitSurface(nb[0], NULL, ecran, &pos_s[i][0]);
					}
            	}
            	break;
            default:
            	break;
    	}
    	SDL_Flip(ecran);
    }
            	
	return continuer;
}
示例#12
0
int o_sdev(const char *basemap, const char *covermap, const char *outputmap,
	   int usecats, struct Categories *cats)
{
    struct Popen stats_child, reclass_child;
    FILE *stats, *reclass;
    int first, mem, i, count;
    long basecat, covercat, catb, catc;
    double value, sdev, x;
    double *tab;


    mem = MEM * sizeof(double);
    tab = (double *)G_malloc(mem);

    stats = run_stats(&stats_child, basemap, covermap, "-cn");
    reclass = run_reclass(&reclass_child, basemap, outputmap);

    first = 1;
    while (read_stats(stats, &basecat, &covercat, &value)) {
	if (first) {
	    first = 0;
	    catb = basecat;
	    catc = covercat;
	    i = 0;
	    count = 0;
	}

	if (basecat != catb) {
	    s_dev(tab, count, &sdev);
	    fprintf(reclass, "%ld = %ld %f\n", catb, catb, sdev);
	    catb = basecat;
	    catc = covercat;
	    count = 0;
	}

	if (usecats)
	    sscanf(Rast_get_c_cat((CELL *) &covercat, cats), "%lf", &x);
	else
	    x = covercat;

	for (i = 0; i < value; i++) {
	    if (count * sizeof(double) >= mem) {
		mem += MEM * sizeof(double);
		tab = (double *)G_realloc(tab, mem);
		/* fprintf(stderr,"MALLOC: %d KB needed\n",(int)(mem/1024));  */
	    }
	    tab[count++] = x;
	}

    }
    if (first) {
	catb = catc = 0;
    }

    s_dev(tab, count, &sdev);
    fprintf(reclass, "%ld = %ld %f\n", catb, catb, sdev);
    G_debug(5, "%ld = %ld %f\n", catb, catb, sdev);

    G_popen_close(&stats_child);
    G_popen_close(&reclass_child);

    return 0;
}
示例#13
0
文件: main.c 项目: imincik/pkg-grass
int main(int argc, char *argv[])
{
    struct GModule *module;
    struct
    {
	struct Option *base, *cover, *output;
    } parm;
    char *basemap, *base_mapset;
    char *covermap, *cover_mapset;
    char *outmap;
    char command[1024];
    struct Categories cover_cats;
    FILE *stats, *reclass;
    int first;
    long basecat, covercat, catb, catc;
    double value, max;

    G_gisinit(argv[0]);

    module = G_define_module();
    module->keywords = _("raster, statistics");
    module->description =
	_("Finds the mode of values in a cover map within "
	  "areas assigned the same category value in a "
	  "user-specified base map.");

    parm.base = G_define_option();
    parm.base->key = "base";
    parm.base->description = _("Base map to be reclassified");
    parm.base->required = YES;
    parm.base->type = TYPE_STRING;
    parm.base->gisprompt = "old,cell,raster";

    parm.cover = G_define_option();
    parm.cover->key = "cover";
    parm.cover->description = _("Coverage map");
    parm.cover->required = YES;
    parm.cover->type = TYPE_STRING;
    parm.cover->gisprompt = "old,cell,raster";

    parm.output = G_define_option();
    parm.output->key = "output";
    parm.output->description = _("Output map");
    parm.output->required = YES;
    parm.output->type = TYPE_STRING;
    parm.output->gisprompt = "new,cell,raster";

    if (G_parser(argc, argv))
	exit(1);

    basemap = parm.base->answer;
    covermap = parm.cover->answer;
    outmap = parm.output->answer;

    base_mapset = G_find_cell2(basemap, "");
    if (base_mapset == NULL) {
	G_fatal_error(_("%s: base raster map not found"), basemap);
    }

    cover_mapset = G_find_cell2(covermap, "");
    if (cover_mapset == NULL) {
	G_fatal_error(_("%s: cover raster map not found"), covermap);
    }
    if (G_legal_filename(outmap) < 0) {
	G_fatal_error(_("<%s> is an illegal file name"), outmap);
    }
    if (strcmp(G_mapset(), base_mapset) == 0 && strcmp(basemap, outmap) == 0) {
	G_fatal_error(_("%s: base map and output map must be different"),
		      outmap);
    }
    if (G_read_cats(covermap, cover_mapset, &cover_cats) < 0) {
	G_fatal_error(_("%s: Unable to read category labels"), covermap);
    }

    strcpy(command, "r.stats -an \"");
    strcat(command, G_fully_qualified_name(basemap, base_mapset));
    strcat(command, ",");
    strcat(command, G_fully_qualified_name(covermap, cover_mapset));
    strcat(command, "\"");

    /* printf(command); */
    stats = popen(command, "r");

    sprintf(command, "r.reclass i=\"%s\" o=\"%s\"",
	    G_fully_qualified_name(basemap, base_mapset), outmap);

    /* printf(command); */
    reclass = popen(command, "w");

    first = 1;
    while (read_stats(stats, &basecat, &covercat, &value)) {
	if (first) {
	    first = 0;
	    catb = basecat;
	    catc = covercat;
	    max = value;
	}
	if (basecat != catb) {
	    write_reclass(reclass, catb, catc, G_get_cat(catc, &cover_cats));
	    catb = basecat;
	    catc = covercat;
	    max = value;
	}
	if (value > max) {
	    catc = covercat;
	    max = value;
	}
    }
    if (first) {
	catb = catc = 0;
    }
    write_reclass(reclass, catb, catc, G_get_cat(catc, &cover_cats));

    pclose(stats);
    pclose(reclass);

    exit(0);
}
示例#14
0
int
main(int argc, char *argv[])
{
	int interval = 5;
	int count;
	int always = 1;
	int opt;
	int projects = 0;
	int zones = 0;
	/* project reporting is the default if no option is specified */
	rcid_type_t stat_type = RCIDT_PROJECT;

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);
	(void) setpname("rcapstat");

	global = unformatted = 0;
	while ((opt = getopt(argc, argv, "gpuzT:")) != (int)EOF) {
		switch (opt) {
		case 'g':
			global = 1;
			break;
		case 'p':
			projects = 1;
			stat_type = RCIDT_PROJECT;
			break;
		case 'u':
			unformatted = 1;
			break;
		case 'z':
			stat_type = RCIDT_ZONE;
			zones = 1;
			break;
		case 'T':
			if (optarg) {
				if (*optarg == 'u')
					timestamp_fmt = UDATE;
				else if (*optarg == 'd')
					timestamp_fmt = DDATE;
				else
					usage();
			} else {
				usage();
			}
			break;
		default:
			usage();
		}
	}

	if (argc > optind)
		if ((interval = xatoi(argv[optind++])) <= 0)
			die(gettext("invalid interval specified\n"));
	if (argc > optind) {
		if ((count = xatoi(argv[optind++])) <= 0)
			die(gettext("invalid count specified\n"));
		always = 0;
	}
	if (argc > optind || (projects > 0 && zones > 0))
		usage();

	while (always || count-- > 0) {
		if (read_stats(stat_type) != E_SUCCESS)
			return (E_ERROR);
		if (timestamp_fmt != NODATE)
			print_timestamp(timestamp_fmt);
		if (!unformatted) {
			print_stats(stat_type);
			(void) fflush(stdout);
			if (count || always)
				(void) sleep(interval);
		} else {
			struct stat st;

			print_unformatted_stats();
			(void) fflush(stdout);
			while (stat(STAT_FILE_DEFAULT, &st) == 0 &&
			    st.st_mtime == stat_mod)
				(void) usleep((useconds_t)(0.2 * MICROSEC));
		}
	}

	return (E_SUCCESS);
}
示例#15
0
static int test_loopback(void)
{
   int ret = 0;

   ret = bcm_drv_init();

   if (!ret) {
      struct file fil;
      fil.private_data = NULL;

      do { // Empty loop
         size_t todo;
         size_t delay;
         size_t total_done_tx;
         size_t done_tx;
         size_t done_rx;
         ssize_t len;

         // Open device
         ret = bcm_drv_open(NULL, &(fil));
         if (ret) {
            fprintf(stderr, "Error opening device.\n");
            break;
         }
         fil.f_flags = O_RDWR | O_NONBLOCK;

         read_regs(&(fil));

         for (done_tx = 0; (done_tx < sizeof(buf_tx)); done_tx += 1) {
            buf_tx[done_tx] = (__u8)(done_tx);
         }
         memset(buf_rx, 0, sizeof(buf_rx));

         // We configure
         ret = start(&(fil));
         if (ret) {
            break;
         }

         // We fill TX buffers before starting PCM
         total_done_tx = 0;
         done_tx = 0;
         todo = sizeof(buf_tx) - done_tx;
         len = bcm_drv_write(&(fil), (const char *)(&(buf_tx[done_tx])), todo, NULL);
         if (len < 0) {
            if (-EAGAIN != len) {
               fprintf(stderr, "Error writing to the device.\n");
               break;
            }
         }
         else {
            total_done_tx += len;
            done_tx += len;
         }

         // Then we start PCM
         ret = start_pcm(&(fil));
         if (ret) {
            break;
         }

         ret = set_line_mode(&(fil), 0, BCMPH_MODE_OFF_TALKING);
         if (ret) {
            break;
         }

         done_rx = 0;
         delay = 0;
         while (done_rx < sizeof(buf_rx)) {
            int pause = 1;
            todo = sizeof(buf_rx) - done_rx;
            len = bcm_drv_read(&(fil), (char *)(&(buf_rx[done_rx])), todo, NULL);
            if (len < 0) {
               if (-EAGAIN != len) {
                  fprintf(stderr, "Error reading from the device.\n");
                  break;
               }
            }
            else if (len > 0) {
               done_rx += len;
               pause = 0;
            }

            if (done_tx >= sizeof(buf_tx)) {
               done_tx = 0;
            }
            todo = sizeof(buf_tx) - done_tx;
            len = bcm_drv_write(&(fil), (const char *)(&(buf_tx[done_tx])), todo, NULL);
            if (len < 0) {
               if (-EAGAIN != len) {
                  fprintf(stderr, "Error writing to the device.\n");
                  break;
               }
            }
            else if (len > 0) {
               total_done_tx += len;
               done_tx += len;
               pause = 0;
            }
            if (pause) {
               delay += 1;
               usleep(10000);
               if (delay > 100) {
                  delay = 0;
                  fprintf(stdout, "%lu bytes written and %lu bytes read...\n", (unsigned long)(total_done_tx), (unsigned long)(done_rx));
               }
            }
         }
         fprintf(stdout, "%ld bytes written to the device\n", (long)(total_done_tx));

         sleep(3);

         // We stop PCM
         stop_pcm(&(fil));

         // We read remaining bytes
         todo = sizeof(buf_rx) - done_rx;
         len = bcm_drv_read(&(fil), (char *)(&(buf_rx[done_rx])), todo, NULL);
         if (len < 0) {
            if (-EAGAIN != len) {
               fprintf(stderr, "Error reading from the device.\n");
               break;
            }
         }
         done_rx += len;

         // We stop
         stop(&(fil));

         // Dump the buffer received
         dump_buf("Bytes received", buf_rx, done_rx);

         read_stats(&(fil));

         for (todo = 0; ((todo < done_tx) && (todo < done_rx)); todo += 1) {
            if (buf_tx[todo] != buf_rx[todo]) {
               break;
            }
         }
         if ((todo >= done_tx) || (todo >= done_rx)) {
            fprintf(stdout, "Bytes received are the same as those sent.\n");
         }
         else {
            fprintf(stdout, "Byte %lu received differ from the one sent : %x vs %x.\n", (unsigned long)(todo), (unsigned int)(buf_rx[todo]), (unsigned int)(buf_tx[todo]));
         }
      } while (0);

      if (NULL != fil.private_data) {
         bcm_drv_release(NULL, &(fil));
         fil.private_data = NULL;
      }

      bcm_drv_exit();
   }

   return (ret);
}
示例#16
0
文件: main.c 项目: imincik/pkg-grass
int main(int argc, char *argv[])
{
    struct GModule *module;
    struct
    {
	struct Option *base, *cover, *output;
    } parm;
    char *basemap, *base_mapset;
    char *covermap, *cover_mapset;
    char *outmap;
    char command[1024];
    struct Categories cover_cats;
    FILE *stats_fd, *reclass_fd;
    int first;
    long basecat, covercat, catb, catc;
    double area;
    struct stats stats;

    G_gisinit(argv[0]);

    module = G_define_module();
    module->keywords = _("raster, statistics");
    module->description =
	_("Finds the median of values in a cover map within "
	  "areas assigned the same category value in a "
	  "user-specified base map.");

    parm.base = G_define_standard_option(G_OPT_R_INPUT);
    parm.base->key = "base";
    parm.base->description = _("Name of base raster map");

    parm.cover = G_define_standard_option(G_OPT_R_INPUT);
    parm.cover->key = "cover";
    parm.cover->description = _("Name of cover raster map");

    parm.output = G_define_standard_option(G_OPT_R_OUTPUT);

    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    basemap = parm.base->answer;
    covermap = parm.cover->answer;
    outmap = parm.output->answer;

    base_mapset = G_find_cell2(basemap, "");
    if (base_mapset == NULL)
	G_fatal_error(_("Base raster map <%s> not found"), basemap);

    cover_mapset = G_find_cell2(covermap, "");
    if (cover_mapset == NULL)
	G_fatal_error(_("Raster map <%s> not found"), covermap);
    if (G_legal_filename(outmap) < 0)
	G_fatal_error(_("<%s> is an illegal file name"), outmap);
    if (strcmp(G_mapset(), base_mapset) == 0 && strcmp(basemap, outmap) == 0)
	G_fatal_error(_("Base map and output map <%s> must be different"),
		      outmap);
    if (G_read_cats(covermap, cover_mapset, &cover_cats) < 0)
	G_fatal_error(_("Unable to read category labels of raster map <%s>"),
		      covermap);

    strcpy(command, "r.stats -an \"");
    strcat(command, G_fully_qualified_name(basemap, base_mapset));
    strcat(command, ",");
    strcat(command, G_fully_qualified_name(covermap, cover_mapset));
    strcat(command, "\"");

    /* strcpy (command,"cat /tmp/t"); */
    G_debug(3, "command: %s", command);
    stats_fd = popen(command, "r");

    G_debug(3, "r.reclass i=\"%s\" o=\"%s\"",
	    G_fully_qualified_name(basemap, base_mapset), outmap);
    sprintf(command, "r.reclass i=\"%s\" o=\"%s\"",
	    G_fully_qualified_name(basemap, base_mapset), outmap);

    reclass_fd = popen(command, "w");

    first = 1;
    while (read_stats(stats_fd, &basecat, &covercat, &area)) {
	if (first) {
	    stats.n = 0;
	    stats.nalloc = 16;
	    stats.cat = (long *)
		G_calloc(stats.nalloc, sizeof(long));
	    stats.area = (double *)
		G_calloc(stats.nalloc, sizeof(double));
	    first = 0;
	    catb = basecat;
	}
	if (basecat != catb) {
	    catc = median(&stats);
	    write_reclass(reclass_fd, catb, catc,
			  G_get_cat(catc, &cover_cats));
	    catb = basecat;
	    stats.n = 0;
	}
	stats.n++;
	if (stats.n > stats.nalloc) {
	    stats.nalloc *= 2;
	    stats.cat = (long *)
		G_realloc(stats.cat, stats.nalloc * sizeof(long));
	    stats.area = (double *)
		G_realloc(stats.area, stats.nalloc * sizeof(double));
	}
	stats.cat[stats.n - 1] = covercat;
	stats.area[stats.n - 1] = area;
    }
    if (!first) {
	catc = median(&stats);
	write_reclass(reclass_fd, catb, catc, G_get_cat(catc, &cover_cats));
    }

    pclose(stats_fd);
    pclose(reclass_fd);

    exit(EXIT_SUCCESS);
}
示例#17
0
文件: sysmon.c 项目: RPI-WCL/sysmon
int main(int argc, char* argv[])
{
    struct stats st, st_prev;
    struct ext_stats ext_st;
    unsigned long long interval;
    char *logfile = LOG_FILE;
    int speed = -1, duplex = -1;
    

    /* Usage: ./sysmon [real-time logging switch] [log filename] [print flag] (netif speed) (netif duplex)
       netif duplex takes C_DUPLEX_HALF=1 or C_DUPLEX_FULL=2 */
    
    if (2 <= argc)      realtime_logging = (strcmp("1", argv[1]) == 0);
    if (3 <= argc)      logfile = argv[2];
    if (4 <= argc && (strncmp("0x", argv[3], 2) == 0))
        print_flag = (int)strtol(argv[3], NULL, 0); /* Expecting 0x... */
    if (5 <= argc)      speed = atoi(argv[4]);
    if (6 <= argc)      duplex = atoi(argv[5]);

    printf ("realtime logging : %s\n", realtime_logging ? "enabled" : "disabled");
    printf ("log file         : %s\n", logfile);
    printf ("print flag       : 0x%08x\n", print_flag);
    printf ("speed            : %d\n", speed);
    printf ("duplex           : %d\n", duplex);

    #ifndef DEBUG
    if ((fp = fopen(logfile, "w+")) == NULL) {
        fprintf(stderr, "opening logfile failed\n");
        exit(EXIT_FAILURE);
    }
    #else
    fp = stdout;
    #endif


    /* Initialization */
    signal(SIGINT, signal_callback_handler); /* Set up a signal handler */
    read_stats(&base_stats);
    clock_gettime(CLOCK_MONOTONIC, &base_time);
    /* Obtain speed/duplex info for eth0 */
    read_net_dev(&st.net, 1);
    read_if_info(&st.net, 1); 
    /* VMs on Google Clouds do not let you read files under /sys/class/net/eth0/
       See also: http://unix.stackexchange.com/questions/77750/checking-the-speed-of-active-network-connections */
    base_stats.net.speed = (0 < speed) ? speed : st.net.speed;
    base_stats.net.duplex = (0 < duplex) ? duplex : st.net.duplex;
    st = st_prev = base_stats;
    /* Initialize variables for min/max/mean/var computation */
    num_samples = 1;
    set_max_ext_stats(&min);
    memset((void *)&max, 0x00, sizeof(struct ext_stats));
    memset((void *)&mean, 0x00, sizeof(struct ext_stats));
    memset((void *)&var, 0x00, sizeof(struct ext_stats));
    /* For real-time logging */
    if (realtime_logging) fprint_header(fp, print_flag);
    /* Get HZ */
    get_HZ();

    while (1) { /* Anyways, we record stats for average computation */
        usleep(INTERVAL_SEC * SEC_TO_MSEC);
        read_stats(&st);
        interval = get_interval(st_prev.uptime, st.uptime);

        /* Compute extended stats from raw stats */
        compute_ext_stats(interval, &st, &st_prev, &ext_st);
        update_ext_stats(num_samples, &ext_st, &min, &max, &mean, &var);
        if (realtime_logging) fprint_stats(fp, print_flag, &st, &ext_st);
        
        /* Update stats (min/max/mean/var) for ext_stats */
        st_prev = st;
        num_samples++;
    }

    /* Program never gets here */
    printf("Waiting for the SIGINT signal\n");
    pause();

    return EXIT_SUCCESS;
}
示例#18
0
文件: o_var.c 项目: imincik/pkg-grass
int
o_var(char *basemap, char *covermap, char *outputmap, int usecats,
      struct Categories *cats)
{
    char command[1024];
    FILE *stats, *reclass;
    int first, mem, i, count;
    long basecat, covercat, catb, catc;
    double value, vari, x;
    double *tab;


    mem = MEM * sizeof(double);
    tab = (double *)G_malloc(mem);

    sprintf(command, "r.stats -cn input=\"%s,%s\" fs=space", basemap,
	    covermap);
    stats = popen(command, "r");

    sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
    reclass = popen(command, "w");


    first = 1;
    while (read_stats(stats, &basecat, &covercat, &value)) {
	if (first) {
	    first = 0;
	    catb = basecat;
	    catc = covercat;
	    i = 0;
	    count = 0;
	}

	if (basecat != catb) {
	    m_var(tab, count, &vari);
	    fprintf(reclass, "%ld = %ld %f\n", catb, catb, vari);
	    /*fprintf (stdout, "1. %ld = %ld %f\n", catb, catb, vari); */
	    catb = basecat;
	    catc = covercat;
	    count = 0;
	}

	if (usecats)
	    sscanf(G_get_cat((CELL) covercat, cats), "%lf", &x);
	else
	    x = covercat;

	for (i = 0; i < value; i++) {
	    if (count * sizeof(double) >= mem) {
		mem += MEM * sizeof(double);
		tab = (double *)G_realloc(tab, mem);
		/* fprintf(stderr,"MALLOC: %d KB needed\n",(int)(mem/1024)); */
	    }
	    tab[count++] = x;
	}

    }

    if (first) {
	catb = catc = 0;
    }

    m_var(tab, count, &vari);
    fprintf(reclass, "%ld = %ld %f\n", catb, catb, vari);
    G_debug(5, "2. %ld = %ld %f", catb, catb, vari);

    pclose(stats);
    pclose(reclass);

    return (0);
}
示例#19
0
文件: ifc.c 项目: dbittman/sea
int main(int argc, char **argv)
{
	int c;
	int read = 1; /* by default, we read data and print it out. operation modes that
				   * write settings change this to 0. */
	const char *name = 0;
	/* parse options to look for the interface name... */
	opterr = 0;
	while((c = getopt(argc, argv, "hi:")) != -1) {
		switch(c) {
			case 'h':
				usage();
				break;
			case 'i':
				name = optarg;
				break;
		}
	}

	/* reset getopt() and set the things */
	optind = 0;
	opterr = 1;
	while((c = getopt(argc, argv, "u:s:n:m:i:hb:")) != -1) {
		switch(c) {
			case 'u':
				read = 0;
				if(!name) {
					fprintf(stderr, "ifc: no interface specified\n");
					exit(1);
				}
				if(set_flag(name, optarg, 1))
					exit(1);
				break;
			case 's':
				read = 0;
				if(!name) {
					fprintf(stderr, "ifc: no interface specified\n");
					exit(1);
				}
				if(set_flag(name, optarg, 0))
					exit(1);
				break;
			case 'n':
				read = 0;
				if(!name) {
					fprintf(stderr, "ifc: no interface specified\n");
					exit(1);
				}
				if(set_addr(name, optarg))
					exit(1);
				break;
			case 'b':
				read = 0;
				if(!name) {
					fprintf(stderr, "ifc: no interface specified\n");
					exit(1);
				}
				if(set_broad_addr(name, optarg))
					exit(1);
				break;
			case 'm':
				read = 0;
				if(!name) {
					fprintf(stderr, "ifc: no interface specified\n");
					exit(1);
				}
				if(set_mask(name, optarg))
					exit(1);
				break;
			case 't':
				read = 0;
				if(!name) {
					fprintf(stderr, "ifc: no interface specified\n");
					exit(1);
				}
				if(set_mtu(name, optarg))
					exit(1);
				break;
			case 'i': break;
			default:
				exit(1);
		}
	}
	if(read)
		read_stats(name);
	return 0;
}