[8] | 1 | /* |
---|
| 2 | * Revision Control Information |
---|
| 3 | * |
---|
| 4 | * $Id: texpand.c,v 1.3 2002/08/25 02:37:11 fabio Exp $ |
---|
| 5 | * |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #include "util.h" |
---|
| 9 | |
---|
| 10 | #if HAVE_PWD_H |
---|
| 11 | # include <pwd.h> |
---|
| 12 | #endif |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | char * |
---|
| 16 | util_tilde_expand(char *fname) |
---|
| 17 | { |
---|
| 18 | #if HAVE_PWD_H |
---|
| 19 | struct passwd *userRecord; |
---|
| 20 | char username[256], *filename, *dir; |
---|
| 21 | register int i, j; |
---|
| 22 | |
---|
| 23 | filename = ALLOC(char, strlen(fname) + 256); |
---|
| 24 | |
---|
| 25 | /* Clear the return string */ |
---|
| 26 | i = 0; |
---|
| 27 | filename[0] = '\0'; |
---|
| 28 | |
---|
| 29 | /* Tilde? */ |
---|
| 30 | if (fname[0] == '~') { |
---|
| 31 | j = 0; |
---|
| 32 | i = 1; |
---|
| 33 | while ((fname[i] != '\0') && (fname[i] != '/')) { |
---|
| 34 | username[j++] = fname[i++]; |
---|
| 35 | } |
---|
| 36 | username[j] = '\0'; |
---|
| 37 | dir = (char *)0; |
---|
| 38 | if (username[0] == '\0') { |
---|
| 39 | /* ~/ resolves to home directory of current user */ |
---|
| 40 | userRecord = getpwuid(getuid()); |
---|
| 41 | if (userRecord) dir = userRecord->pw_dir; |
---|
| 42 | } else { |
---|
| 43 | /* Special check for ~octtools */ |
---|
| 44 | if (!strcmp(username,"octtools")) |
---|
| 45 | dir = getenv("OCTTOOLS"); |
---|
| 46 | /* ~user/ resolves to home directory of 'user' */ |
---|
| 47 | if (!dir) { |
---|
| 48 | userRecord = getpwnam(username); |
---|
| 49 | if (userRecord) dir = userRecord->pw_dir; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | if (dir) (void) strcat(filename, dir); |
---|
| 53 | else i = 0; /* leave fname as-is */ |
---|
| 54 | } /* if tilde */ |
---|
| 55 | |
---|
| 56 | /* Concantenate remaining portion of file name */ |
---|
| 57 | (void) strcat(filename, fname + i); |
---|
| 58 | return filename; |
---|
| 59 | #else |
---|
| 60 | return util_strsav(fname); |
---|
| 61 | #endif |
---|
| 62 | } |
---|