ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

はじめてのCプログラミングコミュの質問です。

  • mixiチェック
  • このエントリーをはてなブックマークに追加
現在、C言語でシェルを作っているのですがgetenv()を使ってマニュアルでパスを探してコマンドを実行させる事に苦戦してます。
何故か一回目だけは正常に動くのですが、その後は何故かちゃんと動作しません。
どうか助けて頂けないでしょうか?

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <stdbool.h>
#include <signal.h>
#include <string.h>

void ctrlc();
void parseit(char *command, char **params);
void forker(char **params);

int main(char **argv, int argc, char **env)
{
char line[512];
char *params[64];
char *path, buf[512], *p;
struct timeb t1, t2;
int elapsed;

bool timerFlag = false;
while(1)
{
printf("READY: ");
gets(line);
printf("YOU SAID: %s\r\n", line);
ftime(&t1);

signal(SIGINT, ctrlc);

if((strcmp(line, "timer")) == 0){
if(timerFlag == false){
timerFlag = true;
printf("Timer On\r\n");
}
else{
timerFlag = false;
printf("Timer Off\r\n");
}
}
if((strcmp(line, "exit")) == 0)
{
printf("Bye\r\n");
exit(0);
}
if((strcmp(line, "quit")) == 0)
{
printf("Bye\r\n");
exit(0);
}

if((strcmp(line, "timer")) != 0){

//おそらくここから下のコードに問題があると思われます。
path = getenv("PATH");
p = strtok(path, ":");
while(p != NULL){
strcpy(buf, p);
strcat(buf, "/");
strcat(buf, line);
parseit(buf, params);
forker(params);
p = strtok(NULL, ":");
//おそらくここまでが問題部分です。

}
}
ftime(&t2);
elapsed = t2.millitm - t1.millitm;

if((timerFlag == true) && (strcmp(line, "timer")) != 0)
printf("That command took %d milliseconds to run\r\n",
elapsed);

}
return 0;
}

void forker(char **params)
{
int status;
pid_t pid;

if((pid = fork()) < 0){ /*fork a child process */
printf("Fork failed.\r\n");
exit(1);
}
else if(pid == 0){ /* for the child process */
printf("Child Process Start\r\n");
if(execvp(*params, params) < 0){
printf("-bash: %s: command not found\r\n", *params);
exit(1);
}
}
else{
waitpid(-1,&status,0);
}
}


void parseit(char *command, char **params)
{
while(*command != '\0'){
while(*command == ' ' || *command == '\t' || *command == '\n')
*command++ = '\0';
*params++ = command;
while(*command != '\0' && *command != ' ' && *command != '\t'
&& *command != '\n')
command++;
}
*params = '\0';
}

void ctrlc()
{
printf("\r\nBye\r\n");
exit(0);
}

コメント(2)

