Question

Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include...

Hello, how can i compile this C code using option -std=c99 or -std=gnu99

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>

static char* args[512];
pid_t pid;
int command_pipe[2];
static void waiting(int n);
static int command(int input, int first, int last)
{
int fd[2];
int flag=0;
pipe( fd );  
pid = fork();

if (pid == 0) {
for(int i=0;args[i]!=0;i++)
{
if(args[i][0]=='>')
{
fd[1]=open(args[i+1], O_CREAT|O_TRUNC|O_WRONLY, 0644);
flag=1;
}
if(args[i]=='>>' )
{
fd[1]=open(args[i+1], O_APPEND|O_WRONLY, 0644);
flag=1;
}
}
if (first == 1 && last == 0 && input == 0) {
//fprintf(stderr, "here\n");
dup2( fd[1], 1 );
} else if (first == 0 && last == 0 && input != 0) {
dup2(input, 0);
dup2(fd[1], 1);
} else {
if(flag==1)
{
dup2(fd[1],1);
}
dup2( input, 0 );
}
if(flag==1)
{
if(strcmp(args[1],">>")==0)
execlp(args[0], args[0], 0, (char *)0);
else if(strcmp(args[1],">")==0)
execlp(args[0], args[0], 0, (char *)0);
else
execlp(args[0], args[0], args[1], (char *)0);
}
else if (execvp( args[0], args) == -1)
exit(1);
}
  

if (input != 0)
close(input);
close(fd[1]);
if (last == 1)
close(fd[0]);
return fd[0];
}

static int run(char* cmd, int input, int first, int last);
static char line[1024];
static int n = 0; /* number of calls to 'command' */

int main()
{
printf("SIMPLE SHELL: Type 'exit' to exit.\n");
while (1) {
/* Print the command prompt */
printf("$$$ ");
fflush(NULL);

/* 0 a command line */
if (!fgets(line, 1024, stdin))
return 0;

int input = 0;
int first = 1;

char* cmd = line;
char* next = strchr(cmd, '|'); /* Find first '|' */

while (next != NULL) {
/* 'next' points to '|' */
*next = '\0';
input = run(cmd, input, first, 0);

cmd = next + 1;
next = strchr(cmd, '|'); /* Find next '|' */
first = 0;
}
input = run(cmd, input, first, 1);
waiting(n);
n = 0;
}
return 0;
}

static void tokenize(char* cmd);

static int run(char* cmd, int input, int first, int last)
{
tokenize(cmd);
if (args[0] != NULL) {
if (strcmp(args[0], "exit") == 0)
exit(0);
n += 1;
return command(input, first, last);
}
return 0;
}

static char* skipspace(char* s)
{
while (isspace(*s)) ++s;
return s;
}

