c - 如何使用 c++ 在 linux shell 中查看当前路径?
问题描述
我正在创建一个能够更改目录并退出 shell 的 shell 程序。一切正常。我有一个关于显示我所在的当前目录/路径的问题。当我编译和运行我的代码时。我在我的 shell 循环中。我的光标代表ash >我希望该光标代表用户所在的当前路径。
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int ash_exit(char **args);
int ash_cd(char **args);
char const *builtin_str[] = {
"exit", "cd"};
int (*builtin_func[])(char **) = {
&ash_exit, &ash_cd};
int ash_num_builtins()
{
return sizeof(builtin_str) / sizeof(char *);
}
/**
Bultin command: change directory.
args List of args. args[0] is "cd". args[1] is the directory.
Always returns 1, to continue executing.
*/
int ash_cd(char **args)
{
if (args[1] == NULL)
{
fprintf(stderr, "ash: expected argument to \"cd\"\n");
}
else
{
if (chdir(args[1]) != 0)
{
perror("ash");
}
}
return 1;
}
int ash_exit(char **args)
{
return 0;
}
int ash_launch(char **args)
{
pid_t pid;
int status;
pid = fork();
if (pid == 0)
{
// Child process
if (execvp(args[0], args) == -1)
{
perror("ash");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
// Error forking
perror("ash");
}
else
{
// Parent process
do
{
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
int ash_execute(char **args)
{
int i;
if (args[0] == NULL)
{
// An empty command was entered.
return 1;
}
for (i = 0; i < ash_num_builtins(); i++)
{
if (strcmp(args[0], builtin_str[i]) == 0)
{
return (*builtin_func[i])(args);
}
}
return ash_launch(args);
}
char *ash_read_line(void)
{
char *line = NULL;
ssize_t bufsize = 0; // have getline allocate a buffer for us
if (getline(&line, (unsigned long *)&bufsize, stdin) == -1)
{
if (feof(stdin))
{
exit(EXIT_SUCCESS); // We recieved an EOF
}
else
{
perror("readline");
exit(EXIT_FAILURE);
}
}
return line;
}
#define ASH_TOK_BUFSIZE 64
#define ASH_TOK_DELIM " \t\r\n\a"
/**
Split a line into tokens
line The line.
return Null-terminated array of tokens.
*/
char **ash_split_line(char *line)
{
int bufsize = ASH_TOK_BUFSIZE, position = 0;
char **tokens = (char **)malloc(bufsize * sizeof(char *));
char *token, **tokens_backup;
if (!tokens)
{
fprintf(stderr, "ash: allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(line, ASH_TOK_DELIM);
while (token != NULL)
{
tokens[position] = token;
position++;
if (position >= bufsize)
{
bufsize += ASH_TOK_BUFSIZE;
tokens_backup = tokens;
tokens = (char **)realloc(tokens, bufsize * sizeof(char *));
if (!tokens)
{
free(tokens_backup);
fprintf(stderr, "ash: allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, ASH_TOK_DELIM);
}
tokens[position] = NULL;
return tokens;
}
void ash_loop(void)
{
char *line;
char **args;
int status;
do
{
printf("ash > ");
line = ash_read_line();
args = ash_split_line(line);
status = ash_execute(args);
free(line);
free(args);
} while (status);
}
int main(int argc, char *argv[])
{
ash_loop();
return EXIT_SUCCESS;
}
解决方案
getcwd(3)
为您提供当前路径:
char buf[PATH_MAX] = {0};
getcwd(buf, sizeof buf);
printf("ash %s > ", buf);
需要包括<linux/limits.h>
为PATH_MAX
.
推荐阅读
- jquery - JQuery 统计序列化的项目数
- c# - ASP.NET MVC 5:log4net 不写入文件
- jquery - 按钮作为提交和调用函数
- javascript - 如何获取列表中项目的索引并将其映射到 json 对象以执行 onClick 事件
- html - 嵌套的 flexbox 无法在旧的 Android 浏览器中正确显示
- php - Codeigniter - 设置 dbprefix 触发器 [无法修改标头信息 - 标头已发送]
- sql-server - 在 Excel 2016 中创建向后兼容的 SQL Server 数据连接
- c# - Xamarin.Forms - 禁用按钮动画 UWP
- java - 如何比较 JSON 文档并返回与 Jackson 或 Gson 的差异?
- amazon-web-services - awscli - lambda 函数更新触发器