I'm having problems performing implementations of some of the shell commands in C.
From my point of view it seems that it is due to an incorrect handling of pointers in C (I have not learned this language for a short time).
At the moment I am trying to implement the directory change and export functions, although the directory change works when I write the path to the directory to which I want to move in the code itself. It does not do it when that path is entered by the user. I'm pretty sure it's because of that args[1]
that happened as an argument. The same happens with the export, although in this case directly from segment violation. I tried to print args [1] as you will see in the code but it gives segment violation when doing that. I would appreciate someone helping me to learn how to do what I want to do correctly.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#define PROMPT "$"
#define MAX_LINE 512
int parse_args(char **args, char *line){
int n=0;
char* token;
char delimit[]=" \t\r\n\v\f";
token=strtok(line,delimit);
while(token!=NULL){
printf("token%i: %s\n",n,token);
args=token;
n++;
args++;
token=strtok(NULL,delimit);
}
printf("token%i: %s\n",n,token);
args=token;
return n;
}
char *read_line(char *line){
printf("%s%s ",getenv("USER"),PROMPT);
fflush(stdout);
line=fgets(line,MAX_LINE,stdin);
return line;
}
int execute_line(char *line){
char **args;
parse_args(args,line);
check_internal(args);
return 0;
}
int check_internal(char **args){
if( strcmp(args, "cd")==0 ){
internal_cd();
} else{
if( strcmp(args, "export")==0 ){
internal_export();
}else{
if( strcmp(args, "source")==0 ){
internal_source();
}else{
if( strcmp(args, "jobs")==0 ){
internal_jobs();
}else{
return 0;
}
}
}
}
}
int internal_cd(char **args){
char buff[50];
printf("Comando cd \n");
char directorio []= "/home/jamengual1/Escritorio/FlashDRIVE";
printf("%s", args+1);
if (chdir(directorio) == -1){
fprintf(stderr, "Error %d: %s\n", errno, strerror(errno));
perror("Error");
return -1;
} else{
printf("Estás en el directorio: %s \n", getcwd(buff, 50));
return 1;
}
}
//así es como tendría que ser pasándole args pero me da Bad Address todo el rato
/*int internal_cd(char **args){
printf("%s","cambio de directorio\n");
char buff[50];
printf("Comando cd \n");
//printf("%s", args[1]); //violación de segmento
if (chdir(args[1]) == -1)
{
//fprintf(stderr, "Error %d: %s\n", errno,strerror(errno));
perror("Error");
return -1;
}
printf("Estás en el directorio: %s \n", getcwd(buff, 50));
return 1;
}
*/
int internal_export(char **args) {
printf("%s","éste es el export\n");
char *variable;
char *nuevo_valor;
char *aux;
variable = strtok(args[1], "=");
nuevo_valor = strtok(NULL, args[1]);
aux = getenv(variable);
if((int)aux == -1)
{
perror("Error: getenv");
return -1;
}
printf("VAR: '%s'. Valor: '%s'. Nuevo valor: '%s'\n", variable, aux, nuevo_valor);
if(!nuevo_valor){
perror("Error: error de sintaxis");
return -1;
}
if ((setenv(variable,nuevo_valor,1)== -1))
{
perror("Error: llamada al sistema con setenv");
return -1;
}
aux = getenv(variable);
if((int)aux == -1)
{
perror("Error: llamada al sistema con getenv");
return -1;
}
printf("Nuevo valor: '%s': '%s'\n", variable, aux);
return 1;
}
int internal_source(char **args) {
printf("%s","éste es el source\n");
return 1;
}
int internal_jobs(char **args){
printf("%s","éste es el jobs\n");
return 1;
}
void main(){
char line[MAX_LINE];
while(read_line(line)){
execute_line(line);
}
}