Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-12-2008, 12:38 PM
Exia Exia is offline
Newbie
 
Join Date: Apr 2008
Posts: 2
Rep Power: 0
Exia is on a distinguished road
Default Own shell programming,linux,POSIX

Hello everyone!

My girlfriend gave me a home-made,half-ready shell.
It's code:

Code:
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <wait.h>
#include <ctype.h>

#define LINE_LENGTH 100

typedef void (*my_fork_t)(char **);

int builtin_pid (char **);
int builtin_result (char **);

typedef struct builtins_t 
{
  char *command;
  int (*function)(char **argv);
} builtins_t;

builtins_t builtins[] = {
  { "pid", builtin_pid },
  { "result", builtin_result },
  { NULL, NULL }
};

int last_result = 0;

int
builtin_pid (char **argv)
{
  printf ("%d\n", getpid());
  return 0;
}

int
builtin_result (char **argv)
{
  printf ("%d\n", last_result);
  return 0;
}


int
my_fork (my_fork_t fn, char **argv)
{
  pid_t child;
  child = fork();
  if (child > 0)
    {
      int status;
      while (1)
        {
          if (waitpid (child, &status, 0) == -1)
            {
              perror ("waitpid");
              return -1;
            }
          if (WIFEXITED (status))
            {
              return WEXITSTATUS (status);
            }
          else if (WIFSIGNALED (status))
            {
              fprintf (stderr, "%s", strsignal (WTERMSIG (status)));
              return -1;
            }
        }
    }
  else if (child == 0)
    {
      fn (argv);
      exit (0);
    }
  else
    {
      perror ("fork");
      exit (1);
    }
}

void
run_argv (char **argv)
{
  execvp (argv[0], argv);
  perror (argv[0]);
  exit (1);
}

#define ARGV_LIMIT 50

char **
cmdline_to_argv (char *cmdline)
{
  int i = 0;
  char **cmd_argv = malloc (ARGV_LIMIT * sizeof (char *));
  char *p = cmdline;
  int in_word = 0;
  
  while (*p)
    {
      if (in_word)
        {
          if (isspace (*p))
            {
              *p = 0;
              in_word = 0;
            }
        }
      else
        {
          if (!isspace (*p))
            {
              cmd_argv[i++] = p;
              in_word = 1;
            }
        }
      p++;
    }
  cmd_argv[i] = NULL;
  return cmd_argv;
}

void
free_cmd_argv (char **cmd_argv)
{
  free (cmd_argv);
}

int
run_command (char **argv)
{
  int i = 0;
  while (builtins[i].command)
    {
      if (strcmp(builtins[i].command, argv[0]) == 0)
        {
          return builtins[i].function (argv);
        }
      i++;
    }
  return my_fork (run_argv, argv);
}


int
main (int argc, char **argv)
{
  char cmdline[LINE_LENGTH];

  while (printf ("$ "), fgets (cmdline, LINE_LENGTH, stdin))
    {
      char **cmd_argv = cmdline_to_argv (cmdline);
      if (cmd_argv[0])
        last_result = run_command (cmd_argv);
      free_cmd_argv (cmd_argv);
    }
  return 0;
}
She wants to make the "set" builtin command ,what can set an environment variable to a value,for example:' set APPLE "RINGO" ' sets the APPLE environment variable to the RINGO value.
Unfortunately I'm not too good at POSIX prohramming,so I can't help her.
So please if you don't mind help us.
+She will be very happy if somebody show,how can she made a "get" bulletin, what gives back the value of an environment variable.

Thank you for your help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Shell, using wget and awk? TIGA General Programming 0 04-11-2008 07:31 PM
Job Control in a Shell dayrinni C and C++ 0 09-14-2007 01:59 AM
[PHP] Faking Shell Access Through PHP pranky PHP Tutorials 2 03-29-2007 07:28 AM
Shell: MySQL Database Backup Jordan Tutorials 0 08-15-2006 04:18 PM
Shell Programming Void General Programming 3 07-08-2006 12:47 PM


All times are GMT -5. The time now is 10:57 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads