int main()

in programs/zstdcli.c [799:1534]


int main(int argCount, const char* argv[])
{
    int argNb,
        followLinks = 0,
        allowBlockDevices = 0,
        forceStdin = 0,
        forceStdout = 0,
        hasStdout = 0,
        ldmFlag = 0,
        main_pause = 0,
        nbWorkers = 0,
        adapt = 0,
        useRowMatchFinder = 0,
        adaptMin = MINCLEVEL,
        adaptMax = MAXCLEVEL,
        rsyncable = 0,
        nextArgumentsAreFiles = 0,
        operationResult = 0,
        separateFiles = 0,
        setRealTimePrio = 0,
        singleThread = 0,
        defaultLogicalCores = 0,
        showDefaultCParams = 0,
        ultra=0,
        contentSize=1;
    double compressibility = 0.5;
    unsigned bench_nbSeconds = 3;   /* would be better if this value was synchronized from bench */
    size_t blockSize = 0;

    FIO_prefs_t* const prefs = FIO_createPreferences();
    FIO_ctx_t* const fCtx = FIO_createContext();
    zstd_operation_mode operation = zom_compress;
    ZSTD_compressionParameters compressionParams;
    int cLevel = init_cLevel();
    int cLevelLast = MINCLEVEL - 1;  /* lower than minimum */
    unsigned recursive = 0;
    unsigned memLimit = 0;
    FileNamesTable* filenames = UTIL_allocateFileNamesTable((size_t)argCount);  /* argCount >= 1 */
    FileNamesTable* file_of_names = UTIL_allocateFileNamesTable((size_t)argCount);  /* argCount >= 1 */
    const char* programName = argv[0];
    const char* outFileName = NULL;
    const char* outDirName = NULL;
    const char* outMirroredDirName = NULL;
    const char* dictFileName = NULL;
    const char* patchFromDictFileName = NULL;
    const char* suffix = ZSTD_EXTENSION;
    unsigned maxDictSize = g_defaultMaxDictSize;
    unsigned dictID = 0;
    size_t streamSrcSize = 0;
    size_t targetCBlockSize = 0;
    size_t srcSizeHint = 0;
    size_t nbInputFileNames = 0;
    int dictCLevel = g_defaultDictCLevel;
    unsigned dictSelect = g_defaultSelectivityLevel;
#ifndef ZSTD_NODICT
    ZDICT_cover_params_t coverParams = defaultCoverParams();
    ZDICT_fastCover_params_t fastCoverParams = defaultFastCoverParams();
    dictType dict = fastCover;
#endif
#ifndef ZSTD_NOBENCH
    BMK_advancedParams_t benchParams = BMK_initAdvancedParams();
#endif
    ZSTD_paramSwitch_e literalCompressionMode = ZSTD_ps_auto;


    /* init */
    checkLibVersion();
    (void)recursive; (void)cLevelLast;    /* not used when ZSTD_NOBENCH set */
    (void)memLimit;
    assert(argCount >= 1);
    if ((filenames==NULL) || (file_of_names==NULL)) { DISPLAY("zstd: allocation error \n"); exit(1); }
    programName = lastNameFromPath(programName);
#ifdef ZSTD_MULTITHREAD
    nbWorkers = init_nbThreads();
#endif

    /* preset behaviors */
    if (exeNameMatch(programName, ZSTD_ZSTDMT)) nbWorkers=0, singleThread=0;
    if (exeNameMatch(programName, ZSTD_UNZSTD)) operation=zom_decompress;
    if (exeNameMatch(programName, ZSTD_CAT)) { operation=zom_decompress; FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; outFileName=stdoutmark; g_displayLevel=1; }     /* supports multiple formats */
    if (exeNameMatch(programName, ZSTD_ZCAT)) { operation=zom_decompress; FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; outFileName=stdoutmark; g_displayLevel=1; }    /* behave like zcat, also supports multiple formats */
    if (exeNameMatch(programName, ZSTD_GZ)) {   /* behave like gzip */
        suffix = GZ_EXTENSION; FIO_setCompressionType(prefs, FIO_gzipCompression); FIO_setRemoveSrcFile(prefs, 1);
        dictCLevel = cLevel = 6;  /* gzip default is -6 */
    }
    if (exeNameMatch(programName, ZSTD_GUNZIP)) { operation=zom_decompress; FIO_setRemoveSrcFile(prefs, 1); }                                                     /* behave like gunzip, also supports multiple formats */
    if (exeNameMatch(programName, ZSTD_GZCAT)) { operation=zom_decompress; FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; outFileName=stdoutmark; g_displayLevel=1; }   /* behave like gzcat, also supports multiple formats */
    if (exeNameMatch(programName, ZSTD_LZMA)) { suffix = LZMA_EXTENSION; FIO_setCompressionType(prefs, FIO_lzmaCompression); FIO_setRemoveSrcFile(prefs, 1); }    /* behave like lzma */
    if (exeNameMatch(programName, ZSTD_UNLZMA)) { operation=zom_decompress; FIO_setCompressionType(prefs, FIO_lzmaCompression); FIO_setRemoveSrcFile(prefs, 1); } /* behave like unlzma, also supports multiple formats */
    if (exeNameMatch(programName, ZSTD_XZ)) { suffix = XZ_EXTENSION; FIO_setCompressionType(prefs, FIO_xzCompression); FIO_setRemoveSrcFile(prefs, 1); }          /* behave like xz */
    if (exeNameMatch(programName, ZSTD_UNXZ)) { operation=zom_decompress; FIO_setCompressionType(prefs, FIO_xzCompression); FIO_setRemoveSrcFile(prefs, 1); }     /* behave like unxz, also supports multiple formats */
    if (exeNameMatch(programName, ZSTD_LZ4)) { suffix = LZ4_EXTENSION; FIO_setCompressionType(prefs, FIO_lz4Compression); }                                       /* behave like lz4 */
    if (exeNameMatch(programName, ZSTD_UNLZ4)) { operation=zom_decompress; FIO_setCompressionType(prefs, FIO_lz4Compression); }                                   /* behave like unlz4, also supports multiple formats */
    memset(&compressionParams, 0, sizeof(compressionParams));

    /* init crash handler */
    FIO_addAbortHandler();

    /* command switches */
    for (argNb=1; argNb<argCount; argNb++) {
        const char* argument = argv[argNb];
        if (!argument) continue;   /* Protection if argument empty */

        if (nextArgumentsAreFiles) {
            UTIL_refFilename(filenames, argument);
            continue;
        }

        /* "-" means stdin/stdout */
        if (!strcmp(argument, "-")){
            UTIL_refFilename(filenames, stdinmark);
            continue;
        }

        /* Decode commands (note : aggregated commands are allowed) */
        if (argument[0]=='-') {

            if (argument[1]=='-') {
                /* long commands (--long-word) */
                if (!strcmp(argument, "--")) { nextArgumentsAreFiles=1; continue; }   /* only file names allowed from now on */
                if (!strcmp(argument, "--list")) { operation=zom_list; continue; }
                if (!strcmp(argument, "--compress")) { operation=zom_compress; continue; }
                if (!strcmp(argument, "--decompress")) { operation=zom_decompress; continue; }
                if (!strcmp(argument, "--uncompress")) { operation=zom_decompress; continue; }
                if (!strcmp(argument, "--force")) { FIO_overwriteMode(prefs); forceStdin=1; forceStdout=1; followLinks=1; allowBlockDevices=1; continue; }
                if (!strcmp(argument, "--version")) { printVersion(); CLEAN_RETURN(0); }
                if (!strcmp(argument, "--help")) { usage_advanced(programName); CLEAN_RETURN(0); }
                if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; }
                if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
                if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; FIO_setRemoveSrcFile(prefs, 0); g_displayLevel-=(g_displayLevel==2); continue; }
                if (!strcmp(argument, "--ultra")) { ultra=1; continue; }
                if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(prefs, 2); continue; }
                if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(prefs, 0); continue; }
                if (!strcmp(argument, "--sparse")) { FIO_setSparseWrite(prefs, 2); continue; }
                if (!strcmp(argument, "--no-sparse")) { FIO_setSparseWrite(prefs, 0); continue; }
                if (!strcmp(argument, "--test")) { operation=zom_test; continue; }
                if (!strcmp(argument, "--asyncio")) { FIO_setAsyncIOFlag(prefs, 1); continue;}
                if (!strcmp(argument, "--no-asyncio")) { FIO_setAsyncIOFlag(prefs, 0); continue;}
                if (!strcmp(argument, "--train")) { operation=zom_train; if (outFileName==NULL) outFileName=g_defaultDictName; continue; }
                if (!strcmp(argument, "--no-dictID")) { FIO_setDictIDFlag(prefs, 0); continue; }
                if (!strcmp(argument, "--keep")) { FIO_setRemoveSrcFile(prefs, 0); continue; }
                if (!strcmp(argument, "--rm")) { FIO_setRemoveSrcFile(prefs, 1); continue; }
                if (!strcmp(argument, "--priority=rt")) { setRealTimePrio = 1; continue; }
                if (!strcmp(argument, "--show-default-cparams")) { showDefaultCParams = 1; continue; }
                if (!strcmp(argument, "--content-size")) { contentSize = 1; continue; }
                if (!strcmp(argument, "--no-content-size")) { contentSize = 0; continue; }
                if (!strcmp(argument, "--adapt")) { adapt = 1; continue; }
                if (!strcmp(argument, "--no-row-match-finder")) { useRowMatchFinder = 1; continue; }
                if (!strcmp(argument, "--row-match-finder")) { useRowMatchFinder = 2; continue; }
                if (longCommandWArg(&argument, "--adapt=")) { adapt = 1; if (!parseAdaptParameters(argument, &adaptMin, &adaptMax)) { badusage(programName); CLEAN_RETURN(1); } continue; }
                if (!strcmp(argument, "--single-thread")) { nbWorkers = 0; singleThread = 1; continue; }
                if (!strcmp(argument, "--format=zstd")) { suffix = ZSTD_EXTENSION; FIO_setCompressionType(prefs, FIO_zstdCompression); continue; }
#ifdef ZSTD_GZCOMPRESS
                if (!strcmp(argument, "--format=gzip")) { suffix = GZ_EXTENSION; FIO_setCompressionType(prefs, FIO_gzipCompression); continue; }
                if (exeNameMatch(programName, ZSTD_GZ)) {     /* behave like gzip */
                    if (!strcmp(argument, "--best")) { dictCLevel = cLevel = 9; continue; }
                    if (!strcmp(argument, "--no-name")) { /* ignore for now */; continue; }
                }
#endif
#ifdef ZSTD_LZMACOMPRESS
                if (!strcmp(argument, "--format=lzma")) { suffix = LZMA_EXTENSION; FIO_setCompressionType(prefs, FIO_lzmaCompression);  continue; }
                if (!strcmp(argument, "--format=xz")) { suffix = XZ_EXTENSION; FIO_setCompressionType(prefs, FIO_xzCompression);  continue; }
#endif
#ifdef ZSTD_LZ4COMPRESS
                if (!strcmp(argument, "--format=lz4")) { suffix = LZ4_EXTENSION; FIO_setCompressionType(prefs, FIO_lz4Compression);  continue; }
#endif
                if (!strcmp(argument, "--rsyncable")) { rsyncable = 1; continue; }
                if (!strcmp(argument, "--compress-literals")) { literalCompressionMode = ZSTD_ps_enable; continue; }
                if (!strcmp(argument, "--no-compress-literals")) { literalCompressionMode = ZSTD_ps_disable; continue; }
                if (!strcmp(argument, "--no-progress")) { FIO_setProgressSetting(FIO_ps_never); continue; }
                if (!strcmp(argument, "--progress")) { FIO_setProgressSetting(FIO_ps_always); continue; }
                if (!strcmp(argument, "--exclude-compressed")) { FIO_setExcludeCompressedFile(prefs, 1); continue; }

                /* long commands with arguments */
