Add support for tab-completion when selecting by rule
[alpine.git] / pith / osdep / fnexpand.c
blob09fbee1a998150324eb5f623da2747f7771d178a
1 /*
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 * ========================================================================
15 #include <system.h>
17 #include "../charconv/filesys.h"
19 #include "fnexpand.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
32 ----*/
33 char *
34 fnexpand(char *buf, int len)
36 #ifdef _WINDOWS
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");
43 else{
44 /* should eventually strip this out of get_user_info */
45 char *p, *q;
47 if((p = (char *) getenv("HOMEDRIVE"))
48 && (q = (char *) getenv("HOMEPATH")))
49 snprintf(home_buf, sizeof(home_buf), "%s%s", p, q);
50 else
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));
58 return(buf);
59 #else /* UNIX */
60 struct passwd *pw;
61 register char *x,*y;
62 char name[20], *tbuf;
64 if(*buf == '~') {
65 for(x = buf+1, y = name;
66 *x != '/' && *x != '\0' && y < name + sizeof(name)-1;
67 *y++ = *x++)
70 *y = '\0';
71 if(x == buf + 1)
72 pw = getpwuid(getuid());
73 else
74 pw = getpwnam(name);
76 if(pw == NULL)
77 return((char *)NULL);
78 if(strlen(pw->pw_dir) + strlen(buf) > len) {
79 return((char *)NULL);
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);
85 free((void *)tbuf);
89 return(len ? buf : (char *)NULL);
90 #endif /* UNIX */