/* Copyright (C) 2017 Alex Vong * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA */ #include #include #include #include #include static SCM system_star_star(SCM prog, SCM args) { SCM prog_args = scm_cons(prog, args); size_t len = scm_to_size_t(scm_length(prog_args)); char **argv = scm_gc_malloc((len + 1) * sizeof(char *), "argv"); for (size_t k = 0; k < len; ++k) { SCM arg = SCM_CAR(prog_args); size_t len = scm_c_string_length(arg); argv[k] = scm_gc_malloc_pointerless(len + 1, "argv[k]"); scm_to_locale_stringbuf(arg, argv[k], len); argv[k][len] = '\0'; prog_args = SCM_CDR(prog_args); } argv[len] = NULL; int status = 0; pid_t pid = fork(); if (pid == -1) scm_syserror("system**"); if (pid == 0) /* child */ { execvp(argv[0], argv); _exit(127); } else /* parent */ { if (waitpid(pid, &status, 0) == -1) scm_syserror("system**"); } return scm_from_int(status); } void init_system_star_star(void) { scm_c_define_gsubr("system**", 1, 0, 1, system_star_star); }