2
* slinks.c -- $Id: slinks.c 685 2003-03-08 15:26:51Z travo $
3
* u_track_link and u_find_exe functions from playu.h
5
* Copyright (c) 2000. See accompanying LEGAL file for details.
8
/* need these to pick up readlink prototype (unistd.h) */
10
#define _XOPEN_SOURCE 1
12
#ifndef _XOPEN_SOURCE_EXTENDED
13
#define _XOPEN_SOURCE_EXTENDED 1
25
u_track_link(const char *name)
30
if (name != p_wkspc.c)
31
for (i=0 ; (p_wkspc.c[i] = name[i]) && i<P_WKSIZ ; i++);
33
len = readlink(p_wkspc.c, link, P_WKSIZ);
35
for (i=0 ; i<len ; i++) p_wkspc.c[i] = link[i];
45
u_find_exe(const char *argv0)
47
char *wkspc = p_wkspc.c;
51
while (argv0[i] && argv0[i]!='/') i++;
53
if (!argv0[i]) { /* search for argv0 on PATH environment variable */
54
char *path = getenv("PATH");
55
char c = path? path[0] : 0;
58
while (c && c!=':') c = path[k++];
60
for (j=0 ; j<k-1 && j<P_WKSIZ ; j++) wkspc[j] = path[j];
61
if (wkspc[j-1] == '/') s = 0;
62
else s = 1, wkspc[j] = '/';
63
for (; j<k+i && j<P_WKSIZ ; j++) wkspc[j+s] = argv0[j-k+1];
64
if (access(wkspc, X_OK) >= 0) break;
73
if (i) { /* argv0 refers to subdirectory of cwd */
77
for (j=0 ; wkspc[j] ; j++);
78
if (j && wkspc[j-1] != '/') wkspc[j++] = '/';
79
while (argv0[0]=='.' && argv0[1]=='/') argv0 += 2;
80
for (i=j ; argv0[i-j] && i<P_WKSIZ ; i++) wkspc[i] = argv0[i-j];
86
} else { /* argv0 is absolute pathname */
87
for (i=0 ; argv0[i] && i<P_WKSIZ ; i++) wkspc[i] = argv0[i];
91
return (access(wkspc, X_OK) >= 0)? wkspc : 0;