Init the object variable for the registergroup so that
[cake.git] / compiler / clib / execvp.c
blob8a9592e22780561ba46b991997811df30110df80
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX function execvp().
6 */
8 #include <aros/debug.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <stdlib.h>
15 /*****************************************************************************
17 NAME */
18 #include <unistd.h>
20 int execvp(
22 /* SYNOPSIS */
23 const char *file,
24 char *const argv[])
26 /* FUNCTION
27 Executes a file with given name. The search paths for the executed
28 file are paths specified in the PATH environment variable.
30 INPUTS
31 file - Name of the file to execute.
32 argv - Array of arguments given to main() function of the executed
33 file.
35 RESULT
36 Returns -1 and sets errno appropriately in case of error, otherwise
37 doesn't return.
39 NOTES
41 EXAMPLE
43 BUGS
45 SEE ALSO
46 execve(), execl(), execlp(), execv()
48 INTERNALS
50 ******************************************************************************/
52 char *path = NULL, *path_item, *path_ptr;
53 char default_path[] = ":/bin:/usr/bin";
54 char *full_path;
55 int saved_errno;
56 int i;
58 if(index(file, '/') || index(file, ':'))
60 /* file argument is a path, don't search */
61 return execve(file, argv, environ);
64 /* argument is a file, search for this file in PATH variable entries */
65 char *name = "PATH";
66 if(environ)
68 for(i = 0; environ[i]; i++)
70 if(strncmp(environ[i], name, strlen(name)) == 0)
72 path = &environ[i][strlen(name)+1];
73 break;
78 if(!path)
79 path = getenv("PATH");
81 if(!path)
82 path = default_path;
83 else
84 path = strdup(path);
86 path_ptr = path;
88 while((path_item = strsep(&path_ptr, ",:")))
90 if(path_item[0] == '\0')
91 path_item = ".";
93 if((full_path = malloc(strlen(path_item) + strlen(file) + 2)))
95 full_path[0] = '\0';
96 strcat(full_path, path_item);
97 strcat(full_path, "/");
98 strcat(full_path, file);
100 /* try executing execve with this path */
101 if(execve(full_path, argv, environ) == 0)
103 free(full_path);
104 return 0;
106 else
107 if(errno == EACCES)
108 saved_errno = EACCES;
109 free(full_path);
111 else
113 saved_errno = ENOMEM;
114 goto error;
118 /* set ENOENT error if there were errors other than EACCES */
119 if(saved_errno != EACCES)
120 saved_errno = ENOENT;
122 error:
123 errno = saved_errno;
124 return -1;
125 } /* execvp() */