Refactor zzcat for clarity.
This commit is contained in:
parent
f324673387
commit
2e6ce6d101
193
test/zzcat.c
193
test/zzcat.c
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* zzcat - various cat reimplementations for testing purposes
|
* zzcat - various cat reimplementations for testing purposes
|
||||||
* Copyright (c) 2006, 2007 Sam Hocevar <sam@zoy.org>
|
* Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net>
|
||||||
* All Rights Reserved
|
* All Rights Reserved
|
||||||
*
|
*
|
||||||
* $Id$
|
* $Id$
|
||||||
@ -37,6 +37,20 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
static int zzcat_read(char const *, unsigned char *, int64_t);
|
||||||
|
static int zzcat_fread(char const *, unsigned char *, int64_t);
|
||||||
|
static int zzcat_getc(char const *, unsigned char *, int64_t, int);
|
||||||
|
static int zzcat_fgetc(char const *, unsigned char *, int64_t);
|
||||||
|
#if defined HAVE_GETLINE
|
||||||
|
static int zzcat_getdelim_getc(char const *, unsigned char *, int64_t, int);
|
||||||
|
#endif
|
||||||
|
static int zzcat_fread_getc(char const *, unsigned char *, int64_t, int);
|
||||||
|
static int zzcat_random_socket(char const *, unsigned char *, int64_t);
|
||||||
|
static int zzcat_random_stream(char const *, unsigned char *, int64_t);
|
||||||
|
#if defined HAVE_MMAP
|
||||||
|
static int zzcat_random_mmap(char const *, unsigned char *, int64_t);
|
||||||
|
#endif
|
||||||
|
|
||||||
static inline unsigned int myrand(void)
|
static inline unsigned int myrand(void)
|
||||||
{
|
{
|
||||||
static int seed = 1;
|
static int seed = 1;
|
||||||
@ -52,9 +66,7 @@ int main(int argc, char *argv[])
|
|||||||
int64_t len;
|
int64_t len;
|
||||||
unsigned char *data;
|
unsigned char *data;
|
||||||
char const *name;
|
char const *name;
|
||||||
FILE *stream;
|
int ret, cmd, fd;
|
||||||
int cmd, i, j, fd;
|
|
||||||
char c;
|
|
||||||
|
|
||||||
if(argc != 3)
|
if(argc != 3)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
@ -76,62 +88,106 @@ int main(int argc, char *argv[])
|
|||||||
/* Read shit here and there, using different methods */
|
/* Read shit here and there, using different methods */
|
||||||
switch((cmd = atoi(argv[1])))
|
switch((cmd = atoi(argv[1])))
|
||||||
{
|
{
|
||||||
/* 0x: simple fd calls
|
case 0: ret = zzcat_read(name, data, len); break;
|
||||||
* 1x: complex fd calls */
|
case 20: ret = zzcat_fread(name, data, len); break;
|
||||||
case 0: /* only read() calls */
|
case 21: ret = zzcat_getc(name, data, len, 0); break;
|
||||||
fd = open(name, O_RDONLY);
|
#if defined HAVE_GETC_UNLOCKED
|
||||||
|
case 22: ret = zzcat_getc(name, data, len, 1); break;
|
||||||
|
#endif
|
||||||
|
case 23: ret = zzcat_fgetc(name, data, len); break;
|
||||||
|
#if defined HAVE_GETLINE
|
||||||
|
case 24: ret = zzcat_getdelim_getc(name, data, len, 0); break;
|
||||||
|
# if defined HAVE_GETC_UNLOCKED
|
||||||
|
case 25: ret = zzcat_getdelim_getc(name, data, len, 1); break;
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
case 30: ret = zzcat_fread_getc(name, data, len, 0); break;
|
||||||
|
case 31: ret = zzcat_fread_getc(name, data, len, 1); break;
|
||||||
|
case 40: ret = zzcat_random_socket(name, data, len); break;
|
||||||
|
case 41: ret = zzcat_random_stream(name, data, len); break;
|
||||||
|
#if defined HAVE_MMAP
|
||||||
|
case 42: ret = zzcat_random_mmap(name, data, len); break;
|
||||||
|
#endif
|
||||||
|
default: ret = EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write what we have read */
|
||||||
|
fwrite(data, len, 1, stdout);
|
||||||
|
free(data);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Only read(1) calls */
|
||||||
|
static int zzcat_read(char const *name, unsigned char *data, int64_t len)
|
||||||
|
{
|
||||||
|
int i, fd = open(name, O_RDONLY);
|
||||||
if(fd < 0)
|
if(fd < 0)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
for(i = 0; i < len; i++)
|
for(i = 0; i < len; i++)
|
||||||
read(fd, data + i, 1);
|
read(fd, data + i, 1);
|
||||||
close(fd);
|
close(fd);
|
||||||
break;
|
return EXIT_SUCCESS;
|
||||||
/* 2x: simple stdio calls
|
}
|
||||||
* 3x: complex stdio calls */
|
|
||||||
case 20: /* only fread() calls */
|
/* Only fread() calls */
|
||||||
stream = fopen(name, "r");
|
static int zzcat_fread(char const *name, unsigned char *data, int64_t len)
|
||||||
|
{
|
||||||
|
FILE *stream = fopen(name, "r");
|
||||||
|
int i;
|
||||||
if(!stream)
|
if(!stream)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
for(i = 0; i < len; i++)
|
for(i = 0; i < len; i++)
|
||||||
fread(data + i, 1, 1, stream);
|
fread(data + i, 1, 1, stream);
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
break;
|
return EXIT_SUCCESS;
|
||||||
case 21: /* only getc() calls */
|
}
|
||||||
#if defined HAVE_GETC_UNLOCKED
|
|
||||||
case 22: /* only getc_unlocked() calls */
|
/* Only getc() or getc_unlocked() calls */
|
||||||
#endif
|
static int zzcat_getc(char const *name, unsigned char *data, int64_t len,
|
||||||
stream = fopen(name, "r");
|
int unlocked)
|
||||||
|
{
|
||||||
|
FILE *stream = fopen(name, "r");
|
||||||
|
int i;
|
||||||
if(!stream)
|
if(!stream)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
for(i = 0; i < len; i++)
|
for(i = 0; i < len; i++)
|
||||||
#if defined HAVE_GETC_UNLOCKED
|
#if defined HAVE_GETC_UNLOCKED
|
||||||
data[i] = cmd == 21 ? getc(stream)
|
data[i] = unlocked ? getc_unlocked(stream)
|
||||||
: getc_unlocked(stream);
|
: getc(stream);
|
||||||
#else
|
#else
|
||||||
data[i] = getc(stream);
|
data[i] = getc(stream);
|
||||||
#endif
|
#endif
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
break;
|
return EXIT_SUCCESS;
|
||||||
case 23: /* only fgetc() calls */
|
}
|
||||||
stream = fopen(name, "r");
|
|
||||||
|
/* Only fgetc() calls */
|
||||||
|
static int zzcat_fgetc(char const *name, unsigned char *data, int64_t len)
|
||||||
|
{
|
||||||
|
FILE *stream = fopen(name, "r");
|
||||||
|
int i;
|
||||||
if(!stream)
|
if(!stream)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
for(i = 0; i < len; i++)
|
for(i = 0; i < len; i++)
|
||||||
data[i] = fgetc(stream);
|
data[i] = fgetc(stream);
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
break;
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
#if defined HAVE_GETLINE
|
#if defined HAVE_GETLINE
|
||||||
case 24: /* getline() and getc() calls */
|
/* getdelim() and getc() calls */
|
||||||
#if defined HAVE_GETC_UNLOCKED
|
static int zzcat_getdelim_getc(char const *name, unsigned char *data,
|
||||||
case 25: /* getline() and getc_unlocked() calls */
|
int64_t len, int unlocked)
|
||||||
#endif
|
{
|
||||||
stream = fopen(name, "r");
|
FILE *stream = fopen(name, "r");
|
||||||
|
int i = 0, j;
|
||||||
|
char c;
|
||||||
if(!stream)
|
if(!stream)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
i = 0;
|
(void)len;
|
||||||
#if defined HAVE_GETC_UNLOCKED
|
#if defined HAVE_GETC_UNLOCKED
|
||||||
while ((c = (cmd == 24 ? getc(stream)
|
while ((c = (unlocked ? getc_unlocked(stream) : getc(stream))) != EOF)
|
||||||
: getc_unlocked(stream))) != EOF)
|
|
||||||
#else
|
#else
|
||||||
while ((c = getc(stream)) != EOF)
|
while ((c = getc(stream)) != EOF)
|
||||||
#endif
|
#endif
|
||||||
@ -147,29 +203,30 @@ int main(int argc, char *argv[])
|
|||||||
data[i] = line[j];
|
data[i] = line[j];
|
||||||
}
|
}
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
break;
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
case 30: /* one fread(), then only getc() calls */
|
|
||||||
stream = fopen(name, "r");
|
/* One fread(), then only getc() or fgetc() calls */
|
||||||
|
static int zzcat_fread_getc(char const *name, unsigned char *data,
|
||||||
|
int64_t len, int use_fgetc)
|
||||||
|
{
|
||||||
|
FILE *stream = fopen(name, "r");
|
||||||
|
int i;
|
||||||
if(!stream)
|
if(!stream)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
fread(data, 1, 10, stream);
|
fread(data, 1, 10, stream);
|
||||||
for(i = 10; i < len; i++)
|
for(i = 10; i < len; i++)
|
||||||
data[i] = getc(stream);
|
data[i] = use_fgetc ? fgetc(stream) : getc(stream);
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
break;
|
return EXIT_SUCCESS;
|
||||||
case 31: /* one fread(), then only fgetc() calls */
|
}
|
||||||
stream = fopen(name, "r");
|
|
||||||
if(!stream)
|
/* Socket seeks and reads */
|
||||||
return EXIT_FAILURE;
|
static int zzcat_random_socket(char const *name, unsigned char *data,
|
||||||
fread(data, 1, 10, stream);
|
int64_t len)
|
||||||
for(i = 10; i < len; i++)
|
{
|
||||||
data[i] = fgetc(stream);
|
int i, j, fd = open(name, O_RDONLY);
|
||||||
fclose(stream);
|
|
||||||
break;
|
|
||||||
/* 4x: complex, random stuff */
|
|
||||||
case 40: /* socket seeks and reads */
|
|
||||||
fd = open(name, O_RDONLY);
|
|
||||||
if(fd < 0)
|
if(fd < 0)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
for(i = 0; i < 128; i++)
|
for(i = 0; i < 128; i++)
|
||||||
@ -184,9 +241,15 @@ int main(int argc, char *argv[])
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
break;
|
return EXIT_SUCCESS;
|
||||||
case 41: /* std streams seeks and reads */
|
}
|
||||||
stream = fopen(name, "r");
|
|
||||||
|
/* Standard stream seeks and reads */
|
||||||
|
static int zzcat_random_stream(char const *name, unsigned char *data,
|
||||||
|
int64_t len)
|
||||||
|
{
|
||||||
|
FILE *stream = fopen(name, "r");
|
||||||
|
int i, j;
|
||||||
if(!stream)
|
if(!stream)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
for(i = 0; i < 128; i++)
|
for(i = 0; i < 128; i++)
|
||||||
@ -205,12 +268,17 @@ int main(int argc, char *argv[])
|
|||||||
data[now + j] = fgetc(stream);
|
data[now + j] = fgetc(stream);
|
||||||
}
|
}
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
break;
|
return EXIT_SUCCESS;
|
||||||
case 42: /* mmap() */
|
}
|
||||||
fd = open(name, O_RDONLY);
|
|
||||||
|
#ifdef HAVE_MMAP
|
||||||
|
/* mmap() followed by random memory reads */
|
||||||
|
static int zzcat_random_mmap(char const *name, unsigned char *data,
|
||||||
|
int64_t len)
|
||||||
|
{
|
||||||
|
int i, j, fd = open(name, O_RDONLY);
|
||||||
if(fd < 0)
|
if(fd < 0)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
#ifdef HAVE_MMAP
|
|
||||||
for(i = 0; i < 128; i++)
|
for(i = 0; i < 128; i++)
|
||||||
{
|
{
|
||||||
char *map;
|
char *map;
|
||||||
@ -230,17 +298,8 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
munmap(map, mlen);
|
munmap(map, mlen);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
close(fd);
|
close(fd);
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write what we have read */
|
|
||||||
fwrite(data, len, 1, stdout);
|
|
||||||
free(data);
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user