char* read_file(char* fname){ FILE* f; char* buf = GC_MALLOC_ATOMIC(8192); size_t r; str_list_t* sl = sl_create(); f = fopen(fname, "r"); DFSCH_UNWIND { if (!f){ dfsch_operating_system_error(dfsch_saprintf("Cannot open file %d", fname)); } while (!feof(f)){ r = fread(buf, 1, 8192, f); if (r != 0){ sl_nappend(sl, buf, r); buf = GC_MALLOC_ATOMIC(8192); } else { if (ferror(f)){ dfsch_operating_system_error(dfsch_saprintf("Error reading file %d", fname)); } } } } DFSCH_PROTECT { fclose(f); } DFSCH_PROTECT_END; return sl_value(sl); }
static char* convert_arg(dfsch_object_t* obj){ if (dfsch_keyword_p(obj)){ char* str = dfsch_symbol(obj); if (strlen(str) == 1){ return dfsch_saprintf("-%s", str); } else { return dfsch_saprintf("--%s", str); } } else if (dfsch_string_p(obj)) { return dfsch_string_to_cstr(obj); } else { return dfsch_object_2_string(obj, 10, 1); } }
char* dfsch_realpath(char* path){ #ifdef __WIN32__ if (*path == '\\' || *path == '/' || (path[0] != '\0' && path[1]==':')){ return path; } else { return dfsch_saprintf("%s\\%s", dfsch_getcwd(), path); } #else char* rp = realpath(path, NULL); char* res = dfsch_stracpy(rp); free(rp); return res; #endif }
char* dfsch_tcl_quote_list(dfsch_object_t* list){ dfsch_str_list_t* sl = dfsch_sl_create(); dfsch_object_t* i; while (DFSCH_PAIR_P(list)){ dfsch_sl_append(sl, " "); i = DFSCH_FAST_CAR(list); if (dfsch_string_p(i)){ dfsch_sl_append(sl, dfsch_tcl_quote(dfsch_string_to_cstr(i))); } else if (dfsch_keyword_p(i)){ dfsch_sl_append(sl, dfsch_saprintf("-%s", dfsch_symbol(i))); } else if (DFSCH_PAIR_P(i)){ dfsch_sl_append(sl, dfsch_tcl_quote(dfsch_tcl_quote_list(i))); } else { dfsch_sl_append(sl, dfsch_tcl_quote(dfsch_object_2_string(i, 10, 1))); } list = DFSCH_FAST_CDR(list); } return dfsch_sl_value(sl); }