#ifndef ZSTD_NODICT
                if (longCommandWArg(&argument, "--train-cover")) {
                  operation = zom_train;
                  if (outFileName == NULL)
                      outFileName = g_defaultDictName;
                  dict = cover;
                  /* Allow optional arguments following an = */
                  if (*argument == 0) { memset(&coverParams, 0, sizeof(coverParams)); }
                  else if (*argument++ != '=') { badusage(programName); CLEAN_RETURN(1); }
                  else if (!parseCoverParameters(argument, &coverParams)) { badusage(programName); CLEAN_RETURN(1); }
                  continue;
                }
                if (longCommandWArg(&argument, "--train-fastcover")) {
                  operation = zom_train;
                  if (outFileName == NULL)
                      outFileName = g_defaultDictName;
                  dict = fastCover;
                  /* Allow optional arguments following an = */
                  if (*argument == 0) { memset(&fastCoverParams, 0, sizeof(fastCoverParams)); }
                  else if (*argument++ != '=') { badusage(programName); CLEAN_RETURN(1); }
                  else if (!parseFastCoverParameters(argument, &fastCoverParams)) { badusage(programName); CLEAN_RETURN(1); }
                  continue;
                }
                if (longCommandWArg(&argument, "--train-legacy")) {
                  operation = zom_train;
                  if (outFileName == NULL)
                      outFileName = g_defaultDictName;
                  dict = legacy;
                  /* Allow optional arguments following an = */
                  if (*argument == 0) { continue; }
                  else if (*argument++ != '=') { badusage(programName); CLEAN_RETURN(1); }
                  else if (!parseLegacyParameters(argument, &dictSelect)) { badusage(programName); CLEAN_RETURN(1); }
                  continue;
                }
