| Newsletter Index | Quick-Tip Index | Search Newsletters |
Here is the preliminary schedule of speakers and talks:
Wednesday, Dec. 8
Eurotools is at:
http://www.eurotools.orgThe annual report and a new survey are available. You are kindly requested to complete the on-line questionnaire. Follow the User Needs and Requirements On HPC Software Tools link on the home page.
http://hpc-journals.ecs.soton.ac.uk/PEMCS/Papers/you'll find:
Comparing the Communication Performance and Scalability of a SGI Origin 2000, a Cluster of Origin 2000's and a Cray T3E-1200 using SHMEM and MPI Routines, Glenn R. Luecke, Bruno Raffin and James J. Coyle, Iowa State University, Ames, Iowa, USA, October, 1999.
A: {{ Can you write a program (C or Fortran) that prints the path,
{{ relative or absolute, to its executable?
{{
{{ It should work whether you type the path to the executable
{{ explicitly or if you type its name, and the system must search the
{{ PATH variable to locate it. (In other words, argv[0] won't always
{{ hold the answer.)
Many thanks (and prizes!) go to three readers:
Here are the solutions: Richard's:
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <unistd.h>
#define PATH_MAX 1024
int checkpath( char *path, char *exec ) {
int rc; /* Return code for strlen and access */
char *str; /* Temporary string for access() */
DIR *dir; /* Dir struct for opendir() and readdir() */
struct dirent *ent; /* Dir entry struct from readdir() */
/* Find an executable file in path that has same name as exec */
dir = opendir( path );
while ( ( ent = readdir( dir ) ) != NULL )
if ( strcmp( ent->d_name, exec ) == 0 ) {
str = ( char * ) malloc( PATH_MAX * sizeof( char ) );
rc = strlen( path );
strncpy( str, path, rc );
str[rc] = '/'; str[rc+1] = '\0';
rc = access( strcat( str, ent->d_name ), X_OK );
free( str ); closedir( dir );
return rc == 0 ? 1 : 0;
}
closedir( dir ); return 0;
}
void printrelative( char *path, char *exec ) {
int len, /* Length of cwd */
i; /* Index */
char *cwd; /* Current working directory */
cwd = ( char * ) malloc( PATH_MAX * sizeof( char ) );
cwd = getcwd( cwd, PATH_MAX );
len = strlen( cwd );
if ( strcmp( cwd, path ) == 0 ) { /* Are cwd and path the same? */
path[0] = '.'; path[1] = '\0';
} else if ( len < strlen( path ) ) { /* Remove CWD from path */
for ( i = 0; i < len; i++ )
if ( cwd[i] != path[i] ) break;
if ( i == len ) /* CWD is in beginning of path */
if ( i == 1 ) { path = path + i; } /* Fix up path if cwd == '/' */
else { path = path + i + 1; }
}
printf( "\"%s\" is in directory: %s\n", exec, path );
free( cwd );
}
int main( int argc, char *argv[] ) {
int s, /* Start of a path in PATH envvar */
len, /* Length of PATH envvar */
i; /* Index */
char *str; /* PATH envvar or executable name */
if ( ( str = rindex( argv[0], '/' ) ) != NULL ) {
str[0] = '\0';
printf( "\"%s\" is in directory: %s\n", str+1, argv[0] );
} else {
str = getenv( "PATH" ); len = strlen( str );
for ( i = 0, s = 0; i < len; i++ )
if ( str[i] == ':' ) {
str[i] = '\0';
if ( checkpath( str + s, argv[0] ) ) break;
else s = i + 1;
}
printrelative( str + s, argv[0] );
}
return 0;
}
Brad's:
// A few assumptions:
//
// * /tmp/.findme.pathname can be written to
// * path names will not be longer than 255 characters
// * executable's name will not be longer than ~220 characters
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// someplace to store the output of a shell "which" command
static const char* const tmpfilename = "/tmp/.findme.pathname";
void main(int argc,char* argv[]) {
char* lastslash; // pointer to rightmost slash in string
char* progname; // pointer to program name
char* pathname; // pointer to path name
char command[256]; // buffer for sprintf()ing commands
FILE* infile; // file pointer for reading shell "which" command output
char fullpath[256]; // buffer for storing full path
lastslash = strrchr(argv[0],'/'); // see if path is on argv[0]
if (lastslash != NULL) { // if so...
*lastslash = '\0'; // cap string there
progname = lastslash + 1; // set pointers to filename, pathname
pathname = argv[0];
} else {
progname = argv[0]; // else argv[0] holds program name
sprintf(command,"which %s > %s",argv[0],tmpfilename); // setup a which
system(command); // & execute it
infile = fopen(tmpfilename,"r"); // read the output
fscanf(infile,"%s",fullpath);
fclose(infile);
sprintf(command,"rm %s",tmpfilename); // remove tmp file
system(command);
pathname = fullpath; // search for slash
lastslash = strrchr(pathname,'/'); // cap string
*lastslash = '\0';
}
printf("\"%s\" is in directory: %s\n",progname,pathname); // output results
}
Derek's:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
/*
* This code is ksh specific. It works because ksh keeps a reference to the
* last command in the "_" environment variable. Since it does this after
* doing aliases and pattern matching, "_" happens to hold the path to
* whatever was executed.
*/
char *myPath; // holds ptr to the "_" variable contents
char *myName; // holds ptr to name of executable
char *temp; // holds temporary stuff (D'oh :-)
temp = strrchr(argv[0],'/'); // set temp to last occur. of '/'
if (temp != NULL) { // if '/'s exist
myName = temp + 1; // set myName to remainder of string
} else { // otherwise
myName = argv[0]; // set myName to first arg.
}
myPath = getenv("_"); // get "_" variable from environment
temp = strrchr(myPath,'/'); // set temp to last occur. of '/'
*temp = '\0'; // strip off the executable name
printf("\"%s\" is in directory: %s\n",myName,myPath); // print
return 0;
}
Q: My colleague finally sent me some critical files, but now he's off
to Roswell for a millennium vigil, and I can't untar them!
It looks like he gave absolute paths when he tar'ed the files,
but the system won't let me recreate the paths:
c-yukon<36> tar tf progs.tar
/tmp/vgt_tmp/progs/ellips.c
/tmp/vgt_tmp/progs/genseq.c
c-yukon<37> tar xf progs.tar
cmd-3749 tar: mkdir '/tmp/vgt_tmp/progs' failed: Permission denied
cmd-1004 tar: Cannot create '/tmp/vgt_tmp/progs/ellips.c'.
cmd-3749 tar: mkdir '/tmp/vgt_tmp/progs' failed: Permission denied
cmd-1004 tar: Cannot create '/tmp/vgt_tmp/progs/genseq.c'.
How can I extract these files?
[ Answers, questions, and tips graciously accepted. ]
Contact:
Thomas J. Baring ARSC Web Specialist ph: 907-450-8619 Donald Bahls ARSC User Consultant ph: 907-450-8674 Arctic Region Supercomputing Center University of Alaska Fairbanks PO Box 756020 Fairbanks AK 99775-6020
Send comments and questions to the current editors using this Contact Form.Email Subscriptions:
| Newsletter Index | Quick-Tip Index | Search Newsletters |
Arctic Region Supercomputing Center
PO Box 756020, Fairbanks, AK 99775 | voice: 907-450-8600 | email:
home | search | about | support | news | science | resources