前回のシェルの質問なのですが、何とか自力で解決できました。
今度はls > testなどの>に対応させようと思っているのですが、そこで躓いています(汗
みなさま助けて頂けるとありがたいです。
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/timeb.h>
#include <stdbool.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#define MAX_DIR_PATH 2048

void ctrlc();
bool fexist(char **params);
void parseit(char *command, char **params);
void forker(char **params);
int befparse(const char *s, const char *deli, char ***argvp);
int ecmd(char **incmd);

bool fs = false;
struct passwd* user;
bool timerFlag = false;
char cwd[MAX_DIR_PATH];
int fflag = 0;
char redirect[20];

int main(char **argv, int argc, char **env)
{
char line[512];
char *params[64];
char **befpar;
int len;
int argnum;
char *path, buf[512], spath[512], *p;
struct timeb t1, t2;
int elapsed;

while(1)
{
signal(SIGINT, ctrlc);

printf("READY: ");
gets(line);
user = getpwuid(getuid());
printf("%s SAID: %s\r\n", user->pw_name, line);

len = strlen(line);
if(line[len-1] == '\n')
line[len-1] = 0;
argnum = befparse(line, " ", &befpar);

if(argnum <= 0)
continue;

ftime(&t1);
if(ecmd(befpar) == -1){
path = getenv("PATH");
strcpy(spath, path);
p = strtok(path, ":");
while(p != NULL){
strcpy(buf, p);
strcat(buf, "/");
strcat(buf, line);
parseit(buf, params);
if(fexist(params)){
forker(params);
break;
}
p = strtok(NULL, ":");
}
}
ftime(&t2);
elapsed = t2.millitm - t1.millitm;

if((timerFlag == true) && (strcmp(line, "timer")) != 0)
printf("That command took %d milliseconds to run\r\n",
elapsed);
setenv("PATH", spath, 1);
}
return 0;
}

void forker(char **params)
{
int status;
pid_t pid;

if(fflag == 1){
int fd = open(redirect, O_WRONLY | O_CREAT , S_IRUSR | S_IWUSR);
printf("shell message: open file %s\r\n", redirect);
close(STDOUT_FILENO);
dup(fd);
close(fd);
fflag = 0;
}
if((pid = fork()) < 0){ /*fork a child process */
printf("Fork failed.\r\n");
exit(1);
}
else if(pid == 0){ /* for the child process */
printf("Child Process Start\r\n");
if(execvp(*params, params) < 0){
printf("-bash: %s: command not found\r\n", *params);
exit(1);
}
}
else{
waitpid(-1,&status,0);
}
}

void parseit(char *command, char **params)
{
while(*command != '\0'){
if(*command == '>'){
fflag = 1;
*command = ' ';
while(*command == ' ') *command++ = '\0';
int i = 0;
for(i = 0; i < 20; i++) redirect[i] = '\0';
int j = 0;
while(*command != ' ' && *command != '\t' && *command != '\n'){
redirect[j] = *command;
*command++ = '\0';
j++;
}
*params++ = command;
redirect[j+1] = '\0';
}
while(*command == ' ' || *command == '\t' || *command == '\n')
*command++ = '\0';
*params++ = command;
while(*command != '\0' && *command != ' ' && *command != '\t'
&& *command != '\n')
command++;
}
*params = '\0';
}

void ctrlc()
{
printf("\r\nBye\r\n");
exit(0);
}

更に続きです。

bool fexist(char **params)
{
struct stat st;
if(stat(*params, &st) == 0)
return true;
else
return false;
}

int befparse(const char *s, const char *deli, char ***argvp)
{
int error, i , numtokens;
const char *snew;
char *t;

if ((s==NULL) || (deli ==NULL) || (argvp == NULL))
{
errno = EINVAL;
return -1;
}
*argvp = NULL;
snew = s +strspn(s,deli);
if ((t=malloc(strlen(snew)+1)) == NULL)
return -1;
strcpy(t,snew);
numtokens = 0;
if(strtok(t,deli) != NULL)
for (numtokens = 1;strtok(NULL,deli) != NULL ; numtokens++);

if ((*argvp = malloc((numtokens +1)*sizeof(char *)))==NULL)
{
error = errno;
free(t);
errno = error;
return -1;
}

if(numtokens ==0)
free(t);
else
{
strcpy(t,snew);
**argvp = strtok(t,deli);
for (i=1; i<numtokens;i++ )
*((*argvp)+i)= strtok(NULL,deli);
}
*((argvp) + numtokens)=NULL;
return numtokens;
}

int ecmd(char **incmd)
{
int forkflag = -1;

if((strcmp(incmd[0], "timer")) == 0){
if(timerFlag == false){
timerFlag = true;
printf("Timer On\r\n");
}
else{
timerFlag = false;
printf("Timer Off\r\n");
}
}

if(strcmp(incmd[0], "cd") == 0){
forkflag = 0;
chdir(incmd[1]);
if(!getcwd(cwd, MAX_DIR_PATH))
printf("Unable to display current directory\n");
else
printf("Move to %s\n", cwd);
}
if(strcmp(incmd[0], "pwd") == 0){
forkflag = 0;
if(!getcwd(cwd, MAX_DIR_PATH))
printf("Unable to display current directory\n");
else
printf("%s\n", cwd);
}

if((strcmp(incmd[0], "exit")) == 0)
{
printf("Bye\r\n");
forkflag = 0;
exit(0);
}
if((strcmp(incmd[0], "quit")) == 0)
{
printf("Bye\r\n");
forkflag = 0;
exit(0);
}
return (forkflag);
}

ログインすると、みんなのコメントがもっと見れるよ

mixiユーザー
ログインしてコメントしよう!

はじめてのCプログラミング 更新情報

はじめてのCプログラミングのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。