Linux/쉘, 쉘 명령어 구현하기
[쉘 구현하기] pwd 명령어 구현
Study with Me!
2023. 8. 8. 22:59
현재 작업 위치를 출력하는 pwd 명령어를 구현했다.
이 명령어는 자식 프로세스를 생성하지는 않았고, 헤더 파일에 저장돼 있는 execPath를 출력만 했다.
// SM_shell.c
#include "my_header.h"
...
void prompt() {
char input[STR_MAX];
int command;
while (1) {
printf("SM_shell > ");
fgets(input, STR_MAX, stdin);
input[strlen(input) - 1] = '\0';
if (!strcmp(input, commandList[0])) { // exit
fprintf(stdout, "* SM_shell exit... *\n");
exit(0);
} else if (!strcmp(input, commandList[1])) { // help
command = CMD_HELP;
} else if (!strcmp(input, commandList[2])) {
printf("%s\n\n", execPath);
continue;
}
...
}
int main(int argc, char **argv) {
...
}
코드는 생략했지만 헤더 파일에 commandList에도 "pwd"를 추가했다.
위 코드는 아래 이미지를 클릭해 깃허브에서도 확인할 수 있다.