Skip to content
Snippets Groups Projects
Commit 2c9f9573 authored by Alexandru's avatar Alexandru
Browse files

Tema4 update words

parent b625baf5
No related branches found
No related tags found
No related merge requests found
Pipeline #50367 passed
......@@ -61,19 +61,10 @@ static int parse_simple(simple_command_t *s, int level, command_t *father)
// in final returnez ceva (0 sau 1) si trec la urmatoarea comanda
if (s->verb && s->verb->next_part && !strcmp(s->verb->next_part->string, "=")) {
word_t *p = s->verb->next_part->next_part;
char str[300], *strc;
while (p) {
strc = str;
if (p->expand)
snprintf(str, sizeof(str), "%s%s", strc, getenv(p->string));
else
snprintf(str, sizeof(str), "%s%s", strc, p->string);
p = p->next_part;
}
char *str = get_word(p);
int ret = setenv(s->verb->string, str, 1);
free(str);
if (ret < 0)
return 1;
return 0;
......@@ -96,6 +87,7 @@ static int parse_simple(simple_command_t *s, int level, command_t *father)
return 1;
if (path[0] == '\0' && strcmp(s->verb->string, "cd"))
return 1;
fclose(fc);
/* TODO: If builtin command, execute the command. */
if (!strcmp(s->verb->string, "true"))
......@@ -109,7 +101,6 @@ static int parse_simple(simple_command_t *s, int level, command_t *father)
return 1;
}
/* TODO: If external command:
* 1. Fork new process
* 2c. Perform redirections in child
......@@ -149,27 +140,13 @@ static int parse_simple(simple_command_t *s, int level, command_t *father)
}
// verific daca iesirea este redirectata
if (s->out) {
char strOut[300], *strcOut;
// consider si cazul in care fisierul de iesire are variabile de mediu
for (word_t *pOut = s->out; pOut; pOut = pOut->next_part) {
if (pOut->expand) {
char *x = NULL;
x = getenv(pOut->string);
if (x) {
strcOut = strOut;
snprintf(strOut, sizeof(strOut), "%s%s", strcOut, x);
}
} else {
strcOut = strOut;
snprintf(strOut, sizeof(strOut), "%s%s", strcOut, pOut->string);
}
}
char *strOut = get_word(s->out);
if (s->io_flags == IO_OUT_APPEND || s->err)
fd_out = open(strOut, O_WRONLY | O_CREAT | O_APPEND, 0644);
else
fd_out = open(strOut, O_WRONLY | O_CREAT | O_TRUNC, 0644);
free(strOut);
if (fd_out < 0)
exit(1);
dup2(fd_out, 1);
......@@ -184,55 +161,41 @@ static int parse_simple(simple_command_t *s, int level, command_t *father)
if (s->err) {
dup2(fd_stderr, 2);
close(fd_err);
close(fd_stderr);
}
if (s->in) {
dup2(fd_stdin, 0);
close(fd_in);
close(fd_stderr);
}
if (s->out) {
dup2(fd_stdout, 1);
close(fd_out);
close(fd_stdout);
}
exit(ret);
}
// execut comanda
char *args[300], *argsc;
int i = 0;
args[i++] = (char *)s->verb->string;
for (word_t *p = s->params; p; p = p->next_part) {
for (word_t *q = p; q; q = q->next_word) {
if (q->expand) {
char *x = NULL;
x = getenv(q->string);
args[i++] = x;
} else {
const char *t = q->string;
if (*t == '\t' && i > 1) {
argsc = args[i - 1];
snprintf(args[i - 1], sizeof(args), "%s%s", argsc, t);
} else {
args[i++] = (char *)t;
}
}
}
}
args[i] = NULL;
execvp(s->verb->string, args);
int i;
char **argv = get_argv(s, &i);
execvp(s->verb->string, argv);
free(argv);
//redirectez stdin, stdout si stderr unde erau initial
if (s->err) {
dup2(fd_stderr, 2);
close(fd_err);
close(fd_stderr);
}
if (s->in) {
dup2(fd_stdin, 0);
close(fd_in);
close(fd_stderr);
}
if (s->out) {
dup2(fd_stdout, 1);
close(fd_out);
close(fd_stdout);
}
if (rc == false)
exit(1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment