]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Tool/secToolFileIO.c
6f5a0e9ededb00a62ca819f6400dd6e6e2587231
[apple/security.git] / OSX / sec / SOSCircle / Tool / secToolFileIO.c
1 //
2 // secToolFileIO.c
3 // sec
4 //
5 //
6 //
7
8 #include "secToolFileIO.h"
9
10 #include <copyfile.h>
11 #include <libgen.h>
12 #include <utilities/SecCFWrappers.h>
13
14 FILE *outFile = NULL;
15 FILE *errFile = NULL;
16
17 void _printcfmsg(FILE *ff, CFDictionaryRef formatOptions, CFStringRef format, ...)
18 {
19 va_list args;
20 va_start(args, format);
21 CFStringRef message = CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, formatOptions, format, args);
22 va_end(args);
23 CFStringPerformWithCString(message, ^(const char *utf8String) { fprintf(ff, utf8String, ""); });
24 CFRelease(message);
25 }
26
27
28 int SOSLogSetOutputTo(char *dir, char *filename) {
29 size_t pathlen = 0;
30
31 if(dir && filename) {
32 pathlen = strlen(dir) + strlen(filename) + 2;
33 char path[pathlen];
34 snprintf(path, pathlen, "%s/%s", dir, filename);
35 outFile = fopen(path, "a");
36 } else if(dir || filename) {
37 outFile = stdout;
38 return -1;
39 } else {
40 outFile = stdout;
41 }
42 errFile = stderr;
43 return 0;
44 }
45
46 void closeOutput(void) {
47 if(outFile != stdout) {
48 fclose(outFile);
49 }
50 outFile = stdout;
51 }
52
53 int copyFileToOutputDir(char *dir, char *toCopy) {
54 char *bname = basename(toCopy);
55 char destpath[256];
56 int status;
57 copyfile_state_t cpfilestate = copyfile_state_alloc();
58
59 status = snprintf(destpath, 256, "%s/%s", dir, bname);
60 if(status < 0 || status > 256) return -1;
61
62 int retval = copyfile(toCopy, destpath, cpfilestate, COPYFILE_ALL);
63
64 copyfile_state_free(cpfilestate);
65 return retval;
66 }