Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  gfdhdfhgfhgf
Language: C/C++
Code:
//Janine A. Boletic
//Francia Mae Pante
//G051

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <unistd.h>

#define MAX_BUFFER 1024
#define MAX_ARGS 64
#define SEPARATORS " tn"

extern char **environ;
void syserrmsg(char *, char *);

using namespace std;

int main (int argc, char ** argv)
{
    char lbuf[MAX_BUFFER], cbuf[MAX_BUFFER], cwdbuf[MAX_BUFFER], line[200], delim[] = " ,.!;:n";
    char * args[MAX_ARGS];
    char *words;
    char *pointer;
    char *prompt = "Enter Command (parse, environ, dir, clr, quit) =>" ;
    char ** arg;
    int counter = 0, i;


    while (!feof(stdin))
    {
        if(getcwd(cwdbuf, sizeof(cwdbuf)))
            printf(prompt, "%s%s", cwdbuf);
        else
            printf("getcwd ERROR %s",prompt);
        fflush(stdout);


        if (fgets(lbuf, MAX_BUFFER, stdin ))
        {
                arg = args;
                *arg++ = strtok(lbuf,SEPARATORS);
                while ((*arg++ = strtok(NULL,SEPARATORS)));

            if (args[0])
            {
                cbuf[0] = 0;

                if (!strcmp(args[0],"cd"))
                {
                    if (!args[1])
                    {
                        if (getcwd(cwdbuf, sizeof(cwdbuf)))
                            printf("%sn", cwdbuf);
                        else
                            syserrmsg("retrieving cwd",NULL);
                    }

                    else
                    {
                        if (chdir(args[1]))
                        {
                            syserrmsg("change directory failed",NULL);
                        }
                        else
                        {
                            strcpy(cwdbuf,"PWD=");
                            if ((getcwd(&cwdbuf[4], sizeof(cwdbuf)-4)))
                            {
                                if (putenv(strdup(cwdbuf)))
                                    syserrmsg("Change of PWD environment variable failed",NULL);
                            } else
                            {
                                syserrmsg("Retrieving cwd",NULL);
                            }
                        }
                    }
                }
                else if (!strcmp(args[0],"clr"))
                {
                    system ("clear");
                }
                else if (!strcmp(args[0],"dir"))
                {
                    strcpy(cbuf, "ls -al ");
                    if (!args[1])
                        args[1] = ".";
                    strcat(cbuf, args[1]);
                }
                else if (!strcmp(args[0],"environ"))
                {
                    char ** envstr = environ;
                    string val, newval;

                    while (*envstr)
                    {
                        printf("%sn",*envstr++);

                    }
                        printf("Enter environ variable: ");
                        cin >> val;
                        printf("Enter new environ variable: ");
                        cin >> newval;
                        setenv(val.c_str(),newval.c_str(),1);
                }
                else if (!strcmp(args[0],"parse"))
                {
                    counter = 0;
                    printf("Input string: n");
                    gets(line);
                    pointer= line;
                    while ((words = strtok(pointer, delim)) !=NULL)
                {
                    printf("nargv[%d]: %sn", counter, words);
                    counter++;
                    pointer = NULL;
                }
                    printf("nargc = %dnn",counter);
                }
                else if (!strcmp(args[0],"quit"))
                {
                    break;
                }

                else
                {
                    int i = 1;
                    strcpy(cbuf, args[0]);
                    while (args[i]) {
                        strcat(cbuf, " ");
                        strcat(cbuf, args[i++]);
                    }
                }
                if (cbuf[0])
                    system(cbuf);
            }
        }
    }
    return 0;
}
void syserrmsg(char * m1, char * m2)
{
    if (m1)
        fprintf(stderr,"ERROR - %s ", m1);
    if (m2)
        fprintf(stderr,"%s ", m2);
    fprintf(stderr,"; ");
    perror(NULL);
    return;
}


          
          
Comments: