The following function receives an array of strings ( **args
) and, specifically, args[1]
would contain the path when executing the line.
This function is part of a program that has a method to read a line from the terminal and another method to divide the line into tokens, the result is stored in the variable args
.
Whatever the path, by executing: cd / home, for example, the resulting int i
always gets the value of "-1" and does not perform the chdir
function that it wanted.
int internal_cd(char **args){
char *directorio=args[1];
printf("%s",directorio);
int i=chdir(directorio);
chdir(directorio);
printf("%d",i);
if(chdir(args[1])==-1) printf("Error en la ruta especificada\n");
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL){
fprintf(stdout, "Directorio actual: %s\n", cwd);
}
else{
perror("getcwd() error");
}
}
@eferion Using the following method I receive a line from the keyboard with the fgets function and after analyzing it I discover that together with the characters that compose it, the last one turns out to be some kind of special character that is what prevented me from doing the chdir. With the later loop I remove that character but I would really like to know why after the fgets I include that character to know if I could save the loop ("patch") that I have included later.
/* read_line imprime el PROMPT y devuelve la linea introducida por teclado*/
char *read_line(char *line){
printf("%s",PROMPT);
//El último carácter de lineAux tras fgets me devuelve un valor especial al final de la linea, en el bucle lo
//eliminaremos.
char *lineAux =(char *)malloc(1000*sizeof(char));
fgets(lineAux,1000,stdin);
int i=strlen(lineAux);
int z=0;
while(z<i-1){
line[z]=lineAux[z];
z++;
}
fflush(stdout);
return line;
}