Language C on Linux: chdir returns value -1 always

0

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;
}
    
asked by Víctor Martínez Llamas 16.11.2016 в 23:51
source

2 answers

1

This is my test and I have not had any problems.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

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");
    }
}

int main(int argc, char *argv[]){
    internal_cd(argv);
    return 0;
}

Entry:

./a.out /home

Exit:

/home0
Directorio actual: /home
    
answered by 17.11.2016 в 00:47
0

chdir does not need the cd instruction. If what reaches args[1] is something such that cd /home what you have to do is, first, delete the first three characters.

One way to simulate such elimination would be the following:

chdir(args[1]+3));

If it still does not work, check that the string does not have strange characters at the end of the string (such as line breaks).

    
answered by 17.11.2016 в 00:01