2 * ========================================================================
3 * Copyright 2013-2022 Eduardo Chappa
4 * Copyright 2006 University of Washington
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
17 #include "../charconv/filesys.h"
24 /*----------------------------------------------------------------------
25 Expand the ~ in a file ala the csh (as home directory)
27 Args: buf -- The filename to expand (nothing happens unless begins with ~)
28 len -- The length of the buffer passed in (expansion is in place)
30 Result: Expanded string is returned using same storage as passed in.
31 If expansion fails, NULL is returned
34 fnexpand(char *buf
, int len
)
37 /* We used to use ps_global->home_dir, now we have to build it */
38 if(*buf
== '~' && *(buf
+1) == '\\'){
39 char temp_path
[_MAX_PATH
], home_buf
[_MAX_PATH
], *temp_home_str
;
41 if(getenv("HOME") != NULL
)
42 temp_home_str
= getenv("HOME");
44 /* should eventually strip this out of get_user_info */
47 if((p
= (char *) getenv("HOMEDRIVE"))
48 && (q
= (char *) getenv("HOMEPATH")))
49 snprintf(home_buf
, sizeof(home_buf
), "%s%s", p
, q
);
51 snprintf(home_buf
, sizeof(home_buf
), "%c:\\", '@' + _getdrive());
53 temp_home_str
= home_buf
;
55 snprintf(temp_path
, sizeof(temp_path
), "%s", buf
+1);
56 snprintf(buf
, sizeof(buf
), "%s%s", temp_path
, fname_to_utf8(temp_home_str
));
65 for(x
= buf
+1, y
= name
;
66 *x
!= '/' && *x
!= '\0' && y
< name
+ sizeof(name
)-1;
72 pw
= getpwuid(getuid());
78 if(strlen(pw
->pw_dir
) + strlen(buf
) > len
) {
82 if((tbuf
= (char *) malloc((len
+1)*sizeof(char))) != NULL
){
83 snprintf(tbuf
, len
, "%s%s", pw
->pw_dir
, x
);
84 snprintf(buf
, len
, "%s", tbuf
);
89 return(len
? buf
: (char *)NULL
);