~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 247d0c106cc3c32caef42a555948bb0b8cd32ff6 CTU-FEE-B0B35APO-Semestral-project/file-browser/src/file_execute.c -rw-r--r-- 946 bytes
247d0c10 — František Boháček fix: floating point exception 3 years ago
                                                                                
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
#include "file_execute.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

executing_file_error_t executing_file_execute(char *path, char *args) {
  executing_file_error_t ret;
  pid_t pid = fork();

  if (pid == -1) {
    ret.error = true;
    return ret;
  }

  if (pid == 0) {
    execl(path, args, (char*)NULL);
    exit(errno);
  }

  ret.error = false;
  ret.file.pid = pid;
  ret.file.output_signal = 0;
  ret.file.exited = false;
  return ret;
}

int executing_file_wait(executing_file_t *file) {
  pid_t data = waitpid(file->pid, &file->output_signal, 0);
  if (data == -1) {
    return -1;
  }

  file->exited = true;
  return file->output_signal;
}

bool executing_file_has_ended(executing_file_t *file) {
  if (file->exited) {
    return true;
  }

  if (waitpid(file->pid, &file->output_signal, WNOHANG) == 0) {
    file->exited = true;
    return true;
  }

  return false;
}
Do not follow this link