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...

  • 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...

  • 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);...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

  • The original code using the gets() function is written below. You need to do (a) change...

    The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...

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
ADVERTISEMENT