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-2015, 04:22 AM   #31
Kslkgh
LQ Newbie
 
Registered: Jul 2015
Location: The computer
Distribution: Debian, Kali Linux, Linux Mint, BackTrack, elementary OS
Posts: 26

Original Poster
Rep: Reputation: Disabled

My code is as follows:
Code:
/**
	@file vsh.c
	
	@author George Gibson
	
	@date Wednesday 29th July 2015
	
	@brief vsh Shell Version 1.0 BETA
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>

#define LINESIZE 1024
#define RL_BUFSIZE 1024
#define TOK_BUFSIZE 64
#define TOK_DELIM " \t\r\n\a"

char **split_line(char *line)
{
    int bufsize = TOK_BUFSIZE, position = 0;
    char **tokens = malloc(bufsize * sizeof(char*));
    char *token, **tokens_backup;

    if (!tokens) {
        fprintf(stderr, "vsh: allocation error\n");
        exit(EXIT_FAILURE);
    }
		
    token = strtok(line, TOK_DELIM);
    while (token != NULL) {
        tokens[position] = token;
        position++;
	
        if (position >= bufsize) {
            bufsize += TOK_BUFSIZE;
            tokens_backup = tokens;
            tokens = realloc(tokens, bufsize * sizeof(char*));
            if (!tokens) {
		        free(tokens_backup);
                fprintf(stderr, "vsh: allocation error\n");
                exit(EXIT_FAILURE);
            }
        }
	
        token = strtok(NULL, TOK_DELIM);
    }
    tokens[position] = NULL;
    
	return tokens;
}

int main(int argc, char **argv)
{
	int finish = 0;
	int rc = 0;
	char *user = getenv("USER");
	char hostname[1024];
	char *pwd = getenv("PWD");
    char line[LINESIZE] = {0};
    char lineCopy[LINESIZE] = {0};
    char *command = NULL;
    char **args;
    char *directory;
    
    // Print out a welcome message
    printf("Welcome to vsh\n");
	
	// Print out the message of the day
    system("/bin/cat /etc/motd");
    
    printf("\n");
	
	gethostname(hostname, 1024);
	
    while (!finish)
    {
		// Update 'pwd' variable
		pwd = getenv("PWD");
		// Print out prompt
		printf("[");
        printf(user);
        printf("@");
        printf(hostname);
        printf("] ");
        printf(pwd);
        printf(" $ ");
        fflush(stdout);
        
        if(NULL == fgets(line, sizeof line, stdin))
        {
			// If NULL, leave vsh
            finish = 1;
            printf("\nLeaving vsh\n");
        }
        else
        {
			// If there is something there...
            printf("The command read was s", line);
            printf("\n");
            char *newLine = strchr(line, '\n');
            
            if(newLine != NULL)
            {
                *newLine = '\0';
                strcpy(lineCopy, line);
            }
            
            command = strchr(line, ' ');
            
            if(command != NULL)
            {
                *command++ = '\0';
                printf("Command= _%s_\n\n", line);                
            }
            
            args = split_line(line);
            
            if(strcmp(line, "") == 0) {
				fprintf(stderr, "vsh: Expected command\n");
			}
            else if(strcmp(args[0], "cd") == 0)
            {
				directory = args[1];
				rc = chdir(directory);
				
				if (rc) {
					int ern = errno;

					fprintf(stderr, "*** Error in chdir('%s') errno=%d: %s\n", directory, ern, strerror(ern));
				} 
				else {
					char wd[4096]= "";

					getcwd(wd, sizeof (wd));
					fprintf(stderr, "chdir('%s') succeed; cwd=%s\n", directory, wd);
				}
			}
            else if(strcmp(line,"programmer") == 0)
            {
                printf("The programmer of vsh is George Gibson\n");
            }
            else if(strcmp(line,"ver") == 0)
            {
                printf("The current version is 1.0\n");
            }
            else if(strcmp(line, "help") == 0)
            {
				printf("Help for vsh\n\nType a command to run it. The builtin commands are : cd, programmer, ver, help and exit.Their functions are as follows:\n\ncd: Change Directory. Changes the current working directory.\n\nprogrammer: Display the programmers of vsh.\n\nver: Display the version of vsh you are running.\n\nhelp: Launches this program.\n\nexit: Leave vsh Shell.\n\nThese are the only builtins in vsh, but note that if you define a command in /bin with one of these names, your program will not run in vsh unless you use it's explicit name (/bin/yourprogram) rather than \"yourprogram\". vsh 1.0 written and programmed by George Gibson.\n");
			}
            else if(strcmp(line,"exit") == 0)
			{
				finish = 1;
				printf("\nLeaving vsh...\n");
				getchar();
			}
            else
            {
				// Run the command
                pid_t pid;
				int err;

				pid = fork();
				
				if(pid == -1) {
					perror("vsh");
				}
				else if(pid == 0) {
					execvp(args[0], args);
				    perror("vsh");
				}
				else {
					waitpid(pid, &err, 0);
				}
            }
        }
    }
    
    return 0;
}
 
Old 08-05-2015, 04:56 AM   #32
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Code:
            command = strchr(line, ' ');

            if(command != NULL)
            {
                *command++ = '\0';
                printf("Command= _%s_\n\n", line);                
            }
here you will overwrite line, it will contain exactly the same string as command (therefore argument to cd is now lost). split_line will not be able to parse it.
 
1 members found this post helpful.
Old 08-05-2015, 07:03 AM   #33
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
deleted: I was too slow

Last edited by NevemTeve; 08-05-2015 at 07:04 AM. Reason: deleted: I was too slow;)
 
Old 08-05-2015, 01:48 PM   #34
Kslkgh
LQ Newbie
 
Registered: Jul 2015
Location: The computer
Distribution: Debian, Kali Linux, Linux Mint, BackTrack, elementary OS
Posts: 26

Original Poster
Rep: Reputation: Disabled
Thanks pan64. The code can now be found on GitHub.

Last edited by Kslkgh; 08-07-2015 at 01:24 AM.
 
  


Reply

Tags
shell



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
builtin: not found. when running shell file. Ewais Linux - Software 1 08-04-2013 11:33 AM
Creating a shell using C which could Implement LS command imrrann Linux - Newbie 7 09-07-2010 01:26 AM
Implement a Unix Shell with History Feature vipin_jss Linux - Newbie 2 05-07-2009 07:38 PM
how to implement suspended job handling of a shell using c chrislam Programming 1 09-19-2007 11:43 AM
Shell implement in c SatYr_84 Programming 8 10-06-2005 12:44 AM

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

All times are GMT -5. The time now is 06:21 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