1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: fnexpand.c 761 2007-10-23 22:35:18Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2013-2020 Eduardo Chappa
8 * Copyright 2006 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
21 #include "../charconv/filesys.h"
28 /*----------------------------------------------------------------------
29 Expand the ~ in a file ala the csh (as home directory)
31 Args: buf -- The filename to expand (nothing happens unless begins with ~)
32 len -- The length of the buffer passed in (expansion is in place)
34 Result: Expanded string is returned using same storage as passed in.
35 If expansion fails, NULL is returned
38 fnexpand(char *buf
, int len
)
41 /* We used to use ps_global->home_dir, now we have to build it */
42 if(*buf
== '~' && *(buf
+1) == '\\'){
43 char temp_path
[_MAX_PATH
], home_buf
[_MAX_PATH
], *temp_home_str
;
45 if(getenv("HOME") != NULL
)
46 temp_home_str
= getenv("HOME");
48 /* should eventually strip this out of get_user_info */
51 if((p
= (char *) getenv("HOMEDRIVE"))
52 && (q
= (char *) getenv("HOMEPATH")))
53 snprintf(home_buf
, sizeof(home_buf
), "%s%s", p
, q
);
55 snprintf(home_buf
, sizeof(home_buf
), "%c:\\", '@' + _getdrive());
57 temp_home_str
= home_buf
;
59 snprintf(temp_path
, sizeof(temp_path
), "%s", buf
+1);
60 snprintf(buf
, sizeof(buf
), "%s%s", temp_path
, fname_to_utf8(temp_home_str
));
69 for(x
= buf
+1, y
= name
;
70 *x
!= '/' && *x
!= '\0' && y
< name
+ sizeof(name
)-1;
76 pw
= getpwuid(getuid());
82 if(strlen(pw
->pw_dir
) + strlen(buf
) > len
) {
86 if((tbuf
= (char *) malloc((len
+1)*sizeof(char))) != NULL
){
87 snprintf(tbuf
, len
, "%s%s", pw
->pw_dir
, x
);
88 snprintf(buf
, len
, "%s", tbuf
);
93 return(len
? buf
: (char *)NULL
);