OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems-20020807/] [c/] [src/] [tests/] [psxtests/] [psxfile01/] [test_cat.c] - Blame information for rev 1771

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

Line No. Rev Author Line
1 1026 ivang
/*
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
 *  test_cat.c,v 1.2 2002/08/02 00:53:21 joel 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
#include <pmacros.h>
21
 
22
/*
23
 *  test_cat routine
24
 */
25
 
26
unsigned char test_cat_buffer[ 1024 ];
27
 
28
void test_cat(
29
  char *file,
30
  int   offset_arg,
31
  int   length
32
)
33
{
34
  int            fd;
35
  int            status;
36
  int            is_printable = 0;
37
  int            my_length;
38
  int            i;
39
  unsigned char  c;
40
  int            count = 0;
41
  off_t          offset = (off_t)offset_arg;
42
 
43
  my_length = (length) ? length : sizeof( test_cat_buffer );
44
  assert( my_length <= sizeof( test_cat_buffer ) );
45
 
46
  fd = open( file, O_RDONLY );
47
  if ( fd == -1 ) {
48
    printf( "test_cat: open( %s ) failed : %s\n", file, strerror( errno ) );
49
    rtems_test_exit( 0 );
50
  }
51
 
52
  for ( ;; ) {
53
    status = lseek( fd, offset, SEEK_SET );
54
    assert( status != -1 );
55
 
56
    status = read( fd, test_cat_buffer, sizeof(test_cat_buffer) );
57
    if ( status <= 0 ) {
58
      if (!is_printable)
59
        printf( "(%d)", count );
60
      puts( "" );
61
      break;
62
    }
63
 
64
    for ( i=0 ; i<status ; i++ ) {
65
      c = test_cat_buffer[i];
66
      if (isprint(c) || isspace(c)) {
67
        if (!is_printable) {
68
          printf( "(%d)", count );
69
          count = 0;
70
          is_printable = 1;
71
        }
72
        putchar(c);
73
      } else {
74
        is_printable = 0;
75
        count++;
76
      }
77
    }
78
    offset += status;
79
  }
80
 
81
  status = close( fd );
82
  assert( !status );
83
}

powered by: WebSVN 2.1.0

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