source: trunk/sys/dietlibc/execvp.c @ 186

Last change on this file since 186 was 1, checked in by alain, 7 years ago

First import

File size: 1.1 KB
Line 
1#include <limits.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <errno.h>
6#include "dietfeatures.h"
7
8#define _PATH_DEFPATH "/BIN:/USR/BIN:"
9
10
11int execvp(const char *file, char *const argv[]) {
12  const char *path=getenv("PATH");
13  char *cur,*next;
14  char buf[PATH_MAX];
15  if (strchr((char*)file,'/')) {
16    if (execve(file,argv,environ)==-1) {
17      return -1;
18    }
19  }
20  if (!path) path=_PATH_DEFPATH;
21  for (cur=(char*)path; cur; cur=next) {
22    next=strchr(cur,':');
23    if (!next)
24      next=cur+strlen(cur);
25    if (next==cur) {
26      buf[0]='.';
27      cur--;
28    } else {
29      if (next-cur>=PATH_MAX-3) { error: errno=EINVAL; return -1; }
30      memmove(buf,cur,(size_t)(next-cur));
31    }
32    buf[next-cur]='/';
33    {
34      int len=strlen(file);
35      if (len+(next-cur)>=PATH_MAX-2) goto error;
36      memmove(&buf[next-cur+1],file,strlen(file)+1);
37    }
38    if (execve(buf,argv,environ)==-1) {
39      return -1;
40      if ((errno!=EACCES) && (errno!=ENOENT) && (errno!=ENOTDIR)) return -1;
41    }
42    if (*next==0) break;
43    next++;
44  }
45  return -1;
46}
Note: See TracBrowser for help on using the repository browser.