diff options
| author | Tom Yu <tlyu@mit.edu> | 2009-12-07 22:36:41 +0000 |
|---|---|---|
| committer | Tom Yu <tlyu@mit.edu> | 2009-12-07 22:36:41 +0000 |
| commit | ac911f663389980be59a0f9ee98f062e6c627658 (patch) | |
| tree | c423ac7174c1a82a107013894a6944a506e68774 /src/util/profile | |
| parent | 741938feb12538b659a36d7e0329efe6a5550669 (diff) | |
| download | krb5-ac911f663389980be59a0f9ee98f062e6c627658.tar.gz krb5-ac911f663389980be59a0f9ee98f062e6c627658.tar.xz krb5-ac911f663389980be59a0f9ee98f062e6c627658.zip | |
Mark and reindent util, with some exceptions
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@23455 dc483132-0cff-0310-8789-dd5450dbe970
Diffstat (limited to 'src/util/profile')
| -rw-r--r-- | src/util/profile/argv_parse.c | 209 | ||||
| -rw-r--r-- | src/util/profile/argv_parse.h | 1 | ||||
| -rw-r--r-- | src/util/profile/prof_FSp_glue.c | 9 | ||||
| -rw-r--r-- | src/util/profile/prof_file.c | 713 | ||||
| -rw-r--r-- | src/util/profile/prof_get.c | 551 | ||||
| -rw-r--r-- | src/util/profile/prof_init.c | 471 | ||||
| -rw-r--r-- | src/util/profile/prof_parse.c | 789 | ||||
| -rw-r--r-- | src/util/profile/prof_set.c | 431 | ||||
| -rw-r--r-- | src/util/profile/prof_tree.c | 955 | ||||
| -rw-r--r-- | src/util/profile/test_parse.c | 79 | ||||
| -rw-r--r-- | src/util/profile/test_profile.c | 227 |
11 files changed, 2223 insertions, 2212 deletions
diff --git a/src/util/profile/argv_parse.c b/src/util/profile/argv_parse.c index acdced8a3a..5101281dd5 100644 --- a/src/util/profile/argv_parse.c +++ b/src/util/profile/argv_parse.c @@ -1,6 +1,7 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * argv_parse.c --- utility function for parsing a string into a - * argc, argv array. + * argc, argv array. * * This file defines a function argv_parse() which parsing a * passed-in string, handling double quotes and backslashes, and @@ -37,100 +38,100 @@ #include <string.h> #include "argv_parse.h" -#define STATE_WHITESPACE 1 -#define STATE_TOKEN 2 -#define STATE_QUOTED 3 +#define STATE_WHITESPACE 1 +#define STATE_TOKEN 2 +#define STATE_QUOTED 3 /* * Returns 0 on success, -1 on failure. */ int argv_parse(char *in_buf, int *ret_argc, char ***ret_argv) { - int argc = 0, max_argc = 0; - char **argv, **new_argv, *buf, ch; - char *cp = 0, *outcp = 0; - int state = STATE_WHITESPACE; + int argc = 0, max_argc = 0; + char **argv, **new_argv, *buf, ch; + char *cp = 0, *outcp = 0; + int state = STATE_WHITESPACE; - buf = malloc(strlen(in_buf)+1); - if (!buf) - return -1; + buf = malloc(strlen(in_buf)+1); + if (!buf) + return -1; - max_argc = 0; argc = 0; argv = 0; - outcp = buf; - for (cp = in_buf; (ch = *cp); cp++) { - if (state == STATE_WHITESPACE) { - if (isspace((int) ch)) - continue; - /* Not whitespace, so start a new token */ - state = STATE_TOKEN; - if (argc >= max_argc) { - max_argc += 3; - new_argv = realloc(argv, - (max_argc+1)*sizeof(char *)); - if (!new_argv) { - if (argv) free(argv); - free(buf); - return -1; - } - argv = new_argv; - } - argv[argc++] = outcp; - } - if (state == STATE_QUOTED) { - if (ch == '"') - state = STATE_TOKEN; - else - *outcp++ = ch; - continue; - } - /* Must be processing characters in a word */ - if (isspace((int) ch)) { - /* - * Terminate the current word and start - * looking for the beginning of the next word. - */ - *outcp++ = 0; - state = STATE_WHITESPACE; - continue; - } - if (ch == '"') { - state = STATE_QUOTED; - continue; - } - if (ch == '\\') { - ch = *++cp; - switch (ch) { - case '\0': - ch = '\\'; cp--; break; - case 'n': - ch = '\n'; break; - case 't': - ch = '\t'; break; - case 'b': - ch = '\b'; break; - } - } - *outcp++ = ch; - } - if (state != STATE_WHITESPACE) - *outcp++ = '\0'; - if (argv == 0) { - argv = malloc(sizeof(char *)); - free(buf); - } - argv[argc] = 0; - if (ret_argc) - *ret_argc = argc; - if (ret_argv) - *ret_argv = argv; - return 0; + max_argc = 0; argc = 0; argv = 0; + outcp = buf; + for (cp = in_buf; (ch = *cp); cp++) { + if (state == STATE_WHITESPACE) { + if (isspace((int) ch)) + continue; + /* Not whitespace, so start a new token */ + state = STATE_TOKEN; + if (argc >= max_argc) { + max_argc += 3; + new_argv = realloc(argv, + (max_argc+1)*sizeof(char *)); + if (!new_argv) { + if (argv) free(argv); + free(buf); + return -1; + } + argv = new_argv; + } + argv[argc++] = outcp; + } + if (state == STATE_QUOTED) { + if (ch == '"') + state = STATE_TOKEN; + else + *outcp++ = ch; + continue; + } + /* Must be processing characters in a word */ + if (isspace((int) ch)) { + /* + * Terminate the current word and start + * looking for the beginning of the next word. + */ + *outcp++ = 0; + state = STATE_WHITESPACE; + continue; + } + if (ch == '"') { + state = STATE_QUOTED; + continue; + } + if (ch == '\\') { + ch = *++cp; + switch (ch) { + case '\0': + ch = '\\'; cp--; break; + case 'n': + ch = '\n'; break; + case 't': + ch = '\t'; break; + case 'b': + ch = '\b'; break; + } + } + *outcp++ = ch; + } + if (state != STATE_WHITESPACE) + *outcp++ = '\0'; + if (argv == 0) { + argv = malloc(sizeof(char *)); + free(buf); + } + argv[argc] = 0; + if (ret_argc) + *ret_argc = argc; + if (ret_argv) + *ret_argv = argv; + return 0; } void argv_free(char **argv) { - if (*argv) - free(*argv); - free(argv); + if (*argv) + free(*argv); + free(argv); } #ifdef DEBUG @@ -142,27 +143,27 @@ void argv_free(char **argv) int main(int argc, char **argv) { - int ac, ret; - char **av, **cpp; - char buf[256]; + int ac, ret; + char **av, **cpp; + char buf[256]; - while (!feof(stdin)) { - if (fgets(buf, sizeof(buf), stdin) == NULL) - break; - ret = argv_parse(buf, &ac, &av); - if (ret != 0) { - printf("Argv_parse returned %d!\n", ret); - continue; - } - printf("Argv_parse returned %d arguments...\n", ac); - for (cpp = av; *cpp; cpp++) { - if (cpp != av) - printf(", "); - printf("'%s'", *cpp); - } - printf("\n"); - argv_free(av); - } - exit(0); + while (!feof(stdin)) { + if (fgets(buf, sizeof(buf), stdin) == NULL) + break; + ret = argv_parse(buf, &ac, &av); + if (ret != 0) { + printf("Argv_parse returned %d!\n", ret); + continue; + } + printf("Argv_parse returned %d arguments...\n", ac); + for (cpp = av; *cpp; cpp++) { + if (cpp != av) + printf(", "); + printf("'%s'", *cpp); + } + printf("\n"); + argv_free(av); + } + exit(0); } #endif /* DEBUG */ diff --git a/src/util/profile/argv_parse.h b/src/util/profile/argv_parse.h index 86f4564e5e..a84bdee79a 100644 --- a/src/util/profile/argv_parse.h +++ b/src/util/profile/argv_parse.h @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * argv_parse.h --- header file for the argv parser. * diff --git a/src/util/profile/prof_FSp_glue.c b/src/util/profile/prof_FSp_glue.c index 6b9b5f0632..f1c7b07a67 100644 --- a/src/util/profile/prof_FSp_glue.c +++ b/src/util/profile/prof_FSp_glue.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * prof_FSp_glue.c --- Deprecated FSSpec functions. Mac-only. */ @@ -13,8 +14,8 @@ long KRB5_CALLCONV FSp_profile_init_path (const FSSpec* files, profile_t *ret_pr errcode_t KRB5_CALLCONV FSp_profile_init(files, ret_profile) - const FSSpec* files; - profile_t *ret_profile; + const FSSpec* files; + profile_t *ret_profile; { unsigned int fileCount = 0; const FSSpec *nextSpec; @@ -84,8 +85,8 @@ FSp_profile_init(files, ret_profile) errcode_t KRB5_CALLCONV FSp_profile_init_path(files, ret_profile) - const FSSpec* files; - profile_t *ret_profile; + const FSSpec* files; + profile_t *ret_profile; { return FSp_profile_init (files, ret_profile); } diff --git a/src/util/profile/prof_file.c b/src/util/profile/prof_file.c index 13aa18c4c0..cbc274da74 100644 --- a/src/util/profile/prof_file.c +++ b/src/util/profile/prof_file.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * prof_file.c ---- routines that manipulate an individual profile file. */ @@ -31,13 +32,13 @@ #include "k5-platform.h" struct global_shared_profile_data { - /* This is the head of the global list of shared trees */ - prf_data_t trees; - /* Lock for above list. */ - k5_mutex_t mutex; + /* This is the head of the global list of shared trees */ + prf_data_t trees; + /* Lock for above list. */ + k5_mutex_t mutex; }; -#define g_shared_trees (krb5int_profile_shared_data.trees) -#define g_shared_trees_mutex (krb5int_profile_shared_data.mutex) +#define g_shared_trees (krb5int_profile_shared_data.trees) +#define g_shared_trees_mutex (krb5int_profile_shared_data.mutex) static struct global_shared_profile_data krb5int_profile_shared_data = { 0, @@ -60,9 +61,9 @@ void profile_library_finalizer(void) { if (! INITIALIZER_RAN(profile_library_initializer) || PROGRAM_EXITING()) { #ifdef SHOW_INITFINI_FUNCS - printf("profile_library_finalizer: skipping\n"); + printf("profile_library_finalizer: skipping\n"); #endif - return; + return; } #ifdef SHOW_INITFINI_FUNCS printf("profile_library_finalizer\n"); @@ -76,82 +77,82 @@ static void profile_free_file_data(prf_data_t); #if 0 -#define scan_shared_trees_locked() \ - { \ - prf_data_t d; \ - k5_mutex_assert_locked(&g_shared_trees_mutex); \ - for (d = g_shared_trees; d; d = d->next) { \ - assert(d->magic == PROF_MAGIC_FILE_DATA); \ - assert((d->flags & PROFILE_FILE_SHARED) != 0); \ - assert(d->filespec[0] != 0); \ - assert(d->fslen <= 1000); /* XXX */ \ - assert(d->filespec[d->fslen] == 0); \ - assert(d->fslen = strlen(d->filespec)); \ - assert(d->root != NULL); \ - } \ - } - -#define scan_shared_trees_unlocked() \ - { \ - int r; \ - r = k5_mutex_lock(&g_shared_trees_mutex); \ - assert (r == 0); \ - scan_shared_trees_locked(); \ - k5_mutex_unlock(&g_shared_trees_mutex); \ - } +#define scan_shared_trees_locked() \ + { \ + prf_data_t d; \ + k5_mutex_assert_locked(&g_shared_trees_mutex); \ + for (d = g_shared_trees; d; d = d->next) { \ + assert(d->magic == PROF_MAGIC_FILE_DATA); \ + assert((d->flags & PROFILE_FILE_SHARED) != 0); \ + assert(d->filespec[0] != 0); \ + assert(d->fslen <= 1000); /* XXX */ \ + assert(d->filespec[d->fslen] == 0); \ + assert(d->fslen = strlen(d->filespec)); \ + assert(d->root != NULL); \ + } \ + } + +#define scan_shared_trees_unlocked() \ + { \ + int r; \ + r = k5_mutex_lock(&g_shared_trees_mutex); \ + assert (r == 0); \ + scan_shared_trees_locked(); \ + k5_mutex_unlock(&g_shared_trees_mutex); \ + } #else -#define scan_shared_trees_locked() { ; } -#define scan_shared_trees_unlocked() { ; } +#define scan_shared_trees_locked() { ; } +#define scan_shared_trees_unlocked() { ; } #endif static int rw_access(const_profile_filespec_t filespec) { #ifdef HAVE_ACCESS - if (access(filespec, W_OK) == 0) - return 1; - else - return 0; + if (access(filespec, W_OK) == 0) + return 1; + else + return 0; #else - /* - * We're on a substandard OS that doesn't support access. So - * we kludge a test using stdio routines, and hope fopen - * checks the r/w permissions. - */ - FILE *f; - - f = fopen(filespec, "r+"); - if (f) { - fclose(f); - return 1; - } - return 0; + /* + * We're on a substandard OS that doesn't support access. So + * we kludge a test using stdio routines, and hope fopen + * checks the r/w permissions. + */ + FILE *f; + + f = fopen(filespec, "r+"); + if (f) { + fclose(f); + return 1; + } + return 0; #endif } static int r_access(const_profile_filespec_t filespec) { #ifdef HAVE_ACCESS - if (access(filespec, R_OK) == 0) - return 1; - else - return 0; + if (access(filespec, R_OK) == 0) + return 1; + else + return 0; #else - /* - * We're on a substandard OS that doesn't support access. So - * we kludge a test using stdio routines, and hope fopen - * checks the r/w permissions. - */ - FILE *f; - - f = fopen(filespec, "r"); - if (f) { - fclose(f); - return 1; - } - return 0; + /* + * We're on a substandard OS that doesn't support access. So + * we kludge a test using stdio routines, and hope fopen + * checks the r/w permissions. + */ + FILE *f; + + f = fopen(filespec, "r"); + if (f) { + fclose(f); + return 1; + } + return 0; #endif } @@ -175,10 +176,10 @@ profile_make_prf_data(const char *filename) slen = offsetof(struct _prf_data_t, filespec); len = slen + flen + 1; if (len < sizeof(struct _prf_data_t)) - len = sizeof(struct _prf_data_t); + len = sizeof(struct _prf_data_t); d = malloc(len); if (d == NULL) - return NULL; + return NULL; memset(d, 0, len); fcopy = (char *) d + slen; assert(fcopy == d->filespec); @@ -193,189 +194,189 @@ profile_make_prf_data(const char *filename) } errcode_t profile_open_file(const_profile_filespec_t filespec, - prf_file_t *ret_prof) + prf_file_t *ret_prof) { - prf_file_t prf; - errcode_t retval; - char *home_env = 0; - prf_data_t data; - char *expanded_filename; - - retval = CALL_INIT_FUNCTION(profile_library_initializer); - if (retval) - return retval; - - scan_shared_trees_unlocked(); - - prf = malloc(sizeof(struct _prf_file_t)); - if (!prf) - return ENOMEM; - memset(prf, 0, sizeof(struct _prf_file_t)); - prf->magic = PROF_MAGIC_FILE; - - if (filespec[0] == '~' && filespec[1] == '/') { - home_env = getenv("HOME"); + prf_file_t prf; + errcode_t retval; + char *home_env = 0; + prf_data_t data; + char *expanded_filename; + + retval = CALL_INIT_FUNCTION(profile_library_initializer); + if (retval) + return retval; + + scan_shared_trees_unlocked(); + + prf = malloc(sizeof(struct _prf_file_t)); + if (!prf) + return ENOMEM; + memset(prf, 0, sizeof(struct _prf_file_t)); + prf->magic = PROF_MAGIC_FILE; + + if (filespec[0] == '~' && filespec[1] == '/') { + home_env = getenv("HOME"); #ifdef HAVE_PWD_H - if (home_env == NULL) { - uid_t uid; - struct passwd *pw, pwx; - char pwbuf[BUFSIZ]; - - uid = getuid(); - if (!k5_getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw) - && pw != NULL && pw->pw_dir[0] != 0) - home_env = pw->pw_dir; - } + if (home_env == NULL) { + uid_t uid; + struct passwd *pw, pwx; + char pwbuf[BUFSIZ]; + + uid = getuid(); + if (!k5_getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw) + && pw != NULL && pw->pw_dir[0] != 0) + home_env = pw->pw_dir; + } #endif - } - if (home_env) { - if (asprintf(&expanded_filename, "%s%s", home_env, - filespec + 1) < 0) - expanded_filename = 0; - } else - expanded_filename = strdup(filespec); - if (expanded_filename == 0) { - free(prf); - return ENOMEM; - } - - retval = k5_mutex_lock(&g_shared_trees_mutex); - if (retval) { - free(expanded_filename); - free(prf); - scan_shared_trees_unlocked(); - return retval; - } - scan_shared_trees_locked(); - for (data = g_shared_trees; data; data = data->next) { - if (!strcmp(data->filespec, expanded_filename) - /* Check that current uid has read access. */ - && r_access(data->filespec)) - break; - } - if (data) { - data->refcount++; - (void) k5_mutex_unlock(&g_shared_trees_mutex); - retval = profile_update_file_data(data); - free(expanded_filename); - prf->data = data; - *ret_prof = prf; - scan_shared_trees_unlocked(); - return retval; - } - (void) k5_mutex_unlock(&g_shared_trees_mutex); - data = profile_make_prf_data(expanded_filename); - if (data == NULL) { - free(prf); - free(expanded_filename); - return ENOMEM; - } - free(expanded_filename); - prf->data = data; - - retval = k5_mutex_init(&data->lock); - if (retval) { - free(data); - free(prf); - return retval; - } - - retval = profile_update_file(prf); - if (retval) { - profile_close_file(prf); - return retval; - } - - retval = k5_mutex_lock(&g_shared_trees_mutex); - if (retval) { - profile_close_file(prf); - scan_shared_trees_unlocked(); - return retval; - } - scan_shared_trees_locked(); - data->flags |= PROFILE_FILE_SHARED; - data->next = g_shared_trees; - g_shared_trees = data; - scan_shared_trees_locked(); - (void) k5_mutex_unlock(&g_shared_trees_mutex); - - *ret_prof = prf; - return 0; + } + if (home_env) { + if (asprintf(&expanded_filename, "%s%s", home_env, + filespec + 1) < 0) + expanded_filename = 0; + } else + expanded_filename = strdup(filespec); + if (expanded_filename == 0) { + free(prf); + return ENOMEM; + } + + retval = k5_mutex_lock(&g_shared_trees_mutex); + if (retval) { + free(expanded_filename); + free(prf); + scan_shared_trees_unlocked(); + return retval; + } + scan_shared_trees_locked(); + for (data = g_shared_trees; data; data = data->next) { + if (!strcmp(data->filespec, expanded_filename) + /* Check that current uid has read access. */ + && r_access(data->filespec)) + break; + } + if (data) { + data->refcount++; + (void) k5_mutex_unlock(&g_shared_trees_mutex); + retval = profile_update_file_data(data); + free(expanded_filename); + prf->data = data; + *ret_prof = prf; + scan_shared_trees_unlocked(); + return retval; + } + (void) k5_mutex_unlock(&g_shared_trees_mutex); + data = profile_make_prf_data(expanded_filename); + if (data == NULL) { + free(prf); + free(expanded_filename); + return ENOMEM; + } + free(expanded_filename); + prf->data = data; + + retval = k5_mutex_init(&data->lock); + if (retval) { + free(data); + free(prf); + return retval; + } + + retval = profile_update_file(prf); + if (retval) { + profile_close_file(prf); + return retval; + } + + retval = k5_mutex_lock(&g_shared_trees_mutex); + if (retval) { + profile_close_file(prf); + scan_shared_trees_unlocked(); + return retval; + } + scan_shared_trees_locked(); + data->flags |= PROFILE_FILE_SHARED; + data->next = g_shared_trees; + g_shared_trees = data; + scan_shared_trees_locked(); + (void) k5_mutex_unlock(&g_shared_trees_mutex); + + *ret_prof = prf; + return 0; } errcode_t profile_update_file_data_locked(prf_data_t data) { - errcode_t retval; + errcode_t retval; #ifdef HAVE_STAT - struct stat st; - unsigned long frac; - time_t now; + struct stat st; + unsigned long frac; + time_t now; #endif - FILE *f; + FILE *f; #ifdef HAVE_STAT - now = time(0); - if (now == data->last_stat && data->root != NULL) { - return 0; - } - if (stat(data->filespec, &st)) { - return errno; - } - data->last_stat = now; + now = time(0); + if (now == data->last_stat && data->root != NULL) { + return 0; + } + if (stat(data->filespec, &st)) { + return errno; + } + data->last_stat = now; #if defined HAVE_STRUCT_STAT_ST_MTIMENSEC - frac = st.st_mtimensec; + frac = st.st_mtimensec; #elif defined HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC - frac = st.st_mtimespec.tv_nsec; + frac = st.st_mtimespec.tv_nsec; #elif defined HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC - frac = st.st_mtim.tv_nsec; + frac = st.st_mtim.tv_nsec; #else - frac = 0; + frac = 0; #endif - if (st.st_mtime == data->timestamp - && frac == data->frac_ts - && data->root != NULL) { - return 0; - } - if (data->root) { - profile_free_node(data->root); - data->root = 0; - } - if (data->comment) { - free(data->comment); - data->comment = 0; - } + if (st.st_mtime == data->timestamp + && frac == data->frac_ts + && data->root != NULL) { + return 0; + } + if (data->root) { + profile_free_node(data->root); + data->root = 0; + } + if (data->comment) { + free(data->comment); + data->comment = 0; + } #else - /* - * If we don't have the stat() call, assume that our in-core - * memory image is correct. That is, we won't reread the - * profile file if it changes. - */ - if (data->root) { - return 0; - } + /* + * If we don't have the stat() call, assume that our in-core + * memory image is correct. That is, we won't reread the + * profile file if it changes. + */ + if (data->root) { + return 0; + } #endif - errno = 0; - f = fopen(data->filespec, "r"); - if (f == NULL) { - retval = errno; - if (retval == 0) - retval = ENOENT; - return retval; - } - set_cloexec_file(f); - data->upd_serial++; - data->flags &= PROFILE_FILE_SHARED; /* FIXME same as '=' operator */ - retval = profile_parse_file(f, &data->root); - fclose(f); - if (retval) { - return retval; - } - assert(data->root != NULL); + errno = 0; + f = fopen(data->filespec, "r"); + if (f == NULL) { + retval = errno; + if (retval == 0) + retval = ENOENT; + return retval; + } + set_cloexec_file(f); + data->upd_serial++; + data->flags &= PROFILE_FILE_SHARED; /* FIXME same as '=' operator */ + retval = profile_parse_file(f, &data->root); + fclose(f); + if (retval) { + return retval; + } + assert(data->root != NULL); #ifdef HAVE_STAT - data->timestamp = st.st_mtime; - data->frac_ts = frac; + data->timestamp = st.st_mtime; + data->frac_ts = frac; #endif - return 0; + return 0; } errcode_t profile_update_file_data(prf_data_t data) @@ -384,7 +385,7 @@ errcode_t profile_update_file_data(prf_data_t data) retval = k5_mutex_lock(&data->lock); if (retval) - return retval; + return retval; retval = profile_update_file_data_locked(data); retval2 = k5_mutex_unlock(&data->lock); return retval ? retval : retval2; @@ -401,118 +402,118 @@ make_hard_link(const char *oldpath, const char *newpath) } static errcode_t write_data_to_file(prf_data_t data, const char *outfile, - int can_create) + int can_create) { - FILE *f; - profile_filespec_t new_file; - profile_filespec_t old_file; - errcode_t retval = 0; - - retval = ENOMEM; - - new_file = old_file = 0; - if (asprintf(&new_file, "%s.$$$", outfile) < 0) { - new_file = NULL; - goto errout; - } - if (asprintf(&old_file, "%s.bak", outfile) < 0) { - old_file = NULL; - goto errout; - } - - errno = 0; - - f = fopen(new_file, "w"); - if (!f) { - retval = errno; - if (retval == 0) - retval = PROF_FAIL_OPEN; - goto errout; - } - - set_cloexec_file(f); - profile_write_tree_file(data->root, f); - if (fclose(f) != 0) { - retval = errno; - goto errout; - } - - unlink(old_file); - if (make_hard_link(outfile, old_file) == 0) { - /* Okay, got the hard link. Yay. Now we've got our - backup version, so just put the new version in - place. */ - if (rename(new_file, outfile)) { - /* Weird, the rename didn't work. But the old version - should still be in place, so no special cleanup is - needed. */ - retval = errno; - goto errout; - } - } else if (errno == ENOENT && can_create) { - if (rename(new_file, outfile)) { - retval = errno; - goto errout; - } - } else { - /* Couldn't make the hard link, so there's going to be a - small window where data->filespec does not refer to - either version. */ + FILE *f; + profile_filespec_t new_file; + profile_filespec_t old_file; + errcode_t retval = 0; + + retval = ENOMEM; + + new_file = old_file = 0; + if (asprintf(&new_file, "%s.$$$", outfile) < 0) { + new_file = NULL; + goto errout; + } + if (asprintf(&old_file, "%s.bak", outfile) < 0) { + old_file = NULL; + goto errout; + } + + errno = 0; + + f = fopen(new_file, "w"); + if (!f) { + retval = errno; + if (retval == 0) + retval = PROF_FAIL_OPEN; + goto errout; + } + + set_cloexec_file(f); + profile_write_tree_file(data->root, f); + if (fclose(f) != 0) { + retval = errno; + goto errout; + } + + unlink(old_file); + if (make_hard_link(outfile, old_file) == 0) { + /* Okay, got the hard link. Yay. Now we've got our + backup version, so just put the new version in + place. */ + if (rename(new_file, outfile)) { + /* Weird, the rename didn't work. But the old version + should still be in place, so no special cleanup is + needed. */ + retval = errno; + goto errout; + } + } else if (errno == ENOENT && can_create) { + if (rename(new_file, outfile)) { + retval = errno; + goto errout; + } + } else { + /* Couldn't make the hard link, so there's going to be a + small window where data->filespec does not refer to + either version. */ #ifndef _WIN32 - sync(); + sync(); #endif - if (rename(outfile, old_file)) { - retval = errno; - goto errout; - } - if (rename(new_file, outfile)) { - retval = errno; - rename(old_file, outfile); /* back out... */ - goto errout; - } - } - - data->flags = 0; - retval = 0; + if (rename(outfile, old_file)) { + retval = errno; + goto errout; + } + if (rename(new_file, outfile)) { + retval = errno; + rename(old_file, outfile); /* back out... */ + goto errout; + } + } + + data->flags = 0; + retval = 0; errout: - if (new_file) - free(new_file); - if (old_file) - free(old_file); - return retval; + if (new_file) + free(new_file); + if (old_file) + free(old_file); + return retval; } errcode_t profile_flush_file_data_to_buffer (prf_data_t data, char **bufp) { - errcode_t retval; - retval = k5_mutex_lock(&data->lock); - if (retval) - return retval; - retval = profile_write_tree_to_buffer(data->root, bufp); - k5_mutex_unlock(&data->lock); - return retval; + errcode_t retval; + retval = k5_mutex_lock(&data->lock); + if (retval) + return retval; + retval = profile_write_tree_to_buffer(data->root, bufp); + k5_mutex_unlock(&data->lock); + return retval; } errcode_t profile_flush_file_data(prf_data_t data) { - errcode_t retval = 0; + errcode_t retval = 0; - if (!data || data->magic != PROF_MAGIC_FILE_DATA) - return PROF_MAGIC_FILE_DATA; + if (!data || data->magic != PROF_MAGIC_FILE_DATA) + return PROF_MAGIC_FILE_DATA; - retval = k5_mutex_lock(&data->lock); - if (retval) - return retval; + retval = k5_mutex_lock(&data->lock); + if (retval) + return retval; - if ((data->flags & PROFILE_FILE_DIRTY) == 0) { - k5_mutex_unlock(&data->lock); - return 0; - } + if ((data->flags & PROFILE_FILE_DIRTY) == 0) { + k5_mutex_unlock(&data->lock); + return 0; + } - retval = write_data_to_file(data, data->filespec, 0); - k5_mutex_unlock(&data->lock); - return retval; + retval = write_data_to_file(data, data->filespec, 0); + k5_mutex_unlock(&data->lock); + return retval; } errcode_t profile_flush_file_data_to_file(prf_data_t data, const char *outfile) @@ -520,11 +521,11 @@ errcode_t profile_flush_file_data_to_file(prf_data_t data, const char *outfile) errcode_t retval = 0; if (!data || data->magic != PROF_MAGIC_FILE_DATA) - return PROF_MAGIC_FILE_DATA; + return PROF_MAGIC_FILE_DATA; retval = k5_mutex_lock(&data->lock); if (retval) - return retval; + return retval; retval = write_data_to_file(data, outfile, 1); k5_mutex_unlock(&data->lock); return retval; @@ -537,7 +538,7 @@ void profile_dereference_data(prf_data_t data) int err; err = k5_mutex_lock(&g_shared_trees_mutex); if (err) - return; + return; profile_dereference_data_locked(data); (void) k5_mutex_unlock(&g_shared_trees_mutex); } @@ -546,7 +547,7 @@ void profile_dereference_data_locked(prf_data_t data) scan_shared_trees_locked(); data->refcount--; if (data->refcount == 0) - profile_free_file_data(data); + profile_free_file_data(data); scan_shared_trees_locked(); } @@ -570,27 +571,27 @@ static void profile_free_file_data(prf_data_t data) { scan_shared_trees_locked(); if (data->flags & PROFILE_FILE_SHARED) { - /* Remove from linked list. */ - if (g_shared_trees == data) - g_shared_trees = data->next; - else { - prf_data_t prev, next; - prev = g_shared_trees; - next = prev->next; - while (next) { - if (next == data) { - prev->next = next->next; - break; - } - prev = next; - next = next->next; - } - } + /* Remove from linked list. */ + if (g_shared_trees == data) + g_shared_trees = data->next; + else { + prf_data_t prev, next; + prev = g_shared_trees; + next = prev->next; + while (next) { + if (next == data) { + prev->next = next->next; + break; + } + prev = next; + next = next->next; + } + } } if (data->root) - profile_free_node(data->root); + profile_free_node(data->root); if (data->comment) - free(data->comment); + free(data->comment); data->magic = 0; k5_mutex_destroy(&data->lock); free(data); @@ -599,11 +600,11 @@ static void profile_free_file_data(prf_data_t data) errcode_t profile_close_file(prf_file_t prf) { - errcode_t retval; + errcode_t retval; - retval = profile_flush_file(prf); - if (retval) - return retval; - profile_free_file(prf); - return 0; + retval = profile_flush_file(prf); + if (retval) + return retval; + profile_free_file(prf); + return 0; } diff --git a/src/util/profile/prof_get.c b/src/util/profile/prof_get.c index 87861fce38..460d2e5f21 100644 --- a/src/util/profile/prof_get.c +++ b/src/util/profile/prof_get.c @@ -1,6 +1,7 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * prof_get.c --- routines that expose the public interfaces for - * querying items from the profile. + * querying items from the profile. * */ @@ -27,9 +28,9 @@ */ struct profile_string_list { - char **list; - unsigned int num; - unsigned int max; + char **list; + unsigned int num; + unsigned int max; }; /* @@ -37,13 +38,13 @@ struct profile_string_list { */ static errcode_t init_list(struct profile_string_list *list) { - list->num = 0; - list->max = 10; - list->list = malloc(list->max * sizeof(char *)); - if (list->list == 0) - return ENOMEM; - list->list[0] = 0; - return 0; + list->num = 0; + list->max = 10; + list->list = malloc(list->max * sizeof(char *)); + if (list->list == 0) + return ENOMEM; + list->list[0] = 0; + return 0; } /* @@ -52,21 +53,21 @@ static errcode_t init_list(struct profile_string_list *list) */ static void end_list(struct profile_string_list *list, char ***ret_list) { - char **cp; - - if (list == 0) - return; - - if (ret_list) { - *ret_list = list->list; - return; - } else { - for (cp = list->list; *cp; cp++) - free(*cp); - free(list->list); - } - list->num = list->max = 0; - list->list = 0; + char **cp; + + if (list == 0) + return; + + if (ret_list) { + *ret_list = list->list; + return; + } else { + for (cp = list->list; *cp; cp++) + free(*cp); + free(list->list); + } + list->num = list->max = 0; + list->list = 0; } /* @@ -74,24 +75,24 @@ static void end_list(struct profile_string_list *list, char ***ret_list) */ static errcode_t add_to_list(struct profile_string_list *list, const char *str) { - char *newstr, **newlist; - unsigned int newmax; - - if (list->num+1 >= list->max) { - newmax = list->max + 10; - newlist = realloc(list->list, newmax * sizeof(char *)); - if (newlist == 0) - return ENOMEM; - list->max = newmax; - list->list = newlist; - } - newstr = strdup(str); - if (newstr == 0) - return ENOMEM; - - list->list[list->num++] = newstr; - list->list[list->num] = 0; - return 0; + char *newstr, **newlist; + unsigned int newmax; + + if (list->num+1 >= list->max) { + newmax = list->max + 10; + newlist = realloc(list->list, newmax * sizeof(char *)); + if (newlist == 0) + return ENOMEM; + list->max = newmax; + list->list = newlist; + } + newstr = strdup(str); + if (newstr == 0) + return ENOMEM; + + list->list[list->num++] = newstr; + list->list[list->num] = 0; + return 0; } /* @@ -99,16 +100,16 @@ static errcode_t add_to_list(struct profile_string_list *list, const char *str) */ static int is_list_member(struct profile_string_list *list, const char *str) { - char **cpp; + char **cpp; - if (!list->list) - return 0; + if (!list->list) + return 0; - for (cpp = list->list; *cpp; cpp++) { - if (!strcmp(*cpp, str)) - return 1; - } - return 0; + for (cpp = list->list; *cpp; cpp++) { + if (!strcmp(*cpp, str)) + return 1; + } + return 0; } /* @@ -117,51 +118,51 @@ static int is_list_member(struct profile_string_list *list, const char *str) */ void KRB5_CALLCONV profile_free_list(char **list) { - char **cp; + char **cp; if (list == 0) - return; + return; for (cp = list; *cp; cp++) - free(*cp); + free(*cp); free(list); } errcode_t KRB5_CALLCONV profile_get_values(profile_t profile, const char *const *names, - char ***ret_values) + char ***ret_values) { - errcode_t retval; - void *state; - char *value; - struct profile_string_list values; - - if ((retval = profile_node_iterator_create(profile, names, - PROFILE_ITER_RELATIONS_ONLY, - &state))) - return retval; - - if ((retval = init_list(&values))) - return retval; - - do { - if ((retval = profile_node_iterator(&state, 0, 0, &value))) - goto cleanup; - if (value) - add_to_list(&values, value); - } while (state); - - if (values.num == 0) { - retval = PROF_NO_RELATION; - goto cleanup; - } - - end_list(&values, ret_values); - return 0; + errcode_t retval; + void *state; + char *value; + struct profile_string_list values; + + if ((retval = profile_node_iterator_create(profile, names, + PROFILE_ITER_RELATIONS_ONLY, + &state))) + return retval; + + if ((retval = init_list(&values))) + return retval; + + do { + if ((retval = profile_node_iterator(&state, 0, 0, &value))) + goto cleanup; + if (value) + add_to_list(&values, value); + } while (state); + + if (values.num == 0) { + retval = PROF_NO_RELATION; + goto cleanup; + } + + end_list(&values, ret_values); + return 0; cleanup: - end_list(&values, 0); - return retval; + end_list(&values, 0); + return retval; } /* @@ -169,105 +170,105 @@ cleanup: * helper function for profile_get_string, profile_get_integer, etc. */ errcode_t profile_get_value(profile_t profile, const char **names, - const char **ret_value) + const char **ret_value) { - errcode_t retval; - void *state; - char *value; + errcode_t retval; + void *state; + char *value; - if ((retval = profile_node_iterator_create(profile, names, - PROFILE_ITER_RELATIONS_ONLY, - &state))) - return retval; + if ((retval = profile_node_iterator_create(profile, names, + PROFILE_ITER_RELATIONS_ONLY, + &state))) + return retval; - if ((retval = profile_node_iterator(&state, 0, 0, &value))) - goto cleanup; + if ((retval = profile_node_iterator(&state, 0, 0, &value))) + goto cleanup; - if (value) - *ret_value = value; - else - retval = PROF_NO_RELATION; + if (value) + *ret_value = value; + else + retval = PROF_NO_RELATION; cleanup: - profile_node_iterator_free(&state); - return retval; + profile_node_iterator_free(&state); + return retval; } errcode_t KRB5_CALLCONV profile_get_string(profile_t profile, const char *name, const char *subname, - const char *subsubname, const char *def_val, - char **ret_string) + const char *subsubname, const char *def_val, + char **ret_string) { - const char *value; - errcode_t retval; - const char *names[4]; - - if (profile) { - names[0] = name; - names[1] = subname; - names[2] = subsubname; - names[3] = 0; - retval = profile_get_value(profile, names, &value); - if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) - value = def_val; - else if (retval) - return retval; - } else - value = def_val; - - if (value) { - *ret_string = strdup(value); - if (*ret_string == 0) - return ENOMEM; - } else - *ret_string = 0; - return 0; + const char *value; + errcode_t retval; + const char *names[4]; + + if (profile) { + names[0] = name; + names[1] = subname; + names[2] = subsubname; + names[3] = 0; + retval = profile_get_value(profile, names, &value); + if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) + value = def_val; + else if (retval) + return retval; + } else + value = def_val; + + if (value) { + *ret_string = strdup(value); + if (*ret_string == 0) + return ENOMEM; + } else + *ret_string = 0; + return 0; } errcode_t KRB5_CALLCONV profile_get_integer(profile_t profile, const char *name, const char *subname, - const char *subsubname, int def_val, int *ret_int) + const char *subsubname, int def_val, int *ret_int) { - const char *value; - errcode_t retval; - const char *names[4]; - char *end_value; - long ret_long; - - *ret_int = def_val; - if (profile == 0) - return 0; - - names[0] = name; - names[1] = subname; - names[2] = subsubname; - names[3] = 0; - retval = profile_get_value(profile, names, &value); - if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) { - *ret_int = def_val; - return 0; - } else if (retval) - return retval; - - if (value[0] == 0) - /* Empty string is no good. */ - return PROF_BAD_INTEGER; - errno = 0; - ret_long = strtol (value, &end_value, 10); - - /* Overflow or underflow. */ - if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0) - return PROF_BAD_INTEGER; - /* Value outside "int" range. */ - if ((long) (int) ret_long != ret_long) - return PROF_BAD_INTEGER; - /* Garbage in string. */ - if (end_value != value + strlen (value)) - return PROF_BAD_INTEGER; - - - *ret_int = ret_long; - return 0; + const char *value; + errcode_t retval; + const char *names[4]; + char *end_value; + long ret_long; + + *ret_int = def_val; + if (profile == 0) + return 0; + + names[0] = name; + names[1] = subname; + names[2] = subsubname; + names[3] = 0; + retval = profile_get_value(profile, names, &value); + if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) { + *ret_int = def_val; + return 0; + } else if (retval) + return retval; + + if (value[0] == 0) + /* Empty string is no good. */ + return PROF_BAD_INTEGER; + errno = 0; + ret_long = strtol (value, &end_value, 10); + + /* Overflow or underflow. */ + if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0) + return PROF_BAD_INTEGER; + /* Value outside "int" range. */ + if ((long) (int) ret_long != ret_long) + return PROF_BAD_INTEGER; + /* Garbage in string. */ + if (end_value != value + strlen (value)) + return PROF_BAD_INTEGER; + + + *ret_int = ret_long; + return 0; } static const char *const conf_yes[] = { @@ -286,50 +287,50 @@ profile_parse_boolean(const char *s, int *ret_boolean) const char *const *p; if (ret_boolean == NULL) - return PROF_EINVAL; + return PROF_EINVAL; for(p=conf_yes; *p; p++) { - if (!strcasecmp(*p,s)) { - *ret_boolean = 1; - return 0; - } + if (!strcasecmp(*p,s)) { + *ret_boolean = 1; + return 0; + } } for(p=conf_no; *p; p++) { - if (!strcasecmp(*p,s)) { - *ret_boolean = 0; - return 0; - } + if (!strcasecmp(*p,s)) { + *ret_boolean = 0; + return 0; + } } - return PROF_BAD_BOOLEAN; + return PROF_BAD_BOOLEAN; } errcode_t KRB5_CALLCONV profile_get_boolean(profile_t profile, const char *name, const char *subname, - const char *subsubname, int def_val, int *ret_boolean) + const char *subsubname, int def_val, int *ret_boolean) { - const char *value; - errcode_t retval; - const char *names[4]; - - if (profile == 0) { - *ret_boolean = def_val; - return 0; - } - - names[0] = name; - names[1] = subname; - names[2] = subsubname; - names[3] = 0; - retval = profile_get_value(profile, names, &value); - if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) { - *ret_boolean = def_val; - return 0; - } else if (retval) - return retval; - - return profile_parse_boolean (value, ret_boolean); + const char *value; + errcode_t retval; + const char *names[4]; + + if (profile == 0) { + *ret_boolean = def_val; + return 0; + } + + names[0] = name; + names[1] = subname; + names[2] = subsubname; + names[3] = 0; + retval = profile_get_value(profile, names, &value); + if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) { + *ret_boolean = def_val; + return 0; + } else if (retval) + return retval; + + return profile_parse_boolean (value, ret_boolean); } /* @@ -338,34 +339,34 @@ profile_get_boolean(profile_t profile, const char *name, const char *subname, */ errcode_t KRB5_CALLCONV profile_get_subsection_names(profile_t profile, const char **names, - char ***ret_names) + char ***ret_names) { - errcode_t retval; - void *state; - char *name; - struct profile_string_list values; + errcode_t retval; + void *state; + char *name; + struct profile_string_list values; - if ((retval = profile_node_iterator_create(profile, names, - PROFILE_ITER_LIST_SECTION | PROFILE_ITER_SECTIONS_ONLY, - &state))) - return retval; + if ((retval = profile_node_iterator_create(profile, names, + PROFILE_ITER_LIST_SECTION | PROFILE_ITER_SECTIONS_ONLY, + &state))) + return retval; - if ((retval = init_list(&values))) - return retval; + if ((retval = init_list(&values))) + return retval; - do { - if ((retval = profile_node_iterator(&state, 0, &name, 0))) - goto cleanup; - if (name) - add_to_list(&values, name); - } while (state); + do { + if ((retval = profile_node_iterator(&state, 0, &name, 0))) + goto cleanup; + if (name) + add_to_list(&values, name); + } while (state); - end_list(&values, ret_names); - return 0; + end_list(&values, ret_names); + return 0; cleanup: - end_list(&values, 0); - return retval; + end_list(&values, 0); + return retval; } /* @@ -374,85 +375,85 @@ cleanup: */ errcode_t KRB5_CALLCONV profile_get_relation_names(profile_t profile, const char **names, - char ***ret_names) + char ***ret_names) { - errcode_t retval; - void *state; - char *name; - struct profile_string_list values; + errcode_t retval; + void *state; + char *name; + struct profile_string_list values; - if ((retval = profile_node_iterator_create(profile, names, - PROFILE_ITER_LIST_SECTION | PROFILE_ITER_RELATIONS_ONLY, - &state))) - return retval; + if ((retval = profile_node_iterator_create(profile, names, + PROFILE_ITER_LIST_SECTION | PROFILE_ITER_RELATIONS_ONLY, + &state))) + return retval; - if ((retval = init_list(&values))) - return retval; + if ((retval = init_list(&values))) + return retval; - do { - if ((retval = profile_node_iterator(&state, 0, &name, 0))) - goto cleanup; - if (name && !is_list_member(&values, name)) - add_to_list(&values, name); - } while (state); + do { + if ((retval = profile_node_iterator(&state, 0, &name, 0))) + goto cleanup; + if (name && !is_list_member(&values, name)) + add_to_list(&values, name); + } while (state); - end_list(&values, ret_names); - return 0; + end_list(&values, ret_names); + return 0; cleanup: - end_list(&values, 0); - return retval; + end_list(&values, 0); + return retval; } errcode_t KRB5_CALLCONV profile_iterator_create(profile_t profile, const char *const *names, int flags, - void **ret_iter) + void **ret_iter) { - return profile_node_iterator_create(profile, names, flags, ret_iter); + return profile_node_iterator_create(profile, names, flags, ret_iter); } void KRB5_CALLCONV profile_iterator_free(void **iter_p) { - profile_node_iterator_free(iter_p); + profile_node_iterator_free(iter_p); } errcode_t KRB5_CALLCONV profile_iterator(void **iter_p, char **ret_name, char **ret_value) { - char *name, *value; - errcode_t retval; - - retval = profile_node_iterator(iter_p, 0, &name, &value); - if (retval) - return retval; - - if (ret_name) { - if (name) { - *ret_name = strdup(name); - if (!*ret_name) - return ENOMEM; - } else - *ret_name = 0; - } - if (ret_value) { - if (value) { - *ret_value = strdup(value); - if (!*ret_value) { - if (ret_name) { - free(*ret_name); - *ret_name = 0; - } - return ENOMEM; - } - } else - *ret_value = 0; - } - return 0; + char *name, *value; + errcode_t retval; + + retval = profile_node_iterator(iter_p, 0, &name, &value); + if (retval) + return retval; + + if (ret_name) { + if (name) { + *ret_name = strdup(name); + if (!*ret_name) + return ENOMEM; + } else + *ret_name = 0; + } + if (ret_value) { + if (value) { + *ret_value = strdup(value); + if (!*ret_value) { + if (ret_name) { + free(*ret_name); + *ret_name = 0; + } + return ENOMEM; + } + } else + *ret_value = 0; + } + return 0; } void KRB5_CALLCONV profile_release_string(char *str) { - free(str); + free(str); } diff --git a/src/util/profile/prof_init.c b/src/util/profile/prof_init.c index 91ace98105..bd42b13805 100644 --- a/src/util/profile/prof_init.c +++ b/src/util/profile/prof_init.c @@ -1,6 +1,7 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * prof_init.c --- routines that manipulate the user-visible profile_t - * object. + * object. */ #include "prof_int.h" @@ -23,62 +24,62 @@ typedef int32_t prof_int32; errcode_t KRB5_CALLCONV profile_init(const_profile_filespec_t *files, profile_t *ret_profile) { - const_profile_filespec_t *fs; - profile_t profile; - prf_file_t new_file, last = 0; - errcode_t retval = 0; - - profile = malloc(sizeof(struct _profile_t)); - if (!profile) - return ENOMEM; - memset(profile, 0, sizeof(struct _profile_t)); - profile->magic = PROF_MAGIC_PROFILE; - - /* - * If the filenames list is not specified or empty, return an empty - * profile. - */ - if ( files && !PROFILE_LAST_FILESPEC(*files) ) { - for (fs = files; !PROFILE_LAST_FILESPEC(*fs); fs++) { - retval = profile_open_file(*fs, &new_file); - /* if this file is missing, skip to the next */ - if (retval == ENOENT || retval == EACCES || retval == EPERM) { - continue; - } - if (retval) { - profile_release(profile); - return retval; - } - if (last) - last->next = new_file; - else - profile->first_file = new_file; - last = new_file; - } - /* - * If last is still null after the loop, then all the files were - * missing, so return the appropriate error. - */ - if (!last) { - profile_release(profile); - return ENOENT; - } - } - - *ret_profile = profile; - return 0; + const_profile_filespec_t *fs; + profile_t profile; + prf_file_t new_file, last = 0; + errcode_t retval = 0; + + profile = malloc(sizeof(struct _profile_t)); + if (!profile) + return ENOMEM; + memset(profile, 0, sizeof(struct _profile_t)); + profile->magic = PROF_MAGIC_PROFILE; + + /* + * If the filenames list is not specified or empty, return an empty + * profile. + */ + if ( files && !PROFILE_LAST_FILESPEC(*files) ) { + for (fs = files; !PROFILE_LAST_FILESPEC(*fs); fs++) { + retval = profile_open_file(*fs, &new_file); + /* if this file is missing, skip to the next */ + if (retval == ENOENT || retval == EACCES || retval == EPERM) { + continue; + } + if (retval) { + profile_release(profile); + return retval; + } + if (last) + last->next = new_file; + else + profile->first_file = new_file; + last = new_file; + } + /* + * If last is still null after the loop, then all the files were + * missing, so return the appropriate error. + */ + if (!last) { + profile_release(profile); + return ENOENT; + } + } + + *ret_profile = profile; + return 0; } -#define COUNT_LINKED_LIST(COUNT, PTYPE, START, FIELD) \ - { \ - size_t cll_counter = 0; \ - PTYPE cll_ptr = (START); \ - while (cll_ptr != NULL) { \ - cll_counter++; \ - cll_ptr = cll_ptr->FIELD; \ - } \ - (COUNT) = cll_counter; \ - } +#define COUNT_LINKED_LIST(COUNT, PTYPE, START, FIELD) \ + { \ + size_t cll_counter = 0; \ + PTYPE cll_ptr = (START); \ + while (cll_ptr != NULL) { \ + cll_counter++; \ + cll_ptr = cll_ptr->FIELD; \ + } \ + (COUNT) = cll_counter; \ + } errcode_t KRB5_CALLCONV profile_copy(profile_t old_profile, profile_t *new_profile) @@ -93,9 +94,9 @@ profile_copy(profile_t old_profile, profile_t *new_profile) COUNT_LINKED_LIST (size, prf_file_t, old_profile->first_file, next); files = malloc ((size+1) * sizeof(*files)); if (files == NULL) - return ENOMEM; + return ENOMEM; for (i = 0, file = old_profile->first_file; i < size; i++, file = file->next) - files[i] = file->data->filespec; + files[i] = file->data->filespec; files[size] = NULL; err = profile_init (files, new_profile); free (files); @@ -104,54 +105,54 @@ profile_copy(profile_t old_profile, profile_t *new_profile) errcode_t KRB5_CALLCONV profile_init_path(const_profile_filespec_list_t filepath, - profile_t *ret_profile) + profile_t *ret_profile) { - unsigned int n_entries; - int i; - unsigned int ent_len; - const char *s, *t; - profile_filespec_t *filenames; - errcode_t retval; - - /* count the distinct filename components */ - for(s = filepath, n_entries = 1; *s; s++) { - if (*s == ':') - n_entries++; - } - - /* the array is NULL terminated */ - filenames = (profile_filespec_t*) malloc((n_entries+1) * sizeof(char*)); - if (filenames == 0) - return ENOMEM; - - /* measure, copy, and skip each one */ - for(s = filepath, i=0; (t = strchr(s, ':')) || (t=s+strlen(s)); s=t+1, i++) { - ent_len = (unsigned int) (t-s); - filenames[i] = (char*) malloc(ent_len + 1); - if (filenames[i] == 0) { - /* if malloc fails, free the ones that worked */ - while(--i >= 0) free(filenames[i]); - free(filenames); - return ENOMEM; - } - strncpy(filenames[i], s, ent_len); - filenames[i][ent_len] = 0; - if (*t == 0) { - i++; - break; - } - } - /* cap the array */ - filenames[i] = 0; - - retval = profile_init((const_profile_filespec_t *) filenames, - ret_profile); - - /* count back down and free the entries */ - while(--i >= 0) free(filenames[i]); - free(filenames); - - return retval; + unsigned int n_entries; + int i; + unsigned int ent_len; + const char *s, *t; + profile_filespec_t *filenames; + errcode_t retval; + + /* count the distinct filename components */ + for(s = filepath, n_entries = 1; *s; s++) { + if (*s == ':') + n_entries++; + } + + /* the array is NULL terminated */ + filenames = (profile_filespec_t*) malloc((n_entries+1) * sizeof(char*)); + if (filenames == 0) + return ENOMEM; + + /* measure, copy, and skip each one */ + for(s = filepath, i=0; (t = strchr(s, ':')) || (t=s+strlen(s)); s=t+1, i++) { + ent_len = (unsigned int) (t-s); + filenames[i] = (char*) malloc(ent_len + 1); + if (filenames[i] == 0) { + /* if malloc fails, free the ones that worked */ + while(--i >= 0) free(filenames[i]); + free(filenames); + return ENOMEM; + } + strncpy(filenames[i], s, ent_len); + filenames[i][ent_len] = 0; + if (*t == 0) { + i++; + break; + } + } + /* cap the array */ + filenames[i] = 0; + + retval = profile_init((const_profile_filespec_t *) filenames, + ret_profile); + + /* count back down and free the entries */ + while(--i >= 0) free(filenames[i]); + free(filenames); + + return retval; } errcode_t KRB5_CALLCONV @@ -187,26 +188,26 @@ profile_is_modified(profile_t profile, int *modified) errcode_t KRB5_CALLCONV profile_flush(profile_t profile) { - if (!profile || profile->magic != PROF_MAGIC_PROFILE) - return PROF_MAGIC_PROFILE; + if (!profile || profile->magic != PROF_MAGIC_PROFILE) + return PROF_MAGIC_PROFILE; - if (profile->first_file) - return profile_flush_file(profile->first_file); + if (profile->first_file) + return profile_flush_file(profile->first_file); - return 0; + return 0; } errcode_t KRB5_CALLCONV profile_flush_to_file(profile_t profile, const_profile_filespec_t outfile) { - if (!profile || profile->magic != PROF_MAGIC_PROFILE) - return PROF_MAGIC_PROFILE; + if (!profile || profile->magic != PROF_MAGIC_PROFILE) + return PROF_MAGIC_PROFILE; - if (profile->first_file) - return profile_flush_file_to_file(profile->first_file, - outfile); + if (profile->first_file) + return profile_flush_file_to_file(profile->first_file, + outfile); - return 0; + return 0; } errcode_t KRB5_CALLCONV @@ -224,48 +225,48 @@ profile_free_buffer(profile_t profile, char *buf) void KRB5_CALLCONV profile_abandon(profile_t profile) { - prf_file_t p, next; + prf_file_t p, next; - if (!profile || profile->magic != PROF_MAGIC_PROFILE) - return; + if (!profile || profile->magic != PROF_MAGIC_PROFILE) + return; - for (p = profile->first_file; p; p = next) { - next = p->next; - profile_free_file(p); - } - profile->magic = 0; - free(profile); + for (p = profile->first_file; p; p = next) { + next = p->next; + profile_free_file(p); + } + profile->magic = 0; + free(profile); } void KRB5_CALLCONV profile_release(profile_t profile) { - prf_file_t p, next; + prf_file_t p, next; - if (!profile || profile->magic != PROF_MAGIC_PROFILE) - return; + if (!profile || profile->magic != PROF_MAGIC_PROFILE) + return; - for (p = profile->first_file; p; p = next) { - next = p->next; - profile_close_file(p); - } - profile->magic = 0; - free(profile); + for (p = profile->first_file; p; p = next) { + next = p->next; + profile_close_file(p); + } + profile->magic = 0; + free(profile); } /* * Here begins the profile serialization functions. */ errcode_t profile_ser_size(const char *unused, profile_t profile, - size_t *sizep) + size_t *sizep) { - size_t required; - prf_file_t pfp; + size_t required; + prf_file_t pfp; required = 3*sizeof(prof_int32); for (pfp = profile->first_file; pfp; pfp = pfp->next) { - required += sizeof(prof_int32); - required += strlen(pfp->data->filespec); + required += sizeof(prof_int32); + required += strlen(pfp->data->filespec); } *sizep += required; return 0; @@ -279,123 +280,123 @@ static void pack_int32(prof_int32 oval, unsigned char **bufpp, size_t *remainp) } errcode_t profile_ser_externalize(const char *unused, profile_t profile, - unsigned char **bufpp, size_t *remainp) + unsigned char **bufpp, size_t *remainp) { - errcode_t retval; - size_t required; - unsigned char *bp; - size_t remain; - prf_file_t pfp; - prof_int32 fcount, slen; + errcode_t retval; + size_t required; + unsigned char *bp; + size_t remain; + prf_file_t pfp; + prof_int32 fcount, slen; required = 0; bp = *bufpp; remain = *remainp; retval = EINVAL; if (profile) { - retval = ENOMEM; - (void) profile_ser_size(unused, profile, &required); - if (required <= remain) { - fcount = 0; - for (pfp = profile->first_file; pfp; pfp = pfp->next) - fcount++; - pack_int32(PROF_MAGIC_PROFILE, &bp, &remain); - pack_int32(fcount, &bp, &remain); - for (pfp = profile->first_file; pfp; pfp = pfp->next) { - slen = (prof_int32) strlen(pfp->data->filespec); - pack_int32(slen, &bp, &remain); - if (slen) { - memcpy(bp, pfp->data->filespec, (size_t) slen); - bp += slen; - remain -= (size_t) slen; - } - } - pack_int32(PROF_MAGIC_PROFILE, &bp, &remain); - retval = 0; - *bufpp = bp; - *remainp = remain; - } + retval = ENOMEM; + (void) profile_ser_size(unused, profile, &required); + if (required <= remain) { + fcount = 0; + for (pfp = profile->first_file; pfp; pfp = pfp->next) + fcount++; + pack_int32(PROF_MAGIC_PROFILE, &bp, &remain); + pack_int32(fcount, &bp, &remain); + for (pfp = profile->first_file; pfp; pfp = pfp->next) { + slen = (prof_int32) strlen(pfp->data->filespec); + pack_int32(slen, &bp, &remain); + if (slen) { + memcpy(bp, pfp->data->filespec, (size_t) slen); + bp += slen; + remain -= (size_t) slen; + } + } + pack_int32(PROF_MAGIC_PROFILE, &bp, &remain); + retval = 0; + *bufpp = bp; + *remainp = remain; + } } return(retval); } static int unpack_int32(prof_int32 *intp, unsigned char **bufpp, - size_t *remainp) + size_t *remainp) { if (*remainp >= sizeof(prof_int32)) { - *intp = load_32_be(*bufpp); - *bufpp += sizeof(prof_int32); - *remainp -= sizeof(prof_int32); - return 0; + *intp = load_32_be(*bufpp); + *bufpp += sizeof(prof_int32); + *remainp -= sizeof(prof_int32); + return 0; } else - return 1; + return 1; } errcode_t profile_ser_internalize(const char *unused, profile_t *profilep, - unsigned char **bufpp, size_t *remainp) + unsigned char **bufpp, size_t *remainp) { - errcode_t retval; - unsigned char *bp; - size_t remain; - int i; - prof_int32 fcount, tmp; - profile_filespec_t *flist = 0; - - bp = *bufpp; - remain = *remainp; - fcount = 0; - - if (remain >= 12) - (void) unpack_int32(&tmp, &bp, &remain); - else - tmp = 0; - - if (tmp != PROF_MAGIC_PROFILE) { - retval = EINVAL; - goto cleanup; - } - - (void) unpack_int32(&fcount, &bp, &remain); - retval = ENOMEM; - - flist = (profile_filespec_t *) malloc(sizeof(profile_filespec_t) * (size_t) (fcount + 1)); - if (!flist) - goto cleanup; - - memset(flist, 0, sizeof(char *) * (size_t) (fcount+1)); - for (i=0; i<fcount; i++) { - if (!unpack_int32(&tmp, &bp, &remain)) { - flist[i] = (char *) malloc((size_t) (tmp+1)); - if (!flist[i]) - goto cleanup; - memcpy(flist[i], bp, (size_t) tmp); - flist[i][tmp] = '\0'; - bp += tmp; - remain -= (size_t) tmp; - } - } - - if (unpack_int32(&tmp, &bp, &remain) || - (tmp != PROF_MAGIC_PROFILE)) { - retval = EINVAL; - goto cleanup; - } - - if ((retval = profile_init((const_profile_filespec_t *) flist, - profilep))) - goto cleanup; - - *bufpp = bp; - *remainp = remain; + errcode_t retval; + unsigned char *bp; + size_t remain; + int i; + prof_int32 fcount, tmp; + profile_filespec_t *flist = 0; + + bp = *bufpp; + remain = *remainp; + fcount = 0; + + if (remain >= 12) + (void) unpack_int32(&tmp, &bp, &remain); + else + tmp = 0; + + if (tmp != PROF_MAGIC_PROFILE) { + retval = EINVAL; + goto cleanup; + } + + (void) unpack_int32(&fcount, &bp, &remain); + retval = ENOMEM; + + flist = (profile_filespec_t *) malloc(sizeof(profile_filespec_t) * (size_t) (fcount + 1)); + if (!flist) + goto cleanup; + + memset(flist, 0, sizeof(char *) * (size_t) (fcount+1)); + for (i=0; i<fcount; i++) { + if (!unpack_int32(&tmp, &bp, &remain)) { + flist[i] = (char *) malloc((size_t) (tmp+1)); + if (!flist[i]) + goto cleanup; + memcpy(flist[i], bp, (size_t) tmp); + flist[i][tmp] = '\0'; + bp += tmp; + remain -= (size_t) tmp; + } + } + + if (unpack_int32(&tmp, &bp, &remain) || + (tmp != PROF_MAGIC_PROFILE)) { + retval = EINVAL; + goto cleanup; + } + + if ((retval = profile_init((const_profile_filespec_t *) flist, + profilep))) + goto cleanup; + + *bufpp = bp; + *remainp = remain; cleanup: - if (flist) { - for (i=0; i<fcount; i++) { - if (flist[i]) - free(flist[i]); - } - free(flist); - } - return(retval); + if (flist) { + for (i=0; i<fcount; i++) { + if (flist[i]) + free(flist[i]); + } + free(flist); + } + return(retval); } diff --git a/src/util/profile/prof_parse.c b/src/util/profile/prof_parse.c index a48ae58e15..413c7dfbb0 100644 --- a/src/util/profile/prof_parse.c +++ b/src/util/profile/prof_parse.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ #include "prof_int.h" #include <stdio.h> @@ -10,297 +11,297 @@ #define SECTION_SEP_CHAR '/' -#define STATE_INIT_COMMENT 1 -#define STATE_STD_LINE 2 -#define STATE_GET_OBRACE 3 +#define STATE_INIT_COMMENT 1 +#define STATE_STD_LINE 2 +#define STATE_GET_OBRACE 3 struct parse_state { - int state; - int group_level; - struct profile_node *root_section; - struct profile_node *current_section; + int state; + int group_level; + struct profile_node *root_section; + struct profile_node *current_section; }; static char *skip_over_blanks(char *cp) { - while (*cp && isspace((int) (*cp))) - cp++; - return cp; + while (*cp && isspace((int) (*cp))) + cp++; + return cp; } static void strip_line(char *line) { - char *p = line + strlen(line); - while (p > line && (p[-1] == '\n' || p[-1] == '\r')) - *p-- = 0; + char *p = line + strlen(line); + while (p > line && (p[-1] == '\n' || p[-1] == '\r')) + *p-- = 0; } static void parse_quoted_string(char *str) { - char *to, *from; - - to = from = str; - - for (to = from = str; *from && *from != '"'; to++, from++) { - if (*from == '\\') { - from++; - switch (*from) { - case 'n': - *to = '\n'; - break; - case 't': - *to = '\t'; - break; - case 'b': - *to = '\b'; - break; - default: - *to = *from; - } - continue; - } - *to = *from; - } - *to = '\0'; + char *to, *from; + + to = from = str; + + for (to = from = str; *from && *from != '"'; to++, from++) { + if (*from == '\\') { + from++; + switch (*from) { + case 'n': + *to = '\n'; + break; + case 't': + *to = '\t'; + break; + case 'b': + *to = '\b'; + break; + default: + *to = *from; + } + continue; + } + *to = *from; + } + *to = '\0'; } static errcode_t parse_init_state(struct parse_state *state) { - state->state = STATE_INIT_COMMENT; - state->group_level = 0; + state->state = STATE_INIT_COMMENT; + state->group_level = 0; - return profile_create_node("(root)", 0, &state->root_section); + return profile_create_node("(root)", 0, &state->root_section); } static errcode_t parse_std_line(char *line, struct parse_state *state) { - char *cp, ch, *tag, *value; - char *p; - errcode_t retval; - struct profile_node *node; - int do_subsection = 0; - void *iter = 0; - - if (*line == 0) - return 0; - cp = skip_over_blanks(line); - if (cp[0] == ';' || cp[0] == '#') - return 0; - strip_line(cp); - ch = *cp; - if (ch == 0) - return 0; - if (ch == '[') { - if (state->group_level > 0) - return PROF_SECTION_NOTOP; - cp++; - p = strchr(cp, ']'); - if (p == NULL) - return PROF_SECTION_SYNTAX; - *p = '\0'; - retval = profile_find_node_subsection(state->root_section, - cp, &iter, 0, - &state->current_section); - if (retval == PROF_NO_SECTION) { - retval = profile_add_node(state->root_section, - cp, 0, - &state->current_section); - if (retval) - return retval; - } else if (retval) - return retval; - - /* - * Finish off the rest of the line. - */ - cp = p+1; - if (*cp == '*') { - profile_make_node_final(state->current_section); - cp++; - } - /* - * A space after ']' should not be fatal - */ - cp = skip_over_blanks(cp); - if (*cp) - return PROF_SECTION_SYNTAX; - return 0; - } - if (ch == '}') { - if (state->group_level == 0) - return PROF_EXTRA_CBRACE; - if (*(cp+1) == '*') - profile_make_node_final(state->current_section); - retval = profile_get_node_parent(state->current_section, - &state->current_section); - if (retval) - return retval; - state->group_level--; - return 0; - } - /* - * Parse the relations - */ - tag = cp; - cp = strchr(cp, '='); - if (!cp) - return PROF_RELATION_SYNTAX; - if (cp == tag) - return PROF_RELATION_SYNTAX; - *cp = '\0'; - p = tag; - /* Look for whitespace on left-hand side. */ - while (p < cp && !isspace((int)*p)) - p++; - if (p < cp) { - /* Found some sort of whitespace. */ - *p++ = 0; - /* If we have more non-whitespace, it's an error. */ - while (p < cp) { - if (!isspace((int)*p)) - return PROF_RELATION_SYNTAX; - p++; - } - } - cp = skip_over_blanks(cp+1); - value = cp; - if (value[0] == '"') { - value++; - parse_quoted_string(value); - } else if (value[0] == 0) { - do_subsection++; - state->state = STATE_GET_OBRACE; - } else if (value[0] == '{' && *(skip_over_blanks(value+1)) == 0) - do_subsection++; - else { - cp = value + strlen(value) - 1; - while ((cp > value) && isspace((int) (*cp))) - *cp-- = 0; - } - if (do_subsection) { - p = strchr(tag, '*'); - if (p) - *p = '\0'; - retval = profile_add_node(state->current_section, - tag, 0, &state->current_section); - if (retval) - return retval; - if (p) - profile_make_node_final(state->current_section); - state->group_level++; - return 0; - } - p = strchr(tag, '*'); - if (p) - *p = '\0'; - profile_add_node(state->current_section, tag, value, &node); - if (p) - profile_make_node_final(node); - return 0; + char *cp, ch, *tag, *value; + char *p; + errcode_t retval; + struct profile_node *node; + int do_subsection = 0; + void *iter = 0; + + if (*line == 0) + return 0; + cp = skip_over_blanks(line); + if (cp[0] == ';' || cp[0] == '#') + return 0; + strip_line(cp); + ch = *cp; + if (ch == 0) + return 0; + if (ch == '[') { + if (state->group_level > 0) + return PROF_SECTION_NOTOP; + cp++; + p = strchr(cp, ']'); + if (p == NULL) + return PROF_SECTION_SYNTAX; + *p = '\0'; + retval = profile_find_node_subsection(state->root_section, + cp, &iter, 0, + &state->current_section); + if (retval == PROF_NO_SECTION) { + retval = profile_add_node(state->root_section, + cp, 0, + &state->current_section); + if (retval) + return retval; + } else if (retval) + return retval; + + /* + * Finish off the rest of the line. + */ + cp = p+1; + if (*cp == '*') { + profile_make_node_final(state->current_section); + cp++; + } + /* + * A space after ']' should not be fatal + */ + cp = skip_over_blanks(cp); + if (*cp) + return PROF_SECTION_SYNTAX; + return 0; + } + if (ch == '}') { + if (state->group_level == 0) + return PROF_EXTRA_CBRACE; + if (*(cp+1) == '*') + profile_make_node_final(state->current_section); + retval = profile_get_node_parent(state->current_section, + &state->current_section); + if (retval) + return retval; + state->group_level--; + return 0; + } + /* + * Parse the relations + */ + tag = cp; + cp = strchr(cp, '='); + if (!cp) + return PROF_RELATION_SYNTAX; + if (cp == tag) + return PROF_RELATION_SYNTAX; + *cp = '\0'; + p = tag; + /* Look for whitespace on left-hand side. */ + while (p < cp && !isspace((int)*p)) + p++; + if (p < cp) { + /* Found some sort of whitespace. */ + *p++ = 0; + /* If we have more non-whitespace, it's an error. */ + while (p < cp) { + if (!isspace((int)*p)) + return PROF_RELATION_SYNTAX; + p++; + } + } + cp = skip_over_blanks(cp+1); + value = cp; + if (value[0] == '"') { + value++; + parse_quoted_string(value); + } else if (value[0] == 0) { + do_subsection++; + state->state = STATE_GET_OBRACE; + } else if (value[0] == '{' && *(skip_over_blanks(value+1)) == 0) + do_subsection++; + else { + cp = value + strlen(value) - 1; + while ((cp > value) && isspace((int) (*cp))) + *cp-- = 0; + } + if (do_subsection) { + p = strchr(tag, '*'); + if (p) + *p = '\0'; + retval = profile_add_node(state->current_section, + tag, 0, &state->current_section); + if (retval) + return retval; + if (p) + profile_make_node_final(state->current_section); + state->group_level++; + return 0; + } + p = strchr(tag, '*'); + if (p) + *p = '\0'; + profile_add_node(state->current_section, tag, value, &node); + if (p) + profile_make_node_final(node); + return 0; } static errcode_t parse_line(char *line, struct parse_state *state) { - char *cp; - - switch (state->state) { - case STATE_INIT_COMMENT: - if (line[0] != '[') - return 0; - state->state = STATE_STD_LINE; - case STATE_STD_LINE: - return parse_std_line(line, state); - case STATE_GET_OBRACE: - cp = skip_over_blanks(line); - if (*cp != '{') - return PROF_MISSING_OBRACE; - state->state = STATE_STD_LINE; - } - return 0; + char *cp; + + switch (state->state) { + case STATE_INIT_COMMENT: + if (line[0] != '[') + return 0; + state->state = STATE_STD_LINE; + case STATE_STD_LINE: + return parse_std_line(line, state); + case STATE_GET_OBRACE: + cp = skip_over_blanks(line); + if (*cp != '{') + return PROF_MISSING_OBRACE; + state->state = STATE_STD_LINE; + } + return 0; } errcode_t profile_parse_file(FILE *f, struct profile_node **root) { -#define BUF_SIZE 2048 - char *bptr; - errcode_t retval; - struct parse_state state; - - bptr = malloc (BUF_SIZE); - if (!bptr) - return ENOMEM; - - retval = parse_init_state(&state); - if (retval) { - free (bptr); - return retval; - } - while (!feof(f)) { - if (fgets(bptr, BUF_SIZE, f) == NULL) - break; +#define BUF_SIZE 2048 + char *bptr; + errcode_t retval; + struct parse_state state; + + bptr = malloc (BUF_SIZE); + if (!bptr) + return ENOMEM; + + retval = parse_init_state(&state); + if (retval) { + free (bptr); + return retval; + } + while (!feof(f)) { + if (fgets(bptr, BUF_SIZE, f) == NULL) + break; #ifndef PROFILE_SUPPORTS_FOREIGN_NEWLINES - retval = parse_line(bptr, &state); - if (retval) { - profile_free_node(state.root_section); - free (bptr); - return retval; - } + retval = parse_line(bptr, &state); + if (retval) { + profile_free_node(state.root_section); + free (bptr); + return retval; + } #else - { - char *p, *end; - - if (strlen(bptr) >= BUF_SIZE - 1) { - /* The string may have foreign newlines and - gotten chopped off on a non-newline - boundary. Seek backwards to the last known - newline. */ - long offset; - char *c = bptr + strlen (bptr); - for (offset = 0; offset > -BUF_SIZE; offset--) { - if (*c == '\r' || *c == '\n') { - *c = '\0'; - fseek (f, offset, SEEK_CUR); - break; - } - c--; - } - } - - /* First change all newlines to \n */ - for (p = bptr; *p != '\0'; p++) { - if (*p == '\r') - *p = '\n'; - } - /* Then parse all lines */ - p = bptr; - end = bptr + strlen (bptr); - while (p < end) { - char* newline; - char* newp; - - newline = strchr (p, '\n'); - if (newline != NULL) - *newline = '\0'; - - /* parse_line modifies contents of p */ - newp = p + strlen (p) + 1; - retval = parse_line (p, &state); - if (retval) { - profile_free_node(state.root_section); - free (bptr); - return retval; - } - - p = newp; - } - } + { + char *p, *end; + + if (strlen(bptr) >= BUF_SIZE - 1) { + /* The string may have foreign newlines and + gotten chopped off on a non-newline + boundary. Seek backwards to the last known + newline. */ + long offset; + char *c = bptr + strlen (bptr); + for (offset = 0; offset > -BUF_SIZE; offset--) { + if (*c == '\r' || *c == '\n') { + *c = '\0'; + fseek (f, offset, SEEK_CUR); + break; + } + c--; + } + } + + /* First change all newlines to \n */ + for (p = bptr; *p != '\0'; p++) { + if (*p == '\r') + *p = '\n'; + } + /* Then parse all lines */ + p = bptr; + end = bptr + strlen (bptr); + while (p < end) { + char* newline; + char* newp; + + newline = strchr (p, '\n'); + if (newline != NULL) + *newline = '\0'; + + /* parse_line modifies contents of p */ + newp = p + strlen (p) + 1; + retval = parse_line (p, &state); + if (retval) { + profile_free_node(state.root_section); + free (bptr); + return retval; + } + + p = newp; + } + } #endif - } - *root = state.root_section; + } + *root = state.root_section; - free (bptr); - return 0; + free (bptr); + return 0; } /* @@ -308,15 +309,15 @@ errcode_t profile_parse_file(FILE *f, struct profile_node **root) */ static int need_double_quotes(char *str) { - if (!str) - return 0; - if (str[0] == '\0') - return 1; - if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1)))) - return 1; - if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b')) - return 1; - return 0; + if (!str) + return 0; + if (str[0] == '\0') + return 1; + if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1)))) + return 1; + if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b')) + return 1; + return 0; } /* @@ -324,41 +325,41 @@ static int need_double_quotes(char *str) * of characters as necessary. */ static void output_quoted_string(char *str, void (*cb)(const char *,void *), - void *data) + void *data) { - char ch; - char buf[2]; - - cb("\"", data); - if (!str) { - cb("\"", data); - return; - } - buf[1] = 0; - while ((ch = *str++)) { - switch (ch) { - case '\\': - cb("\\\\", data); - break; - case '\n': - cb("\\n", data); - break; - case '\t': - cb("\\t", data); - break; - case '\b': - cb("\\b", data); - break; - default: - /* This would be a lot faster if we scanned - forward for the next "interesting" - character. */ - buf[0] = ch; - cb(buf, data); - break; - } - } - cb("\"", data); + char ch; + char buf[2]; + + cb("\"", data); + if (!str) { + cb("\"", data); + return; + } + buf[1] = 0; + while ((ch = *str++)) { + switch (ch) { + case '\\': + cb("\\\\", data); + break; + case '\n': + cb("\\n", data); + break; + case '\t': + cb("\\t", data); + break; + case '\b': + cb("\\b", data); + break; + default: + /* This would be a lot faster if we scanned + forward for the next "interesting" + character. */ + buf[0] = ch; + cb(buf, data); + break; + } + } + cb("\"", data); } @@ -373,124 +374,124 @@ static void output_quoted_string(char *str, void (*cb)(const char *,void *), /* Errors should be returned, not ignored! */ static void dump_profile(struct profile_node *root, int level, - void (*cb)(const char *, void *), void *data) + void (*cb)(const char *, void *), void *data) { - int i; - struct profile_node *p; - void *iter; - long retval; - char *name, *value; - - iter = 0; - do { - retval = profile_find_node_relation(root, 0, &iter, - &name, &value); - if (retval) - break; - for (i=0; i < level; i++) - cb("\t", data); - if (need_double_quotes(value)) { - cb(name, data); - cb(" = ", data); - output_quoted_string(value, cb, data); - cb(EOL, data); - } else { - cb(name, data); - cb(" = ", data); - cb(value, data); - cb(EOL, data); - } - } while (iter != 0); - - iter = 0; - do { - retval = profile_find_node_subsection(root, 0, &iter, - &name, &p); - if (retval) - break; - if (level == 0) { /* [xxx] */ - cb("[", data); - cb(name, data); - cb("]", data); - cb(profile_is_node_final(p) ? "*" : "", data); - cb(EOL, data); - dump_profile(p, level+1, cb, data); - cb(EOL, data); - } else { /* xxx = { ... } */ - for (i=0; i < level; i++) - cb("\t", data); - cb(name, data); - cb(" = {", data); - cb(EOL, data); - dump_profile(p, level+1, cb, data); - for (i=0; i < level; i++) - cb("\t", data); - cb("}", data); - cb(profile_is_node_final(p) ? "*" : "", data); - cb(EOL, data); - } - } while (iter != 0); + int i; + struct profile_node *p; + void *iter; + long retval; + char *name, *value; + + iter = 0; + do { + retval = profile_find_node_relation(root, 0, &iter, + &name, &value); + if (retval) + break; + for (i=0; i < level; i++) + cb("\t", data); + if (need_double_quotes(value)) { + cb(name, data); + cb(" = ", data); + output_quoted_string(value, cb, data); + cb(EOL, data); + } else { + cb(name, data); + cb(" = ", data); + cb(value, data); + cb(EOL, data); + } + } while (iter != 0); + + iter = 0; + do { + retval = profile_find_node_subsection(root, 0, &iter, + &name, &p); + if (retval) + break; + if (level == 0) { /* [xxx] */ + cb("[", data); + cb(name, data); + cb("]", data); + cb(profile_is_node_final(p) ? "*" : "", data); + cb(EOL, data); + dump_profile(p, level+1, cb, data); + cb(EOL, data); + } else { /* xxx = { ... } */ + for (i=0; i < level; i++) + cb("\t", data); + cb(name, data); + cb(" = {", data); + cb(EOL, data); + dump_profile(p, level+1, cb, data); + for (i=0; i < level; i++) + cb("\t", data); + cb("}", data); + cb(profile_is_node_final(p) ? "*" : "", data); + cb(EOL, data); + } + } while (iter != 0); } static void dump_profile_to_file_cb(const char *str, void *data) { - fputs(str, data); + fputs(str, data); } errcode_t profile_write_tree_file(struct profile_node *root, FILE *dstfile) { - dump_profile(root, 0, dump_profile_to_file_cb, dstfile); - return 0; + dump_profile(root, 0, dump_profile_to_file_cb, dstfile); + return 0; } struct prof_buf { - char *base; - size_t cur, max; - int err; + char *base; + size_t cur, max; + int err; }; static void add_data_to_buffer(struct prof_buf *b, const void *d, size_t len) { - if (b->err) - return; - if (b->max - b->cur < len) { - size_t newsize; - char *newptr; - - newsize = b->max + (b->max >> 1) + len + 1024; - newptr = realloc(b->base, newsize); - if (newptr == NULL) { - b->err = 1; - return; - } - b->base = newptr; - b->max = newsize; - } - memcpy(b->base + b->cur, d, len); - b->cur += len; /* ignore overflow */ + if (b->err) + return; + if (b->max - b->cur < len) { + size_t newsize; + char *newptr; + + newsize = b->max + (b->max >> 1) + len + 1024; + newptr = realloc(b->base, newsize); + if (newptr == NULL) { + b->err = 1; + return; + } + b->base = newptr; + b->max = newsize; + } + memcpy(b->base + b->cur, d, len); + b->cur += len; /* ignore overflow */ } static void dump_profile_to_buffer_cb(const char *str, void *data) { - add_data_to_buffer((struct prof_buf *)data, str, strlen(str)); + add_data_to_buffer((struct prof_buf *)data, str, strlen(str)); } errcode_t profile_write_tree_to_buffer(struct profile_node *root, - char **buf) + char **buf) { - struct prof_buf prof_buf = { 0, 0, 0, 0 }; - - dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf); - if (prof_buf.err) { - *buf = NULL; - return ENOMEM; - } - add_data_to_buffer(&prof_buf, "", 1); /* append nul */ - if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) { - char *newptr = realloc(prof_buf.base, prof_buf.cur); - if (newptr) - prof_buf.base = newptr; - } - *buf = prof_buf.base; - return 0; + struct prof_buf prof_buf = { 0, 0, 0, 0 }; + + dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf); + if (prof_buf.err) { + *buf = NULL; + return ENOMEM; + } + add_data_to_buffer(&prof_buf, "", 1); /* append nul */ + if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) { + char *newptr = realloc(prof_buf.base, prof_buf.cur); + if (newptr) + prof_buf.base = newptr; + } + *buf = prof_buf.base; + return 0; } diff --git a/src/util/profile/prof_set.c b/src/util/profile/prof_set.c index a08bfd7574..893048ffb9 100644 --- a/src/util/profile/prof_set.c +++ b/src/util/profile/prof_set.c @@ -1,6 +1,7 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * prof_set.c --- routines that expose the public interfaces for - * inserting, updating and deleting items from the profile. + * inserting, updating and deleting items from the profile. * * WARNING: These routines only look at the first file opened in the * profile. It's not clear how to handle multiple files, actually. @@ -22,55 +23,55 @@ static errcode_t rw_setup(profile_t profile) { - prf_file_t file; - errcode_t retval = 0; - - if (!profile) - return PROF_NO_PROFILE; - - if (profile->magic != PROF_MAGIC_PROFILE) - return PROF_MAGIC_PROFILE; - - file = profile->first_file; - - retval = profile_lock_global(); - if (retval) - return retval; - - /* Don't update the file if we've already made modifications */ - if (file->data->flags & PROFILE_FILE_DIRTY) { - profile_unlock_global(); - return 0; - } - - if ((file->data->flags & PROFILE_FILE_SHARED) != 0) { - prf_data_t new_data; - new_data = profile_make_prf_data(file->data->filespec); - if (new_data == NULL) { - retval = ENOMEM; - } else { - retval = k5_mutex_init(&new_data->lock); - if (retval == 0) { - new_data->root = NULL; - new_data->flags = file->data->flags & ~PROFILE_FILE_SHARED; - new_data->timestamp = 0; - new_data->upd_serial = file->data->upd_serial; - } - } - - if (retval != 0) { - profile_unlock_global(); - free(new_data); - return retval; - } - profile_dereference_data_locked(file->data); - file->data = new_data; - } - - profile_unlock_global(); - retval = profile_update_file(file); - - return retval; + prf_file_t file; + errcode_t retval = 0; + + if (!profile) + return PROF_NO_PROFILE; + + if (profile->magic != PROF_MAGIC_PROFILE) + return PROF_MAGIC_PROFILE; + + file = profile->first_file; + + retval = profile_lock_global(); + if (retval) + return retval; + + /* Don't update the file if we've already made modifications */ + if (file->data->flags & PROFILE_FILE_DIRTY) { + profile_unlock_global(); + return 0; + } + + if ((file->data->flags & PROFILE_FILE_SHARED) != 0) { + prf_data_t new_data; + new_data = profile_make_prf_data(file->data->filespec); + if (new_data == NULL) { + retval = ENOMEM; + } else { + retval = k5_mutex_init(&new_data->lock); + if (retval == 0) { + new_data->root = NULL; + new_data->flags = file->data->flags & ~PROFILE_FILE_SHARED; + new_data->timestamp = 0; + new_data->upd_serial = file->data->upd_serial; + } + } + + if (retval != 0) { + profile_unlock_global(); + free(new_data); + return retval; + } + profile_dereference_data_locked(file->data); + file->data = new_data; + } + + profile_unlock_global(); + retval = profile_update_file(file); + + return retval; } @@ -81,50 +82,50 @@ static errcode_t rw_setup(profile_t profile) */ errcode_t KRB5_CALLCONV profile_update_relation(profile_t profile, const char **names, - const char *old_value, const char *new_value) + const char *old_value, const char *new_value) { - errcode_t retval; - struct profile_node *section, *node; - void *state; - const char **cpp; - - retval = rw_setup(profile); - if (retval) - return retval; - - if (names == 0 || names[0] == 0 || names[1] == 0) - return PROF_BAD_NAMESET; - - if (!old_value || !*old_value) - return PROF_EINVAL; - - retval = k5_mutex_lock(&profile->first_file->data->lock); - if (retval) - return retval; - section = profile->first_file->data->root; - for (cpp = names; cpp[1]; cpp++) { - state = 0; - retval = profile_find_node(section, *cpp, 0, 1, - &state, §ion); - if (retval) { - k5_mutex_unlock(&profile->first_file->data->lock); - return retval; - } - } - - state = 0; - retval = profile_find_node(section, *cpp, old_value, 0, &state, &node); - if (retval == 0) { - if (new_value) - retval = profile_set_relation_value(node, new_value); - else - retval = profile_remove_node(node); - } - if (retval == 0) - profile->first_file->data->flags |= PROFILE_FILE_DIRTY; - k5_mutex_unlock(&profile->first_file->data->lock); - - return retval; + errcode_t retval; + struct profile_node *section, *node; + void *state; + const char **cpp; + + retval = rw_setup(profile); + if (retval) + return retval; + + if (names == 0 || names[0] == 0 || names[1] == 0) + return PROF_BAD_NAMESET; + + if (!old_value || !*old_value) + return PROF_EINVAL; + + retval = k5_mutex_lock(&profile->first_file->data->lock); + if (retval) + return retval; + section = profile->first_file->data->root; + for (cpp = names; cpp[1]; cpp++) { + state = 0; + retval = profile_find_node(section, *cpp, 0, 1, + &state, §ion); + if (retval) { + k5_mutex_unlock(&profile->first_file->data->lock); + return retval; + } + } + + state = 0; + retval = profile_find_node(section, *cpp, old_value, 0, &state, &node); + if (retval == 0) { + if (new_value) + retval = profile_set_relation_value(node, new_value); + else + retval = profile_remove_node(node); + } + if (retval == 0) + profile->first_file->data->flags |= PROFILE_FILE_DIRTY; + k5_mutex_unlock(&profile->first_file->data->lock); + + return retval; } /* @@ -135,40 +136,40 @@ profile_update_relation(profile_t profile, const char **names, errcode_t KRB5_CALLCONV profile_clear_relation(profile_t profile, const char **names) { - errcode_t retval; - struct profile_node *section, *node; - void *state; - const char **cpp; - - retval = rw_setup(profile); - if (retval) - return retval; - - if (names == 0 || names[0] == 0 || names[1] == 0) - return PROF_BAD_NAMESET; - - section = profile->first_file->data->root; - for (cpp = names; cpp[1]; cpp++) { - state = 0; - retval = profile_find_node(section, *cpp, 0, 1, - &state, §ion); - if (retval) - return retval; - } - - state = 0; - do { - retval = profile_find_node(section, *cpp, 0, 0, &state, &node); - if (retval) - return retval; - retval = profile_remove_node(node); - if (retval) - return retval; - } while (state); - - profile->first_file->data->flags |= PROFILE_FILE_DIRTY; - - return 0; + errcode_t retval; + struct profile_node *section, *node; + void *state; + const char **cpp; + + retval = rw_setup(profile); + if (retval) + return retval; + + if (names == 0 || names[0] == 0 || names[1] == 0) + return PROF_BAD_NAMESET; + + section = profile->first_file->data->root; + for (cpp = names; cpp[1]; cpp++) { + state = 0; + retval = profile_find_node(section, *cpp, 0, 1, + &state, §ion); + if (retval) + return retval; + } + + state = 0; + do { + retval = profile_find_node(section, *cpp, 0, 0, &state, &node); + if (retval) + return retval; + retval = profile_remove_node(node); + if (retval) + return retval; + } while (state); + + profile->first_file->data->flags |= PROFILE_FILE_DIRTY; + + return 0; } /* @@ -179,46 +180,46 @@ profile_clear_relation(profile_t profile, const char **names) */ errcode_t KRB5_CALLCONV profile_rename_section(profile_t profile, const char **names, - const char *new_name) + const char *new_name) { - errcode_t retval; - struct profile_node *section, *node; - void *state; - const char **cpp; - - retval = rw_setup(profile); - if (retval) - return retval; - - if (names == 0 || names[0] == 0 || names[1] == 0) - return PROF_BAD_NAMESET; - - retval = k5_mutex_lock(&profile->first_file->data->lock); - if (retval) - return retval; - section = profile->first_file->data->root; - for (cpp = names; cpp[1]; cpp++) { - state = 0; - retval = profile_find_node(section, *cpp, 0, 1, - &state, §ion); - if (retval) { - k5_mutex_unlock(&profile->first_file->data->lock); - return retval; - } - } - - state = 0; - retval = profile_find_node(section, *cpp, 0, 1, &state, &node); - if (retval == 0) { - if (new_name) - retval = profile_rename_node(node, new_name); - else - retval = profile_remove_node(node); - } - if (retval == 0) - profile->first_file->data->flags |= PROFILE_FILE_DIRTY; - k5_mutex_unlock(&profile->first_file->data->lock); - return retval; + errcode_t retval; + struct profile_node *section, *node; + void *state; + const char **cpp; + + retval = rw_setup(profile); + if (retval) + return retval; + + if (names == 0 || names[0] == 0 || names[1] == 0) + return PROF_BAD_NAMESET; + + retval = k5_mutex_lock(&profile->first_file->data->lock); + if (retval) + return retval; + section = profile->first_file->data->root; + for (cpp = names; cpp[1]; cpp++) { + state = 0; + retval = profile_find_node(section, *cpp, 0, 1, + &state, §ion); + if (retval) { + k5_mutex_unlock(&profile->first_file->data->lock); + return retval; + } + } + + state = 0; + retval = profile_find_node(section, *cpp, 0, 1, &state, &node); + if (retval == 0) { + if (new_name) + retval = profile_rename_node(node, new_name); + else + retval = profile_remove_node(node); + } + if (retval == 0) + profile->first_file->data->flags |= PROFILE_FILE_DIRTY; + k5_mutex_unlock(&profile->first_file->data->lock); + return retval; } /* @@ -232,54 +233,54 @@ profile_rename_section(profile_t profile, const char **names, */ errcode_t KRB5_CALLCONV profile_add_relation(profile_t profile, const char **names, - const char *new_value) + const char *new_value) { - errcode_t retval; - struct profile_node *section; - const char **cpp; - void *state; - - retval = rw_setup(profile); - if (retval) - return retval; - - if (names == 0 || names[0] == 0 || names[1] == 0) - return PROF_BAD_NAMESET; - - retval = k5_mutex_lock(&profile->first_file->data->lock); - if (retval) - return retval; - section = profile->first_file->data->root; - for (cpp = names; cpp[1]; cpp++) { - state = 0; - retval = profile_find_node(section, *cpp, 0, 1, - &state, §ion); - if (retval == PROF_NO_SECTION) - retval = profile_add_node(section, *cpp, 0, §ion); - if (retval) { - k5_mutex_unlock(&profile->first_file->data->lock); - return retval; - } - } - - if (new_value == 0) { - retval = profile_find_node(section, *cpp, 0, 1, &state, 0); - if (retval == 0) { - k5_mutex_unlock(&profile->first_file->data->lock); - return PROF_EXISTS; - } else if (retval != PROF_NO_SECTION) { - k5_mutex_unlock(&profile->first_file->data->lock); - return retval; - } - } - - retval = profile_add_node(section, *cpp, new_value, 0); - if (retval) { - k5_mutex_unlock(&profile->first_file->data->lock); - return retval; - } - - profile->first_file->data->flags |= PROFILE_FILE_DIRTY; - k5_mutex_unlock(&profile->first_file->data->lock); - return 0; + errcode_t retval; + struct profile_node *section; + const char **cpp; + void *state; + + retval = rw_setup(profile); + if (retval) + return retval; + + if (names == 0 || names[0] == 0 || names[1] == 0) + return PROF_BAD_NAMESET; + + retval = k5_mutex_lock(&profile->first_file->data->lock); + if (retval) + return retval; + section = profile->first_file->data->root; + for (cpp = names; cpp[1]; cpp++) { + state = 0; + retval = profile_find_node(section, *cpp, 0, 1, + &state, §ion); + if (retval == PROF_NO_SECTION) + retval = profile_add_node(section, *cpp, 0, §ion); + if (retval) { + k5_mutex_unlock(&profile->first_file->data->lock); + return retval; + } + } + + if (new_value == 0) { + retval = profile_find_node(section, *cpp, 0, 1, &state, 0); + if (retval == 0) { + k5_mutex_unlock(&profile->first_file->data->lock); + return PROF_EXISTS; + } else if (retval != PROF_NO_SECTION) { + k5_mutex_unlock(&profile->first_file->data->lock); + return retval; + } + } + + retval = profile_add_node(section, *cpp, new_value, 0); + if (retval) { + k5_mutex_unlock(&profile->first_file->data->lock); + return retval; + } + + profile->first_file->data->flags |= PROFILE_FILE_DIRTY; + k5_mutex_unlock(&profile->first_file->data->lock); + return 0; } diff --git a/src/util/profile/prof_tree.c b/src/util/profile/prof_tree.c index 6663dc1b5d..711fc951b8 100644 --- a/src/util/profile/prof_tree.c +++ b/src/util/profile/prof_tree.c @@ -1,6 +1,7 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * prof_tree.c --- these routines maintain the parse tree of the - * config file. + * config file. * * All of the details of how the tree is stored is abstracted away in * this file; all of the other profile routines build, access, and @@ -28,43 +29,43 @@ #include <ctype.h> struct profile_node { - errcode_t magic; - char *name; - char *value; - int group_level; - unsigned int final:1; /* Indicate don't search next file */ - unsigned int deleted:1; - struct profile_node *first_child; - struct profile_node *parent; - struct profile_node *next, *prev; + errcode_t magic; + char *name; + char *value; + int group_level; + unsigned int final:1; /* Indicate don't search next file */ + unsigned int deleted:1; + struct profile_node *first_child; + struct profile_node *parent; + struct profile_node *next, *prev; }; -#define CHECK_MAGIC(node) \ - if ((node)->magic != PROF_MAGIC_NODE) \ - return PROF_MAGIC_NODE; +#define CHECK_MAGIC(node) \ + if ((node)->magic != PROF_MAGIC_NODE) \ + return PROF_MAGIC_NODE; /* * Free a node, and any children */ void profile_free_node(struct profile_node *node) { - struct profile_node *child, *next; + struct profile_node *child, *next; - if (node->magic != PROF_MAGIC_NODE) - return; + if (node->magic != PROF_MAGIC_NODE) + return; - if (node->name) - free(node->name); - if (node->value) - free(node->value); + if (node->name) + free(node->name); + if (node->value) + free(node->value); - for (child=node->first_child; child; child = next) { - next = child->next; - profile_free_node(child); - } - node->magic = 0; + for (child=node->first_child; child; child = next) { + next = child->next; + profile_free_node(child); + } + node->magic = 0; - free(node); + free(node); } #ifndef HAVE_STRDUP @@ -75,7 +76,7 @@ static char *MYstrdup (const char *s) size_t sz = strlen(s) + 1; char *p = malloc(sz); if (p != 0) - memcpy(p, s, sz); + memcpy(p, s, sz); return p; } #endif @@ -84,31 +85,31 @@ static char *MYstrdup (const char *s) * Create a node */ errcode_t profile_create_node(const char *name, const char *value, - struct profile_node **ret_node) + struct profile_node **ret_node) { - struct profile_node *new; - - new = malloc(sizeof(struct profile_node)); - if (!new) - return ENOMEM; - memset(new, 0, sizeof(struct profile_node)); - /* Set magic here so profile_free_node will free memory */ - new->magic = PROF_MAGIC_NODE; - new->name = strdup(name); - if (new->name == 0) { - profile_free_node(new); - return ENOMEM; - } - if (value) { - new->value = strdup(value); - if (new->value == 0) { - profile_free_node(new); - return ENOMEM; - } - } - - *ret_node = new; - return 0; + struct profile_node *new; + + new = malloc(sizeof(struct profile_node)); + if (!new) + return ENOMEM; + memset(new, 0, sizeof(struct profile_node)); + /* Set magic here so profile_free_node will free memory */ + new->magic = PROF_MAGIC_NODE; + new->name = strdup(name); + if (new->name == 0) { + profile_free_node(new); + return ENOMEM; + } + if (value) { + new->value = strdup(value); + if (new->value == 0) { + profile_free_node(new); + return ENOMEM; + } + } + + *ret_node = new; + return 0; } /* @@ -118,73 +119,73 @@ errcode_t profile_create_node(const char *name, const char *value, */ errcode_t profile_verify_node(struct profile_node *node) { - struct profile_node *p, *last; - errcode_t retval; - - CHECK_MAGIC(node); - - if (node->value && node->first_child) - return PROF_SECTION_WITH_VALUE; - - last = 0; - for (p = node->first_child; p; last = p, p = p->next) { - if (p->prev != last) - return PROF_BAD_LINK_LIST; - if (last && (last->next != p)) - return PROF_BAD_LINK_LIST; - if (node->group_level+1 != p->group_level) - return PROF_BAD_GROUP_LVL; - if (p->parent != node) - return PROF_BAD_PARENT_PTR; - retval = profile_verify_node(p); - if (retval) - return retval; - } - return 0; + struct profile_node *p, *last; + errcode_t retval; + + CHECK_MAGIC(node); + + if (node->value && node->first_child) + return PROF_SECTION_WITH_VALUE; + + last = 0; + for (p = node->first_child; p; last = p, p = p->next) { + if (p->prev != last) + return PROF_BAD_LINK_LIST; + if (last && (last->next != p)) + return PROF_BAD_LINK_LIST; + if (node->group_level+1 != p->group_level) + return PROF_BAD_GROUP_LVL; + if (p->parent != node) + return PROF_BAD_PARENT_PTR; + retval = profile_verify_node(p); + if (retval) + return retval; + } + return 0; } /* * Add a node to a particular section */ errcode_t profile_add_node(struct profile_node *section, const char *name, - const char *value, struct profile_node **ret_node) + const char *value, struct profile_node **ret_node) { - errcode_t retval; - struct profile_node *p, *last, *new; - - CHECK_MAGIC(section); - - if (section->value) - return PROF_ADD_NOT_SECTION; - - /* - * Find the place to insert the new node. We look for the - * place *after* the last match of the node name, since - * order matters. - */ - for (p=section->first_child, last = 0; p; last = p, p = p->next) { - int cmp; - cmp = strcmp(p->name, name); - if (cmp > 0) - break; - } - retval = profile_create_node(name, value, &new); - if (retval) - return retval; - new->group_level = section->group_level+1; - new->deleted = 0; - new->parent = section; - new->prev = last; - new->next = p; - if (p) - p->prev = new; - if (last) - last->next = new; - else - section->first_child = new; - if (ret_node) - *ret_node = new; - return 0; + errcode_t retval; + struct profile_node *p, *last, *new; + + CHECK_MAGIC(section); + + if (section->value) + return PROF_ADD_NOT_SECTION; + + /* + * Find the place to insert the new node. We look for the + * place *after* the last match of the node name, since + * order matters. + */ + for (p=section->first_child, last = 0; p; last = p, p = p->next) { + int cmp; + cmp = strcmp(p->name, name); + if (cmp > 0) + break; + } + retval = profile_create_node(name, value, &new); + if (retval) + return retval; + new->group_level = section->group_level+1; + new->deleted = 0; + new->parent = section; + new->prev = last; + new->next = p; + if (p) + p->prev = new; + if (last) + last->next = new; + else + section->first_child = new; + if (ret_node) + *ret_node = new; + return 0; } /* @@ -192,10 +193,10 @@ errcode_t profile_add_node(struct profile_node *section, const char *name, */ errcode_t profile_make_node_final(struct profile_node *node) { - CHECK_MAGIC(node); + CHECK_MAGIC(node); - node->final = 1; - return 0; + node->final = 1; + return 0; } /* @@ -203,7 +204,7 @@ errcode_t profile_make_node_final(struct profile_node *node) */ int profile_is_node_final(struct profile_node *node) { - return (node->final != 0); + return (node->final != 0); } /* @@ -213,7 +214,7 @@ int profile_is_node_final(struct profile_node *node) */ const char *profile_get_node_name(struct profile_node *node) { - return node->name; + return node->name; } /* @@ -223,7 +224,7 @@ const char *profile_get_node_name(struct profile_node *node) */ const char *profile_get_node_value(struct profile_node *node) { - return node->value; + return node->value; } /* @@ -241,63 +242,63 @@ const char *profile_get_node_value(struct profile_node *node) * */ errcode_t profile_find_node(struct profile_node *section, const char *name, - const char *value, int section_flag, void **state, - struct profile_node **node) + const char *value, int section_flag, void **state, + struct profile_node **node) { - struct profile_node *p; - - CHECK_MAGIC(section); - p = *state; - if (p) { - CHECK_MAGIC(p); - } else - p = section->first_child; - - for (; p; p = p->next) { - if (name && (strcmp(p->name, name))) - continue; - if (section_flag) { - if (p->value) - continue; - } else { - if (!p->value) - continue; - if (value && (strcmp(p->value, value))) - continue; - } - if (p->deleted) - continue; - /* A match! */ - if (node) - *node = p; - break; - } - if (p == 0) { - *state = 0; - return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION; - } - /* - * OK, we've found one match; now let's try to find another - * one. This way, if we return a non-zero state pointer, - * there's guaranteed to be another match that's returned. - */ - for (p = p->next; p; p = p->next) { - if (name && (strcmp(p->name, name))) - continue; - if (section_flag) { - if (p->value) - continue; - } else { - if (!p->value) - continue; - if (value && (strcmp(p->value, value))) - continue; - } - /* A match! */ - break; - } - *state = p; - return 0; + struct profile_node *p; + + CHECK_MAGIC(section); + p = *state; + if (p) { + CHECK_MAGIC(p); + } else + p = section->first_child; + + for (; p; p = p->next) { + if (name && (strcmp(p->name, name))) + continue; + if (section_flag) { + if (p->value) + continue; + } else { + if (!p->value) + continue; + if (value && (strcmp(p->value, value))) + continue; + } + if (p->deleted) + continue; + /* A match! */ + if (node) + *node = p; + break; + } + if (p == 0) { + *state = 0; + return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION; + } + /* + * OK, we've found one match; now let's try to find another + * one. This way, if we return a non-zero state pointer, + * there's guaranteed to be another match that's returned. + */ + for (p = p->next; p; p = p->next) { + if (name && (strcmp(p->name, name))) + continue; + if (section_flag) { + if (p->value) + continue; + } else { + if (!p->value) + continue; + if (value && (strcmp(p->value, value))) + continue; + } + /* A match! */ + break; + } + *state = p; + return 0; } @@ -315,23 +316,23 @@ errcode_t profile_find_node(struct profile_node *section, const char *name, * exported interface), it should be strdup()'ed. */ errcode_t profile_find_node_relation(struct profile_node *section, - const char *name, void **state, - char **ret_name, char **value) + const char *name, void **state, + char **ret_name, char **value) { - struct profile_node *p; - errcode_t retval; - - retval = profile_find_node(section, name, 0, 0, state, &p); - if (retval) - return retval; - - if (p) { - if (value) - *value = p->value; - if (ret_name) - *ret_name = p->name; - } - return 0; + struct profile_node *p; + errcode_t retval; + + retval = profile_find_node(section, name, 0, 0, state, &p); + if (retval) + return retval; + + if (p) { + if (value) + *value = p->value; + if (ret_name) + *ret_name = p->name; + } + return 0; } /* @@ -347,34 +348,34 @@ errcode_t profile_find_node_relation(struct profile_node *section, * profile_find_node. */ errcode_t profile_find_node_subsection(struct profile_node *section, - const char *name, void **state, - char **ret_name, - struct profile_node **subsection) + const char *name, void **state, + char **ret_name, + struct profile_node **subsection) { - struct profile_node *p; - errcode_t retval; - - retval = profile_find_node(section, name, 0, 1, state, &p); - if (retval) - return retval; - - if (p) { - if (subsection) - *subsection = p; - if (ret_name) - *ret_name = p->name; - } - return 0; + struct profile_node *p; + errcode_t retval; + + retval = profile_find_node(section, name, 0, 1, state, &p); + if (retval) + return retval; + + if (p) { + if (subsection) + *subsection = p; + if (ret_name) + *ret_name = p->name; + } + return 0; } /* * This function returns the parent of a particular node. */ errcode_t profile_get_node_parent(struct profile_node *section, - struct profile_node **parent) + struct profile_node **parent) { - *parent = section->parent; - return 0; + *parent = section->parent; + return 0; } /* @@ -382,63 +383,63 @@ errcode_t profile_get_node_parent(struct profile_node *section, * match the specified name array. */ struct profile_iterator { - prf_magic_t magic; - profile_t profile; - int flags; - const char *const *names; - const char *name; - prf_file_t file; - int file_serial; - int done_idx; - struct profile_node *node; - int num; + prf_magic_t magic; + profile_t profile; + int flags; + const char *const *names; + const char *name; + prf_file_t file; + int file_serial; + int done_idx; + struct profile_node *node; + int num; }; errcode_t profile_node_iterator_create(profile_t profile, - const char *const *names, int flags, - void **ret_iter) + const char *const *names, int flags, + void **ret_iter) { - struct profile_iterator *iter; - int done_idx = 0; - - if (profile == 0) - return PROF_NO_PROFILE; - if (profile->magic != PROF_MAGIC_PROFILE) - return PROF_MAGIC_PROFILE; - if (!names) - return PROF_BAD_NAMESET; - if (!(flags & PROFILE_ITER_LIST_SECTION)) { - if (!names[0]) - return PROF_BAD_NAMESET; - done_idx = 1; - } - - if ((iter = malloc(sizeof(struct profile_iterator))) == NULL) - return ENOMEM; - - iter->magic = PROF_MAGIC_ITERATOR; - iter->profile = profile; - iter->names = names; - iter->flags = flags; - iter->file = profile->first_file; - iter->done_idx = done_idx; - iter->node = 0; - iter->num = 0; - *ret_iter = iter; - return 0; + struct profile_iterator *iter; + int done_idx = 0; + + if (profile == 0) + return PROF_NO_PROFILE; + if (profile->magic != PROF_MAGIC_PROFILE) + return PROF_MAGIC_PROFILE; + if (!names) + return PROF_BAD_NAMESET; + if (!(flags & PROFILE_ITER_LIST_SECTION)) { + if (!names[0]) + return PROF_BAD_NAMESET; + done_idx = 1; + } + + if ((iter = malloc(sizeof(struct profile_iterator))) == NULL) + return ENOMEM; + + iter->magic = PROF_MAGIC_ITERATOR; + iter->profile = profile; + iter->names = names; + iter->flags = flags; + iter->file = profile->first_file; + iter->done_idx = done_idx; + iter->node = 0; + iter->num = 0; + *ret_iter = iter; + return 0; } void profile_node_iterator_free(void **iter_p) { - struct profile_iterator *iter; - - if (!iter_p) - return; - iter = *iter_p; - if (!iter || iter->magic != PROF_MAGIC_ITERATOR) - return; - free(iter); - *iter_p = 0; + struct profile_iterator *iter; + + if (!iter_p) + return; + iter = *iter_p; + if (!iter || iter->magic != PROF_MAGIC_ITERATOR) + return; + free(iter); + *iter_p = 0; } /* @@ -449,156 +450,156 @@ void profile_node_iterator_free(void **iter_p) * strdup()'ed. */ errcode_t profile_node_iterator(void **iter_p, struct profile_node **ret_node, - char **ret_name, char **ret_value) + char **ret_name, char **ret_value) { - struct profile_iterator *iter = *iter_p; - struct profile_node *section, *p; - const char *const *cpp; - errcode_t retval; - int skip_num = 0; - - if (!iter || iter->magic != PROF_MAGIC_ITERATOR) - return PROF_MAGIC_ITERATOR; - if (iter->file && iter->file->magic != PROF_MAGIC_FILE) - return PROF_MAGIC_FILE; - if (iter->file && iter->file->data->magic != PROF_MAGIC_FILE_DATA) - return PROF_MAGIC_FILE_DATA; - /* - * If the file has changed, then the node pointer is invalid, - * so we'll have search the file again looking for it. - */ - if (iter->file) { - retval = k5_mutex_lock(&iter->file->data->lock); - if (retval) - return retval; - } - if (iter->node && (iter->file->data->upd_serial != iter->file_serial)) { - iter->flags &= ~PROFILE_ITER_FINAL_SEEN; - skip_num = iter->num; - iter->node = 0; - } - if (iter->node && iter->node->magic != PROF_MAGIC_NODE) { - if (iter->file) - k5_mutex_unlock(&iter->file->data->lock); - return PROF_MAGIC_NODE; - } + struct profile_iterator *iter = *iter_p; + struct profile_node *section, *p; + const char *const *cpp; + errcode_t retval; + int skip_num = 0; + + if (!iter || iter->magic != PROF_MAGIC_ITERATOR) + return PROF_MAGIC_ITERATOR; + if (iter->file && iter->file->magic != PROF_MAGIC_FILE) + return PROF_MAGIC_FILE; + if (iter->file && iter->file->data->magic != PROF_MAGIC_FILE_DATA) + return PROF_MAGIC_FILE_DATA; + /* + * If the file has changed, then the node pointer is invalid, + * so we'll have search the file again looking for it. + */ + if (iter->file) { + retval = k5_mutex_lock(&iter->file->data->lock); + if (retval) + return retval; + } + if (iter->node && (iter->file->data->upd_serial != iter->file_serial)) { + iter->flags &= ~PROFILE_ITER_FINAL_SEEN; + skip_num = iter->num; + iter->node = 0; + } + if (iter->node && iter->node->magic != PROF_MAGIC_NODE) { + if (iter->file) + k5_mutex_unlock(&iter->file->data->lock); + return PROF_MAGIC_NODE; + } get_new_file: - if (iter->node == 0) { - if (iter->file == 0 || - (iter->flags & PROFILE_ITER_FINAL_SEEN)) { - if (iter->file) - k5_mutex_unlock(&iter->file->data->lock); - profile_node_iterator_free(iter_p); - if (ret_node) - *ret_node = 0; - if (ret_name) - *ret_name = 0; - if (ret_value) - *ret_value =0; - return 0; - } - if ((retval = profile_update_file_locked(iter->file))) { - k5_mutex_unlock(&iter->file->data->lock); - if (retval == ENOENT || retval == EACCES) { - /* XXX memory leak? */ - iter->file = iter->file->next; - if (iter->file) { - retval = k5_mutex_lock(&iter->file->data->lock); - if (retval) { - profile_node_iterator_free(iter_p); - return retval; - } - } - skip_num = 0; - retval = 0; - goto get_new_file; - } else { - profile_node_iterator_free(iter_p); - return retval; - } - } - iter->file_serial = iter->file->data->upd_serial; - /* - * Find the section to list if we are a LIST_SECTION, - * or find the containing section if not. - */ - section = iter->file->data->root; - assert(section != NULL); - for (cpp = iter->names; cpp[iter->done_idx]; cpp++) { - for (p=section->first_child; p; p = p->next) { - if (!strcmp(p->name, *cpp) && !p->value) - break; - } - if (!p) { - section = 0; - break; - } - section = p; - if (p->final) - iter->flags |= PROFILE_ITER_FINAL_SEEN; - } - if (!section) { - k5_mutex_unlock(&iter->file->data->lock); - iter->file = iter->file->next; - if (iter->file) { - retval = k5_mutex_lock(&iter->file->data->lock); - if (retval) { - profile_node_iterator_free(iter_p); - return retval; - } - } - skip_num = 0; - goto get_new_file; - } - iter->name = *cpp; - iter->node = section->first_child; - } - /* - * OK, now we know iter->node is set up correctly. Let's do - * the search. - */ - for (p = iter->node; p; p = p->next) { - if (iter->name && strcmp(p->name, iter->name)) - continue; - if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) && - p->value) - continue; - if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) && - !p->value) - continue; - if (skip_num > 0) { - skip_num--; - continue; - } - if (p->deleted) - continue; - break; - } - iter->num++; - if (!p) { - k5_mutex_unlock(&iter->file->data->lock); - iter->file = iter->file->next; - if (iter->file) { - retval = k5_mutex_lock(&iter->file->data->lock); - if (retval) { - profile_node_iterator_free(iter_p); - return retval; - } - } - iter->node = 0; - skip_num = 0; - goto get_new_file; - } - k5_mutex_unlock(&iter->file->data->lock); - if ((iter->node = p->next) == NULL) - iter->file = iter->file->next; - if (ret_node) - *ret_node = p; - if (ret_name) - *ret_name = p->name; - if (ret_value) - *ret_value = p->value; - return 0; + if (iter->node == 0) { + if (iter->file == 0 || + (iter->flags & PROFILE_ITER_FINAL_SEEN)) { + if (iter->file) + k5_mutex_unlock(&iter->file->data->lock); + profile_node_iterator_free(iter_p); + if (ret_node) + *ret_node = 0; + if (ret_name) + *ret_name = 0; + if (ret_value) + *ret_value =0; + return 0; + } + if ((retval = profile_update_file_locked(iter->file))) { + k5_mutex_unlock(&iter->file->data->lock); + if (retval == ENOENT || retval == EACCES) { + /* XXX memory leak? */ + iter->file = iter->file->next; + if (iter->file) { + retval = k5_mutex_lock(&iter->file->data->lock); + if (retval) { + profile_node_iterator_free(iter_p); + return retval; + } + } + skip_num = 0; + retval = 0; + goto get_new_file; + } else { + profile_node_iterator_free(iter_p); + return retval; + } + } + iter->file_serial = iter->file->data->upd_serial; + /* + * Find the section to list if we are a LIST_SECTION, + * or find the containing section if not. + */ + section = iter->file->data->root; + assert(section != NULL); + for (cpp = iter->names; cpp[iter->done_idx]; cpp++) { + for (p=section->first_child; p; p = p->next) { + if (!strcmp(p->name, *cpp) && !p->value) + break; + } + if (!p) { + section = 0; + break; + } + section = p; + if (p->final) + iter->flags |= PROFILE_ITER_FINAL_SEEN; + } + if (!section) { + k5_mutex_unlock(&iter->file->data->lock); + iter->file = iter->file->next; + if (iter->file) { + retval = k5_mutex_lock(&iter->file->data->lock); + if (retval) { + profile_node_iterator_free(iter_p); + return retval; + } + } + skip_num = 0; + goto get_new_file; + } + iter->name = *cpp; + iter->node = section->first_child; + } + /* + * OK, now we know iter->node is set up correctly. Let's do + * the search. + */ + for (p = iter->node; p; p = p->next) { + if (iter->name && strcmp(p->name, iter->name)) + continue; + if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) && + p->value) + continue; + if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) && + !p->value) + continue; + if (skip_num > 0) { + skip_num--; + continue; + } + if (p->deleted) + continue; + break; + } + iter->num++; + if (!p) { + k5_mutex_unlock(&iter->file->data->lock); + iter->file = iter->file->next; + if (iter->file) { + retval = k5_mutex_lock(&iter->file->data->lock); + if (retval) { + profile_node_iterator_free(iter_p); + return retval; + } + } + iter->node = 0; + skip_num = 0; + goto get_new_file; + } + k5_mutex_unlock(&iter->file->data->lock); + if ((iter->node = p->next) == NULL) + iter->file = iter->file->next; + if (ret_node) + *ret_node = p; + if (ret_name) + *ret_name = p->name; + if (ret_value) + *ret_value = p->value; + return 0; } /* @@ -608,14 +609,14 @@ get_new_file: */ errcode_t profile_remove_node(struct profile_node *node) { - CHECK_MAGIC(node); + CHECK_MAGIC(node); - if (node->parent == 0) - return PROF_EINVAL; /* Can't remove the root! */ + if (node->parent == 0) + return PROF_EINVAL; /* Can't remove the root! */ - node->deleted = 1; + node->deleted = 1; - return 0; + return 0; } /* @@ -624,23 +625,23 @@ errcode_t profile_remove_node(struct profile_node *node) * TYT, 2/25/99 */ errcode_t profile_set_relation_value(struct profile_node *node, - const char *new_value) + const char *new_value) { - char *cp; + char *cp; - CHECK_MAGIC(node); + CHECK_MAGIC(node); - if (!node->value) - return PROF_SET_SECTION_VALUE; + if (!node->value) + return PROF_SET_SECTION_VALUE; - cp = strdup(new_value); - if (!cp) - return ENOMEM; + cp = strdup(new_value); + if (!cp) + return ENOMEM; - free(node->value); - node->value = cp; + free(node->value); + node->value = cp; - return 0; + return 0; } /* @@ -650,59 +651,59 @@ errcode_t profile_set_relation_value(struct profile_node *node, */ errcode_t profile_rename_node(struct profile_node *node, const char *new_name) { - char *new_string; - struct profile_node *p, *last; - - CHECK_MAGIC(node); - - if (strcmp(new_name, node->name) == 0) - return 0; /* It's the same name, return */ - - /* - * Make sure we can allocate memory for the new name, first! - */ - new_string = strdup(new_name); - if (!new_string) - return ENOMEM; - - /* - * Find the place to where the new node should go. We look - * for the place *after* the last match of the node name, - * since order matters. - */ - for (p=node->parent->first_child, last = 0; p; last = p, p = p->next) { - if (strcmp(p->name, new_name) > 0) - break; - } - - /* - * If we need to move the node, do it now. - */ - if ((p != node) && (last != node)) { - /* - * OK, let's detach the node - */ - if (node->prev) - node->prev->next = node->next; - else - node->parent->first_child = node->next; - if (node->next) - node->next->prev = node->prev; - - /* - * Now let's reattach it in the right place. - */ - if (p) - p->prev = node; - if (last) - last->next = node; - else - node->parent->first_child = node; - node->next = p; - node->prev = last; - } - - free(node->name); - node->name = new_string; - return 0; + char *new_string; + struct profile_node *p, *last; + + CHECK_MAGIC(node); + + if (strcmp(new_name, node->name) == 0) + return 0; /* It's the same name, return */ + + /* + * Make sure we can allocate memory for the new name, first! + */ + new_string = strdup(new_name); + if (!new_string) + return ENOMEM; + + /* + * Find the place to where the new node should go. We look + * for the place *after* the last match of the node name, + * since order matters. + */ + for (p=node->parent->first_child, last = 0; p; last = p, p = p->next) { + if (strcmp(p->name, new_name) > 0) + break; + } + + /* + * If we need to move the node, do it now. + */ + if ((p != node) && (last != node)) { + /* + * OK, let's detach the node + */ + if (node->prev) + node->prev->next = node->next; + else + node->parent->first_child = node->next; + if (node->next) + node->next->prev = node->prev; + + /* + * Now let's reattach it in the right place. + */ + if (p) + p->prev = node; + if (last) + last->next = node; + else + node->parent->first_child = node; + node->next = p; + node->prev = last; + } + + free(node->name); + node->name = new_string; + return 0; } diff --git a/src/util/profile/test_parse.c b/src/util/profile/test_parse.c index f524c90f2d..2fcd046e85 100644 --- a/src/util/profile/test_parse.c +++ b/src/util/profile/test_parse.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ #include "prof_int.h" #include <stdio.h> @@ -11,44 +12,44 @@ void dump_profile (struct profile_node *root, int level); int main(argc, argv) - int argc; - char **argv; + int argc; + char **argv; { - struct profile_node *root; - unsigned long retval; - FILE *f; - - initialize_prof_error_table(); - if (argc != 2) { - fprintf(stderr, "%s: Usage <filename>\n", argv[0]); - exit(1); - } - - f = fopen(argv[1], "r"); - if (!f) { - perror(argv[1]); - exit(1); - } - - retval = profile_parse_file(f, &root); - if (retval) { - printf("profile_parse_file error %s\n", - error_message((errcode_t) retval)); - exit(1); - } - fclose(f); - - printf("\n\nDebugging dump.\n"); - profile_write_tree_file(root, stdout); - - retval = profile_verify_node(root); - if (retval) { - printf("profile_verify_node reported an error: %s\n", - error_message((errcode_t) retval)); - exit(1); - } - - profile_free_node(root); - - return 0; + struct profile_node *root; + unsigned long retval; + FILE *f; + + initialize_prof_error_table(); + if (argc != 2) { + fprintf(stderr, "%s: Usage <filename>\n", argv[0]); + exit(1); + } + + f = fopen(argv[1], "r"); + if (!f) { + perror(argv[1]); + exit(1); + } + + retval = profile_parse_file(f, &root); + if (retval) { + printf("profile_parse_file error %s\n", + error_message((errcode_t) retval)); + exit(1); + } + fclose(f); + + printf("\n\nDebugging dump.\n"); + profile_write_tree_file(root, stdout); + + retval = profile_verify_node(root); + if (retval) { + printf("profile_verify_node reported an error: %s\n", + error_message((errcode_t) retval)); + exit(1); + } + + profile_free_node(root); + + return 0; } diff --git a/src/util/profile/test_profile.c b/src/util/profile/test_profile.c index 6f47a7d4e4..8155156eee 100644 --- a/src/util/profile/test_profile.c +++ b/src/util/profile/test_profile.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * test_profile.c --- testing program for the profile routine */ @@ -15,151 +16,151 @@ const char *program_name = "test_profile"; -#define PRINT_VALUE 1 -#define PRINT_VALUES 2 +#define PRINT_VALUE 1 +#define PRINT_VALUES 2 static void do_batchmode(profile) - profile_t profile; + profile_t profile; { - errcode_t retval; - int argc, ret; - char **argv, **values, **cpp; - char buf[256]; - const char **names, *value; - char *cmd; - int print_status; + errcode_t retval; + int argc, ret; + char **argv, **values, **cpp; + char buf[256]; + const char **names, *value; + char *cmd; + int print_status; - while (!feof(stdin)) { - if (fgets(buf, sizeof(buf), stdin) == NULL) - break; - printf(">%s", buf); - ret = argv_parse(buf, &argc, &argv); - if (ret != 0) { - printf("Argv_parse returned %d!\n", ret); - continue; - } - cmd = *(argv); - names = (const char **) argv + 1; - print_status = 0; - retval = 0; - if (cmd == 0) { - argv_free(argv); - continue; - } - if (!strcmp(cmd, "query")) { - retval = profile_get_values(profile, names, &values); - print_status = PRINT_VALUES; - } else if (!strcmp(cmd, "query1")) { - retval = profile_get_value(profile, names, &value); - print_status = PRINT_VALUE; - } else if (!strcmp(cmd, "list_sections")) { - retval = profile_get_subsection_names(profile, names, - &values); - print_status = PRINT_VALUES; - } else if (!strcmp(cmd, "list_relations")) { - retval = profile_get_relation_names(profile, names, - &values); - print_status = PRINT_VALUES; - } else if (!strcmp(cmd, "dump")) { - retval = profile_write_tree_file - (profile->first_file->data->root, stdout); - } else if (!strcmp(cmd, "clear")) { - retval = profile_clear_relation(profile, names); - } else if (!strcmp(cmd, "update")) { - retval = profile_update_relation(profile, names+2, - *names, *(names+1)); - } else if (!strcmp(cmd, "verify")) { - retval = profile_verify_node - (profile->first_file->data->root); - } else if (!strcmp(cmd, "rename_section")) { - retval = profile_rename_section(profile, names+1, - *names); - } else if (!strcmp(cmd, "add")) { - value = *names; - if (strcmp(value, "NULL") == 0) - value = NULL; - retval = profile_add_relation(profile, names+1, - value); - } else if (!strcmp(cmd, "flush")) { - retval = profile_flush(profile); - } else { - printf("Invalid command.\n"); - } - if (retval) { - com_err(cmd, retval, ""); - print_status = 0; - } - switch (print_status) { - case PRINT_VALUE: - printf("%s\n", value); - break; - case PRINT_VALUES: - for (cpp = values; *cpp; cpp++) - printf("%s\n", *cpp); - profile_free_list(values); - break; - } - printf("\n"); - argv_free(argv); - } - profile_release(profile); - exit(0); + while (!feof(stdin)) { + if (fgets(buf, sizeof(buf), stdin) == NULL) + break; + printf(">%s", buf); + ret = argv_parse(buf, &argc, &argv); + if (ret != 0) { + printf("Argv_parse returned %d!\n", ret); + continue; + } + cmd = *(argv); + names = (const char **) argv + 1; + print_status = 0; + retval = 0; + if (cmd == 0) { + argv_free(argv); + continue; + } + if (!strcmp(cmd, "query")) { + retval = profile_get_values(profile, names, &values); + print_status = PRINT_VALUES; + } else if (!strcmp(cmd, "query1")) { + retval = profile_get_value(profile, names, &value); + print_status = PRINT_VALUE; + } else if (!strcmp(cmd, "list_sections")) { + retval = profile_get_subsection_names(profile, names, + &values); + print_status = PRINT_VALUES; + } else if (!strcmp(cmd, "list_relations")) { + retval = profile_get_relation_names(profile, names, + &values); + print_status = PRINT_VALUES; + } else if (!strcmp(cmd, "dump")) { + retval = profile_write_tree_file + (profile->first_file->data->root, stdout); + } else if (!strcmp(cmd, "clear")) { + retval = profile_clear_relation(profile, names); + } else if (!strcmp(cmd, "update")) { + retval = profile_update_relation(profile, names+2, + *names, *(names+1)); + } else if (!strcmp(cmd, "verify")) { + retval = profile_verify_node + (profile->first_file->data->root); + } else if (!strcmp(cmd, "rename_section")) { + retval = profile_rename_section(profile, names+1, + *names); + } else if (!strcmp(cmd, "add")) { + value = *names; + if (strcmp(value, "NULL") == 0) + value = NULL; + retval = profile_add_relation(profile, names+1, + value); + } else if (!strcmp(cmd, "flush")) { + retval = profile_flush(profile); + } else { + printf("Invalid command.\n"); + } + if (retval) { + com_err(cmd, retval, ""); + print_status = 0; + } + switch (print_status) { + case PRINT_VALUE: + printf("%s\n", value); + break; + case PRINT_VALUES: + for (cpp = values; *cpp; cpp++) + printf("%s\n", *cpp); + profile_free_list(values); + break; + } + printf("\n"); + argv_free(argv); + } + profile_release(profile); + exit(0); } int main(argc, argv) - int argc; - char **argv; + int argc; + char **argv; { - profile_t profile; - long retval; - char **values, **cpp; - const char *value; - const char **names; - char *cmd; - int print_value = 0; + profile_t profile; + long retval; + char **values, **cpp; + const char *value; + const char **names; + char *cmd; + int print_value = 0; if (argc < 2) { - fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name); - exit(1); + fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name); + exit(1); } initialize_prof_error_table(); retval = profile_init_path(argv[1], &profile); if (retval) { - com_err(program_name, retval, "while initializing profile"); - exit(1); + com_err(program_name, retval, "while initializing profile"); + exit(1); } cmd = *(argv+2); names = (const char **) argv+3; if (!cmd || !strcmp(cmd, "batch")) - do_batchmode(profile); + do_batchmode(profile); if (!strcmp(cmd, "query")) { - retval = profile_get_values(profile, names, &values); + retval = profile_get_values(profile, names, &values); } else if (!strcmp(cmd, "query1")) { - retval = profile_get_value(profile, names, &value); - print_value++; + retval = profile_get_value(profile, names, &value); + print_value++; } else if (!strcmp(cmd, "list_sections")) { - retval = profile_get_subsection_names(profile, names, &values); + retval = profile_get_subsection_names(profile, names, &values); } else if (!strcmp(cmd, "list_relations")) { - retval = profile_get_relation_names(profile, names, &values); + retval = profile_get_relation_names(profile, names, &values); } else { - fprintf(stderr, "Invalid command.\n"); - exit(1); + fprintf(stderr, "Invalid command.\n"); + exit(1); } if (retval) { - com_err(argv[0], retval, "while getting values"); - profile_release(profile); - exit(1); + com_err(argv[0], retval, "while getting values"); + profile_release(profile); + exit(1); } if (print_value) { - printf("%s\n", value); + printf("%s\n", value); } else { - for (cpp = values; *cpp; cpp++) - printf("%s\n", *cpp); - profile_free_list(values); + for (cpp = values; *cpp; cpp++) + printf("%s\n", *cpp); + profile_free_list(values); } profile_release(profile); |
