current pwd in prompt

Conrad Kwok kwok at iris.ucdavis.edu
Wed Feb 10 11:31:02 AEST 1988


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 <stdio.h>

#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 at iris.ucdavis.edu
csnet: kwok at ucd.csnet 
csnet: kwok%iris.ucdavis.edu at csnet.relay
uucp: {ucbvax, uunet, lll-lcc, ...}!ucdavis!iris!kwok



More information about the Comp.unix.wizards mailing list