#define CHAZ_USE_SHORT_NAMES #include "Charmonizer/Core/ModHandler.h" #include "Charmonizer/Core/Util.h" #include "Charmonizer/Core/HeadCheck.h" #include "Charmonizer/Probe/DirSep.h" #include #include #include /* TODO: move mkdir and rmdir to a Core module a la Stat. */ static char mkdir_code[] = METAQUOTE #include "_charm.h" %s #include int main () { Charm_Setup; if (mkdir("_charm_test_dir_orig", 0777) == 0) { printf("1"); } return 0; } METAQUOTE; static char *headers[] = { "direct.h", "unistd.h", "sys/stat.h", NULL, }; static char rmdir_code[] = METAQUOTE #include "_charm.h" %s int main () { Charm_Setup; printf("1"); rmdir("_charm_test_dir_orig"); rmdir("_charm_test_dir_mod"); return 0; } METAQUOTE; void chaz_DirSep_run(void) { int i; char *output; size_t output_len; char dir_sep[3]; chaz_bool_t dir_sep_is_valid = false; char *includes_buf = strdup(""); size_t inc_buf_len = 2; size_t needed; char *code_buf; START_RUN("DirSep"); /* Include any possible relevant headers. */ for (i = 0; headers[i] != NULL; i++) { if (!check_header(headers[i])) break; inc_buf_len = append_strings(&includes_buf, inc_buf_len, "#include <", headers[i], ">\n", NULL); } needed = strlen(mkdir_code) + inc_buf_len + 10; code_buf = malloc(needed); sprintf(code_buf, mkdir_code, includes_buf); /* create a directory */ output = capture_output(code_buf, strlen(code_buf), &output_len); if (output != NULL && (strcmp(output, "1") == 0)) { FILE *f; /* clean up */ free(output); /* try to create files under the new directory */ if ( (f = fopen("_charm_test_dir_orig\\backslash", "w")) != NULL) fclose(f); if ( (f = fopen("_charm_test_dir_orig/slash", "w")) != NULL) fclose(f); /* rename the directory, and see which file we can get to */ rename("_charm_test_dir_orig", "_charm_test_dir_mod"); if ( (f = fopen("_charm_test_dir_mod\\backslash", "r")) != NULL) { fclose(f); strcpy(dir_sep, "\\\\"); dir_sep_is_valid = true; } else if ( (f = fopen("_charm_test_dir_mod/slash", "r")) != NULL) { fclose(f); strcpy(dir_sep, "/"); dir_sep_is_valid = true; } } /* clean up - delete all possible files without verifying */ remove("_charm_test_dir_mod/slash"); remove("_charm_test_dir_mod\\backslash"); remove("_charm_test_dir_orig/slash"); remove("_charm_test_dir_orig\\backslash"); remove("_charm_test_dir_orig"); remove("_charm_test_dir_mod"); /* Try rmdir. */ sprintf(code_buf, rmdir_code, includes_buf); output = capture_output(code_buf, strlen(code_buf), &output_len); if (dir_sep_is_valid) { append_conf("#define CHY_DIR_SEP \"%s\"\n", dir_sep); /* shorten */ START_SHORT_NAMES; shorten_macro("DIR_SEP"); END_SHORT_NAMES; } END_RUN; free(code_buf); free(includes_buf); } /** * Copyright 2006 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */