-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathMacOS.c
151 lines (143 loc) · 3.95 KB
/
MacOS.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* File MacOS.c
*
* This file need only be compiled on MacOS.
* It contains some functions only needed there:
*
* MaOSstartup(char *path) : create working dir in "$HOME/Library/Application Support" etc.
*
* where *path is argv[0], the file name of the running executable
*
*/
#ifdef __APPLE__
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pwd.h>
#include <uuid/uuid.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
void MacOSstartup(char *path) {
//
// We used to do this from a wrapper shell script.
// However, this confuses MacOS when it comes to
// granting access to the local microphone therefore
// it is now built into the binary
//
// We have to distinguish two basic situations:
//
// a) piHPSDR is called from a command line
// b) piHPSDR is within an app bundle
//
// In case a) nothing has to be done here (standard UNIX),
// but in case b) we do the following:
//
// - if not yet existing, create the directory "$HOME/Library/Application Support/piHPSDR"
// - copy the icon to that directory
// - chdir to that directory
//
// Case b) is present if the current working directory is "/".
//
// If "$HOME/Library/Application Support" does not exist, fall back to case a)
//
char *c;
char source[1024];
char workdir[1024];
char *homedir;
const char *AppSupport ="/Library/Application Support/piHPSDR";
const char *IconInApp ="/../Resources/hpsdr.png";
struct stat st;
int fdin, fdout;
int rc;
static IOPMAssertionID keep_awake = 0;
//
// This is to prevent "going to sleep" or activating the screen saver
// while piHPSDR is running
//
// works from macOS 10.6 so should be enough
// no return check is needed
IOPMAssertionCreateWithName (kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn,
CFSTR ("piHPSDR"), &keep_awake);
//
// If the current work dir is NOT "/", just do nothing
//
*workdir=0;
if (getcwd(workdir,sizeof(workdir)) == NULL) return;
if (strlen(workdir) != 1 || *workdir != '/') return;
//
// Determine working directory,
// "$HOME/Library/Application Support/piHPSDR"
// and create it if it does not exist
// take care to enclose name with quotation marks
//
if ((homedir = getenv("HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir;
}
if (strlen(homedir) +strlen(AppSupport) > 1020) return;
strcpy(workdir,homedir);
strcat(workdir, AppSupport);
//
// Check if working dir exists, otherwise try to create it
//
if (stat(workdir, &st) < 0) {
mkdir(workdir, 0755);
}
//
// Is it there? If not, give up
//
if (stat(workdir, &st) < 0) {
return;
}
//
// Is it a directory? If not, give up
//
if ((st.st_mode & S_IFDIR) == 0) {
return;
}
//
// chdir to the work dir
//
chdir(workdir);
//
// Make two local files for stdout and stderr, to allow
// post-mortem debugging
//
(void) freopen("pihpsdr.stdout", "w", stdout);
(void) freopen("pihpsdr.stderr", "w", stderr);
//
// Copy icon from app bundle to the work dir
//
if (getcwd(workdir,sizeof(workdir)) != NULL && strlen(path) < 1024) {
//
// source = basename of the executable
//
strcpy(source, path);
c=rindex(source,'/');
if (c) {
*c=0;
if ((strlen(source) + strlen(IconInApp) < 1024)) {
strcat(source, IconInApp);
//
// Now copy the file from "source" to "workdir"
//
fdin=open(source, O_RDONLY);
fdout=open("hpsdr.png", O_WRONLY | O_CREAT | O_TRUNC, (mode_t) 0400);
if (fdin >= 0 && fdout >= 0) {
//
// Now do the copy, use "source" as I/O buffer
//
while ((rc=read(fdin, source, 1024)) > 0) {
write (fdout, source, rc);
}
close(fdin);
close(fdout);
}
}
}
}
}
#endif