Path: utzoo!utgpu!water!watmath!clyde!rutgers!husc6!bbn!uwmcsd1!ig!agate!ucbvax!ucdavis!iris!kwok From: kwok@iris.ucdavis.edu (Conrad Kwok) Newsgroups: comp.unix.wizards Subject: Re: current pwd in prompt Message-ID: <1079@ucdavis.ucdavis.edu> Date: 10 Feb 88 01:31:02 GMT References: <11657@brl-adm.ARPA> <17877@topaz.rutgers.edu> Sender: uucp@ucdavis.ucdavis.edu Reply-To: kwok@iris.UUCP (Conrad Kwok) Organization: U.C. Davis - College of Engineering Lines: 82 I have a program to set my prompt to the current directory. Using the program you can get several feature that you cannot get by using shell script. o the home directory is abbreviated to "~" so that the length of the prompt is shorter when you are working under your home directory. o When the length of the path is greater than a certain number (currently set to 25), the upper level directory is abbreviated to "...". so you won't get a prompt close to your line width. Due the problem of link (hard or soft link), the home directory name may be different from the name get from the system call getcwd. One way to get around this is to write any program to set an environment variable say TRUEHOME to your home directory name get from getcwd in .login. But the way I choose now is to hard code it in the program. Therefore when you get the program be sure to set the string HOME to your home directory. This program has been used on ULTRIX 2.0, SUN-3. But on BSD 4.2 or 4.3 you may have to change getcwd to getwd according to the syntax of getwd. Good Luck! -- Conrad -----------------------put them in .cshrc---------------- alias cd 'cd \!*; set prompt="`~/bin/prompt` "' alias pushd 'pushd \!*; set prompt="`~/bin/prompt` "' alias popd 'popd; set prompt="`~/bin/prompt` "' -------------------------prompt.c------------------------- #include #define MAXLEN 80 #define PATHLEN 25 char HOME[]="/YOUR/HOME/DIRECTORY"; /* char HOME[MAXLEN]; */ char *getenv(); main(argc,argv) int argc; char **argv; { char cwd[MAXLEN+2], *newpath, *tmpptr; int loop; /* strcpy(HOME,getenv("TRUEHOME")); */ if (getcwd(cwd,MAXLEN)==NULL) { fprintf(stderr,"Error in reading working directory name\n"); printf("%%"); exit(1); } tmpptr=HOME; newpath=cwd; for(loop=strlen(HOME)-1; loop>=0; loop--) { if (*tmpptr++ != *newpath++) break; } if (loop>=0) newpath=cwd; else *(--newpath)='~'; while (strlen(newpath) > PATHLEN) { newpath+=4; while (*newpath!='/' && *newpath!=NULL) newpath++; for (loop=3; loop>0; loop--) *(--newpath)='.'; } strcat(newpath,">"); printf("%s", newpath); } ------------------- internet: kwok@iris.ucdavis.edu csnet: kwok@ucd.csnet csnet: kwok%iris.ucdavis.edu@csnet.relay uucp: {ucbvax, uunet, lll-lcc, ...}!ucdavis!iris!kwok