* Merged fdcat and streamcat into zzcat.

This commit is contained in:
Sam Hocevar 2007-01-28 00:53:19 +00:00 committed by sam
parent dc895f2511
commit 7426def162
5 changed files with 118 additions and 161 deletions

View File

@ -1,8 +1,6 @@
EXTRA_DIST = testsuite.sh file-00 file-ff file-random file-text
noinst_PROGRAMS = fdcat streamcat
fdcat_SOURCES = fdcat.c
streamcat_SOURCES = streamcat.c
noinst_PROGRAMS = zzcat
zzcat_SOURCES = zzcat.c

View File

@ -1,76 +0,0 @@
/*
* fdcat - cat reimplementation
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#include "config.h"
#define _LARGEFILE64_SOURCE /* for lseek64() */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static inline int myrand(void)
{
static int seed = 1;
int x, y;
x = (seed + 0x12345678) << 11;
y = (seed + 0xfedcba98) >> 21;
seed = x * 1010101 + y * 343434;
return seed;
}
int main(int argc, char *argv[])
{
long int pos;
unsigned char *data;
int i, j, fd;
if(argc != 2)
return EXIT_FAILURE;
fd = open(argv[1], O_RDONLY);
if(fd < 0)
return EXIT_FAILURE;
pos = lseek(fd, 0, SEEK_END);
if(pos < 0)
return EXIT_FAILURE;
/* Read the whole file */
data = malloc(pos);
lseek(fd, 0, SEEK_SET);
read(fd, data, pos);
/* Read shit here and there */
for(i = 0; i < 128; i++)
{
lseek(fd, myrand() % pos, SEEK_SET);
for(j = 0; j < 16; j++)
read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
#ifdef HAVE_LSEEK64
lseek64(fd, myrand() % pos, SEEK_SET);
for(j = 0; j < 16; j++)
read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
#endif
}
fwrite(data, pos, 1, stdout);
return EXIT_SUCCESS;
}

View File

@ -1,75 +0,0 @@
/*
* streamcat - cat reimplementation
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static inline int myrand(void)
{
static int seed = 1;
int x, y;
x = (seed + 0x12345678) << 11;
y = (seed + 0xfedcba98) >> 21;
seed = x * 1010101 + y * 343434;
return seed;
}
int main(int argc, char *argv[])
{
long int pos;
unsigned char *data;
int i, j;
FILE *stream;
if(argc != 2)
return EXIT_FAILURE;
stream = fopen(argv[1], "r");
if(!stream)
return EXIT_FAILURE;
fseek(stream, 0, SEEK_END);
pos = ftell(stream);
if(pos < 0)
return EXIT_FAILURE;
/* Read the whole file */
data = malloc(pos + 16); /* 16 safety bytes */
fseek(stream, 0, SEEK_SET);
fread(data, pos, 1, stream);
/* Read shit here and there */
for(i = 0; i < 128; i++)
{
long int now;
fseek(stream, myrand() % pos, SEEK_SET);
for(j = 0; j < 16; j++)
fread(data + ftell(stream), myrand() % 4096, 1, stream);
fseek(stream, myrand() % pos, SEEK_SET);
now = ftell(stream);
for(j = 0; j < 16; j++)
data[now + j] = getc(stream);
now = ftell(stream);
for(j = 0; j < 16; j++)
data[now + j] = fgetc(stream);
}
fwrite(data, pos, 1, stdout);
return EXIT_SUCCESS;
}

View File

@ -30,10 +30,9 @@ check()
seed=$((0+0$1))
DIR="$(dirname "$0")"
ZZUF="$DIR/../src/zzuf"
FDCAT="$DIR/fdcat"
STREAMCAT="$DIR/streamcat"
if [ ! -f "$FDCAT" -o ! -f "$STREAMCAT" ]; then
echo "error: test/fdcat or test/streamcat are missing"
ZZCAT="$DIR/zzcat"
if [ ! -f "$ZZCAT" ]; then
echo "error: test/zzcat is missing"
exit 1
fi
if file /bin/cat | grep -q 'statically linked'; then
@ -64,8 +63,8 @@ for r in 0.0 0.00001 0.001 0.1 10.0; do
else
check "$ZZOPTS" "< $file" "zzuf"
fi
check "$ZZOPTS" "$FDCAT $file" "fdcat"
check "$ZZOPTS" "$STREAMCAT $file" "streamcat"
check "$ZZOPTS" "$ZZCAT 1 $file" "zzcat 1"
check "$ZZOPTS" "$ZZCAT 2 $file" "zzcat 2"
if [ "$STATIC_CAT" = "" ]; then
check "$ZZOPTS" "cat $file" "cat"
check "$ZZOPTS" "-i cat < $file" "|cat"

111
test/zzcat.c Normal file
View File

@ -0,0 +1,111 @@
/*
* zzcat - various cat reimplementations for testing purposes
* Copyright (c) 2006, 2007 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#include "config.h"
#define _LARGEFILE64_SOURCE /* for lseek64() */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static inline unsigned int myrand(void)
{
static int seed = 1;
int x, y;
x = (seed + 0x12345678) << 11;
y = (seed + 0xfedcba98) >> 21;
seed = x * 1010101 + y * 343434;
return seed;
}
int main(int argc, char *argv[])
{
int64_t len;
unsigned char *data;
char const *name;
FILE *stream;
int i, j, fd;
if(argc != 3)
return EXIT_FAILURE;
name = argv[2];
/* Read the whole file */
fd = open(name, O_RDONLY);
if(fd < 0)
return EXIT_FAILURE;
len = lseek(fd, 0, SEEK_END);
if(len < 0)
return EXIT_FAILURE;
data = malloc(len + 16); /* 16 safety bytes */
lseek(fd, 0, SEEK_SET);
read(fd, data, len);
close(fd);
/* Read shit here and there, using different methods */
switch(atoi(argv[1]))
{
case 1: /* socket seeks and reads */
fd = open(name, O_RDONLY);
if(fd < 0)
return EXIT_FAILURE;
for(i = 0; i < 128; i++)
{
lseek(fd, myrand() % len, SEEK_SET);
for(j = 0; j < 4; j++)
read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
#ifdef HAVE_LSEEK64
lseek64(fd, myrand() % len, SEEK_SET);
for(j = 0; j < 4; j++)
read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
#endif
}
close(fd);
break;
case 2: /* std streams seeks and reads */
stream = fopen(name, "r");
if(!stream)
return EXIT_FAILURE;
for(i = 0; i < 128; i++)
{
long int now;
fseek(stream, myrand() % len, SEEK_SET);
for(j = 0; j < 4; j++)
fread(data + ftell(stream), myrand() % 4096, 1, stream);
fseek(stream, myrand() % len, SEEK_SET);
now = ftell(stream);
for(j = 0; j < 16; j++)
data[now + j] = getc(stream);
now = ftell(stream);
for(j = 0; j < 16; j++)
data[now + j] = fgetc(stream);
}
fclose(stream);
break;
}
/* Write what we have read */
fwrite(data, len, 1, stdout);
free(data);
return EXIT_SUCCESS;
}