-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilt_in_command.c
68 lines (56 loc) · 1.37 KB
/
built_in_command.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "built_in_command.h"
static void iaeCdParent(int pipe_fd[]);
static void iaeCdChild(char command[], char command_argv[], int pipe_fd[]);
/**
* ビルトインコマンド実行(親プロセス)
**/
int runBuiltInCommand(int builtInCommandNum, int pipe_fd[])
{
if (builtInCommandNum == exitNum) {
return 1;
}
if (builtInCommandNum == cdNum) {
iaeCdParent(pipe_fd);
}
return 0;
}
/**
* ビルトインコマンド判定(子プロセス)
**/
void builtInCommandJudge(char command[], char command_argv[], int pipe_fd[])
{
if (strcmp(command, "exit") == 0) {
_exit(exitNum);
}
if(strcmp(command, "cd") == 0) {
iaeCdChild(command, command_argv, pipe_fd);
}
}
/**
* cdコマンド(親プロセス呼び出し)
**/
static void iaeCdParent(int pipe_fd[])
{
char buf[512] = "";
close(pipe_fd[1]);
read(pipe_fd[0], buf, sizeof buf);
close(pipe_fd[0]);
// 現在のディレクトリを変更
if (chdir(buf) != 0) {
perror("cd");
}
}
/**
* cdコマンド(子プロセス呼び出し)
**/
static void iaeCdChild(char command[], char command_argv[], int pipe_fd[])
{
close(pipe_fd[0]);
write(pipe_fd[1], command_argv, strlen(command_argv));
close(pipe_fd[1]);
_exit(cdNum);
}