summaryrefslogtreecommitdiff
path: root/src/lib/libast/path/pathprog.c
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2012-06-24 22:28:35 +0000
committerIgor Pashev <pashev.igor@gmail.com>2012-06-24 22:28:35 +0000
commit3950ffe2a485479f6561c27364d3d7df5a21d124 (patch)
tree468c6e14449d1b1e279222ec32f676b0311917d2 /src/lib/libast/path/pathprog.c
downloadksh-upstream.tar.gz
Imported Upstream version 93u+upstream
Diffstat (limited to 'src/lib/libast/path/pathprog.c')
-rw-r--r--src/lib/libast/path/pathprog.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/lib/libast/path/pathprog.c b/src/lib/libast/path/pathprog.c
new file mode 100644
index 0000000..ac29427
--- /dev/null
+++ b/src/lib/libast/path/pathprog.c
@@ -0,0 +1,128 @@
+/***********************************************************************
+* *
+* This software is part of the ast package *
+* Copyright (c) 1985-2011 AT&T Intellectual Property *
+* and is licensed under the *
+* Eclipse Public License, Version 1.0 *
+* by AT&T Intellectual Property *
+* *
+* A copy of the License is available at *
+* http://www.eclipse.org/org/documents/epl-v10.html *
+* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+* *
+* Information and Software Systems Research *
+* AT&T Research *
+* Florham Park NJ *
+* *
+* Glenn Fowler <gsf@research.att.com> *
+* David Korn <dgk@research.att.com> *
+* Phong Vo <kpv@research.att.com> *
+* *
+***********************************************************************/
+#pragma prototyped
+/*
+ * Glenn Fowler
+ * AT&T Research
+ *
+ * return the full path of the current program in path
+ * command!=0 is used as a default
+ */
+
+#include <ast.h>
+
+#if _WINIX
+#include <ast_windows.h>
+#include <ctype.h>
+#endif
+
+#include "FEATURE/prog"
+
+#if _hdr_macho_o_dyld && _lib__NSGetExecutablePath
+#include <mach-o/dyld.h>
+#else
+#undef _lib__NSGetExecutablePath
+#endif
+
+static size_t
+prog(const char* command, char* path, size_t size)
+{
+ ssize_t n;
+ char* s;
+#if _WINIX
+ char* t;
+ char* e;
+ int c;
+ int q;
+#endif
+#if _lib__NSGetExecutablePath
+ uint32_t z;
+#endif
+
+#ifdef _PROC_PROG
+ if ((n = readlink(_PROC_PROG, path, size)) > 0 && *path == '/')
+ {
+ if (n < size)
+ path[n] = 0;
+ return n;
+ }
+#endif
+#if _lib_getexecname
+ if ((s = (char*)getexecname()) && *s == '/')
+ goto found;
+#endif
+#if _lib__NSGetExecutablePath
+ z = size;
+ if (!_NSGetExecutablePath(path, &z) && *s == '/')
+ return strlen(s);
+#endif
+#if _WINIX
+ if (s = GetCommandLine())
+ {
+ n = 0;
+ q = 0;
+ t = path;
+ e = path + size - 1;
+ while (c = *s++)
+ {
+ if (c == q)
+ q = 0;
+ else if (!q && c == '"')
+ q = c;
+ else if (!q && isspace(c))
+ break;
+ else if (t < e)
+ *t++ = c == '\\' ? '/' : c;
+ else
+ n++;
+ }
+ if (t < e)
+ *t = 0;
+ return (t - path) + n;
+ }
+#endif
+ if (command)
+ {
+ s = (char*)command;
+ goto found;
+ }
+ return 0;
+ found:
+ n = strlen(s);
+ if (n < size)
+ memcpy(path, s, n + 1);
+ return n;
+}
+
+size_t
+pathprog(const char* command, char* path, size_t size)
+{
+ char* rel;
+ ssize_t n;
+
+ if ((n = prog(command, path, size)) > 0 && n < size && *path != '/' && (rel = strdup(path)))
+ {
+ n = pathpath(rel, NiL, PATH_REGULAR|PATH_EXECUTE, path, size) ? strlen(path) : 0;
+ free(rel);
+ }
+ return n;
+}