Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: Notesfiles $Revision: 1.6.2.16 $; site mirror.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!bbnccv!mirror!rs From: rs@mirror.UUCP Newsgroups: net.sources Subject: doing tildes in filenames Message-ID: <12100006@mirror.UUCP> Date: Tue, 27-Aug-85 16:54:00 EDT Article-I.D.: mirror.12100006 Posted: Tue Aug 27 16:54:00 1985 Date-Received: Sat, 31-Aug-85 03:34:24 EDT Lines: 82 Nf-ID: #N:mirror:12100006:000:1480 Nf-From: mirror!rs Aug 27 16:54:00 1985 This short little function has come in handy in a couple of programs where I wanted to get a filename or directory name from a luser. It expands csh/uucp "~"-escapes in place. Enjoy, /r$ #include extern char *getenv(); extern char *strcpy(); extern char *strcat(); /* ** Depending on the system, "USER" might be "NAME" or "LOGNAME", ** or the whole thing might just be "/tmp". */ #define DEFAULT_HOME (getpwnam(getenv("USER"))->pw_dir) #define SIZE 128 char * nameglob(s) register char *s; { static char *home; register struct passwd *pwd; register int c; register char *p; char buff[SIZE + 1]; if (*s == '~') { (void)strcpy(buff, s + 1); if (buff[0] == '/' || buff[0] == '\0') { if (!home && !(home = getenv("HOME"))) home = DEFAULT_HOME; (void)strcpy(s, home); (void)strcat(s, buff); } else { for (p = buff; *p && *p != '/'; p++) ; if (c = *p) *p = '\0'; if ((pwd = getpwnam(buff)) == NULL) return(NULL); (void)strcpy(s, pwd->pw_dir); if (*p = c) strcat(s, p); } } return(s); } #ifdef TEST extern char *gets(); main() { char buff[SIZE]; while (gets(buff)) if (nameglob(buff)) printf("\t|%s|\t\n", nameglob(buff)); else printf("No such user: %s\n", buff); exit(0); } #endif TEST -- Rich $alz {mit-eddie, ihnp4!inmet, wjh12, cca, datacube} !mirror!rs Mirror Systems 2067 Massachusetts Ave. 617-661-0777 Cambridge, MA, 02140