[英]How to see the current path in linux shell using c++?
我正在創建一個 shell 程序,它能夠更改目錄並退出 shell。 一切正常。 我有一個關於顯示我所在的當前目錄/路徑的問題。當我編譯和運行我的代碼時。 我在我的 shell 循環中。 我的 cursor 代表ash >我希望 cursor 代表用戶所在的當前路徑。
#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);
PATH_MAX
需要包含<linux/limits.h>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.