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