示例#1
0
文件: alias.c 项目: radosroka/sudo
/*
 * Add an alias to the aliases redblack tree.
 * Note that "file" must be a reference-counted string.
 * Returns NULL on success and an error string on failure.
 */
const char *
alias_add(char *name, int type, char *file, int lineno, struct member *members)
{
    static char errbuf[512];
    struct alias *a;
    debug_decl(alias_add, SUDOERS_DEBUG_ALIAS)

    a = calloc(1, sizeof(*a));
    if (a == NULL) {
	strlcpy(errbuf, N_("unable to allocate memory"), sizeof(errbuf));
	debug_return_str(errbuf);
    }
    a->name = name;
    a->type = type;
    /* a->used = false; */
    a->file = rcstr_addref(file);
    a->lineno = lineno;
    HLTQ_TO_TAILQ(&a->members, members, entries);
    switch (rbinsert(aliases, a, NULL)) {
    case 1:
	snprintf(errbuf, sizeof(errbuf), N_("Alias \"%s\" already defined"), name);
	alias_free(a);
	debug_return_str(errbuf);
    case -1:
	strlcpy(errbuf, N_("unable to allocate memory"), sizeof(errbuf));
	alias_free(a);
	debug_return_str(errbuf);
    }
    debug_return_str(NULL);
}
示例#2
0
文件: alias.c 项目: jlec/weechat
void
alias_free_all ()
{
    while (alias_list)
    {
        alias_free (alias_list);
    }
}
示例#3
0
文件: alias.c 项目: Distrotech/sudo
/*
 * Add an alias to the aliases redblack tree.
 * Returns NULL on success and an error string on failure.
 */
char *
alias_add(char *name, int type, struct member *members)
{
    static char errbuf[512];
    struct alias *a;
    debug_decl(alias_add, SUDO_DEBUG_ALIAS)

    a = ecalloc(1, sizeof(*a));
    a->name = name;
    a->type = type;
    /* a->used = false; */
    list2tq(&a->members, members);
    if (rbinsert(aliases, a)) {
	snprintf(errbuf, sizeof(errbuf), N_("Alias `%s' already defined"), name);
	alias_free(a);
	debug_return_str(errbuf);
    }
    debug_return_str(NULL);
}
示例#4
0
void
alias_config_cmd_delete_cb (void *data, struct t_config_option *option)
{
    struct t_config_option *ptr_option_completion;
    struct t_alias *ptr_alias;

    /* make C compiler happy */
    (void) data;

    ptr_option_completion = weechat_config_search_option (alias_config_file,
                                                          alias_config_section_completion,
                                                          weechat_config_option_get_pointer (option, "name"));

    ptr_alias = alias_search (weechat_config_option_get_pointer (option, "name"));
    if (ptr_alias)
        alias_free (ptr_alias);

    if (ptr_option_completion)
        weechat_config_option_free (ptr_option_completion);
}
示例#5
0
int
alias_config_cmd_create_option_cb (void *data,
                                   struct t_config_file *config_file,
                                   struct t_config_section *section,
                                   const char *option_name, const char *value)
{
    struct t_alias *ptr_alias;
    int rc;

    /* make C compiler happy */
    (void) data;
    (void) config_file;
    (void) section;

    /* create configuration option */
    alias_config_cmd_new_option (option_name, value);

    /* create alias */
    ptr_alias = alias_search (option_name);
    if (ptr_alias)
        alias_free (ptr_alias);
    if (value && value[0])
        rc = (alias_new (option_name, value, NULL)) ?
            WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE : WEECHAT_CONFIG_OPTION_SET_ERROR;
    else
        rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;

    if (rc == WEECHAT_CONFIG_OPTION_SET_ERROR)
    {
        weechat_printf (NULL,
                        _("%s%s: error creating alias \"%s\" => \"%s\""),
                        weechat_prefix ("error"), ALIAS_PLUGIN_NAME,
                        option_name, value);
    }

    return rc;
}
示例#6
0
int main (int argc, char**argv) {
	FILE *file;
	FILE *tmp;
	uint64_t j, i = 0;
	uint32_t *res;
	uint32_t *map;
	uint32_t *cnt;
	uint32_t draw;
	double   *table;
	double c = 0;

	/* getopt */
	int opt;
    int option_index = 0;
    static const char *optstring = "1:2:c:N:a:f:m:i";
    static const struct option long_options[] = {
        {"alpha",    required_argument,     0,    'a'},
        {"elements", required_argument,     0,    'N'},
        {"universe", required_argument,     0,    'm'},
        {"count",    required_argument,     0,    'c'},
        {"file",     required_argument,     0,    'f'},
        {"seed1",    required_argument,     0,    '1'},
        {"seed2",    required_argument,     0,    '2'},
        {"info",     required_argument,     0,    'i'},
    };

	double alpha   = 0.5;
	uint64_t count = (1 << 25);
	uint32_t N     = (1 << 20);
	uint32_t m     = UINT32_MAX;
	char *filename = NULL;

	while( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1) {
		switch(opt) {
			case 'a':
				alpha = strtod(optarg, NULL);
				break;
			case 'N':
				N = strtol(optarg, NULL, 10);
				break;
			case 'm':
				m = strtol(optarg, NULL, 10);
				break;
			case 'c':
				count = strtoll(optarg, NULL, 10);
				break;
			case 'f':
				filename = strndup(optarg, 256);
				break;
			case '1':
				I1 = strtoll(optarg, NULL, 10);
				break;
			case '2':
				I2 = strtoll(optarg, NULL, 10);
				break;
			case 'i':
			default:
				printusage(argv);
				exit(EXIT_FAILURE);
		}

		if ( errno != 0 ) {
			if (filename != NULL) {
				free(filename);
			}
			xerror(strerror(errno), __LINE__, __FILE__);
		}
	}

	if (filename == NULL) {
		printusage(argv);
		exit(EXIT_FAILURE);
	}

	cnt   = xmalloc( sizeof(uint32_t) * N );
	map   = xmalloc( sizeof(uint32_t) * N );
	table = xmalloc( sizeof(double) * N );
	res   = xmalloc( sizeof(uint32_t) * BUFFER );

	tmp   = tmpfile();
	file  = fopen(filename, "wb");
	if ( file == NULL ) {
		free(cnt);
		free(res);
		free(map);
		free(filename);
		free(table);
		xerror("Failed to open/create file.", __LINE__, __FILE__);
	}

	fprintf(file, "#N:        %"PRIu32"\n", N);
	fprintf(file, "#Universe: %"PRIu32"\n", m);
	fprintf(file, "#Alpha:    %lf\n", alpha);
	fprintf(file, "#Count:    %"PRIu64"\n", count);
	fprintf(file, "#Filename: %s\n", filename);
	fprintf(file, "#Seed1:    %"PRIu32"\n", I1);
	fprintf(file, "#Seed2:    %"PRIu32"\n", I2);

	for (i = 1; i <= N; i++) {
		table[i-1] = pow( (double)i, alpha );
		c = c + (1. / table[i-1]);
	}
	c = 1./c;

	for (i = 0; i < N; i++) {
		cnt[i] = 0;
		map[i] = i;
		table[i] = (c / table[i]);
	}

	shuffle(map, N);

	for (i = N; i < m; i++) {
		j = xuni_rand()*(i+1);
		if (j < N) {
			map[j] = i;
		}
	}

	alias_t *a = alias_preprocess(N, table);
	free(table);

	i = 0;
	while ( likely(i < count) ) {
		memset(res, '\0', sizeof(uint32_t)*BUFFER);
		for (j = 0; likely( (i < count && j < BUFFER) ); j++, i++) {
			draw = (uint32_t)alias_draw(a);
			cnt[draw] += 1;
			res[j] = map[draw];
		}

		if ( unlikely(fwrite(res, sizeof(uint32_t), j, tmp) < j) ) {
			fclose(file);
			fclose(tmp);
			free(res);
			free(filename);
			free(map);
			xerror("Failed to write all data.", __LINE__, __FILE__);
		}
	}

	alias_free(a);

	quicksort_map(cnt, 0, N-1, map); 

	// Print the weights of the top k probabilities into header of file
	fprintf(file, "#====== TOP %d ======\n", TOPK);
	for (i = 0; i < TOPK && i < N; i++) {
		fprintf(file, "#%"PRIu32": %.10lf\n", map[i], (double)cnt[i]/count);
	}
	fprintf(file, "\n");

	rewind(tmp);

	memset(res, '\0', sizeof(uint32_t)*BUFFER);
	uint32_t err;
	while ( (err = fread(res, sizeof(uint32_t), BUFFER, tmp)) > 0 ) {
		if ( unlikely(fwrite(res, sizeof(uint32_t), err, file) < err) ) {
			fclose(file);
			fclose(tmp);
			free(res);
			free(filename);
			free(map);
			xerror("Failed to write all data.", __LINE__, __FILE__);
		}
	}

	fclose(file);
	fclose(tmp);
	free(res);
	free(filename);
	free(map);

	return EXIT_SUCCESS;
}
示例#7
0
pc_data::~pc_data()
{
	DREAM_DATA *dream;
	ALIAS_DATA *tmp_alias;
	ROLE_DATA *trole;

	while (this->aliases)
	{
		tmp_alias = this->aliases;
		this->aliases = this->aliases->next_alias;
		alias_free (tmp_alias);
	}
	while (this->dreams)
	{
		dream = this->dreams;

		this->dreams = this->dreams->next;

		if (dream->dream && *dream->dream)
		{
			free_mem (dream->dream);
			dream->dream = NULL;
		}

		free_mem (dream);
		dream = NULL;
	}
	while (this->dreamed)
	{
		dream = this->dreamed;
		this->dreamed = this->dreamed->next;
		if (dream->dream && *dream->dream)
		{
			free_mem (dream->dream);
			dream->dream = NULL;
		}
		free_mem (dream);
		dream = NULL;
	}

	if ((trole = this->special_role) != NULL)
	{
		if (trole->summary && *trole->summary)
			free_mem (trole->summary);
		if (trole->body && *trole->body)
			free_mem (trole->body);
		if (trole->date && *trole->date)
			free_mem (trole->date);
		if (trole->poster && *trole->poster)
			free_mem (trole->poster);
		free_mem (trole);
		this->special_role = NULL;
	}

	if (this->account_name && *this->account_name)
	{
		free_mem (this->account_name);
		this->account_name = NULL;
	}

	if (this->site_lie && *this->site_lie)
	{
		free_mem (this->site_lie);
		this->site_lie = NULL;
	}

	if (this->imm_leave && *this->imm_leave)
	{
		free_mem (this->imm_leave);
		this->imm_leave = NULL;
	}

	if (this->imm_enter && *this->imm_enter)
	{
		free_mem (this->imm_enter);
		this->imm_enter = NULL;
	}

	if (this->creation_comment && *this->creation_comment)
	{
		free_mem (this->creation_comment);
		this->creation_comment = NULL;
	}

	if (this->msg && *this->msg)
	{
		free_mem (this->msg);
		this->msg = NULL;
	}

	this->owner = NULL;
}
示例#8
0
文件: alias.c 项目: jlec/weechat
struct t_alias *
alias_new (const char *name, const char *command, const char *completion)
{
    struct t_alias *new_alias, *ptr_alias, *pos_alias;

    if (!name || !name[0] || !command || !command[0])
        return NULL;

    while (weechat_string_is_command_char (name))
    {
        name = weechat_utf8_next_char (name);
    }

    ptr_alias = alias_search (name);
    if (ptr_alias)
        alias_free (ptr_alias);

    new_alias = malloc (sizeof (*new_alias));
    if (new_alias)
    {
        new_alias->hook = NULL;
        new_alias->name = strdup (name);
        new_alias->command = strdup (command);
        new_alias->completion = (completion) ? strdup (completion) : NULL;
        new_alias->running = 0;

        alias_hook_command (new_alias);

        if (alias_list)
        {
            pos_alias = alias_find_pos (name);
            if (pos_alias)
            {
                /* insert alias into the list (before alias found) */
                new_alias->prev_alias = pos_alias->prev_alias;
                new_alias->next_alias = pos_alias;
                if (pos_alias->prev_alias)
                    (pos_alias->prev_alias)->next_alias = new_alias;
                else
                    alias_list = new_alias;
                pos_alias->prev_alias = new_alias;
            }
            else
            {
                /* add alias to end of list */
                new_alias->prev_alias = last_alias;
                new_alias->next_alias = NULL;
                last_alias->next_alias = new_alias;
                last_alias = new_alias;
            }
        }
        else
        {
            new_alias->prev_alias = NULL;
            new_alias->next_alias = NULL;
            alias_list = new_alias;
            last_alias = new_alias;
        }
    }

    return new_alias;
}
示例#9
0
struct t_alias *
alias_new (const char *name, const char *command, const char *completion)
{
    struct t_alias *new_alias, *ptr_alias, *pos_alias;

    if (!alias_name_valid (name))
    {
        dogechat_printf (NULL,
                        _("%s%s: invalid alias name: \"%s\""),
                        dogechat_prefix ("error"), ALIAS_PLUGIN_NAME,
                        name);
        return NULL;
    }

    if (!command || !command[0])
        return NULL;

    while (dogechat_string_is_command_char (name))
    {
        name = dogechat_utf8_next_char (name);
    }

    ptr_alias = alias_search (name);
    if (ptr_alias)
        alias_free (ptr_alias);

    new_alias = malloc (sizeof (*new_alias));
    if (new_alias)
    {
        new_alias->hook = NULL;
        new_alias->name = strdup (name);
        new_alias->command = strdup (command);
        new_alias->completion = (completion) ? strdup (completion) : NULL;
        new_alias->running = 0;

        alias_hook_command (new_alias);

        if (alias_list)
        {
            pos_alias = alias_find_pos (name);
            if (pos_alias)
            {
                /* insert alias into the list (before alias found) */
                new_alias->prev_alias = pos_alias->prev_alias;
                new_alias->next_alias = pos_alias;
                if (pos_alias->prev_alias)
                    (pos_alias->prev_alias)->next_alias = new_alias;
                else
                    alias_list = new_alias;
                pos_alias->prev_alias = new_alias;
            }
            else
            {
                /* add alias to end of list */
                new_alias->prev_alias = last_alias;
                new_alias->next_alias = NULL;
                last_alias->next_alias = new_alias;
                last_alias = new_alias;
            }
        }
        else
        {
            new_alias->prev_alias = NULL;
            new_alias->next_alias = NULL;
            alias_list = new_alias;
            last_alias = new_alias;
        }
    }

    return new_alias;
}