Total members 11806 |It is currently Mon Nov 18, 2019 3:08 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






Implementing command line shell functions :

.h File:
cpp code
/*	
* Minishell - A minimal shell for Linux
*
* builtins.h - Includes functions for builtin commands
* Created: 18/10/2006
*/

#include <unistd.h> // For pid_t and syscalls
#include <sys/stat.h> // For mkdir()
#include <signal.h> // For kill()
/* -- Directory Operation Commands -- */
void change_dir(const char*); // Implements 'cd' builtin command
void make_dir(const char*); // Implements 'mkdir' builtin command
void remove_dir(const char*); // Implements 'rmdir' builtin command
int list_dir(const char*); // Implements 'ls' builtin command
/* -- ---------------------------- -- */

/* -- File Manipulation Commands -- */
void mv(const char*, const char*); //Implements 'mv' builtin command
void cp(const char*, const char*); //Implements 'cp' builtin command
void rm(const char*); //Implements 'rm' builtin command
/* -- -------------------------- -- */

/* -- Process Mainpulation Commands -- */
void ps(); //Implements 'ps' builtin command
void kill_process(pid_t); //Implements 'kill' builtin
/* -- ----------------------------- -- */


Cpp File :
cpp code
/*
* Minishell - A minimal shell for Linux
* Author:
* builtins.c - Implementation file for builtin commands
*/

#include <stdio.h>
#include "builtins.h"

/* -- Directory Manipulation Commands -- */
void change_dir(const char *path)
{
if(chdir(path) != 0)
perror("Couldn't change directory");
}

void make_dir(const char *path)
{
/*
* Creates with read-write-execute permission for all users
* we need to change that for real-use
*/
if(mkdir(path,0777) != 0)
perror("Couldn't create directory");
}

int list_dir(const char *path)
{
}

void remove_dir(const char* path)
{
if(rmdir(path)!=0)
perror("Couldn't remove directory");
}
/* -- ------------------------------- -- */

/* -- File Manipulation Commands -- */
void mv(const char *src, const char *dest)
{
if(link(src,dest) != 0)
{
perror("Couldn't move file: ");
return;
}

if(unlink(src) != 0)
perror("File copied successfully, however it wasn't moved: ");
}

void cp(const char *src, const char *dest)
{
if(link(src,dest) != 0)
perror("Couldn't copy file: ");
}

void rm(const char *path)
{
if(unlink(path) != 0)
perror("Couldn't remove file: ");
}
/* -- -------------------------- -- */

/* -- Process Mainpulation Commands -- */
void ps()
{
/*struct task_struct *task;

printf("PID\tProcess");
for_each_process(task)
printf("%d\t%s",task->pid,task->comm);*/
}

void kill_process(pid_t pid)
{
//kill a process without sending any special signals
if(kill(pid,0) != 0)
perror("Couldn't kill process: ");
}
/* -- ----------------------------- -- */


Main CPP
cpp code
#include "builtins.h"
#include "helpers.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>

/*
* This function counts spaces within a command string.
* It's need in order to know how much memory to allocate
* for the argv array which represent command line arguments
*/
int get_argc(char *line)
{
int count=0;

for(int i=0;i<strlen(line);i++)
{
if(line[i] == ' ' || line[i] =='\t')
count++;
}

return count+1; //There is always one word after the last space
}

/*
* After a lot of playing with scanf() and getline()
* and suffering with segfaults readline() finally
* saved the day, the only problem with it that the
* lines returned from the function need to be parsed separately
* This function solves the problem, it parses the command string and
* divides it into separate arguments so that we can know what command
* to be executed and with what arguments
*/
char** get_argv(char* line)
{
char** my_argv;
char* save_ptr;
char* temp;
*my_argv = (char*)malloc(get_argc(line)+1);

temp = strtok_r(line,"\t\n ",&save_ptr);
int i=0;
while(temp!=NULL)
{
my_argv[i]=temp;
i++;
temp = strtok_r(NULL,"\t\n ",&save_ptr);
}

my_argv[get_argc(line)+1]=NULL;
return my_argv;
}

/*
* This is main(), could we explain more :p
*/
int main()
{
char **cmd;
int my_argc;
do
{
int i=0;
char *line=NULL;

update_prompt();
/*
* After a lot of playing with scanf() and getline()
* and suffering with segfaults readline() finally
* saved the day, the only problem with it that the
* lines returned from the function need to be parsed

*/
line= readline(NULL);
cmd = get_argv(line);
my_argc = get_argc(line)+1;

if(!strcmp(cmd[0],"cd"))
{
if(my_argc != 2)
printf("Usage: cd <dir_name>\n");
else
change_dir(cmd[1]);
}
else if(!strcmp(cmd[0],"mkdir"))
{
if(my_argc != 2)
printf("Usage: mkdir <dir_name>\n");
else
make_dir(cmd[1]);
}
else if(!strcmp(cmd[0],"rmdir"))
{
if(my_argc != 2)
printf("Usage: rmdir <dir_name>\n");
else
remove_dir(cmd[1]);
}
else if(!strcmp(cmd[0],"ls"))
{
}
else if(!strcmp(cmd[0],"mv"))
{
if(my_argc != 3)
printf("Usage: mv <source> <destination>\n");
else
mv(cmd[1],cmd[2]);
}
else if(!strcmp(cmd[0],"cp"))
{
if(my_argc != 3)
printf("Usage: cp <source> <destination>\n");
else
cp(cmd[1],cmd[2]);
}
else if(!strcmp(cmd[0],"mv"))
{
if(my_argc != 2)
printf("Usage: rm <filename>\n");
else
rm(cmd[1]);
}
else if(!strcmp(cmd[0],"ps"))
{
}
else if(!strcmp(cmd[0],"kill"))
{
if(my_argc != 2)
printf("Usage: kill <pid>\n");
else
kill_process((pid_t)atoi(cmd[1]));
}
else if(!strcmp(cmd[0],NULL))
{
continue;
}
else
{
//fork() and exec() here
}

}while(strcmp(cmd[0],"exit")!=0);

free(*cmd);

return 0;
}


helpers.h
cpp code
#include <stdio.h>
#include <string.h>
#include <unistd.h>

void update_prompt();
//char** parse_command(char *cmd);
//int exec_external(char *cmd);


helpers.cpp
cpp code
#include <unistd.h>
#include <string.h>
#include <stdio.h>

void update_prompt()
{
char buf[256];
getcwd(buf,sizeof(buf));
if(getuid() != 0)
strcat(buf," $ ");
else
strcat(buf," # ");
printf("%s",buf);
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : small-minimal shell for Linux- functions for builtin command
 program to run shell command line functions     -  
 Shell style comments     -  
 How can I call a C program in a Shell Script from Java     -  
 Simple code for taking input from shell     -  
 c++ in linux     -  
 usage of small tag     -  
 Small bug in GreenMiles2     -  
 java on LINUX platform     -  
 Linux Administrator at ENY Ltd, in India     -  
 installing j2ee in linux     -  



Topic Tags

C++ Projects
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com