LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2004, 02:55 PM   #1
pony85
LQ Newbie
 
Registered: Aug 2004
Location: Bangalore
Distribution: Redhat Linux 9.0
Posts: 25

Rep: Reputation: 15
Linux shell development


Hello everyone

could you please help me out with a project related to operating systems...
i plan to create our own shell in linux.. with a few functions
but dont know how to implement it...
could u please help!!

I know linux, and C and C++ prog
with a course in datastructures,ada
 
Old 08-05-2004, 03:42 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Use the search function up there, looks like building a new shell is a popular project these days !
 
Old 08-05-2004, 09:46 PM   #3
james.zhang
LQ Newbie
 
Registered: Feb 2004
Location: China Hunan
Posts: 9

Rep: Reputation: 0
You can read the source code of linux sys cmd , such as ls , pwd ......
 
Old 08-05-2004, 10:04 PM   #4
james.zhang
LQ Newbie
 
Registered: Feb 2004
Location: China Hunan
Posts: 9

Rep: Reputation: 0
Shell development

It is my mini-shell code.
Code:
/* mshell.c */ 
#include <sys/types.h> 
#include <unistd.h> 
#include <sys/wait.h> 
#include <string.h> 
#include <errno.h> 
#include <stdio.h> 

void do_cd(char *argv[]); 
void execute_new(char *argv[]); 

main() 
{ 
   char *cmd=(void *)malloc(256*sizeof(char)); 
   char *cmd_arg[10]; 
   int cmdlen,i,j,tag; 

   do{ 
      /* init cmd */ 
      for(i=0;i<255;i++) cmd[i]='\0'; 

      printf("|*-=My Shell=-*| "); 
      fgets(cmd,256,stdin); 

      cmdlen=strlen(cmd); 
      cmdlen--; 
      cmd[cmdlen]='\0'; 

      for(i=0;i<10;i++) cmd_arg[i]=NULL; 
      i=0; j=0; tag=0; 
      while(i<cmdlen && j<10){ 
         if(cmd[i]==' '){ 
            cmd[i]='\0'; 
            tag=0; 
         }else{ 
            if(tag==0) 
               cmd_arg[j++]=cmd+i; 
            tag=1; 
         } 
         i++; 
      } 
       
      if(j>=10 && i<cmdlen){ 
         printf("TOO MANY ARGUMENTS\n"); 
         continue; 
      } 
       
      /* cmd quit: exit Mini Shell */ 
      if(strcmp(cmd_arg[0],"quit")==0) 
         break; 

      /* cmd cd */ 
      if(strcmp(cmd_arg[0],"cd")==0){ 
         do_cd(cmd_arg); 
         continue; 
      } 
       
      /* other cmd */ 
      execute_new(cmd_arg); 
   }while(1); 
} 

/* cd cmd */ 
void do_cd(char *argv[]) 
{ 
   if(argv[1]!=NULL){ 
      if(chdir(argv[1])<0) 
         switch(errno){ 
         case ENOENT: 
            printf("DIRECTORY NOT FOUND\n"); 
            break; 
         case ENOTDIR: 
            printf("NOT A DIRECTORY NAME\n"); 
            break; 
         case EACCES: 
            printf("YOU DO NOT HAVE RIGHT TO ACCESS\n"); 
            break; 
         default: 
            printf("SOME ERROR HAPPENED IN CHDIR\n"); 
         } 
   } 

} 

/* other cmd */ 
void execute_new(char *argv[]) 
{ 
   pid_t pid; 

   pid=fork(); 
   if(pid<0){ 
      printf("SOME ERROR HAPPENED IN FORK\n"); 
      exit(2); 
   }else if(pid==0){ 
      if(execvp(argv[0],argv)<0) 
         switch(errno){ 
         case ENOENT: 
               printf("COMMAND OR FILENAME NOT FOUND\n"); 
               break; 
            case EACCES: 
               printf("YOU DO NOT HAVE RIGHT TO ACCESS\n"); 
               break; 
                           default: 
                                   printf("SOME ERROR HAPPENED IN EXEC\n"); 
         } 
         exit(3); 
      }else 
         wait(NULL); 
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ or pure C for Linux kernel module, Linux device driver development. What to use? Igor007 Programming 91 07-19-2014 11:55 AM
Linux Development AndrewB Mandriva 4 03-21-2005 06:04 PM
Overview of Linux development? plnelson Programming 16 10-15-2004 06:57 PM
Linux shell development pony85 Linux - Newbie 2 08-05-2004 03:28 PM
Linux Development p1az Linux - General 1 10-16-2003 11:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration