- char *ret;
- ret = getcwd(str, size);
- str[size - 1] = 0;
- return ret ? true : false;
+ struct stat st;
+ int err;
+
+ err = stat(str(), &st);
+ if (err) {
+ return false;
+ }
+
+ return true;
+}
+
+int execute(char * const * args)
+{
+ pid_t pid = fork();
+ if (pid == 0) {
+ // child
+ execvp(args[0], args);
+ fprintf(stderr, "error: failed to execute:");
+ for (unsigned i = 0; args[i]; ++i) {
+ fprintf(stderr, " %s", args[i]);
+ }
+ fprintf(stderr, "\n");
+ exit(-1);
+ } else {
+ // parent
+ if (pid == -1) {
+ fprintf(stderr, "error: failed to fork\n");
+ return -1;
+ }
+ int status = -1;
+ int ret;
+ waitpid(pid, &status, 0);
+ if (WIFEXITED(status)) {
+ ret = WEXITSTATUS(status);
+ } else if (WIFSIGNALED(status)) {
+ // match shell return code
+ ret = WTERMSIG(status) + 128;
+ } else {
+ ret = 128;
+ }
+ return ret;
+ }