X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/822b670c6f91d089ccb51b77e24b6ac80406b337..e3d460c9de4426da6c630c3ae3f46173a99f82d8:/OSX/sec/SOSCircle/Tool/secToolFileIO.c?ds=inline diff --git a/OSX/sec/SOSCircle/Tool/secToolFileIO.c b/OSX/sec/SOSCircle/Tool/secToolFileIO.c new file mode 100644 index 00000000..233e170f --- /dev/null +++ b/OSX/sec/SOSCircle/Tool/secToolFileIO.c @@ -0,0 +1,67 @@ +// +// secToolFileIO.c +// sec +// +// Created by Richard Murphy on 1/22/16. +// +// + +#include "secToolFileIO.h" + +#include +#include +#include + +FILE *outFile = NULL; +FILE *errFile = NULL; + +void _printcfmsg(FILE *ff, CFDictionaryRef formatOptions, CFStringRef format, ...) +{ + va_list args; + va_start(args, format); + CFStringRef message = CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, formatOptions, format, args); + va_end(args); + CFStringPerformWithCString(message, ^(const char *utf8String) { fprintf(ff, utf8String, ""); }); + CFRelease(message); +} + + +int setOutputTo(char *dir, char *filename) { + size_t pathlen = 0; + + if(dir && filename) { + pathlen = strlen(dir) + strlen(filename) + 2; + char path[pathlen]; + snprintf(path, pathlen, "%s/%s", dir, filename); + outFile = fopen(path, "a"); + } else if(dir || filename) { + outFile = stdout; + return -1; + } else { + outFile = stdout; + } + errFile = stderr; + return 0; +} + +void closeOutput(void) { + if(outFile != stdout) { + fclose(outFile); + } + outFile = stdout; +} + +int copyFileToOutputDir(char *dir, char *toCopy) { + char *bname = basename(toCopy); + char destpath[256]; + int status; + copyfile_state_t cpfilestate = copyfile_state_alloc(); + + status = snprintf(destpath, 256, "%s/%s", dir, bname); + if(status < 0 || status > 256) return -1; + + int retval = copyfile(toCopy, destpath, cpfilestate, COPYFILE_ALL); + + copyfile_state_free(cpfilestate); + return retval; +}