コード例 #1
0
ファイル: asarray.c プロジェクト: aabhasgarg/accessgrid
void
asarray_destroy(asarray **ppa)
{
    asarray    *pa;
    const char *key;

    pa = *ppa;
    assert(pa != NULL);

    while ((key = asarray_get_key_no(pa, 0)) != NULL) {
        asarray_remove(pa, key);
    }

    xfree(pa);
    *ppa = NULL;
    xmemchk();
}
コード例 #2
0
ファイル: settings.c プロジェクト: JensenSung/rat
static void save_done(uint32_t type)
{
#ifndef WIN32
        /* settings table has entries we want to write.  Settings file
         * may contain comments and info for other apps, therefore read
         * file one line at a time, see if write update line if we have one
         * and output to a new file.  Then copy from one to the other.
         */
        char linebuf[255], keybuf[255], *key, *value, *tmpname;
        const char *ckey;
        FILE *n = settings_file_open(SETTINGS_FILE_TMP, "w");
        FILE *o = settings_file_open(type, "r");

        if (n == NULL) {
                debug_msg("Could not open temporary settings file\n");
                goto save_stops_here;
        }

        if (o != NULL) {
                while (fgets(linebuf, sizeof(linebuf)/sizeof(linebuf[0]), o) != NULL) {
                        cr_terminate(linebuf, sizeof(linebuf)/sizeof(linebuf[0]));
                        if (linebuf[0] != '*') {
                                fprintf(n, linebuf);
                        } else {
                                strcpy(keybuf, linebuf);
                                key = keybuf + 1;       /* Ignore asterisk */
                                key = strtok(key, ":"); /* key ends at colon */
                                if (asarray_lookup(aa, key, &value)) {
                                        /* We have a newer value */
                                        fprintf(n, "*%s: %s\n", key, value);
                                        asarray_remove(aa, key);
                                } else {
                                        /* We have no ideas about this value */
                                        fprintf(n, linebuf);
                                }
                        }
                }
        }


        while ((ckey = asarray_get_key_no(aa, 0)) != NULL) {
                /* Write out stuff not written out already */
                if (asarray_lookup(aa, ckey, &value)) {
                        fprintf(n, "*%s: %s\n", ckey, value);
                }
                asarray_remove(aa, ckey);
        }

        settings_file_close(n);
        if (o) {
                settings_file_close(o);
        }
        o = settings_file_open(type, "w+");
        n = settings_file_open(SETTINGS_FILE_TMP, "r");
        if (o && n) {
                while(feof(n) == 0) {
                        if (fgets(linebuf, sizeof(linebuf)/sizeof(linebuf[0]), n)) {
                                char *x;
                                x = linebuf;
                                /* Don't use fprintf because user
                                 * may have format modifiers in values (ugh)
                                 */
                                while (*x != '\0') {
                                        fwrite(x, 1, 1, o);
                                        x++;
                                }
                        }
                }
        } else {
                debug_msg("Failed to open settings file\n");
        }
        fflush(n);

save_stops_here:
        if (o) {
                settings_file_close(o);
        }
        if (n) {
                settings_file_close(n);
        }

        asarray_destroy(&aa);

        tmpname = settings_file_name(SETTINGS_FILE_TMP);
        unlink(tmpname);
        xfree(tmpname);
#endif
        UNUSED(type);
}