libc: add pexec() to execute a command in $PATH

This commit is contained in:
Giacomo Tesio 2017-12-14 01:11:55 +01:00
parent 2dcdf47b04
commit 8c5dff6c0b
3 changed files with 43 additions and 1 deletions

View File

@ -601,6 +601,7 @@ extern int jehanne_brk(void*);
extern int jehanne_execl(const char*, ...);
extern int jehanne_forgivewkp(int64_t);
extern pid_t jehanne_fork(void);
extern int jehanne_pexec(const char* cmd, char* args[]);
extern int32_t jehanne_readn(int, void*, int32_t);
extern void* jehanne_segbrk(uint32_t);
#define sbrk(incr) jehanne_segbrk(incr)
@ -674,4 +675,3 @@ extern char *argv0;
/* this is used by segbrk and brk, it's a really bad idea to redefine it */
extern char end[];

View File

@ -0,0 +1,41 @@
/*
* This file is part of Jehanne.
*
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
*
* Jehanne is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* Jehanne 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <libc.h>
#include <envvars.h>
int
jehanne_pexec(const char *f, char *args[])
{
char *epath, *path, *entries[33];
int n, i;
if(f[0] == '/' || (epath = jehanne_getenv(ENV_PATH)) == nil)
return exec(f, (const char**)args);
n = jehanne_getfields(epath, entries, nelem(entries), 1, ":");
for(i = 0; i < n; ++i){
path = jehanne_smprint("%s/%s", entries[i], f);
exec(path, args);
jehanne_free(path);
}
jehanne_free(epath);
return -1;
}

View File

@ -60,6 +60,7 @@
"9sys/nsec.c",
"9sys/nulldir.c",
"9sys/ocreate.c",
"9sys/pexec.c",
"9sys/pipe.c",
"9sys/postnote.c",
"9sys/privalloc.c",