source: rtems/testsuites/psxtests/psxfile01/test_cat.c @ 9a4eca5

5
Last change on this file since 9a4eca5 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/**
2 *  @file
3 *
4 *  A test support function which performs a crude version of
5 *  "cat" so you can look at specific parts of a file.
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2012.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <stdio.h>
22
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <unistd.h>
27#include <errno.h>
28#include <string.h>
29#include <ctype.h>
30
31#include <tmacros.h>
32
33/* forward declarations to avoid warnings */
34void test_cat(char *file, int offset_arg, int length);
35
36/*
37 *  test_cat routine
38 */
39
40unsigned char test_cat_buffer[ 1024 ];
41
42void test_cat(
43  char *file,
44  int   offset_arg,
45  int   length
46)
47{
48  int            fd;
49  int            status;
50  int            is_printable = 0;
51  int            my_length;
52  int            i;
53  unsigned char  c;
54  int            count = 0;
55  off_t          offset = (off_t)offset_arg;
56
57  my_length = (length) ? length : sizeof( test_cat_buffer );
58  rtems_test_assert( my_length <= sizeof( test_cat_buffer ) );
59
60  fd = open( file, O_RDONLY );
61  if ( fd == -1 ) {
62    printf( "test_cat: open( %s ) failed : %s\n", file, strerror( errno ) );
63    rtems_test_exit( 0 );
64  }
65
66  for ( ;; ) {
67    status = lseek( fd, offset, SEEK_SET );
68    rtems_test_assert( status != -1 );
69
70    status = read( fd, test_cat_buffer, sizeof(test_cat_buffer) );
71    if ( status <= 0 ) {
72      if (!is_printable)
73        printf( "(%d)", count );
74      puts( "" );
75      break;
76    }
77
78    for ( i=0 ; i<status ; i++ ) {
79      c = test_cat_buffer[i];
80      if (isprint(c) || isspace(c)) {
81        if (!is_printable) {
82          printf( "(%d)", count );
83          count = 0;
84          is_printable = 1;
85        }
86        putchar(c);
87      } else {
88        is_printable = 0;
89        count++;
90      }
91    }
92    offset += status;
93  }
94
95  status = close( fd );
96  rtems_test_assert( !status );
97}
Note: See TracBrowser for help on using the repository browser.