波浪号展开(Tilde Expansion)

博客涉及Linux系统下的Shell和Bash相关内容,虽内容待编辑,但可推测会围绕二者在Linux环境中的应用、操作等信息技术方面展开。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

待编辑

char* filename_completion_function(const char* text, int state) { static DIR* dir = NULL; static char * filename = NULL, *dirname = NULL; static size_t filename_len = 0; struct dirent* entry; char* temp; size_t len; if (state == 0 || dir == NULL) { if (dir != NULL) { closedir(dir); dir = NULL; } temp = strrchr(text, '/'); if (temp) { temp++; filename = XREALLOC(MTYPE_TMP, filename, strlen(temp) + 1); (void)strcpy(filename, temp); len = temp - text; /* including last slash */ dirname = XREALLOC(MTYPE_TMP, dirname, len + 1); (void)strncpy(dirname, text, len); dirname[len] = '\0'; } else { filename = XSTRDUP(0, text); dirname = NULL; } /* support for ``~user'' syntax */ if (dirname && *dirname == '~') { temp = tilde_expand(dirname); dirname = XREALLOC(MTYPE_TMP, dirname, strlen(temp) + 1); (void)strcpy(dirname, temp); /* safe */ XFREE(0, temp); /* no longer needed */ } /* will be used in cycle */ filename_len = strlen(filename); if (filename_len == 0) return (NULL); /* no expansion possible */ dir = opendir(dirname ? dirname : "."); if (!dir) return (NULL); /* cannot open the directory */ } /* find the match */ while ((entry = readdir(dir)) != NULL) { /* otherwise, get first entry where first */ /* filename_len characters are equal */ if (entry->d_name[0] == filename[0] #if defined(__SVR4) || defined(__linux__) && strlen(entry->d_name) >= filename_len #else && entry->d_namlen >= filename_len #endif && strncmp(entry->d_name, filename, filename_len) == 0) break; } if (entry) { /* match found */ struct stat stbuf; #if defined(__SVR4) || defined(__linux__) len = strlen(entry->d_name) + #else len = entry->d_namlen + #endif ((dirname) ? strlen(dirname) : 0) + 1 + 1; temp = XMALLOC(0, len); (void)sprintf(temp, "%s%s", dirname ? dirname : "", entry->d_name); /* safe */ /* test, if it's directory */ if (stat(temp, &stbuf) == 0 && S_ISDIR(stbuf.st_mode)) strcat(temp, "/"); /* safe */ } else temp = NULL; return (temp); } 在这个基础上优化修改
最新发布
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值