My terminal Program (how to Create a own terminal in c with linux)
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 50
#define COMMAND_LEN 1024
int getHostname()
{
int length;
char *path = NULL;
char sysname[SIZE + 1];
char *user=getenv("USER");
path = getcwd(NULL, 0);
return gethostname(sysname, sizeof sysname) == -1 || printf("\n[%s@%s:~%s]-->", user,sysname,path) < 0 ? 1 : 0;
}
int getPath(char path[],char newpath[])
{
int len=strlen(path),i=3,j=0;
if ( 3 > len)
return 1;
while (path[i])
newpath[j++] = path[i++];
newpath[j++]='\0';
return 0;
}
int main()
{
char command[COMMAND_LEN]={0};
char cd[SIZE]={0};
system("clear");
printf("\n\n\t\t<< << Welcome to my Terminal >> >>\n\n\t\tCreated by Rajesh.S (rajesh@amiindia.co.in)\n\n");
while(1)
{
getHostname();
gets(command);
if (strcasecmp(command,"exit")==0)
break;
if (strcasecmp(command,"su")==0)
{
printf("Permission denied!!!");
continue;
}
if (command[0]=='c' && command[1]=='d')
{
if (1 != getPath(command,cd))
chdir(cd);
}
else
system(command);
}
}
Keep Doing...:)
ReplyDelete