UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
fs.c
Go to the documentation of this file.
1
6/*
7 * Copyright (c) 2018-2023 CESNET, z. s. p. o.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, is permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * 3. Neither the name of CESNET nor the names of its contributors may be
22 * used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
27 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
29 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#ifdef HAVE_CONFIG_H
40#include "config.h"
41#include "config_unix.h"
42#include "config_win32.h"
43#endif
44
45#include <stdbool.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49
50#include "utils/fs.h"
51#include "utils/string.h"
52
56const char *get_temp_dir(void)
57{
58 static __thread char temp_dir[MAX_PATH_SIZE];
59
60 if (temp_dir[0] != '\0') {
61 return temp_dir;
62 }
63
64#ifdef _WIN32
65 if (GetTempPathA(sizeof temp_dir, temp_dir) == 0) {
66 return NULL;
67 }
68#else
69 const char *req_tmp_dir = getenv("TMPDIR");
70 if (req_tmp_dir) {
71 temp_dir[sizeof temp_dir - 1] = '\0';
72 strncpy(temp_dir, req_tmp_dir, sizeof temp_dir - 1);
73 } else {
74 strcpy(temp_dir, P_tmpdir);
75 }
76 strncat(temp_dir, "/", sizeof temp_dir - strlen(temp_dir) - 1);
77#endif
78
79 return temp_dir;
80}
81
82#ifdef _WIN32
83int get_exec_path(char* path) {
84 return GetModuleFileNameA(NULL, path, MAX_PATH_SIZE) != 0;
85}
86#endif
87
88
89#ifdef __linux__
90int get_exec_path(char* path) {
91 return realpath("/proc/self/exe", path) != NULL;
92}
93#endif
94
95
96#ifdef __APPLE__
97#include <mach-o/dyld.h> //_NSGetExecutablePath
98#include <unistd.h>
99
100int get_exec_path(char* path) {
101 char raw_path_name[MAX_PATH_SIZE];
102 uint32_t raw_path_size = (uint32_t)(sizeof(raw_path_name));
103
104 if (_NSGetExecutablePath(raw_path_name, &raw_path_size) != 0) {
105 return false;
106 }
107 return realpath(raw_path_name, path) != NULL;
108}
109#endif
110
116const char *get_install_root(void) {
117 static __thread char exec_path[MAX_PATH_SIZE];
118 if (!get_exec_path(exec_path)) {
119 return NULL;
120 }
121 char *last_path_delim = strrpbrk(exec_path, "/\\");
122 if (!last_path_delim) {
123 return NULL;
124 }
125 *last_path_delim = '\0'; // cut off executable name
126 last_path_delim = strrpbrk(exec_path, "/\\");
127 if (!last_path_delim) {
128 return exec_path;
129 }
130 if (strcmp(last_path_delim + 1, "bin") == 0 || strcmp(last_path_delim + 1, "MacOS") == 0) {
131 *last_path_delim = '\0'; // remove "bin" suffix if there is one (not in Windows builds) or MacOS in a bundle
132 }
133 return exec_path;
134}
135
145FILE *get_temp_file(const char **filename) {
146 static _Thread_local char filename_buf[MAX_PATH_SIZE];
147#ifdef _WIN32
148 *filename = tmpnam(filename_buf);
149 return fopen(*filename, "wbx");
150#else
151 *filename = filename_buf;
152 strncpy(filename_buf, get_temp_dir(), sizeof filename_buf - 1);
153 strncat(filename_buf, "/uv.XXXXXX", sizeof filename_buf - strlen(filename_buf) - 1);
154 umask(S_IRWXG|S_IRWXO);
155 int fd = mkstemp(filename_buf);
156 return fd == -1 ? NULL : fdopen(fd, "wb");
157#endif
158}
159
164char *strdup_path_with_expansion(const char *orig_path) {
165 const char * const home = getenv("HOME");
166 if (strncmp(orig_path, "~/", 2) != 0 || home == NULL) {
167 return strdup(orig_path);
168 }
169 char *new_path = malloc(MAX_PATH_SIZE);
170 new_path[MAX_PATH_SIZE - 1] = '\0';
171 assert(new_path != NULL);
172 strncpy(new_path, home, MAX_PATH_SIZE - 1);
173 strncat(new_path, orig_path + 1, MAX_PATH_SIZE - strlen(new_path) - 1);
174 return new_path;
175}
char * strdup_path_with_expansion(const char *orig_path)
Definition fs.c:164
const char * get_install_root(void)
Definition fs.c:116
const char * get_temp_dir(void)
Definition fs.c:56
FILE * get_temp_file(const char **filename)
Definition fs.c:145
#define MAX_PATH_SIZE
Definition fs.h:48
int get_exec_path(char *path)
char * strrpbrk(char *s, const char *accept)
same as strpbrk but finds in a reverse order (last occurence returned)
Definition string.c:95