rs@mirror.UUCP (08/27/85)
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 <pwd.h> 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