+#define _LARGEFILE_SOURCE
+#define _FILE_OFFSET_BITS 64
+
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int result;
- int fd;
+ FILE *fp;
int print = 0;
if(argc >= 2 && !strcmp(argv[1], "-p"))
print = 1;
- result = fd = open("../trace.dat", O_RDONLY);
- if(result == -1) {
- perror("open");
+ fp = fopen("../trace.dat", "r");
+ if(fp == NULL) {
+ perror("fopen");
return 1;
}
unsigned char arglen;
char *args;
- result = read(fd, ×tamp, 4);
- if(result == 0)
+ fscanf(fp, "%4c", ×tamp);
+ if(feof(fp))
break;
- if(result < 4) {
- perror("read");
- return 1;
- }
-
- result = read(fd, &id, 2);
- if(result < 2) {
- perror("read");
- return 1;
- }
-
- result = read(fd, &arglen, 1);
- if(result < 1) {
- perror("read");
- return 1;
- }
+
+ fscanf(fp, "%2c", &id);
+
+ fscanf(fp, "%1c", &arglen);
args = malloc(arglen);
- result = read(fd, args, arglen);
- if(result < arglen) {
- perror("read");
- return 1;
- }
+ // manually specify length of args
+ fscanf(fp, "%15c", args);
unsigned short arg1;
char *arg2;
printf("timestamp %lu id %hu args=(arg1=%hu arg2=\"%s\")\n", timestamp, id, arg1, arg2);
free(args);
+
}
- close(fd);
+ fclose(fp);
return 0;
}