Exemplo n.º 1
0
/*
 * test main for lut module, usage: a.out [lhs[=rhs]...]
 */
int
main(int argc, char *argv[])
{
	struct lut *r = NULL;
	struct lut *dupr = NULL;
	char *equals;

	err_init(argv[0]);
	setbuf(stdout, NULL);

	for (argv++; *argv; argv++)
		if ((equals = strchr(*argv, '=')) != NULL) {
			*equals++ = '\0';
			r = lut_add(r, *argv, equals);
		} else
			r = lut_add(r, *argv, "NULL");

	printf("lut contains:\n");
	lut_walk(r, printer, r);

	dupr = lut_dup(r);

	lut_free(r, NULL);

	printf("dup lut contains:\n");
	lut_walk(dupr, printer, dupr);

	lut_free(dupr, NULL);

	err_done(0);
	/* NOTREACHED */
	return (0);
}
Exemplo n.º 2
0
/*
 * lut_free -- free a lut
 *
 * if callback is provided, it is called for each value in the table.
 * it the values are things that the caller malloc'd, then you can do:
 *	lut_free(root, free);
 */
void
lut_free(struct lut *root, void (*callback)(void *rhs))
{
	if (root != NULL) {
		lut_free(root->lut_left, callback);
		lut_free(root->lut_right, callback);
		FREE(root->lut_lhs);
		if (callback)
			(*callback)(root->lut_rhs);
		FREE(root);
	}
}
Exemplo n.º 3
0
/*
 * ipath_fini -- free the ipath cache
 */
void
ipath_fini(void)
{
	lut_free(Ipaths, ipath_destructor, NULL);
	Ipaths = NULL;
	lut_free(Usednames, NULL, NULL);
	Usednames = NULL;

	if (Nipath) {
		stats_delete(Nipath);
		Nipath = NULL;
	}

	if (Nbytes) {
		stats_delete(Nbytes);
		Nbytes = NULL;
	}
}
Exemplo n.º 4
0
void
lex_free(void)
{
    struct filestats *nfstats = Fstats;

    /*
     * Free up memory consumed by the lexer
     */
    stats_delete(Tokcount);
    stats_delete(Filecount);
    stats_delete(Lexelapse);
    while (nfstats != NULL) {
        Fstats = nfstats->next;
        stats_delete(nfstats->stats);
        if (nfstats->idstats != NULL)
            stats_delete(nfstats->idstats);
        FREE(nfstats);
        nfstats = Fstats;
    }
    lut_free(Timesuffixlut, NULL, NULL);
    lut_free(Rwordslut, NULL, NULL);
    lut_free(Ident, NULL, NULL);
    lut_free(Dicts, NULL, NULL);
}
Exemplo n.º 5
0
/*
 * conf_close -- close the configuration file
 */
void
conf_close(struct opts *opts)
{
	FILE *fp;

	if (Confchanged && opts_count(opts, "n") == 0 && Conffd != -1) {
		if (opts_count(opts, "v"))
			(void) out("# writing changes to %s\n", Confname);
		if (Debug > 1) {
			(void) fprintf(stderr, "conf_close, %s changed to:\n",
			    Confname);
			conf_print(stderr);
		}
		if (lseek(Conffd, (off_t)0, SEEK_SET) < 0)
			err(EF_SYS, "lseek on %s", Confname);
		if (ftruncate(Conffd, (off_t)0) < 0)
			err(EF_SYS, "ftruncate on %s", Confname);
		if ((fp = fdopen(Conffd, "w")) == NULL)
			err(EF_SYS, "fdopen on %s", Confname);
		conf_print(fp);
		if (fclose(fp) < 0)
			err(EF_SYS, "fclose on %s", Confname);
		Conffd = -1;
		Confchanged = B_FALSE;
	} else if (opts_count(opts, "v")) {
		(void) out("# %s unchanged\n", Confname);
	}

	if (Conffd != -1) {
		(void) close(Conffd);
		Conffd = -1;
	}
	if (Conflut) {
		lut_free(Conflut, free);
		Conflut = NULL;
	}
	if (Confentries) {
		fn_list_free(Confentries);
		Confentries = NULL;
	}
}
Exemplo n.º 6
0
/*
 * conf_close -- close the configuration file
 */
void
conf_close(struct opts *opts)
{
	char cuname[PATH_MAX], tuname[PATH_MAX];
	int cfd, tfd;
	FILE *cfp = NULL, *tfp = NULL;
	boolean_t safe_update = B_TRUE;

	if (Changed == CHG_NONE || opts_count(opts, "n") != 0) {
		if (opts_count(opts, "v"))
			(void) out("# %s and %s unchanged\n",
			    Confname, Timesname);
		goto cleanup;
	}

	if (Debug > 1) {
		(void) fprintf(stderr, "conf_close, saving logadm context:\n");
		conf_print(stderr, NULL);
	}

	cuname[0] = tuname[0] = '\0';
	LOCAL_ERR_BEGIN {
		if (SETJMP) {
			safe_update = B_FALSE;
			LOCAL_ERR_BREAK;
		}
		if (Changed == CHG_BOTH) {
			if (Canchange != CHG_BOTH)
				err(EF_JMP, "internal error: attempting "
				    "to update %s without locking", Confname);
			(void) snprintf(cuname, sizeof (cuname), "%sXXXXXX",
			    Confname);
			if ((cfd = mkstemp(cuname)) == -1)
				err(EF_SYS|EF_JMP, "open %s replacement",
				    Confname);
			if (opts_count(opts, "v"))
				(void) out("# writing changes to %s\n", cuname);
			if (fchmod(cfd, 0644) == -1)
				err(EF_SYS|EF_JMP, "chmod %s", cuname);
			if ((cfp = fdopen(cfd, "w")) == NULL)
				err(EF_SYS|EF_JMP, "fdopen on %s", cuname);
		} else {
			/* just toss away the configuration data */
			cfp = fopen("/dev/null", "w");
		}
		if (!Singlefile) {
			if (Canchange == CHG_NONE)
				err(EF_JMP, "internal error: attempting "
				    "to update %s without locking", Timesname);
			(void) snprintf(tuname, sizeof (tuname), "%sXXXXXX",
			    Timesname);
			if ((tfd = mkstemp(tuname)) == -1)
				err(EF_SYS|EF_JMP, "open %s replacement",
				    Timesname);
			if (opts_count(opts, "v"))
				(void) out("# writing changes to %s\n", tuname);
			if (fchmod(tfd, 0644) == -1)
				err(EF_SYS|EF_JMP, "chmod %s", tuname);
			if ((tfp = fdopen(tfd, "w")) == NULL)
				err(EF_SYS|EF_JMP, "fdopen on %s", tuname);
		}

		conf_print(cfp, tfp);
		if (fclose(cfp) < 0)
			err(EF_SYS|EF_JMP, "fclose on %s", Confname);
		if (tfp != NULL && fclose(tfp) < 0)
			err(EF_SYS|EF_JMP, "fclose on %s", Timesname);
	LOCAL_ERR_END }

	if (!safe_update) {
		if (cuname[0] != 0)
			(void) unlink(cuname);
		if (tuname[0] != 0)
			(void) unlink(tuname);
		err(EF_JMP, "unsafe to update configuration file "
		    "or timestamps");
		return;
	}

	/* rename updated files into place */
	if (cuname[0] != '\0')
		if (rename(cuname, Confname) < 0)
			err(EF_SYS, "rename %s to %s", cuname, Confname);
	if (tuname[0] != '\0')
		if (rename(tuname, Timesname) < 0)
			err(EF_SYS, "rename %s to %s", tuname, Timesname);
	Changed = CHG_NONE;

cleanup:
	if (Conffd != -1) {
		(void) close(Conffd);
		Conffd = -1;
	}
	if (Timesfd != -1) {
		(void) close(Timesfd);
		Timesfd = -1;
	}
	if (Conflut) {
		lut_free(Conflut, free);
		Conflut = NULL;
	}
	if (Confentries) {
		fn_list_free(Confentries);
		Confentries = NULL;
	}
}