#endif
                if (longCommandWArg(&argument, "--threads")) { NEXT_UINT32(nbWorkers); continue; }
                if (longCommandWArg(&argument, "--memlimit")) { NEXT_UINT32(memLimit); continue; }
                if (longCommandWArg(&argument, "--memory")) { NEXT_UINT32(memLimit); continue; }
                if (longCommandWArg(&argument, "--memlimit-decompress")) { NEXT_UINT32(memLimit); continue; }
                if (longCommandWArg(&argument, "--block-size=")) { blockSize = readSizeTFromChar(&argument); continue; }
                if (longCommandWArg(&argument, "--maxdict")) { NEXT_UINT32(maxDictSize); continue; }
                if (longCommandWArg(&argument, "--dictID")) { NEXT_UINT32(dictID); continue; }
                if (longCommandWArg(&argument, "--zstd=")) { if (!parseCompressionParameters(argument, &compressionParams)) { badusage(programName); CLEAN_RETURN(1); } continue; }
                if (longCommandWArg(&argument, "--stream-size=")) { streamSrcSize = readSizeTFromChar(&argument); continue; }
                if (longCommandWArg(&argument, "--target-compressed-block-size=")) { targetCBlockSize = readSizeTFromChar(&argument); continue; }
                if (longCommandWArg(&argument, "--size-hint=")) { srcSizeHint = readSizeTFromChar(&argument); continue; }
                if (longCommandWArg(&argument, "--output-dir-flat")) { NEXT_FIELD(outDirName); continue; }
                if (longCommandWArg(&argument, "--auto-threads")) {
                    const char* threadDefault = NULL;
                    NEXT_FIELD(threadDefault);
                    if (strcmp(threadDefault, "logical") == 0)
                        defaultLogicalCores = 1;
                    continue;
                }
#ifdef UTIL_HAS_MIRRORFILELIST
                if (longCommandWArg(&argument, "--output-dir-mirror")) { NEXT_FIELD(outMirroredDirName); continue; }
#endif
#ifndef ZSTD_NOTRACE
                if (longCommandWArg(&argument, "--trace")) { char const* traceFile; NEXT_FIELD(traceFile); TRACE_enable(traceFile); continue; }
#endif
                if (longCommandWArg(&argument, "--patch-from")) { NEXT_FIELD(patchFromDictFileName); continue; }
                if (longCommandWArg(&argument, "--long")) {
                    unsigned ldmWindowLog = 0;
                    ldmFlag = 1;
                    /* Parse optional window log */
                    if (*argument == '=') {
                        ++argument;
                        ldmWindowLog = readU32FromChar(&argument);
                    } else if (*argument != 0) {
                        /* Invalid character following --long */
                        badusage(programName);
                        CLEAN_RETURN(1);
                    }
                    /* Only set windowLog if not already set by --zstd */
                    if (compressionParams.windowLog == 0)
                        compressionParams.windowLog = ldmWindowLog;
                    continue;
                }
#ifndef ZSTD_NOCOMPRESS   /* linking ZSTD_minCLevel() requires compression support */
                if (longCommandWArg(&argument, "--fast")) {
                    /* Parse optional acceleration factor */
                    if (*argument == '=') {
                        U32 const maxFast = (U32)-ZSTD_minCLevel();
                        U32 fastLevel;
                        ++argument;
                        fastLevel = readU32FromChar(&argument);
                        if (fastLevel > maxFast) fastLevel = maxFast;
                        if (fastLevel) {
                            dictCLevel = cLevel = -(int)fastLevel;
                        } else {
                            badusage(programName);
                            CLEAN_RETURN(1);
                        }
                    } else if (*argument != 0) {
                        /* Invalid character following --fast */
                        badusage(programName);
                        CLEAN_RETURN(1);
                    } else {
                        cLevel = -1;  /* default for --fast */
                    }
                    continue;
                }