static void tokenize(char* cmd)
{
cmd = skipspace(cmd);
char* next = strchr(cmd, ' ');
int i = 0;

while(next != NULL) {
next[0] = '\0';
args[i] = cmd;
++i;
cmd = skipspace(next + 1);
next = strchr(cmd, ' ');
}

if (cmd[0] != '\0') {
args[i] = cmd;
next = strchr(cmd, '\n');
next[0] = '\0';
++i;
}

args[i] = NULL;
}
static void waiting(int n)
{
int i;
for (i = 0; i < n; ++i)
wait(NULL);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <sys/stat.h>

#include <fcntl.h>

static char *args[512];

pid_t pid;

int command_pipe[2];

static void waiting(int n);

static int command(int input, int first, int last)

{

int fd[2];

int flag = 0;

pipe(fd);

pid = fork();

if (pid == 0)

{

for (int i = 0; args[i] != 0; i++)

{

if (args[i][0] == '>')

{

fd[1] = open(args[i + 1], O_CREAT | O_TRUNC | O_WRONLY, 0644);

flag = 1;

}

if (strcmp(args[i], ">>")==0)

{

fd[1] = open(args[i + 1], O_APPEND | O_WRONLY, 0644);

flag = 1;

}

}

if (first == 1 && last == 0 && input == 0)

{

//fprintf(stderr, "here\n");

dup2(fd[1], 1);

}

else if (first == 0 && last == 0 && input != 0)

{

dup2(input, 0);

dup2(fd[1], 1);

}

else

{

if (flag == 1)

{

dup2(fd[1], 1);

}

dup2(input, 0);

}

if (flag == 1)

{

if (strcmp(args[1], ">>") == 0)

execlp(args[0], args[0], 0, (char *)0);

else if (strcmp(args[1], ">") == 0)

execlp(args[0], args[0], 0, (char *)0);

else

execlp(args[0], args[0], args[1], (char *)0);

}

else if (execvp(args[0], args) == -1)

exit(1);

}

if (input != 0)

close(input);

close(fd[1]);

if (last == 1)

close(fd[0]);

return fd[0];

}

static int run(char *cmd, int input, int first, int last);

static char line[1024];

static int n = 0; /* number of calls to 'command' */

int main()

{

printf("SIMPLE SHELL: Type 'exit' to exit.\n");

while (1)

{

/* Print the command prompt */

printf("$$$ ");

fflush(NULL);

/* 0 a command line */

if (!fgets(line, 1024, stdin))

return 0;

int input = 0;

int first = 1;

char *cmd = line;

char *next = strchr(cmd, '|'); /* Find first '|' */

while (next != NULL)

{

/* 'next' points to '|' */

*next = '\0';

input = run(cmd, input, first, 0);

cmd = next + 1;

next = strchr(cmd, '|'); /* Find next '|' */

first = 0;

}

input = run(cmd, input, first, 1);

waiting(n);

n = 0;

}

return 0;

}

static void tokenize(char *cmd);

static int run(char *cmd, int input, int first, int last)

{

tokenize(cmd);

if (args[0] != NULL)

{

if (strcmp(args[0], "exit") == 0)

exit(0);

n += 1;

return command(input, first, last);

}

return 0;

}

static char *skipspace(char *s)

{

while (isspace(*s))

++s;

return s;

}

static void tokenize(char *cmd)

{

cmd = skipspace(cmd);

char *next = strchr(cmd, ' ');

int i = 0;

while (next != NULL)

{

next[0] = '\0';

args[i] = cmd;

++i;

cmd = skipspace(next + 1);

next = strchr(cmd, ' ');

}

if (cmd[0] != '\0')

{

args[i] = cmd;

next = strchr(cmd, '\n');

next[0] = '\0';

++i;

}

args[i] = NULL;

}

static void waiting(int n)

{

int i;

for (i = 0; i < n; ++i)

wait(NULL);

}

Add a comment
Know the answer?
Add Answer to:
Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also,...

    Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also, include a screen shot of the output and terminal window of each command you used. Read carefully to do this task please. shell.c code given below. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <string.h> int main() { int PID; char lineGot[256]; char *cmd; while (1){ printf("cmd: "); fgets(lineGot, 256, stdin); // Get a string from user (includes \n) cmd = strtok(lineGot, "\n");...

  • Hello, I have this code but its not running like it should: #include <iostream> #include &l...

    Hello, I have this code but its not running like it should: #include <iostream> #include <cstdlib> #include <string> using namespace std; class node { public: typedef int data_t; node *next; data_t data; node(data_t d) { next = NULL; data = d; } }; class linked_list { private: node *head; public: linked_list() { head = NULL; } int size() { node *tptr = head; int c = 0; while (tptr) { c++; tptr = tptr->next; } return c; } void add(int...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • Hello, can someone help me solve the remove function since when I try to remove it...

    Hello, can someone help me solve the remove function since when I try to remove it doesn't work, please and thank you. #include <iostream> #include <cstdlib> #include <string> using namespace std; class node { public:    typedef int data_t;    node *next;    data_t data;    node(data_t d) { next = NULL; data = d; } }; class linked_list { private:    node *head; public:    linked_list()    {        head = NULL;    }    // Get the...

  • C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include...

    C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name    loadMusicFile // @Brief   Load the music database //          'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size; song++){       // Allocate memory for each individual character string       database[song] =...

  • Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n...

    Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n characters from the source to the destination. 3 void mystrncpy( ???) 25 26 27 28 29 11- 30 Find the first occurrance of char acter c within a string. 32 ??? mystrchr???) 34 35 36 37 38 39 / Find the last occurrance of character c within a string. 40 II 41 ??? mystrrchr ???) 42 43 45 int main() char userInput[ 81]; char...

  • rewrite this c code in python #include <iostream> using std::cout; using std::cin; using std::endl; int charClass;...

    rewrite this c code in python #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE...

  • #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage:...

    #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage: * ./wc <words | lines> <file> * * If argv[1] is "words", then you should count the number of words. If it * is "lines", then you should count the number of lines. * * For example: * $ cat a.txt * a b c d * $ ./wc words a.txt * 4 * $ ./wc lines a.txt * 1 * * YOUR PROGRAM...

  • Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

    Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{    char Word[21];    int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() {    struct myWord wordList[20];    char line[100];    printf("Enter an English Sentence:\n");    gets(line);    int size = tokenizeLine(line, wordList);    printf("\n");    printf("Unsorted word list.\n");    printList(wordList, size);...

  • CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...

    CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node {    char c;    Node *next; }; class LinkedString {    Node *head; public:    LinkedString();    LinkedString(char[]);    LinkedString(string);    char charAt(int) const;    string concat(const LinkedString &obj) const;    bool isEmpty() const;    int length() const;    LinkedString substring(int, int) const;    //added helper function to add to linekd list private:    void add(char c); };...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT