HPCToolkit
findinstall.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 /*************************** System Include Files ***************************/
48 
49 #include <stdio.h> // for FILENAME_MAX
50 #include <libgen.h> // for dirname/basename
51 
52 #define __USE_XOPEN_EXTENDED // for realpath()
53 #include <stdlib.h>
54 
55 #define __USE_XOPEN_EXTENDED // for strdup()
56 #include <string.h>
57 
58 /**************************** User Include Files ****************************/
59 
60 #include "findinstall.h"
61 #include "pathfind.h"
62 
63 /*************************** Forward Declarations ***************************/
64 
65 /****************************************************************************/
66 
67 /* findinstall
68  *
69  * Note: This is a little trickier than a shell script launcher
70  * because if cmd was invoked as 'foo' with the assumption that it
71  * would be found using PATH, 'cmd' is _not_ a fully resolved a path,
72  * but simply 'foo'.
73 */
74 char*
75 findinstall(const char* cmd, const char* base_cmd)
76 {
77  static char rootdir[FILENAME_MAX]; // PATH_MAX
78  char *bindir, *rootdir_rel;
79  char *cmd1 = NULL, *bindir_tmp = NULL;
80 
81  rootdir[0] = '\0';
82  if (!cmd || !base_cmd) {
83  return NULL;
84  }
85 
86  /* given no path information, we must use pathfind */
87  if (strcmp(cmd, base_cmd) == 0) {
88  cmd1 = pathfind(getenv("PATH"), cmd, "rx");
89  }
90  if (!cmd1) {
91  cmd1 = (char*)cmd;
92  }
93 
94  /* given a command with a path, find the root installation directory */
95  cmd1 = strdup(cmd1);
96  bindir = dirname(cmd1);
97  if (strcmp(bindir, ".") == 0) {
98  rootdir_rel = "..";
99  }
100  else {
101  bindir_tmp = strdup(bindir);
102  rootdir_rel = dirname(bindir_tmp);
103  }
104 
105  // printf("%s // %s // %s\n", cmd, bindir, rootdir_rel);
106 
107  if (realpath(rootdir_rel, rootdir) == NULL) {
108  // fprintf(stderr, "%s: %s\n", cmd, strerror(errno));
109  goto error;
110  }
111 
112  free(cmd1);
113  free(bindir_tmp);
114  return rootdir;
115 
116  error:
117  return NULL;
118 }
119 
120 /****************************************************************************/
void MONITOR_EXT_WRAP_NAME() free(void *ptr)
string dirname(const char *fName)
Definition: FileUtil.cpp:134
char * pathfind(const char *pathList, const char *name, const char *mode)
Definition: pathfind.cpp:71
char * findinstall(const char *cmd, const char *base_cmd)
Definition: findinstall.c:75
#define NULL
Definition: ElfHelper.cpp:85