#endif

                if (longCommandWArg(&argument, "--filelist")) {
                    const char* listName;
                    NEXT_FIELD(listName);
                    UTIL_refFilename(file_of_names, listName);
                    continue;
                }

                /* fall-through, will trigger bad_usage() later on */
            }

            argument++;
            while (argument[0]!=0) {

#ifndef ZSTD_NOCOMPRESS
                /* compression Level */
                if ((*argument>='0') && (*argument<='9')) {
                    dictCLevel = cLevel = (int)readU32FromChar(&argument);
                    continue;
                }
#endif

                switch(argument[0])
                {
                    /* Display help */
                case 'V': printVersion(); CLEAN_RETURN(0);   /* Version Only */
                case 'H':
                case 'h': usage_advanced(programName); CLEAN_RETURN(0);

                     /* Compress */
                case 'z': operation=zom_compress; argument++; break;

                     /* Decoding */
                case 'd':
#ifndef ZSTD_NOBENCH
                        benchParams.mode = BMK_decodeOnly;
                        if (operation==zom_bench) { argument++; break; }  /* benchmark decode (hidden option) */
#endif
                        operation=zom_decompress; argument++; break;

                    /* Force stdout, even if stdout==console */
                case 'c': forceStdout=1; outFileName=stdoutmark; FIO_setRemoveSrcFile(prefs, 0); argument++; break;

                    /* do not store filename - gzip compatibility - nothing to do */
                case 'n': argument++; break;

                    /* Use file content as dictionary */
                case 'D': argument++; NEXT_FIELD(dictFileName); break;

                    /* Overwrite */
                case 'f': FIO_overwriteMode(prefs); forceStdin=1; forceStdout=1; followLinks=1; allowBlockDevices=1; argument++; break;

                    /* Verbose mode */
                case 'v': g_displayLevel++; argument++; break;

                    /* Quiet mode */
                case 'q': g_displayLevel--; argument++; break;

                    /* keep source file (default) */
                case 'k': FIO_setRemoveSrcFile(prefs, 0); argument++; break;

                    /* Checksum */
                case 'C': FIO_setChecksumFlag(prefs, 2); argument++; break;

                    /* test compressed file */
                case 't': operation=zom_test; argument++; break;

                    /* destination file name */
                case 'o': argument++; NEXT_FIELD(outFileName); break;

                    /* limit memory */
                case 'M':
                    argument++;
                    memLimit = readU32FromChar(&argument);
                    break;
                case 'l': operation=zom_list; argument++; break;
#ifdef UTIL_HAS_CREATEFILELIST
                    /* recursive */
                case 'r': recursive=1; argument++; break;
#endif

#ifndef ZSTD_NOBENCH
                    /* Benchmark */
                case 'b':
                    operation=zom_bench;
                    argument++;
                    break;

                    /* range bench (benchmark only) */
                case 'e':
                    /* compression Level */
                    argument++;
                    cLevelLast = (int)readU32FromChar(&argument);
                    break;

                    /* Modify Nb Iterations (benchmark only) */
                case 'i':
                    argument++;
                    bench_nbSeconds = readU32FromChar(&argument);
                    break;

                    /* cut input into blocks (benchmark only) */
                case 'B':
                    argument++;
                    blockSize = readU32FromChar(&argument);
                    break;

                    /* benchmark files separately (hidden option) */
                case 'S':
                    argument++;
                    separateFiles = 1;
                    break;

#endif   /* ZSTD_NOBENCH */

                    /* nb of threads (hidden option) */
                case 'T':
                    argument++;
                    nbWorkers = (int)readU32FromChar(&argument);
                    break;

                    /* Dictionary Selection level */
                case 's':
                    argument++;
                    dictSelect = readU32FromChar(&argument);
                    break;

                    /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */
                case 'p': argument++;
#ifndef ZSTD_NOBENCH
                    if ((*argument>='0') && (*argument<='9')) {
                        benchParams.additionalParam = (int)readU32FromChar(&argument);
                    } else
#endif
                        main_pause=1;
                    break;

                    /* Select compressibility of synthetic sample */
                case 'P':
                    argument++;
                    compressibility = (double)readU32FromChar(&argument) / 100;
                    break;

                    /* unknown command */
                default : badusage(programName); CLEAN_RETURN(1);
                }
            }
            continue;
        }   /* if (argument[0]=='-') */

        /* none of the above : add filename to list */
        UTIL_refFilename(filenames, argument);
    }

    /* Welcome message (if verbose) */
    DISPLAYLEVEL(3, WELCOME_MESSAGE);

#ifdef ZSTD_MULTITHREAD
    if ((nbWorkers==0) && (!singleThread)) {
        /* automatically set # workers based on # of reported cpus */
        if (defaultLogicalCores) {
            nbWorkers = UTIL_countLogicalCores();
            DISPLAYLEVEL(3, "Note: %d logical core(s) detected \n", nbWorkers);
        } else {
            nbWorkers = UTIL_countPhysicalCores();
            DISPLAYLEVEL(3, "Note: %d physical core(s) detected \n", nbWorkers);
        }
    }
#else
    (void)singleThread; (void)nbWorkers; (void)defaultLogicalCores;
#endif

    g_utilDisplayLevel = g_displayLevel;

