1 /*********************************************************************** 2 * * 3 * This software is part of the ast package * 4 * Copyright (c) 1985-2010 AT&T Intellectual Property * 5 * and is licensed under the * 6 * Common Public License, Version 1.0 * 7 * by AT&T Intellectual Property * 8 * * 9 * A copy of the License is available at * 10 * http://www.opensource.org/licenses/cpl1.0.txt * 11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12 * * 13 * Information and Software Systems Research * 14 * AT&T Research * 15 * Florham Park NJ * 16 * * 17 * Glenn Fowler <gsf@research.att.com> * 18 * David Korn <dgk@research.att.com> * 19 * Phong Vo <kpv@research.att.com> * 20 * * 21 ***********************************************************************/ 22 #pragma prototyped 23 /* 24 * Glenn Fowler 25 * AT&T Research 26 * 27 * return the full path of the current program in path 28 * command!=0 is used as a default 29 */ 30 31 #include <ast.h> 32 33 #if _WINIX 34 #include <ast_windows.h> 35 #include <ctype.h> 36 #endif 37 38 #include "FEATURE/prog" 39 40 static size_t 41 prog(const char* command, char* path, size_t size) 42 { 43 ssize_t n; 44 #if _WINIX || _lib_getexecname 45 char* s; 46 #endif 47 #if _WINIX 48 char* t; 49 char* e; 50 int c; 51 int q; 52 #endif 53 54 #ifdef _PROC_PROG 55 if ((n = readlink(_PROC_PROG, path, size)) > 0) 56 { 57 if (n < size) 58 path[n] = 0; 59 return n; 60 } 61 #endif 62 #if _lib_getexecname 63 if (s = (char*)getexecname()) 64 { 65 n = strlen(s); 66 if (n < size) 67 strcpy(path, s); 68 return n; 69 } 70 #endif 71 #if _WINIX 72 if (s = GetCommandLine()) 73 { 74 n = 0; 75 q = 0; 76 t = path; 77 e = path + size - 1; 78 while (c = *s++) 79 { 80 if (c == q) 81 q = 0; 82 else if (!q && c == '"') 83 q = c; 84 else if (!q && isspace(c)) 85 break; 86 else if (t < e) 87 *t++ = c == '\\' ? '/' : c; 88 else 89 n++; 90 } 91 if (t < e) 92 *t = 0; 93 return (t - path) + n; 94 } 95 #endif 96 if (command) 97 { 98 if ((n = strlen(command) + 1) <= size) 99 memcpy(path, command, n); 100 return n; 101 } 102 return 0; 103 } 104 105 size_t 106 pathprog(const char* command, char* path, size_t size) 107 { 108 ssize_t n; 109 char buf[PATH_MAX]; 110 111 if ((n = prog(command, path, size)) > 0 && n <= size && *path != '/') 112 { 113 if (!pathpath(buf, path, NiL, PATH_REGULAR|PATH_EXECUTE)) 114 n = 0; 115 else if ((n = strlen(buf) + 1) <= size) 116 memcpy(path, buf, n); 117 } 118 return n; 119 } 120