Autodoc corrections
[cake.git] / compiler / clib / execvp.c
blobed717b88fb57553409d3517e9e4e64da6c15b472
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 specified arguments.
29 INPUTS
30 file - Name of the file to execute.
31 argv - Array of arguments given to main() function of the executed
32 file.
34 RESULT
35 0 in case of success. In case of failure errno is set appropriately
36 and function returns -1.
38 NOTES
40 EXAMPLE
42 BUGS
43 See execve documentation.
45 SEE ALSO
46 execve()
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() */