문제

Is there a way to make code modify itself every time it is run. By this i mean adding new functions and modifying existing functions. For example if i have a c program that runs a specific function that checks to see if a file exists can i modify the program in such a way that during run time i can make it open another file and store this new program permanently.

Example:

void eng_run()
{

    int itr = 0;
    int itr2 = 0;
    int check = 0;
    int conf = 0;
    char arg1[10];
    char arg2[10];
    while(act_arg[itr] != NULL)
    {
        if((strcmp(act_arg[itr],"what") == 0) || (strcmp(act_arg[itr],"how") == 0) || (strcmp(act_arg[itr],"whats") == 0))
        {
            for(itr2 = itr + 1; act_arg[itr2] != NULL ; itr2++)
            {
                if((strcmp(act_arg[itr2],"list") == 0) || (strcmp(act_arg[itr2],"ls") == 0))
                {
                    printf("\E[32mThe \"ls\" system call lists out all the files in the current directory. \n\E[0m");
                    conf++;
                }
}
}
        else if((strcmp(act_arg[itr],"list") == 0) && conf == 0)
        {
            bzero(act_arg,100);
            act_arg[0] = (char *)malloc(sizeof(char) * 100);
            strcat(act_arg[0],"ls");
            check++;
        }
}
}

In the above function act_arg has the command lline arguments parsed beforehand. Now this function responds to questions like :

What does list do? (or) list all files

Now i want it to modify itself during execution if the uses wants it to by adding something like a way to process cp command.Meaning it accepts :

What does cp do? (or) copy fileone to filetwo

Using copy as a keyword.

도움이 되었습니까?

해결책

You can implement an extensible command list without getting into self-modifying code. Simplest is to put the extensions in a data file that is read-in at program start-up. The program can append new entries to the end of this data file as the user enters them.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top