#ifdef UTIL_HAS_CREATEFILELIST
    if (!followLinks) {
        unsigned u, fileNamesNb;
        unsigned const nbFilenames = (unsigned)filenames->tableSize;
        for (u=0, fileNamesNb=0; u<nbFilenames; u++) {
            if ( UTIL_isLink(filenames->fileNames[u])
             && !UTIL_isFIFO(filenames->fileNames[u])
            ) {
                DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring \n", filenames->fileNames[u]);
            } else {
                filenames->fileNames[fileNamesNb++] = filenames->fileNames[u];
        }   }
        if (fileNamesNb == 0 && nbFilenames > 0)  /* all names are eliminated */
            CLEAN_RETURN(1);
        filenames->tableSize = fileNamesNb;
    }   /* if (!followLinks) */

    /* read names from a file */
    if (file_of_names->tableSize) {
        size_t const nbFileLists = file_of_names->tableSize;
        size_t flNb;
        for (flNb=0; flNb < nbFileLists; flNb++) {
            FileNamesTable* const fnt = UTIL_createFileNamesTable_fromFileName(file_of_names->fileNames[flNb]);
            if (fnt==NULL) {
                DISPLAYLEVEL(1, "zstd: error reading %s \n", file_of_names->fileNames[flNb]);
                CLEAN_RETURN(1);
            }
            filenames = UTIL_mergeFileNamesTable(filenames, fnt);
        }
    }

    nbInputFileNames = filenames->tableSize; /* saving number of input files */

    if (recursive) {  /* at this stage, filenameTable is a list of paths, which can contain both files and directories */
        UTIL_expandFNT(&filenames, followLinks);
    }
#else
    (void)followLinks;
#endif

    if (operation == zom_list) {
#ifndef ZSTD_NODECOMPRESS
        int const ret = FIO_listMultipleFiles((unsigned)filenames->tableSize, filenames->fileNames, g_displayLevel);
        CLEAN_RETURN(ret);
#else
        DISPLAY("file information is not supported \n");
        CLEAN_RETURN(1);
#endif
    }

    /* Check if benchmark is selected */
    if (operation==zom_bench) {
#ifndef ZSTD_NOBENCH
        benchParams.blockSize = blockSize;
        benchParams.nbWorkers = nbWorkers;
        benchParams.realTime = (unsigned)setRealTimePrio;
        benchParams.nbSeconds = bench_nbSeconds;
        benchParams.ldmFlag = ldmFlag;
        benchParams.ldmMinMatch = (int)g_ldmMinMatch;
        benchParams.ldmHashLog = (int)g_ldmHashLog;
        benchParams.useRowMatchFinder = useRowMatchFinder;
        if (g_ldmBucketSizeLog != LDM_PARAM_DEFAULT) {
            benchParams.ldmBucketSizeLog = (int)g_ldmBucketSizeLog;
        }
        if (g_ldmHashRateLog != LDM_PARAM_DEFAULT) {
            benchParams.ldmHashRateLog = (int)g_ldmHashRateLog;
        }
        benchParams.literalCompressionMode = literalCompressionMode;

        if (cLevel > ZSTD_maxCLevel()) cLevel = ZSTD_maxCLevel();
        if (cLevelLast > ZSTD_maxCLevel()) cLevelLast = ZSTD_maxCLevel();
        if (cLevelLast < cLevel) cLevelLast = cLevel;
        if (cLevelLast > cLevel)
            DISPLAYLEVEL(3, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast);
        if (filenames->tableSize > 0) {
            if(separateFiles) {
                unsigned i;
                for(i = 0; i < filenames->tableSize; i++) {
                    int c;
                    DISPLAYLEVEL(3, "Benchmarking %s \n", filenames->fileNames[i]);
                    for(c = cLevel; c <= cLevelLast; c++) {
                        BMK_benchFilesAdvanced(&filenames->fileNames[i], 1, dictFileName, c, &compressionParams, g_displayLevel, &benchParams);
                }   }
            } else {
                for(; cLevel <= cLevelLast; cLevel++) {
                    BMK_benchFilesAdvanced(filenames->fileNames, (unsigned)filenames->tableSize, dictFileName, cLevel, &compressionParams, g_displayLevel, &benchParams);
            }   }
        } else {
            for(; cLevel <= cLevelLast; cLevel++) {
                BMK_syntheticTest(cLevel, compressibility, &compressionParams, g_displayLevel, &benchParams);
        }   }

#else
        (void)bench_nbSeconds; (void)blockSize; (void)setRealTimePrio; (void)separateFiles; (void)compressibility;
#endif
        goto _end;
    }

    /* Check if dictionary builder is selected */
    if (operation==zom_train) {
#ifndef ZSTD_NODICT
        ZDICT_params_t zParams;
        zParams.compressionLevel = dictCLevel;
        zParams.notificationLevel = (unsigned)g_displayLevel;
        zParams.dictID = dictID;
        if (dict == cover) {
            int const optimize = !coverParams.k || !coverParams.d;
            coverParams.nbThreads = (unsigned)nbWorkers;
            coverParams.zParams = zParams;
            operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenames->fileNames, (int)filenames->tableSize, blockSize, NULL, &coverParams, NULL, optimize, memLimit);
        } else if (dict == fastCover) {
            int const optimize = !fastCoverParams.k || !fastCoverParams.d;
            fastCoverParams.nbThreads = (unsigned)nbWorkers;
            fastCoverParams.zParams = zParams;
            operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenames->fileNames, (int)filenames->tableSize, blockSize, NULL, NULL, &fastCoverParams, optimize, memLimit);
        } else {
            ZDICT_legacy_params_t dictParams;
            memset(&dictParams, 0, sizeof(dictParams));
            dictParams.selectivityLevel = dictSelect;
            dictParams.zParams = zParams;
            operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenames->fileNames, (int)filenames->tableSize, blockSize, &dictParams, NULL, NULL, 0, memLimit);
        }
