0

I am trying to write some code that is supposed to add kernel parameters in the file /etc/default/grub inside this line:

GRUB_CMDLINE_LINUX_DEFAULT="quiet pci=noaer resume=UUID=57661f93-9206-48ba-95b4-68fd86519872 loglevel=3 audit=0 amdgpu.ppfeaturemask=0xffffffff"

The kernel parameters is supposed to go inside the quotes (""s). My attempt at doing it was to replace the last " with

the kernel parameter" \n\0

However, that is not working well for me and I get all sorts of formatting issues with my code. I am very new to C so I might not know how to do simple things in C. Here is my code below:

void add_grub_param(char param[100]){
    // Code to add a grub_parameter
    FILE * grub_config = fopen("/etc/default/grub","r");
    FILE * grub_temp = fopen("grub-temp","w");

    char singleLine [5000];
    while(fgets(singleLine, 5000, grub_config)){
        if (strstr(singleLine, "GRUB_CMDLINE_LINUX_DEFAULT=") != NULL){
            if (!strstr(singleLine, param)){
                // I can use a boolean that gets trigerred in the first occurance of "
                // In the second appearance of " , we can replace it with the text
                char newLine[6000];
                
                char textToAdd[150];
                strcpy(textToAdd, " ");
                strcat(textToAdd, param);
                strcat(textToAdd, "\"");
                printf(textToAdd);
                int counter = 0;
                bool first_occurence = false;
                while (true){
                    if (singleLine[counter] == '\"'){
                        if (!first_occurence){
                            first_occurence = true;
                        } else{
                            strcat(newLine, textToAdd);
                            fputs(newLine, grub_temp);
                            break;
                        }
                    }
                    newLine[counter] = singleLine[counter];
                    counter ++;
                }
            } else {
                fputs(singleLine, grub_temp);
            }
        }
        else
            {
                fputs(singleLine, grub_temp);
        }
    }
    fclose(grub_config);
    fclose(grub_temp);
    system("sudo mv grub-temp /etc/default/grub");
    printf("GRUB config file successfully generated. \n");
}

Any insight on the problem would be greatly appreciated. Thank you.

  • 1
    You will want to look at [**Why is while ( !feof (file) ) always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) Get rid of it and use `while (fgets(singleLine, 5000, grub_config)) { ... }` Why are you not simply using `sed` for this? – David C. Rankin Dec 28 '20 at 01:46
  • Thanks. Getting rid of that ended a lot of the formatting issues I was having. Now the only problem I have is an extra " appearing both before and after the parameters as opposed to just after. – Ansh Srivastava Dec 28 '20 at 01:55
  • I will take a look at sed too. – Ansh Srivastava Dec 28 '20 at 01:55
  • Also note in what you write to the file, there is no need for a `'\0'` at the end. (in `char ending[10]="\"\0";`) If you need to append a double quote, `strcat(textToAdd, "\"");` will do. So long as you are concatenating strings with `strcat()`, and have sufficient storage in the destination, the strings will be nul-terminated (otherwise you invoke *Undefined Behavior*) – David C. Rankin Dec 28 '20 at 01:57
  • Yeah. I made that change too. I was just organizing my thoughts while writing the code. – Ansh Srivastava Dec 28 '20 at 02:07
  • With C, I usually end up `popen()`ing an `ed` session and sending commands to it to edit files instead of trying to do it myself. – Shawn Dec 28 '20 at 10:25
  • Thank you all for the valuable feedback. The problem has been solved. It wouldn't be possible without all the help. – Ansh Srivastava Dec 31 '20 at 16:38

0 Answers0