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