#else
        (void)dictCLevel; (void)dictSelect; (void)dictID;  (void)maxDictSize; /* not used when ZSTD_NODICT set */
        DISPLAYLEVEL(1, "training mode not available \n");
        operationResult = 1;
#endif
        goto _end;
    }

#ifndef ZSTD_NODECOMPRESS
    if (operation==zom_test) { FIO_setTestMode(prefs, 1); outFileName=nulmark; FIO_setRemoveSrcFile(prefs, 0); }  /* test mode */
#endif

    /* No input filename ==> use stdin and stdout */
    if (filenames->tableSize == 0) {
      /* It is possible that the input
       was a number of empty directories. In this case
       stdin and stdout should not be used */
       if (nbInputFileNames > 0 ){
        DISPLAYLEVEL(1, "please provide correct input file(s) or non-empty directories -- ignored \n");
        CLEAN_RETURN(0);
       }
       UTIL_refFilename(filenames, stdinmark);
    }

    if (!strcmp(filenames->fileNames[0], stdinmark) && !outFileName)
        outFileName = stdoutmark;  /* when input is stdin, default output is stdout */

    /* Check if input/output defined as console; trigger an error in this case */
    if (!forceStdin
     && !strcmp(filenames->fileNames[0], stdinmark)
     && IS_CONSOLE(stdin) ) {
        DISPLAYLEVEL(1, "stdin is a console, aborting\n");
        CLEAN_RETURN(1);
    }
    if ( outFileName && !strcmp(outFileName, stdoutmark)
      && IS_CONSOLE(stdout)
      && !strcmp(filenames->fileNames[0], stdinmark)
      && !forceStdout
      && operation!=zom_decompress ) {
        DISPLAYLEVEL(1, "stdout is a console, aborting\n");
        CLEAN_RETURN(1);
    }

#ifndef ZSTD_NOCOMPRESS
    /* check compression level limits */
    {   int const maxCLevel = ultra ? ZSTD_maxCLevel() : ZSTDCLI_CLEVEL_MAX;
        if (cLevel > maxCLevel) {
            DISPLAYLEVEL(2, "Warning : compression level higher than max, reduced to %i \n", maxCLevel);
            cLevel = maxCLevel;
    }   }
