aboutsummaryrefslogtreecommitdiffstats
path: root/android/utils
diff options
context:
space:
mode:
authorDavid 'Digit' Turner <digit@android.com>2011-05-03 00:17:48 -0700
committerAndroid Code Review <code-review@android.com>2011-05-03 00:17:48 -0700
commit5bdf6459919c143aef5f285927501629d1c596ca (patch)
tree4bdffdfddd36864cd50d3a8d1dfad07c325d1f21 /android/utils
parent5c1dcb21388749d6aa45eb92bca79c5ae5b0a929 (diff)
parent2d238fd9871687b1557f15b8878a6cf3e9634b57 (diff)
downloadexternal_qemu-5bdf6459919c143aef5f285927501629d1c596ca.zip
external_qemu-5bdf6459919c143aef5f285927501629d1c596ca.tar.gz
external_qemu-5bdf6459919c143aef5f285927501629d1c596ca.tar.bz2
Merge "Add 'emulator' launcher program."
Diffstat (limited to 'android/utils')
-rw-r--r--android/utils/path.c86
-rw-r--r--android/utils/path.h9
2 files changed, 91 insertions, 4 deletions
diff --git a/android/utils/path.c b/android/utils/path.c
index e7ef2b0..f64e517 100644
--- a/android/utils/path.c
+++ b/android/utils/path.c
@@ -265,6 +265,14 @@ path_can_write( const char* path )
return (ret == 0);
}
+ABool
+path_can_exec( const char* path )
+{
+ int ret;
+ CHECKED(ret, access(path, X_OK));
+ return (ret == 0);
+}
+
/* try to make a directory. returns 0 on success, -1 on failure
* (error code in errno) */
APosixStatus
@@ -325,7 +333,7 @@ path_mkdir_recursive( char* path, unsigned len, int mode )
return ret;
}
-/* ensure that a given directory exists, create it if not,
+/* ensure that a given directory exists, create it if not,
0 on success, -1 on failure (error code in errno) */
APosixStatus
path_mkdir_if_needed( const char* path, int mode )
@@ -363,9 +371,9 @@ path_get_size( const char* path, uint64_t *psize )
/* result in getting the size of a different file */
LARGE_INTEGER size;
HANDLE file = CreateFile( /* lpFilename */ path,
- /* dwDesiredAccess */ GENERIC_READ,
- /* dwSharedMode */ FILE_SHARE_READ|FILE_SHARE_WRITE,
- /* lpSecurityAttributes */ NULL,
+ /* dwDesiredAccess */ GENERIC_READ,
+ /* dwSharedMode */ FILE_SHARE_READ|FILE_SHARE_WRITE,
+ /* lpSecurityAttributes */ NULL,
/* dwCreationDisposition */ OPEN_EXISTING,
/* dwFlagsAndAttributes */ 0,
/* hTemplateFile */ NULL );
@@ -564,3 +572,73 @@ path_load_file(const char *fn, size_t *pSize)
return NULL;
}
+#ifdef _WIN32
+# define DIR_SEP ';'
+#else
+# define DIR_SEP ':'
+#endif
+
+char*
+path_search_exec( const char* filename )
+{
+ const char* sysPath = getenv("PATH");
+ char temp[PATH_MAX];
+ int count;
+ int slen;
+ const char* p;
+
+ /* If the file contains a directory separator, don't search */
+#ifdef _WIN32
+ if (strchr(filename, '/') != NULL || strchr(filename, '\\') != NULL) {
+#else
+ if (strchr(filename, '/') != NULL) {
+#endif
+ if (path_exists(filename)) {
+ return strdup(filename);
+ } else {
+ return NULL;
+ }
+ }
+
+ /* If system path is empty, don't search */
+ if (sysPath == NULL || sysPath[0] == '\0') {
+ return NULL;
+ }
+
+ /* Count the number of non-empty items in the system path
+ * Items are separated by DIR_SEP, and two successive separators
+ * correspond to an empty item that will be ignored.
+ * Also compute the required string storage length. */
+ count = 0;
+ slen = 0;
+ p = sysPath;
+
+ while (*p) {
+ char* p2 = strchr(p, DIR_SEP);
+ int len;
+ if (p2 == NULL) {
+ len = strlen(p);
+ } else {
+ len = p2 - p;
+ }
+
+ do {
+ if (len <= 0)
+ break;
+
+ snprintf(temp, sizeof(temp), "%.*s/%s", len, p, filename);
+
+ if (path_exists(temp) && path_can_exec(temp)) {
+ return strdup(temp);
+ }
+
+ } while (0);
+
+ p += len;
+ if (*p == DIR_SEP)
+ p++;
+ }
+
+ /* Nothing, really */
+ return NULL;
+}
diff --git a/android/utils/path.h b/android/utils/path.h
index e15e6ed..419e6bf 100644
--- a/android/utils/path.h
+++ b/android/utils/path.h
@@ -64,6 +64,9 @@ extern ABool path_is_absolute( const char* path );
extern ABool path_can_read( const char* path );
extern ABool path_can_write( const char* path );
+/* checks that one can execute a given file */
+extern ABool path_can_exec( const char* path );
+
/* try to make a directory */
extern APosixStatus path_mkdir( const char* path, int mode );
@@ -109,6 +112,12 @@ extern char* path_dirname( const char* path );
*/
extern char* path_basename( const char* path );
+/* look for a given executable in the system path and return its full path.
+ * Returns NULL if not found. Note that on Windows this doesn't not append
+ * an .exe prefix, or other magical thing like Cygwin usually does.
+ */
+extern char* path_search_exec( const char* filename );
+
/** OTHER FILE UTILITIES
**
** path_empty_file() creates an empty file at a given path location.