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.