nanosleep(3) broken?

I wrote a little c++ program (milli_read.cpp) to display these vt100 animations.
http://artscene.textfiles.com/vt100/

now, whatever values I use for the nanosleep it aways has the same delay.
The logic must be OK because it works fine in linux.

milli_read file
milli_read -d file # double speed
milli_read -dd file # treble speed etc.

does anyone know anything about this?


Code:
#include <iostream>
#include <fstream>
#include <time.h>
#include <string>
#include <cstdlib>

using namespace std;
/*
struct timespec {
 time_t tv_sec;
 long tv_nsec;
}
*/
static timespec ts;

void do_stream(istream &  in, long baud) {

    string s;
    ts.tv_sec = 0;
    ts.tv_nsec = (9600.0/baud) * 1e06;
    char ch;
    int status =0;


    while(in.good()) {
        ch = in.get();
	cout.put(ch) ;
	if (ch == '\n') cout.flush();
	status = nanosleep(&ts, NULL);
	if (status) {
	    perror("nanosleep:");
	    exit(1);
	}
    }
    cout << endl << "milliseconds = " << (ts.tv_nsec / 1e06) << endl;
}

int main(int argc, char ** argv) {

    long baud = 9600;
    int ch;
    while((ch = getopt(argc, argv, "db:")) != -1) {

        switch(ch) {

        case 'b':
             baud = atoi(optarg);
             break;

        case 'd':
             baud += baud ;
             break;

	default:
	    break;
	}

    }
    argc -= optind;
    argv += optind;

    if(argc) {
	while(argc--) {
	    ifstream infile(*argv);
	    argv++;
	    do_stream(infile, baud);
	}
    } else {
	do_stream(cin, baud);
    }


    cout << "baud? ~=" << baud << endl;
    return 0;
}
 
Back
Top