2015-05-11 4 views
1

Я пишу небольшую оболочку в C как упражнение, чтобы узнать о Linux и C. Теперь я могу выполнять пользовательские команды и команду exit, но я не могу выполнить встроенную команду CD, так как я не знаю, как разделите его на две части (команда cd и имя каталога на CD).Как я могу расширить свою функциональность, чтобы иметь команду cd?

Желаемая функциональность заключается в том, что моя программа должна принимать команду cd с параметром, который является каталогом. Я могу сделать это с помощью аргументов командной строки, но я не знаю, как это сделать в текущей форме. Как это можно сделать?

#include <sys/stat.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 
#include <dirent.h> 
#include <errno.h> 
#include <stdarg.h> 
#include <stdlib.h> 
#include <signal.h> 
#include <sys/types.h> 
#include <sys/wait.h> 

#define BUFFER_LEN 1024 
#define BUFFERSIZE 1024 


int mystrcmp(char const *, char const *); 


void err_syserr(char *fmt, ...) 
{ 
    int errnum = errno; 
    va_list args; 
    va_start(args, fmt); 
    vfprintf(stderr, fmt, args); 
    va_end(args); 
    if (errnum != 0) 
     fprintf(stderr, "(%d: %s)\n", errnum, strerror(errnum)); 
    exit(EXIT_FAILURE); 
} 
int main() { 
    char line[BUFFER_LEN]; 
    char* argv[100];   
    char* path= "/bin/";  
    char progpath[20];  
    int argc;    
    size_t length; 
    char *token; 
    int i=0; 
    int pid; 
    while(1) { 
    i = 0; 
     printf("miniShell>> ");      

     if(!fgets(line, BUFFER_LEN, stdin)) { 
      break;         
     } 
     length = strlen(line); 
     if (line[length - 1] == '\n') { 
      line[length - 1] = '\0'; 
     } 
     if(strcmp(line, "exit")==0) {   
      break; 
     } 
    if(strcmp(line, "cd")==0) {   
      /*printf("change directory to %s\n", argv[2]); 
      chdir(argv[2]);*/ 
     } 

     token = strtok(line," "); 

     while(token!=NULL) { 
      argv[i]=token; 
      token = strtok(NULL," "); 
      i++; 
     } 
     argv[i]=NULL;      

     argc=i;       
     for(i=0; i<argc; i++) { 
      printf("%s\n", argv[i]);  
     } 
     strcpy(progpath, path);   
     strcat(progpath, argv[0]);    

     for(i=0; i<strlen(progpath); i++) { 
      if(progpath[i]=='\n') { 
       progpath[i]='\0'; 
      } 
     } 
     pid= fork();    

     if(pid==0) {    
      execvp(progpath,argv); 
      fprintf(stderr, "Child process could not do execvp\n"); 

     } else {     
      wait(NULL); 
      printf("Child exited\n"); 
     } 

    } 
return (0); 
} 

int mystrcmp(char const *p, char const *q) 
{ 
    int i = 0; 
    for(i = 0; q[i]; i++) 
    { 
     if(p[i] != q[i]) 
      return -1; 
    } 
    return 0; 
} 

int cd(char *pth) { 
    char path[BUFFERSIZE]; 
    char cwd[BUFFERSIZE]; 
    char * return_value; 
    int other_return; 
    strcpy(path,pth); 

    if(pth[0] != '/') 
    { 
     return_value = getcwd(cwd,sizeof(cwd)); 
     strcat(cwd,"/"); 
     strcat(cwd,path); 
     other_return = chdir(cwd); 
    } else { 
     other_return = chdir(pth); 
    } 
    printf("Spawned foreground process: %d\n", getpid()); 
    return 0; 
} 
+0

Что вы действительно скачали? Что вы ожидаете, что у вас есть, и как все иначе? – Eregrith

ответ

0

Я предлагаю проанализировать ваши аргументы с помощью функции getopt от библиотеки unistd. Он также покрыт this question.

Here - это документация для команды getopt.

Смежные вопросы