Example #1
0
int proc_color(char *s, char *t, int ind)
{
	int result; int errornum;
	char holder[SHORTLEN];
	char err[MAX_ERR_LENGTH+1];
	regmatch_t pmatch[4];
	regex_t regone;
	char *colpat = 
		"\\([0-9XE][0-9XE]*\\),\\([0-9XE][0-9XE]*\\),\\([0-9XE][0-9XE]*\\)";

	holder[0] = '\0';
	lower_str(s);
	if (strstr(s,"red")) {
		allopts[ind].r = 1000; allopts[ind].g = 0; allopts[ind].b = 0;
	} else if (strstr(s,"blue")) {
		allopts[ind].r = 0; allopts[ind].g = 0; allopts[ind].b = 1000;
	} else if (strstr(s,"green")) {
		allopts[ind].r = 0; allopts[ind].g = 1000; allopts[ind].b = 0;
	} else if (strstr(s,"yellow")) {
		allopts[ind].r = 1000; allopts[ind].g = 1000; allopts[ind].b = 0;
	} else if (strstr(s,"magenta")) {
		allopts[ind].r = 1000; allopts[ind].g = 0; allopts[ind].b = 1000;
	} else if (strstr(s,"cyan")) {
		allopts[ind].r = 0; allopts[ind].g = 1000; allopts[ind].b = 1000;
	} else if (strstr(s,"black")) {
		allopts[ind].r = 0; allopts[ind].g = 0; allopts[ind].b = 0;
	} else if (strstr(s,"white")) {
		allopts[ind].r = 1000; allopts[ind].g = 1000; allopts[ind].b = 1000;
	} else {
		upper_str(s);
		if ((errornum = regcomp(&regone,colpat,0)) != 0) {
			regerror(errornum,&regone,err,MAX_ERR_LENGTH);
			return -1;
		}
		result = regexec(&regone,s,4,pmatch,0);
		regfree(&regone);
		if (result != 0) {		/* no match */
			if (strstr(t,"FORE")) {
				allopts[ind].r = 1000; allopts[ind].g = 1000;
				allopts[ind].b = 1000;
			} else {
				allopts[ind].r = 0; allopts[ind].g = 0;
				allopts[ind].b = 0;
			}
		} else {						/* match */
			sprintf(holder,"%.*s",pmatch[1].rm_eo - pmatch[1].rm_so, 
				s+pmatch[1].rm_so);
			allopts[ind].r = (int) doztodec(holder);
			holder[0] = '\0';
			sprintf(holder,"%.*s",pmatch[2].rm_eo - pmatch[2].rm_so, 
				s+pmatch[2].rm_so);
			allopts[ind].g = (int) doztodec(holder);
			holder[0] = '\0';
			sprintf(holder,"%.*s",pmatch[3].rm_eo - pmatch[3].rm_so, 
				s+pmatch[3].rm_so);
			allopts[ind].b = (int) doztodec(holder);
		}
	}
	return 0;
}
Example #2
0
std::string ToLower(const std::string& str) {
  std::string lower_str(str.size(), ' ');
  std::transform(str.begin(), str.end(), lower_str.begin(), tolower);
  return lower_str;
}
Example #3
0
int case_insensitive_strcmp(char str1[], char str2[]) {
    lower_str(str1);
    lower_str(str2);
    return strcmp(str1, str2);
}
Example #4
0
int main()
{
	//if the compiler supports c++ 2011 standard, std::unordered_set container is perfect for this scenario due to  
	// the requirement that the replacements should be written in the order of their appearance in the dictionary
	std::vector<ITEM> dict;
	int sequence;
	//A terminating null character is automatically added at the end of the stored sequence.
	char word[16];
	while(1)
	{
		scanf("%s", word);
		sequence++;
		if(word[0] != '#')
		{
			ITEM tmp_item;
			tmp_item.m_word = word;
			tmp_item.m_sequence_num = sequence;
			dict.push_back(tmp_item);
		}
		else
			break;
	}

	std::sort(dict.begin(), dict.end());
	while(1)
	{
		scanf("%s", word);
		if(word[0] != '#')
		{
			printf("%s", word);
			bool is_correct = false;
			ITEM equal_item;
			equal_item.m_word = word;
			std::pair<std::vector<ITEM>::iterator,std::vector<ITEM>::iterator> bounds = std::equal_range(dict.begin(), dict.end(), equal_item);
			for(std::vector<ITEM>::iterator equal = bounds.first; equal != bounds.second; ++equal)
			{
				if(equal->m_word == word )
				{
					printf(" is correct");
					is_correct = true;
					break;
				}
			}
			if(!is_correct)
			{
				printf(":");
				std::string lower_str(strlen(word) -1, 'x');
				std::string upper_str(strlen(word) +1, 'x');
				ITEM lower_item, upper_item;
				lower_item.m_word = lower_str;
				upper_item.m_word = upper_str;
				std::vector<ITEM>::iterator low, up;
				low = std::lower_bound(dict.begin(), dict.end(), lower_item);
				up = std::upper_bound(dict.begin(), dict.end(), upper_item);

				std::vector<ITEM> similar_words;
				for(low; low != up; ++low)
				{
					if(!similarity(low->m_word.c_str(), word) )
						similar_words.push_back(*low);
				}
				std::sort(similar_words.begin(), similar_words.end(), comp);
				for(int i = 0; i < similar_words.size(); ++i)
					printf(" %s", similar_words[i].m_word.c_str());
			}
			printf("\n");
		}
		else
			break;
	}
	return 0;
}
Example #5
0
int fix_grub2_inner(lickdir_t *lick, grub2_fix_function function, char original_drive) {
    char drive = original_drive;
    if(!drive) {
        drive = mount_uefi_partition();
        if(!drive) {
            lick->err = strdup2("Could not mount UEFI partition.");
            return 0;
        }
    }

    char *lick_grub = strdup2("?:/efi/lick/" GRUB2_EFI);
    char *lick_shim = strdup2("?:/efi/lick/shim.efi");
    char *lick_mokmanager = strdup2("?:/efi/lick/" MOKMANAGER_EFI);
    char *boot_grub = strdup2("?:/efi/boot/" GRUB2_EFI);
    char *boot_shim = strdup2("?:/efi/boot/shim.efi");
    char *boot_mokmanager = strdup2("?:/efi/boot/" MOKMANAGER_EFI);
    char *boot_file = strdup("?:/efi/boot/bootx64.efi");
    char *boot_file_backup = strdup2("?:/efi/boot/bootx64-orig.efi");
    char *ms_loader = strdup2("?:/efi/microsoft/boot/bootmgfw.efi");
    char *ms_loader_backup = strdup2("?:/efi/microsoft/boot/bootmgfw-backup.efi");
    char *grub_cfg = unix_path(concat_strs(2, lick->drive, "/lickgrub.cfg"));
    char *grub_menu = NULL;

    lick_grub[0] = drive;
    lick_shim[0] = drive;
    lick_mokmanager[0] = drive;
    boot_grub[0] = drive;
    boot_shim[0] = drive;
    boot_mokmanager[0] = drive;
    ms_loader[0] = drive;
    ms_loader_backup[0] = drive;
    boot_file[0] = drive;
    boot_file_backup[0] = drive;

    const char *fatal_warning =
        "WARNING! The fix has only been half applied.\n"
        "The system may not boot when rebooted.\n"
        "Find out why renaming files on the UEFI partition is not working,\n"
        "then move `/EFI/Microsoft` to `/EFI/Microsoft-backup`.";

    grub2_fix_status status = GRUB2_FIX_UNINSTALLED;

    if(path_exists(boot_grub) || path_exists(boot_mokmanager)
            || path_exists(ms_loader_backup)
            || path_exists(boot_file_backup)) {
        if(path_exists(boot_grub) && path_exists(boot_mokmanager)
                && path_exists(ms_loader_backup)
                && path_exists(boot_file_backup))
            status = GRUB2_FIX_INSTALLED;
        else
            status = GRUB2_FIX_PARTLY_INSTALLED;
    }

    int ret = 0;

    if(function == GRUB2_FIX_CHECK) {
        if(status == GRUB2_FIX_INSTALLED)
            ret = 1;
    } else if(status == GRUB2_FIX_PARTLY_INSTALLED)
        lick->err = strdup2("Partial boot fix applied.");
    else if(function == GRUB2_FIX_INSTALL && status == GRUB2_FIX_INSTALLED)
        lick->err = strdup2("Boot fix already applied!");
    else if(function == GRUB2_FIX_UNINSTALL && status == GRUB2_FIX_UNINSTALLED)
        ret = 1;
    else if(function == GRUB2_FIX_INSTALL) {
        /* Steps:
         * 1) Copy `/EFI/LICK/{grubx64,shim,MokManager}.efi` to `/EFI/Boot/`
         * 2) Rename `/EFI/Boot/bootx64.efi` to `bootx64-orig.efi`
         * 3) Rename `/EFI/Boot/shim.efi` to `bootx64.efi`
         * 4) Rename `/EFI/Microsoft/Boot/bootmgfw.efi` to `bootmgfw-backup.efi`
         */
        do {
            int fail = 1;
            do {
                if(!copy_file(boot_grub, lick_grub))
                    break;
                if(!copy_file(boot_shim, lick_shim))
                    break;
                if(!copy_file(boot_mokmanager, lick_mokmanager))
                    break;
                if(!copy_file(boot_file_backup, boot_file))
                    break;

                attrib_t boot_attrs = attrib_open(boot_file);
                if(!replace_file(boot_file, boot_shim)) {
                    attrib_save(boot_file, boot_attrs);
                    lick->err = strdup2("Could not overwrite boot file.");
                    break;
                }
                if(!rename_file(ms_loader_backup, ms_loader)) {
                    // Try to restore the backup.
                    if(!replace_file(boot_file, boot_file_backup)) {
                        // At this point we are stuck with the fix being
                        // half applied. Realistically, this should never occur.
                        attrib_save(boot_file, boot_attrs);
                        lick->err = strdup2(fatal_warning);
                        fail = 0;
                        break;
                    }
                    lick->err = strdup2("Could not rename directory.");
                    break;
                }

                attrib_save(boot_file_backup, boot_attrs);
                fail = 0;
            } while(0);

            if(fail) {
                unlink_file(boot_grub);
                unlink_file(boot_shim);
                unlink_file(boot_mokmanager);
                unlink_file(boot_file_backup);
                if(!lick->err)
                    lick->err = strdup2("Could not copy files on EFI partition.");
                break;
            }

            // Edit grub menu.
            FILE *f = fopen(grub_cfg, "r");
            if(!f) {
                if(!lick->err)
                    lick->err = strdup2("Successfully installed, but could not modify configuration file.");
                break;
            }
            grub_menu = file_to_str(f);
            fclose(f);
            char *grub_menu_lower = lower_str(strdup2(grub_menu));

            // Search for `/efi/microsoft` (case insensitive)
            // and replace with `/efi/microsoft-backup`
            // First, find the number of `/efi/microsoft`
            size_t cnt = 0;
            const char *str = grub_menu_lower;
            const char *needle = "/efi/microsoft/boot/bootmgfw.efi";
            const char *replacement = "/efi/microsoft/boot/bootmgfw-backup.efi";
            for(;;) {
                str = strstr(str, needle);
                if(!str)
                    break;
                ++cnt;
                ++str; // Increment string to find the next occurrance.
            }

            if(cnt > 0) {
                /* Here, there are 3 strings:
                 * - The original menu, grub_menu.
                 *   This is pointed to by start and str_normal.
                 * - The lowercase menu, grub_menu_lower.
                 *   This is pointed to by str.
                 * - The new menu, new_grub_menu.
                 *   This is pointed to by dst.
                 */
                size_t newsize = strlen(grub_menu)
                    + cnt * (strlen(replacement) - strlen(needle))
                    + 1;
                char *new_grub_menu = malloc(newsize);
                char *dst = new_grub_menu;

                const char *start = grub_menu;
                str = grub_menu_lower;
                for(size_t i = 0; i < cnt; ++i) {
                    str = strstr(str, needle);
                    const char *str_normal = str - grub_menu_lower + grub_menu;
                    // Print everything from start to str.
                    size_t len = str_normal - start;
                    memcpy(dst, start, len);
                    dst += len;
                    strcpy(dst, replacement);
                    str += strlen(needle);
                    dst += strlen(replacement);

                    start = str_normal + strlen(needle);
                }
                strcpy(dst, start);
                grub_menu[newsize - 1] = '\0';

                f = fopen(grub_cfg, "w");
                if(!f) {
                    if(!lick->err)
                        lick->err = strdup2("Successfully installed, but could not modify configuration file.");
                    free(new_grub_menu);
                    free(grub_menu_lower);
                    break;
                }
                fprintf(f, "%s", new_grub_menu);
                fclose(f);
                free(new_grub_menu);
                free(grub_menu_lower);
            }
            ret = 1;
        } while(0);
    } else {
        /* Steps:
         * 1) Rename `/EFI/Microsoft/Boot/bootmgfw-backup.efi` to `bootmgfw.efi`
         * 2) Rename `/EFI/Boot/bootx64-orig.efi` to `bootx64.efi`
         * 3) Delete `/EFI/Boot/{grubx64,MokManager}.efi`
         */
        do {
            if(!rename_file(ms_loader, ms_loader_backup)) {
                lick->err = strdup2("Could not rename directory.");
                break;
            }
            attrib_t boot_attrs = attrib_open(boot_file_backup);
            if(!replace_file(boot_file, boot_file_backup)) {
                attrib_save(boot_file_backup, boot_attrs);
                if(!rename_file(ms_loader_backup, ms_loader)) {
                    lick->err = strdup2(fatal_warning);
                    break;
                }
                lick->err = strdup2("Could not replace boot file.");
                break;
            }
            attrib_save(boot_file, boot_attrs);

            unlink_file(boot_grub);
            unlink_file(boot_mokmanager);
            ret = 1;
        } while(0);
    }

    if(!original_drive)
        unmount_uefi_partition(drive);
    free(lick_grub);
    free(lick_shim);
    free(lick_mokmanager);
    free(boot_grub);
    free(boot_shim);
    free(boot_mokmanager);
    free(ms_loader);
    free(ms_loader_backup);
    free(boot_file);
    free(boot_file_backup);
    free(grub_cfg);
    free(grub_menu);
    return ret;
}