-
Notifications
You must be signed in to change notification settings - Fork 8
/
libexpnhome.c
85 lines (74 loc) · 2.11 KB
/
libexpnhome.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* FUNET-NJE Client utilities
*
* Common set of client used utility routines.
* These are collected to here from various modules for eased
* maintance.
*
* Matti Aarnio <[email protected]> 12-Feb-1991, 26-Sep-1993
*/
#include "prototypes.h"
#include "clientutils.h"
#include <sysexits.h>
#include <ctype.h>
/* ExpandHomeDir() -- taken from unix_files.c -- by <[email protected]> */
extern char DefaultSpoolDir[256];
char *
ExpandHomeDir( PathOrDir,HomeDir,ToUser,path )
const char *PathOrDir,*HomeDir,*ToUser;
char *path;
{
struct stat fstats;
int plen;
struct passwd *passwds;
char *s;
char path2[512],path3[512];
if (strcmp(PathOrDir,"default")==0 || *PathOrDir == 0) {
PathOrDir = DefaultSpoolDir;
}
if (*PathOrDir == '~') {
if (PathOrDir[1] != '/') {
/* Uh.. ~/ would have been easy... */
strcpy( path2,PathOrDir+1 );
if ((s = strchr(path2,'/'))) *s = 0;
if ((passwds = getpwnam( path2 ))) {
strcpy( path,passwds->pw_dir );
*s = '/';
strcat( path,s );
} else {
return NULL;
/* Uhh... Wasn't real user... */
}
} else { /* ~/... */
strcpy( path3,PathOrDir ); /* Now that our 'home' isn't blank... */
strcpy( path,HomeDir );
strcat( path,path3+1 );
}
} else
if (path != PathOrDir)
strcpy( path,PathOrDir ); /* Jeeks, they may be different ones */
plen = strlen(path);
s = path + plen - 1;
/* A '/' terminating dir path ? */
if ((plen > 0) && (*s != '/') ) {
if (stat(path,&fstats) == 0) { /* Ok, file does exist */
if ((fstats.st_mode & S_IFMT) == S_IFDIR) { /* Ok, directory */
*++s = '/';
*s = 0;
plen += 1; /* Then fall to create unique fileid. */
} else { /* Not directory, but exists */
return path;
}
} else { /* Ok, file does not exist */
if (errno != ENOENT) {
/* XXX: stating failed, and not because nonexistent file.
This calls for stronger measures, or nice fault ?
Invent something ! */
return NULL;
}
}
} else { /* A '/' -terminated path. */
strcat( path,ToUser );
strcat( path,"/" );
}
return path;
}