summaryrefslogtreecommitdiffstats
path: root/src/misc/util/texpand.c
blob: 37f71cbd338f0ae2ce43a92464a4b01ad2f72c9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Revision Control Information
 *
 * /projects/hsis/CVS/utilities/util/texpand.c,v
 * rajeev
 * 1.3
 * 1995/08/08 22:41:36
 *
 */

#include "util.h"

#if HAVE_PWD_H
#  include <pwd.h>
#endif


char *
util_tilde_expand(fname)
char *fname;
{
#if HAVE_PWD_H
    struct passwd *userRecord;
    char username[256], *filename, *dir;
    register int i, j;

    filename = ALLOC(char, strlen(fname) + 256);

    /* Clear the return string */
    i = 0;
    filename[0] = '\0';

    /* Tilde? */
    if (fname[0] == '~') {
    j = 0;
    i = 1;
    while ((fname[i] != '\0') && (fname[i] != '/')) {
        username[j++] = fname[i++];
    }
    username[j] = '\0';
    dir = (char *)0;
    if (username[0] == '\0') {
        /* ~/ resolves to home directory of current user */
        userRecord = getpwuid(getuid());
        if (userRecord) dir = userRecord->pw_dir;
    } else {
        /* Special check for ~octtools */
        if (!strcmp(username,"octtools"))
            dir = getenv("OCTTOOLS");
        /* ~user/ resolves to home directory of 'user' */
        if (!dir) {
            userRecord = getpwnam(username);
        if (userRecord) dir = userRecord->pw_dir;
        }
    }
    if (dir) (void) strcat(filename, dir);
    else i = 0;    /* leave fname as-is */
    } /* if tilde */

    /* Concantenate remaining portion of file name */
    (void) strcat(filename, fname + i);
    return filename;
#else
    return util_strsav(fname);
#endif
}