1
0

Process_OSX.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "memory/MallocAllocator.h"
  17. #include "util/platform/libc/strlen.h"
  18. #include "util/Process.h"
  19. #include "util/Bits.h"
  20. #include <mach-o/dyld.h> // _NSGetExecutablePath()
  21. #include <sys/param.h> // MAXPATHLEN
  22. #include <stdint.h>
  23. #include <unistd.h>
  24. int Process_spawn(char* binaryPath, char** args)
  25. {
  26. int pid = fork();
  27. if (pid < 0) {
  28. return -1;
  29. } else if (pid == 0) {
  30. char** argv;
  31. {
  32. int argCount;
  33. for (argCount = 0; args[argCount]; argCount++);
  34. struct Allocator* alloc = MallocAllocator_new((argCount + 2) * sizeof(char*));
  35. argv = Allocator_calloc(alloc, (argCount + 2), sizeof(char*));
  36. }
  37. for (int i = 1; args[i-1]; i++) {
  38. argv[i] = args[i-1];
  39. }
  40. argv[0] = binaryPath;
  41. // Goodbye :)
  42. execvp(binaryPath, argv);
  43. _exit(72);
  44. }
  45. return 0;
  46. }
  47. char* Process_getPath(struct Allocator* alloc)
  48. {
  49. char buff[MAXPATHLEN + 1] = {0};
  50. uint32_t size = MAXPATHLEN;
  51. _NSGetExecutablePath(buff, &size);
  52. if (size > MAXPATHLEN) {
  53. return NULL;
  54. }
  55. uint32_t length = strlen(buff);
  56. char* output = Allocator_calloc(alloc, length + 1, 1);
  57. Bits_memcpy(output, buff, length);
  58. return output;
  59. }