#endif

    if (showDefaultCParams) {
        if (operation == zom_decompress) {
            DISPLAY("error : can't use --show-default-cparams in decompression mode \n");
            CLEAN_RETURN(1);
        }
    }

    if (dictFileName != NULL && patchFromDictFileName != NULL) {
        DISPLAY("error : can't use -D and --patch-from=# at the same time \n");
        CLEAN_RETURN(1);
    }

    if (patchFromDictFileName != NULL && filenames->tableSize > 1) {
        DISPLAY("error : can't use --patch-from=# on multiple files \n");
        CLEAN_RETURN(1);
    }

    /* No status message in pipe mode (stdin - stdout) */
    hasStdout = outFileName && !strcmp(outFileName,stdoutmark);

    if ((hasStdout || !IS_CONSOLE(stderr)) && (g_displayLevel==2)) g_displayLevel=1;

    /* IO Stream/File */
    FIO_setHasStdoutOutput(fCtx, hasStdout);
    FIO_setNbFilesTotal(fCtx, (int)filenames->tableSize);
    FIO_determineHasStdinInput(fCtx, filenames);
    FIO_setNotificationLevel(g_displayLevel);
    FIO_setAllowBlockDevices(prefs, allowBlockDevices);
    FIO_setPatchFromMode(prefs, patchFromDictFileName != NULL);
    if (memLimit == 0) {
        if (compressionParams.windowLog == 0) {
            memLimit = (U32)1 << g_defaultMaxWindowLog;
        } else {
            memLimit = (U32)1 << (compressionParams.windowLog & 31);
    }   }
    if (patchFromDictFileName != NULL)
        dictFileName = patchFromDictFileName;
    FIO_setMemLimit(prefs, memLimit);
    if (operation==zom_compress) {
#ifndef ZSTD_NOCOMPRESS
        FIO_setContentSize(prefs, contentSize);
        FIO_setNbWorkers(prefs, nbWorkers);
        FIO_setBlockSize(prefs, (int)blockSize);
        if (g_overlapLog!=OVERLAP_LOG_DEFAULT) FIO_setOverlapLog(prefs, (int)g_overlapLog);
        FIO_setLdmFlag(prefs, (unsigned)ldmFlag);
        FIO_setLdmHashLog(prefs, (int)g_ldmHashLog);
        FIO_setLdmMinMatch(prefs, (int)g_ldmMinMatch);
        if (g_ldmBucketSizeLog != LDM_PARAM_DEFAULT) FIO_setLdmBucketSizeLog(prefs, (int)g_ldmBucketSizeLog);
        if (g_ldmHashRateLog != LDM_PARAM_DEFAULT) FIO_setLdmHashRateLog(prefs, (int)g_ldmHashRateLog);
        FIO_setAdaptiveMode(prefs, (unsigned)adapt);
        FIO_setUseRowMatchFinder(prefs, useRowMatchFinder);
        FIO_setAdaptMin(prefs, adaptMin);
        FIO_setAdaptMax(prefs, adaptMax);
        FIO_setRsyncable(prefs, rsyncable);
        FIO_setStreamSrcSize(prefs, streamSrcSize);
        FIO_setTargetCBlockSize(prefs, targetCBlockSize);
        FIO_setSrcSizeHint(prefs, srcSizeHint);
        FIO_setLiteralCompressionMode(prefs, literalCompressionMode);
        FIO_setSparseWrite(prefs, 0);
        if (adaptMin > cLevel) cLevel = adaptMin;
        if (adaptMax < cLevel) cLevel = adaptMax;

        /* Compare strategies constant with the ground truth */
        { ZSTD_bounds strategyBounds = ZSTD_cParam_getBounds(ZSTD_c_strategy);
          assert(ZSTD_NB_STRATEGIES == strategyBounds.upperBound);
          (void)strategyBounds; }

        if (showDefaultCParams || g_displayLevel >= 4) {
            size_t fileNb;
            for (fileNb = 0; fileNb < (size_t)filenames->tableSize; fileNb++) {
                if (showDefaultCParams)
                    printDefaultCParams(filenames->fileNames[fileNb], dictFileName, cLevel);
                if (g_displayLevel >= 4)
                    printActualCParams(filenames->fileNames[fileNb], dictFileName, cLevel, &compressionParams);
            }
        }

        if (g_displayLevel >= 4)
            FIO_displayCompressionParameters(prefs);
        if ((filenames->tableSize==1) && outFileName)
            operationResult = FIO_compressFilename(fCtx, prefs, outFileName, filenames->fileNames[0], dictFileName, cLevel, compressionParams);
        else
            operationResult = FIO_compressMultipleFilenames(fCtx, prefs, filenames->fileNames, outMirroredDirName, outDirName, outFileName, suffix, dictFileName, cLevel, compressionParams);
#else
        (void)contentSize; (void)suffix; (void)adapt; (void)rsyncable; (void)ultra; (void)cLevel; (void)ldmFlag; (void)literalCompressionMode; (void)targetCBlockSize; (void)streamSrcSize; (void)srcSizeHint; (void)ZSTD_strategyMap; (void)useRowMatchFinder; /* not used when ZSTD_NOCOMPRESS set */
        DISPLAY("Compression not supported \n");
#endif
    } else {  /* decompression or test */
#ifndef ZSTD_NODECOMPRESS
        if (filenames->tableSize == 1 && outFileName) {
            operationResult = FIO_decompressFilename(fCtx, prefs, outFileName, filenames->fileNames[0], dictFileName);
        } else {
            operationResult = FIO_decompressMultipleFilenames(fCtx, prefs, filenames->fileNames, outMirroredDirName, outDirName, outFileName, dictFileName);
        }
#else
        DISPLAY("Decompression not supported \n");
#endif
    }

_end:
    FIO_freePreferences(prefs);
    FIO_freeContext(fCtx);
    if (main_pause) waitEnter();
    UTIL_freeFileNamesTable(filenames);
    UTIL_freeFileNamesTable(file_of_names);
#ifndef ZSTD_NOTRACE
    TRACE_finish();
#endif

    return operationResult;
}