OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [tests/] [psxtests/] [psxfile01/] [test_cat.c] - Blame information for rev 484

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
2
 *  A test support function which performs a crude version of
3
 *  "cat" so you can look at specific parts of a file.
4
 *
5
 *  $Id: test_cat.c,v 1.2 2001-09-27 12:02:25 chris Exp $
6
 */
7
 
8
#include <stdio.h>
9
 
10
#include <sys/types.h>
11
#include <sys/stat.h>
12
#include <fcntl.h>
13
#include <unistd.h>
14
#include <errno.h>
15
#include <string.h>
16
#include <ctype.h>
17
 
18
#include <assert.h>
19
 
20
/*
21
 *  test_cat routine
22
 */
23
 
24
unsigned char test_cat_buffer[ 1024 ];
25
 
26
void test_cat(
27
  char *file,
28
  int   offset_arg,
29
  int   length
30
)
31
{
32
  int            fd;
33
  int            status;
34
  int            is_printable = 0;
35
  int            my_length;
36
  int            i;
37
  unsigned char  c;
38
  int            count = 0;
39
  off_t          offset = (off_t)offset_arg;
40
 
41
  my_length = (length) ? length : sizeof( test_cat_buffer );
42
  assert( my_length <= sizeof( test_cat_buffer ) );
43
 
44
  fd = open( file, O_RDONLY );
45
  if ( fd == -1 ) {
46
    printf( "test_cat: open( %s ) failed : %s\n", file, strerror( errno ) );
47
    exit( 0 );
48
  }
49
 
50
  for ( ;; ) {
51
    status = lseek( fd, offset, SEEK_SET );
52
    assert( status != -1 );
53
 
54
    status = read( fd, test_cat_buffer, sizeof(test_cat_buffer) );
55
    if ( status <= 0 ) {
56
      if (!is_printable)
57
        printf( "(%d)", count );
58
      puts( "" );
59
      break;
60
    }
61
 
62
    for ( i=0 ; i<status ; i++ ) {
63
      c = test_cat_buffer[i];
64
      if (isprint(c) || isspace(c)) {
65
        if (!is_printable) {
66
          printf( "(%d)", count );
67
          count = 0;
68
          is_printable = 1;
69
        }
70
        putchar(c);
71
      } else {
72
        is_printable = 0;
73
        count++;
74
      }
75
    }
76
    offset += status;
77
  }
78
 
79
  status = close( fd );
80
  assert( !status );
81
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.