お髭処blog

ドイツのものを中心としたボードゲーム・カードゲームのプレイ記録・感想を中心としたブログです。最新のドイツゲームから、紀元前から伝わるゲーム、旧西ドイツ製のレアゲーム、日本伝統の博打まで幅広くプレイしています。

続・コマンドプロンプトに sleep がないので

コマンドプロンプトに sleep がないので」へのアクセスが意外とあるので、自作 sleep コマンドの最新版ソースをさらしてみる。
指定秒だけ sleep します。
自分でコンパイルして使ってね。

/*
    2010/05/15 new
    2010/06/05 Sleep中に1秒ごとに文字を印字するオプションを追加
*/


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#ifdef _WIN32 // _WIN32 is defined by all Windows 32 compilers, but not by others.
#include <windows.h>
#elif __unix
#include <unistd.h>
#else
#endif


void printUsage(void);
void printError(const char *s);
long makeItSleep(long mSecond, const char *progress);



#define PROGRAM_NAME "sleepsec.exe"


long main(int argc, char* argv[])
{
    long wait;
    char *progress;

    if ( argc < 2 || argc > 3 ) {
        printUsage();
        return -1;
    }

    wait = strtol(argv[1], NULL, 10);
    if ( wait == 0 ) {
        printUsage();
        return -2;
    }
    else if (wait == LONG_MAX) {
        printError("Too Long Time.");
        printUsage();
        return -3;
    }
    else if (wait < 1 || wait == LONG_MIN) {
        printError("Too Minimal Time.");
        printUsage();
        return -4;
    }

    if ( argc == 3 ) {
        progress = argv[2];
        if ( strlen( progress ) > 1) {
            printError("Progress Charactor is too long.");
            printUsage();
            return -5;
        }
    }
    else {
        progress = "";
    }

    wait = atol(argv[1]);

    if ( strlen( progress ) > 0 ) {
        printf("%s: Wait %d seconds...\n", PROGRAM_NAME, wait);
    }

    makeItSleep( wait, progress );

    if ( strlen( progress ) > 0 ) {
        printf(" Wake.\n");
    }

    return wait;
}


void printError(const char *s) {
    fprintf(stderr, "%s: %s\n", PROGRAM_NAME, s);
}


void printUsage(void) {
    fprintf(stderr, "Usage: sleepsec [Second] [Progress Charactor]\n");
}


long makeItSleep(long mSecond, const char *progress) {

    long i;

    // 1秒Sleepするのを指定回数実行する

    if ( strlen(progress) > 0 ) {
        for ( i = 0; i < mSecond; i++) {
            printf(progress);
#ifdef _WIN32
            Sleep(1000);
#elif __unix
            sleep(1000);
#else
            sleep(1000);
#endif
        }
    }
    else {
        for ( i = 0; i < mSecond; i++) {
#ifdef _WIN32
            Sleep(1000);
#elif __unix
            sleep(1000);
#else
            sleep(1000);
#endif
        }
    }

    return mSecond;
}