Process_OpenBSD.c 1.8 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. extern const char* __progname;
  21. #include <stdint.h>
  22. #include <unistd.h>
  23. #include <sys/param.h> // MAXPATHLEN
  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];
  50. if (NULL == realpath(__progname,buff)) {
  51. return NULL;
  52. }
  53. uint32_t length = strlen(buff);
  54. char* output = Allocator_calloc(alloc, length + 1, 1);
  55. Bits_memcpy(output, buff, length);
  56. return output;
  57. }