Home > Back-end >  How can you copy a string to non-accessible memory?
How can you copy a string to non-accessible memory?

Time:11-14

char* argv[MAXARGS];
char* buf2=malloc(MAXLINE * sizeof(char));

strcpy(buf2, buf); //buf is string with some words
char* ptr = strtok(buf2, " ");

argv[0]=ptr;
strcpy(argv[0], ptr);

free(buf2);

Like above, I want to copy value of ptr to argv[0] but I can't use strcpy(argv[0],ptr) directly because accessing argv[0] without argv[0]=ptr cause segmentation fault. So I made code like above but then, after I free buf2, argv[0] becomes null. How can I copy ptr to argv without using =ptr in advance?

Code:

#define MAXARGS   128
#define MAXLINE   256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
void eval(char* cmdline);
int parseline(char* buf, char** argv);

int main()
{
    char cmdline[MAXLINE]; /* Command line */
    char* ret;
    while (1) {
        /* Read */
        printf("mini> ");
        ret = fgets(cmdline, MAXLINE, stdin);
        if (feof(stdin) || ret == NULL)
            exit(0);

        /* Evaluate */
        eval(cmdline);
    }
}

void eval(char* cmdline)
{
    char** argv=malloc(MAXARGS*sizeof(char)); /* Argument list execve() */
    char buf[MAXLINE];   /* Holds modified command line */
    int bg;              /* Should the job run in bg or fg? */
    pid_t pid;           /* Process id */

    strcpy(buf, cmdline);
    bg = parseline(buf, argv);
    free(argv);
}

int parseline(char* buf, char** argv)
{
    int argc;            /* Number of args */
    int bg;              /* Background job? */
    char* buf2=malloc(MAXLINE * sizeof(char));
    while (*buf && (*buf == ' '))
        buf  ;
    buf[strlen(buf) - 1] = ' ';/* Replace trailing '\n' with space */
    strcpy(buf2, buf);

    /* Build the argv list */
    argc = 0;
    char* ptr = strtok(buf2, " ");
    printf("ptr: %s\n", ptr);
    while (ptr != NULL) {
        //argv[argc]=ptr;
        strcpy(argv[argc  ], ptr);
        ptr = strtok(NULL, " ");
    }
    argv[argc] = NULL;
    printf("0: %s\n", argv[0]);

    /* Ignore blank line */
    if (argc == 0)
        return 1;

    /* Should the job run in the background? */
    if ((bg = (*argv[argc - 1] == '&')) != 0)
        argv[--argc] = NULL;
    free(buf2);
    printf("0: %s\n", argv[0]);
    if(argv[1]!=NULL)
        printf("1: %s\n", argv[1]);
    return bg;
}

CodePudding user response:

Many errors in your code - I will not check everything only your issue.

  1. Wrong allocation:
char** argv=malloc(MAXARGS*sizeof(char));

You need to allocate space for char pointers - you allocate for char. It is better to use objects instead of types.

char **argv=malloc(MAXARGS * sizeof(*argv));

Now you have allocated the memory for pointers - but not for char arrays to accommodate the strings. To directly copy to argv[n] you need to allocate this memory:

argv[n] = malloc(sizeof(**argv) * (strlen(ptr) 1));
if(argv[n]) strcpy(argv[n], ptr);

In your code you never check the result of malloc - you need to do it after every allocation/reallocation

  • Related