/* ----------------------------------------------------------------- */ /* Tilde expansion as csh does. Call if `~' is the first character of buffer. --- Chris McDonald (chris@budgie.cs.uwa.edu.au) */ static char tilde[BUFSIZ]; static void expand_tilde() /* expand in-situ in tilde */ { static char buf[BUFSIZ]; char *s, *t, *t1; struct passwd *p, *getpwnam(); s = tilde; s++; /* skip leading tilde */ t = buf; while (*s && *s != '/') *t++ = *s++; *t = NULL; if(*buf && (p = getpwnam(buf)) == NULL) return; t1 = *buf ? p->pw_dir : (char *)getenv("HOME"); t = buf; while( *t++ = *t1++ ); /* buf <- home_dir */ t--; while( *t++ = *s++ ); /* buf += rest_of_a */ t = tilde; t1 = buf; while( *t++ = *t1++ ); /* s <- buf */ } /* ----------------------------------------------------------------- */ /* Alternitive tilde --- Mark Nagel */ #include #include #include int tilde(file, exp) char *file, *exp; { *exp = '\0'; if (file) { if (*file == '~') { char user[128]; struct passwd *pw = NULL; int i = 0; user[0] = '\0'; file++; while (*file != '/' && i < sizeof(user)) user[i++] = *file++; user[i] = '\0'; if (i == 0) { char *login = (char *) getlogin(); if (login == NULL && (pw = getpwuid(getuid())) == NULL) return (0); if (login != NULL) strcpy(user, login); } if (pw == NULL && (pw = getpwnam(user)) == NULL) return (0); strcpy(exp, pw->pw_dir); } strcat(exp, file); return (1); } return (0); } /* ----------------------------------------------------------------- */