1 /**
2   @file
3 
4   @ingroup util
5 
6   @brief Tilde expansion.
7 
8   @copyright@parblock
9   Copyright (c) 1994-1998 The Regents of the Univ. of California.
10   All rights reserved.
11 
12   Permission is hereby granted, without written agreement and without license
13   or royalty fees, to use, copy, modify, and distribute this software and its
14   documentation for any purpose, provided that the above copyright notice and
15   the following two paragraphs appear in all copies of this software.
16 
17   IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
18   DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
19   OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
20   CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 
22   THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
23   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN
25   "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
26   MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
27   @endparblock
28 
29   @copyright@parblock
30   Copyright (c) 1999-2015, Regents of the University of Colorado
31 
32   All rights reserved.
33 
34   Redistribution and use in source and binary forms, with or without
35   modification, are permitted provided that the following conditions
36   are met:
37 
38   Redistributions of source code must retain the above copyright
39   notice, this list of conditions and the following disclaimer.
40 
41   Redistributions in binary form must reproduce the above copyright
42   notice, this list of conditions and the following disclaimer in the
43   documentation and/or other materials provided with the distribution.
44 
45   Neither the name of the University of Colorado nor the names of its
46   contributors may be used to endorse or promote products derived from
47   this software without specific prior written permission.
48 
49   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
52   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
53   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
59   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60   POSSIBILITY OF SUCH DAMAGE.
61   @endparblock
62 
63 */
64 
65 #include "util.h"
66 
67 #ifdef BSD
68 #include <pwd.h>
69 #endif
70 
71 /**
72  * @brief Expands tilde in a file name
73  */
74 char *
util_tilde_expand(char const * fname)75 util_tilde_expand(char const *fname)
76 {
77 #ifdef BSD
78     struct passwd *userRecord;
79     char username[256], *filename;
80     register int i, j;
81 
82     filename = ALLOC(char, strlen(fname) + 256);
83 
84     /* Clear the return string */
85     i = 0;
86     filename[0] = '\0';
87 
88     /* Tilde? */
89     if (fname[0] == '~') {
90 	j = 0;
91 	i = 1;
92 	while ((fname[i] != '\0') && (fname[i] != '/')) {
93 	    username[j++] = fname[i++];
94 	}
95 	username[j] = '\0';
96 
97 	if (username[0] == '\0') {
98 	    /* ~/ resolves to home directory of current user */
99 	    if ((userRecord = getpwuid(getuid())) != 0) {
100 		(void) strcat(filename, userRecord->pw_dir);
101 	    } else {
102 		i = 0;
103 	    }
104 	} else {
105 	    /* ~user/ resolves to home directory of 'user' */
106 	    if ((userRecord = getpwnam(username)) != 0) {
107 		(void) strcat(filename, userRecord->pw_dir);
108 	    } else {
109 		i = 0;
110 	    }
111 	}
112     }
113 
114     /* Concantenate remaining portion of file name */
115     (void) strcat(filename, fname + i);
116     return filename;
117 #else
118     return util_strsav(fname);
119 #endif
120 }
121