1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void showhelp(char *spork) {
printf("test (c) 1999-2001 Mark Glines <mark@glines.org>\n");
printf("Usage follows:\n\n");
printf("%s [-b xxx|--blocksize=xxx] [-t xxx|--time=xxx]\n\n",spork);
printf("short --long default desc\n");
printf("----------------------------------------------------------------------------\n");
printf("-h --help this (probably uninformative) message\n\n");
printf("-b --blocksize= 1024 set the buffer block size\n\n");
printf("-n --nano= 0 amount of nanoseconds to delay\n\n");
printf("-s --secs= 0 second delay count\n");
exit(0);
}
int main(int argc, char *argv[]) {
int blocksize, i, done, temp;
char *buffer;
struct timespec timebetween;
done = 0;
blocksize = 1024;
timebetween.tv_sec = 0;
timebetween.tv_nsec = 0;
if(argc==1) showhelp(argv[0]);
for(i=1;i<argc;i++) {
if(*argv[i] == '-') {
if((char)*(argv[i]+1) == '-') {
/* long-style switch */
if(strstr(argv[i], "--help")) {
showhelp(argv[1]);
} else
if(strstr(argv[i], "--blocksize=")) {
sscanf(argv[i],"--blocksize=%i",&blocksize);
} else
if(strstr(argv[i], "--nano=")) {
sscanf(argv[i],"--nano=%li",&timebetween.tv_nsec);
} else
if(strstr(argv[i], "--secs=")) {
sscanf(argv[i],"--secs=%i",(int*)&timebetween.tv_sec);
}
} else
if((char)*(argv[i]+1) == 0) {
} else {
/* short-style switch */
if(*(argv[i]+1) == 'h') {
showhelp(argv[1]);
} else
if(*(argv[i]+1) == 'b') {
i++;
sscanf(argv[i],"%i",&blocksize);
} else
if(*(argv[i]+1) == 'n') {
i++;
sscanf(argv[i],"%li",&timebetween.tv_nsec);
} else
if(*(argv[i]+1) == 's') {
i++;
sscanf(argv[i],"%i",(int*)&timebetween.tv_sec);
}
}
}
}
buffer = (char *)malloc(blocksize);
while(done==0) {
temp=read(0,buffer,blocksize);
if(temp==0) {
exit(0);
}
write(1,buffer,temp);
nanosleep(&timebetween,NULL);
}
return 0;
}
|