Thread: Creating shell in linux

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    1

    Question Creating shell in linux

    This is what so far i did
    Code:
     #include <stdio.h>#include <string.h>
    #include <ctype.h>
    #include <bsd/string.h>
    
    int
    main(void)
    {
        char line[100];
        fgets(line, sizeof line, stdin);    // read in one line
                                            // from the user
    
        char words[3][100];
                            // we are able to store 3 words in
                            // this array, each one 100 characters
                            // in length
                            
        int last_was_space = 1;
        int begin = 0;
        int word = 0;
        for (int i = 0; i < strlen(line); i++) {
            if (last_was_space) {
                begin = i;
            }
            if (isspace(line[i])) {
                strlcpy(words[word], line + begin, i - begin + 1);
                word++;
                last_was_space = 1;
            } else {
                last_was_space = 0;
            }
        }
    
        char *words_p[3];
        for (int i = 0; i < sizeof words / sizeof *words; i++) {
            words_p[i] = words[i];
        }
    
        for (int i = 0; i < word; i++) {
            printf("%s\n", words_p[i]);
        }
        return 0; }

    need help to do this
    • Using the fork(), execvp() and waitpid() system calls, launches the requested program and waits until the program has finished

  2. #2
    Registered User
    Join Date
    Jan 2015
    Posts
    12
    good work so far...but don't know how to do it

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    So essentially you need to start by tokenizing a string. Have you looked at: strtok()
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Jass Preet View Post
    good work so far...but don't know how to do it
    That is because you haven't put any effort in. Why don't you actually try to learn? Start here.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a simple shell using C
    By jniewolak in forum C Programming
    Replies: 3
    Last Post: 03-22-2014, 12:38 AM
  2. Creating a Shell
    By Paul1485 in forum C Programming
    Replies: 7
    Last Post: 10-26-2012, 02:25 PM
  3. Creating a Unix Shell in C
    By Opel_Corsa in forum Linux Programming
    Replies: 2
    Last Post: 10-12-2006, 09:57 AM
  4. Creating shell in C
    By LinMach in forum C Programming
    Replies: 8
    Last Post: 02-19-2003, 07:40 PM
  5. Help creating Mini-shell??
    By Unregistered in forum Linux Programming
    Replies: 4
    Last Post: 02-04-2002, 11:44 PM

Tags for this Thread