Example #1
0
File: tcllib.c Project: Nazg-Gul/fm
/**
 * Prepare command for execution
 *
 * @param __format - format of command
 * @param __file_name - name of file to execute
 * @param __cwd - current working directory
 * @return pointer to a buffer with command to be executed
 * @sideeffect allocate memory for return value
 */
static wchar_t*
prepare_exec_command (const wchar_t *__format, const wchar_t *__file_name,
                      const wchar_t *__cwd)
{
  wchar_t *res, *full_name;

  full_name = wcdircatsubdir (__cwd, __file_name);

  if (wcsstr (__format, L"%n") == NULL && wcsstr (__format, L"%fn") == NULL)
    {
      /* Assume that is there is neither `%n` nor `%fn` */
      /* variable in format string, we should append full name of file */
      /* ate the end of the format string */

      size_t len = wcslen (__format) + wcslen (full_name) + 2;
      res = malloc ((len + 1) * sizeof (wchar_t));

      swprintf (res, len, L"%ls %ls", __format, full_name);
    }
  else
    {
      wchar_t *dummy;

      dummy = wcsrep ((wchar_t*)__format, L"%n", __file_name);

      res   = wcsrep (dummy, L"%fn", full_name);
      SAFE_FREE (dummy);
      dummy = res;

      res   = wcsrep (dummy, L"%cwd", __cwd);
      SAFE_FREE (dummy);
    }

  SAFE_FREE (full_name);

  return res;
}
Example #2
0
BOOL GetSystemName(PWSTR pBuf, SIZE_T cchBuf)
{
    static const VENDOR_LONG_NAME LongNames[] =
    {
        { L"ASUSTeK", L"ASUS" },
        { L"First International Computer", L"FIC" },
        { L"Hewlett-Packard", L"HP" },
        { L"MICRO-STAR", L"MSI" },
        { L"SGI.COM", L"SGI" },
        { L"Silicon Graphics International", L"SGI" },
        { L"Intel(R) Client Systems", L"Intel" },
        { L"InformationComputerSystems", L"ICS" },
        { L"CHUWI INNOVATION AND TECHNOLOGY", L"CHUWI" },
        { L"http://www.abit.com.tw/", L"ABIT" },
        { L"www.abit.com.tw", L"ABIT" },
        { L"CASPER BILGISAYAR SISTEMLERI A.S", L"Casper" },
        { L"Colorful Technology And Development", L"Colorful" },
        { L"Colorful Yu Gong Technology And Development", L"Colorful Yu Gong" },
        { L"HaierComputer", L"Haier" },
        { L"HELIOS BUSINESS COMPUTER", L"HELIOS" },
        { L"Shanghai Zongzhi InfoTech", L"Zongzhi" },
        { L"TSING HUA TONGFANG CO.,LTD", L"TSINGHUA TONGFANG" },
        { L"Yeston Digital Technology Co.,LTD", L"Yeston" },
    };
    static const REDUNDANT_WORD RedundantWords[] =
    {
        { L"Corporation", FALSE },
        { L"Communication", FALSE },
        { L"Computer", FALSE },
        { L"Computers", FALSE },
        { L"Group", FALSE },
        { L"Cloud", FALSE },
        { L"Center", FALSE },
        { L"Systems", FALSE },
        { L"Microsystems", FALSE },
        { L"Infosystems", FALSE },
        { L"Electronics", FALSE },
        { L"Electric", FALSE },
        { L"Software", FALSE },
        { L"Foundation", FALSE },
        { L"International", FALSE },
        { L"Interantonal", FALSE }, // on purpose (some MSI boards)
        { L"INTERANTIONAL", FALSE }, // on purpose (some MSI boards)
        { L"Industrial", FALSE },
        { L"Information", FALSE },
        { L"Informatica", FALSE },
        { L"Technology", FALSE },
        { L"Tecohnology", FALSE }, // on purpose (some Gigabyte boards)
        { L"Technologies", FALSE },
        { L"Tecnologia", FALSE },
        { L"Limited", FALSE },
        { L"Int", FALSE },
        { L"Inc", FALSE },
        { L"Co", FALSE },
        { L"Corp", FALSE },
        { L"Crop", FALSE },
        { L"Ltd", FALSE },
        { L"LTDA", FALSE },
        { L"GmbH", FALSE },
        { L"S.p.A", FALSE },
        { L"A.S.", FALSE },
        { L"S.A", FALSE },
        { L"S.A.S", FALSE },
        { L"S/A", FALSE },
        { L"SA", FALSE },
        { L"SAS", FALSE },
        { L"BV", FALSE },
        { L"AG", FALSE },
        { L"OOO", TRUE },
        { L"CJSC", FALSE },
        { L"INT'L", FALSE },
        { L"plc", FALSE },
    };
    PVOID SMBiosBuf;
    PCHAR DmiStrings[ID_STRINGS_MAX] = { 0 };
    WCHAR ven[512], dev[512];
    CHAR tmpstr[512];
    BOOL bTrimProduct, bTrimFamily, bGenericName, bRemove;
    UINT i;
    PWCHAR j;

    SMBiosBuf = LoadSMBiosData(DmiStrings);
    if (!SMBiosBuf)
    {
        return FALSE;
    }

    TrimNonPrintable(DmiStrings[SYS_VENDOR]);
    bTrimProduct = TrimNonPrintable(DmiStrings[SYS_PRODUCT]);
    TrimNonPrintable(DmiStrings[SYS_VERSION]);
    bTrimFamily = TrimNonPrintable(DmiStrings[SYS_FAMILY]);
    TrimNonPrintable(DmiStrings[BOARD_VENDOR]);
    TrimNonPrintable(DmiStrings[BOARD_NAME]);
    TrimNonPrintable(DmiStrings[BOARD_VERSION]);

    if (bTrimProduct)
    {
        if (DmiStrings[SYS_FAMILY] && !bTrimFamily)
        {
            DmiStrings[SYS_PRODUCT] = DmiStrings[SYS_FAMILY];
            bTrimProduct = FALSE;
        }
    }

    GetSMBiosStringW(DmiStrings[SYS_VENDOR], ven, _countof(ven), TRUE);
    GetSMBiosStringW(DmiStrings[SYS_PRODUCT], dev, _countof(dev), TRUE);
    bGenericName = IsGenericSystemName(ven, dev, NULL) || bTrimProduct;

    if (wcslen(dev) == 0 ||
        !wcscmp(dev, ven) ||
        bGenericName)
    {
        BOOL bGenericVen = FALSE, bGenericDev = (wcslen(dev) == 0 || !wcscmp(dev, ven) || bTrimProduct);

        if (bGenericName && IsGenericSystemName(ven, NULL, &bRemove))
        {
            if (bRemove)
            {
                *ven = 0;
            }
            bGenericVen = TRUE;
        }
        if (bGenericName && IsGenericSystemName(NULL, dev, &bRemove))
        {
            if (bRemove)
            {
                *dev = 0;
            }
            bGenericDev = TRUE;
        }
        // system strings are unusable, use board strings
        if (DmiStrings[BOARD_VENDOR] != NULL || !bGenericName)
        {
            if ((DmiStrings[BOARD_VENDOR] &&
                strlen(DmiStrings[BOARD_VENDOR]) >= 2 &&
                strstr(DmiStrings[BOARD_VENDOR], "  ") != DmiStrings[BOARD_VENDOR]) ||
                bGenericVen)
            {
                GetSMBiosStringW(DmiStrings[BOARD_VENDOR], ven, _countof(ven), TRUE);
            }
            GetSMBiosStringW(DmiStrings[BOARD_NAME], dev, _countof(dev), TRUE);

            if (IsGenericSystemName(ven, NULL, &bRemove) && bRemove)
            {
                *ven = 0;
            }
            if (IsGenericSystemName(NULL, dev, &bRemove) && bRemove)
            {
                *dev = 0;

                if (!bGenericDev)
                {
                    GetSMBiosStringW(DmiStrings[SYS_PRODUCT], dev, _countof(dev), TRUE);
                }
            }
            if (wcslen(dev) == 0 &&
                DmiStrings[SYS_VERSION] != NULL)
            {
                GetSMBiosStringW(DmiStrings[SYS_VERSION], dev, _countof(dev), TRUE);

                if (IsGenericSystemName(NULL, dev, &bRemove) && bRemove)
                {
                    *dev = 0;
                }
            }
            if (wcslen(dev) == 0 &&
                DmiStrings[BOARD_VERSION] != NULL)
            {
                GetSMBiosStringW(DmiStrings[BOARD_VERSION], dev, _countof(dev), TRUE);

                if (IsGenericSystemName(NULL, dev, &bRemove) && bRemove)
                {
                    *dev = 0;
                }
            }
        }

        if (wcslen(ven) == 0 && wcslen(dev) == 0)
        {
            // board strings are empty, use BIOS vendor string
            GetSMBiosStringW(DmiStrings[BIOS_VENDOR], ven, _countof(ven), TRUE);
        }
    }
    else
    {
        if (wcslen(ven) < 2)
        {
            GetSMBiosStringW(DmiStrings[BOARD_VENDOR], ven, _countof(ven), TRUE);

            if (IsGenericSystemName(ven, NULL, &bRemove) && bRemove)
            {
                *ven = 0;
            }
        }
    }

    // workaround for LORD ELECTRONICS
    if (((j = wcsstr(ven, L" ")) != NULL) && (j - ven > 2))
    {
        i = j - ven;
        if (!wcsncmp(ven + wcslen(ven) - i, ven, i))
        {
            ven[wcslen(ven) - i] = L'\0';
        }
    }

    // make vendor strings shorter
    for (i = 0; i < _countof(LongNames); i++)
    {
        if (wcsstr(dev, LongNames[i].pwLongName) == dev)
        {
            // swap ven and dev
            StringCchCopyW(pBuf, cchBuf, ven);
            StringCchCopyW(ven, _countof(ven), dev);
            StringCchCopyW(dev, _countof(dev), pBuf);
        }
        wcsrep(ven, LongNames[i].pwLongName, LongNames[i].pwShortName, TRUE);
    }

    // remove redundant words
    for (i = 0; i < _countof(RedundantWords); i++)
    {
        wcsrep(ven, RedundantWords[i].pwStr, L"", RedundantWords[i].bReplaceFirstWord);
    }
    for (i = 0; i < _countof(RedundantWords); i++)
    {
        StringCchCopyW(pBuf, cchBuf, RedundantWords[i].pwStr);
        StringCchCatW(pBuf, cchBuf, L".");
        wcsrep(ven, pBuf, L"", RedundantWords[i].bReplaceFirstWord);
    }

    // workaround for LENOVO notebooks
    if (!wcsicmp(ven, L"LENOVO"))
    {
        StringCchCopyW(ven, _countof(ven), L"Lenovo");

        if (DmiStrings[SYS_VERSION] != NULL)
        {
            if (!strncmp(DmiStrings[SYS_VERSION], "ThinkPad   ", 11))
            {
                DmiStrings[SYS_VERSION][8] = L'\0';
            }
            if (wcslen(dev) > 0 &&
                (!strcmp(DmiStrings[SYS_VERSION], "IdeaCentre") ||
                !strcmp(DmiStrings[SYS_VERSION], "ThinkPad")))
            {
                DmiStrings[SYS_FAMILY] = DmiStrings[SYS_VERSION];
                DmiStrings[SYS_VERSION] = NULL;
            }
            else
            {
                StringCchCopyA(tmpstr, _countof(tmpstr), DmiStrings[SYS_VERSION]);
                _strupr(tmpstr);
            }
        }

        if (DmiStrings[SYS_VERSION] != NULL &&
            strcmp(tmpstr, " ") &&
            strcmp(tmpstr, "LENOVO") &&
            strstr(tmpstr, "LENOVO   ") == NULL &&
            strstr(tmpstr, "LENOVO PRODUCT") == NULL &&
            strstr(tmpstr, "INVALID") == NULL &&
            strncmp(tmpstr, "   ", 3) &&
            strstr(DmiStrings[SYS_VERSION], "Rev ") == NULL &&
            strstr(DmiStrings[SYS_VERSION], "1.") == NULL &&
            wcsistr(dev, L"System ") == NULL && // includes System x and ThinkSystem
            wcsistr(dev, L"IdeaPad ") == NULL &&
            wcsistr(dev, L"ThinkServer ") == NULL)
        {
            GetSMBiosStringW(DmiStrings[SYS_VERSION], dev, _countof(dev), TRUE);
        }

        if (wcsstr(dev, L"Lenovo-") == dev)
        {
            // replace "-" with space
            dev[6] = L' ';
        }

        if (!wcscmp(dev, L"Lenovo"))
        {
            GetSMBiosStringW(DmiStrings[BOARD_NAME], dev, _countof(dev), TRUE);
        }
    }
    if (!wcscmp(ven, L"IBM") &&
        DmiStrings[SYS_VERSION] != NULL &&
        (strstr(DmiStrings[SYS_VERSION], "ThinkPad ") != NULL ||
         strstr(DmiStrings[SYS_VERSION], "ThinkCentre ") != NULL))
    {
        GetSMBiosStringW(DmiStrings[SYS_VERSION], dev, _countof(dev), TRUE);
    }

    // workaround for DEXP
    if (!wcscmp(ven, L"DEXP"))
    {
        if (DmiStrings[SYS_PRODUCT] != NULL &&
            DmiStrings[SYS_VERSION] != NULL &&
            (!stricmp(DmiStrings[SYS_PRODUCT], "Tablet PC") ||
             !stricmp(DmiStrings[SYS_PRODUCT], "Notebook") ||
             !stricmp(DmiStrings[SYS_PRODUCT], "Decktop")))
        {
            GetSMBiosStringW(DmiStrings[SYS_VERSION], dev, _countof(dev), TRUE);
        }
    }

    // workaround for Razer Blade
    if (!wcscmp(ven, L"Razer") && !wcscmp(dev, L"Blade"))
    {
        if (DmiStrings[SYS_VERSION] != NULL)
        {
            StringCchCopyW(ven, _countof(ven), L"Razer Blade");
            GetSMBiosStringW(DmiStrings[SYS_VERSION], dev, _countof(dev), TRUE);
        }
    }

    // workaround for MSI motherboards
    if (!wcscmp(ven, L"MSI") &&
        wcsstr(dev, L"MS-") != NULL &&
        DmiStrings[BOARD_NAME] != NULL &&
        strstr(DmiStrings[BOARD_NAME], "(MS-") != NULL)
    {
        GetSMBiosStringW(DmiStrings[BOARD_NAME], dev, _countof(dev), TRUE);
    }
    if (wcslen(ven) == 0 &&
        wcsstr(dev, L"MS-") == dev)
    {
        StringCchCopyW(ven, _countof(ven), L"MSI");
    }

    // trim redundant characters
    TrimPunctuation(ven);
    TrimPunctuation(dev);

    if (wcsistr(dev, ven) == dev ||
        (!wcscmp(ven, L"ASUS") && wcsstr(dev, L"ASUS") != NULL) ||
        (!wcscmp(ven, L"HP") && wcsstr(dev, L" by HP") != NULL) ||
        (!wcscmp(ven, L"INTEL") && wcsstr(dev, L" INTEL") != NULL))
    {
        // device string contains vendor string, use second only
        StringCchCopyW(pBuf, cchBuf, dev);
    }
    else
    {
        if (wcslen(ven) > 0 && wcslen(dev) > 0 && (j = wcschr(dev, L' ')))
        {
            // check if vendor string ends with first word of device string
            i = j - dev;
            if (wcslen(ven) > i && !_wcsnicmp(ven + wcslen(ven) - i, dev, i))
            {
                ven[wcslen(ven) - i] = L'\0';
                TrimPunctuation(ven);
            }
        }
        StringCchCopyW(pBuf, cchBuf, ven);
        AppendSystemFamily(pBuf, cchBuf, DmiStrings, dev);
        if (wcslen(pBuf) > 0 && wcslen(dev) > 0)
        {
            StringCchCatW(pBuf, cchBuf, L" ");
        }
        StringCchCatW(pBuf, cchBuf, dev);
    }

    FreeSMBiosData(SMBiosBuf);

    return (wcslen(pBuf) > 0);
}