forked from falkTX/dssi-vst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.cpp
79 lines (66 loc) · 1.95 KB
/
paths.cpp
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
// -*- c-basic-offset: 4 -*-
/*
dssi-vst: a DSSI plugin wrapper for VST effects and instruments
Copyright 2012-2013 Filipe Coelho
Copyright 2010-2011 Kristian Amlie
Copyright 2004-2010 Chris Cannam
*/
#include "paths.h"
#include <iostream>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
std::vector<std::string>
Paths::getPath(std::string envVar, std::string deflt, std::string defltHomeRelPath)
{
std::vector<std::string> pathList;
std::string path;
char *cpath = getenv(envVar.c_str());
if (cpath) path = cpath;
if (path == "") {
path = deflt;
char *home = getenv("HOME");
if (home && (defltHomeRelPath != "")) {
path = std::string(home) + defltHomeRelPath + ":" + path;
}
std::cerr << envVar << " not set, defaulting to " << path << std::endl;
}
std::string::size_type index = 0, newindex = 0;
while ((newindex = path.find(':', index)) < path.size()) {
pathList.push_back(path.substr(index, newindex - index));
index = newindex + 1;
}
pathList.push_back(path.substr(index));
return pathList;
}
// Behaves like mkstemp, but for shared memory.
int shm_mkstemp(char *fileBase)
{
const char charSet[] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
int size = strlen(fileBase);
if (size < 6) {
errno = EINVAL;
return -1;
}
if (strcmp(fileBase + size - 6, "XXXXXX") != 0) {
errno = EINVAL;
return -1;
}
while (1) {
for (int c = size - 6; c < size; c++) {
// Note the -1 to avoid the trailing '\0' in charSet.
fileBase[c] = charSet[rand() % (sizeof(charSet) - 1)];
}
int fd = shm_open(fileBase, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd >= 0) {
return fd;
} else if (errno != EEXIST) {
return -1;
}
}
}