Total members 11663 |It is currently Thu Sep 21, 2017 2:35 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





program to run shell command line functions
shell.cpp
cpp code
#include "myfunctions.cpp"
#include <iostream>
#include <cstdlib>
#include<stdio.h>



using namespace std;

int get_argc(char* line)
{
int count=0;
int i;

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

return count+1;
}
/////////////////////////////////////////////////////////////////////////
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;
}

///////////////////////////////////////////////////

int main()
{
char **cmd;
int my_argc;
do
{
int i=0;
char* line;

prompt();

cin.getline(line,100,'\n');
cmd = get_argv(line);
my_argc = get_argc(line);

if(!strcmp(cmd[0],"cd"))
{

if(my_argc != 2)
cout<<"Usage: cd <dir_name>\n";
else
change_dir(cmd[1]);
}
else if(!strcmp(cmd[0],"mkdir"))
{

if(my_argc != 2)
cout<<"Usage: mkdir <dir_name>\n";
else
make_dir(cmd[1]);
}
else if(!strcmp(cmd[0],"rmdir"))
{
if(my_argc != 2)
cout<<"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)
cout<<"Usage: mv <source> <destination>\n";
else
mv_file(cmd[1],cmd[2]);
}
else if(!strcmp(cmd[0],"cp"))
{

if(my_argc != 3)
cout<<"Usage: cp <source> <destination>\n";
else
cp_file(cmd[1],cmd[2]);
}
else if(!strcmp(cmd[0],"rm"))
{

if(my_argc != 2)
cout<<"Usage: rm <filename>\n";
else
rm_file(cmd[1]);
}
else if(!strcmp(cmd[0],"ps"))
{

}
else if(!strcmp(cmd[0],"kill"))
{
if(my_argc != 2)

cout<<"Usage: kill <pid>\n";

else
//kill_process((pid_t)atoi(cmd[1]));
cout<<"Usage: kill <pid>\n";
}
else if(!strcmp(cmd[0],NULL))
{
continue;
}
else
{

}

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



return 0;
}


myfunctions.h
cpp code
void change_dir(const char*);
void make_dir(const char*);
void remove_dir(const char*);
void ls_dir(const char*);

void mv_file(const char*, const char*);
void cp_file(const char*, const char*);
void rm_file(const char*);


void ps();
void prompt();


myfunctions.cpp
cpp code
#include "myfunctions.h"
#include <unistd.h>
#include <sys/stat.h>
#include <signal.h>
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
using namespace std;

void change_dir(const char* path)
{
if(chdir(path)!=0)

perror("Could't change directory ");
}

void make_dir(const char* path)
{
if(mkdir(path,0777))

perror("Could't create directory ");
}

void ls_dir(const char* path)
{
/*
#define FALSE 0
#define TRUE !FALSE

extern int alphasort();
int count=0;
struct direct **files;
int file_select();


count = scandir(path, &files, file_select, alphasort);

if (count <= 0)
{
cout<<"No files in this directory"<<endl;
exit(0);
}

for (i=1;i<count+1;++i)
printf(``%s '',files[i-1]->d_name);
printf(``n''); // flush buffer
*/
}

void remove_dir(const char* path)
{
if(rmdir(path)!=0)

perror("Coudnot remove directory ");
}

void mv_file(const char* src , const char* dest)
{
if(link(src,dest)!=0)
{
perror("Could't move file ");

return;
}

if(unlink(src)!=0)

perror("File copied successfuly ");
}

void cp_file (const char* src ,const char* dest)
{
if(link(src,dest)!=0)

perror("Could't copy file ");
}

void rm_file(const char* path)
{
if(unlink(path) != 0)

perror("Couldn't remove file: ");
}

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: ");
}*/

void prompt()
{
char buf[256];
getcwd(buf,sizeof(buf));

if(getuid() != 0)

strcat(buf," $ ");
else

strcat(buf," # ");

cout<<buf;
}


/*int file_select(struct direct *entry)

{
if ((strcmp(entry->d_name, ``.'') == 0) || (strcmp(entry->d_name, ``..'') == 0))
return (FALSE);
else
return (TRUE);
}
*/


ls.c
cpp code
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>

#define FALSE 0
#define TRUE !FALSE

extern int alphasort();

char pathname[MAXPATHLEN];

main()
{
int count,i;
struct direct **files;
int file_select();

if (getwd(pathname) == NULL )
{ printf("Error getting pathn");
exit(0);
}
printf("Current Working Directory = %sn",pathname);
count =
scandir(pathname, &files, file_select, alphasort);

/* If no files found, make a non-selectable menu item */
if (count <= 0)
{ printf(``No files in this directoryn'');
exit(0);
}

for (i=1;i<count+1;++i)
printf(``%s '',files[i-1]->d_name);
printf(``n''); /* flush buffer */
}





int file_select(struct direct *entry)

{
if ((strcmp(entry->d_name, ``.'') == 0) || (strcmp(entry->d_name, ``..'') == 0))
return (FALSE);
else
return (TRUE);
}

/*int file_select(struct direct *entry)

{char *ptr;
char *rindex(char *s, char c);

if ((strcmp(entry->d_name, ``.'')== 0) ||
(strcmp(entry->d_name, ``..'') == 0))
return (FALSE);

// Check for filename extensions
ptr = rindex(entry->d_name, '.')
if ((ptr != NULL) &&
((strcmp(ptr, ``.c'') == 0)
(strcmp(ptr, ``.h'') == 0)
(strcmp(ptr, ``.o'') == 0) ))
return (TRUE);
else
return(FALSE);
}
*/





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

  Related Posts  to : program to run shell command line functions
 small-minimal shell for Linux- functions for builtin command     -  
 c program - command line prompt - need help     -  
 How can I call a C program in a Shell Script from Java     -  
 read from command line     -  
 Command Line Parser Code Using C++     -  
 How to implement Command Line Parser in C++ Code     -  
 Command line chat (client and server)     -  
 Reading a File Line by Line in php     -  
 Shell style comments     -  
 Simple code for taking input from shell     -  



Topic Tags

C++ Projects







Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team