Article View: comp.sys.sun
Article #10813determining the real directory when using automounts
From: Mark J. Kilgard
Date: Wed, 08 Feb 1989 20:04
Date: Wed, 08 Feb 1989 20:04
93 lines
2638 bytes
2638 bytes
The automount feature of Sun OS 4.O is an incrediably nice feature but it
has the annoying side effect of blurring what your current working
directory is. Example:
uncle-bens:/marsh/mjk#1 -> pwd
/tmp_mnt/auto274a00144/mjk
It kind of makes it hard to know where you are.
The following short program will return the "real" path name when you use
automounts:
/*
* realpwd - returns the "real" name of a directory even when
* obscurred by automounting
*
* by Mark Kilgard
* 1/29/88
*/
#include <stdio.h>
#include <mntent.h>
#include <sys/param.h>
#include <string.h>
#define TMP_MNT_PREFIX "/tmp_mnt/"
#define MTAB "/etc/mtab"
char* real_getwd()
{
static char cwd[MAXPATHLEN];
char mnt_prefix[MAXPATHLEN];
char mnt_suffix[MAXPATHLEN];
int i;
int j;
char* ptr;
FILE* mtab;
struct mntent* mount;
getwd(cwd);
if(!strncmp(cwd,TMP_MNT_PREFIX,sizeof(TMP_MNT_PREFIX)-1)) {
mtab=setmntent(MTAB,"r");
if(mtab==NULL) {
return(cwd);
}
mnt_prefix[0]='/';
i=1;
while(cwd[i]!='/') {
mnt_prefix[i]=cwd[i];
i++;
}
mnt_prefix[i]='/';
i++;
while(cwd[i]!='/' && cwd[i]!='\0') {
mnt_prefix[i]=cwd[i];
i++;
}
mnt_prefix[i]='\0';
j=0;
while(cwd[i]!='\0') {
mnt_suffix[j]=cwd[i];
i++;
j++;
}
mnt_suffix[j]='\0';
while((mount=getmntent(mtab))!=NULL) {
if(!strcmp(mount->mnt_dir,mnt_prefix)) {
ptr=strrchr(mount->mnt_fsname,':');
if(ptr==NULL) {
return(cwd);
}
ptr++;
strcpy(mnt_prefix,ptr);
strcat(mnt_prefix,mnt_suffix);
return(mnt_prefix);
}
}
return(cwd);
}
return(cwd);
}
main()
{
printf("%s\n",real_getwd());
}
Extracting the routine may be useful. Realize the routine stores the
result in a static area for each call. Essentially the routine does a
lookup in /etc/mtab.
- Mark Kilgard <mjk@rice.edu>
Technical Details
Message-ID:
<8901300303.AA02718@trout.rice.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!rice!sun-spots-requestShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!rice!sun-spots-request From: mjk@trout.rice.edu (Mark J. Kilgard) Newsgroups: comp.sys.sun Subject: determining the real directory when using automounts Message-ID: <8901300303.AA02718@trout.rice.edu> Date: 8 Feb 89 20:04:22 GMT Sender: usenet@rice.edu Organization: Sun-Spots Lines: 93 Approved: Sun-Spots@rice.edu Original-Date: Sun, 29 Jan 89 21:03:56 CST X-Sun-Spots-Digest: Volume 7, Issue 144, message 3 of 9