typeprint/c/typeprint.c

44 lines
860 B
C
Raw Normal View History

#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
2015-06-12 04:23:06 -04:00
#include <unistd.h>
2015-06-07 17:09:03 -04:00
static int process(char *name);
2015-06-07 04:54:52 -04:00
static int
2015-06-07 17:09:03 -04:00
process(char *name)
{
2015-06-07 04:10:23 -04:00
FILE *file = fopen(name, "r");
2015-06-07 17:09:03 -04:00
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 10 * 1000000;
2015-06-12 04:23:49 -04:00
if (access(file, F_OK) == -1) {
fprintf(stderr, "[err] no such file or directory\n");
return ENOENT;
2015-06-07 04:10:23 -04:00
}
2015-06-12 04:23:49 -04:00
if (access(file, R_OK) == -1) {
2015-06-12 04:23:06 -04:00
fprintf(stderr, "[err] can't read file, permission denied\n");
return EACCES;
}
2015-06-07 04:10:23 -04:00
int x;
while ((x = fgetc(file)) != EOF) {
2015-06-07 04:54:52 -04:00
printf("%c", x);
fflush(stdout);
2015-06-07 17:09:03 -04:00
nanosleep(&ts, NULL);
2015-06-07 04:10:23 -04:00
}
fclose(file);
2015-06-07 04:10:23 -04:00
return 0;
}
int
main(int argc, char *argv[])
{
return process(argv[1]);
2015-06-07 17:09:03 -04:00
}