summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxfile01/test.c
blob: 4f53c1de8ac707f3979a160672c0e0dd78a408e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/**
 *  @file
 *
 *  Simple test program to exercise some of the basic functionality of
 *  POSIX Files and Directories Support.
 *
 *  This test assumes that the file system is initialized with the
 *  following directory structure:
 *
 *  XXXX fill this in.
 *    /
 *    /dev
 *    /dev/XXX   [where XXX includes at least console]
 */

/*
 *  COPYRIGHT (c) 1989-2012.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>

#include <pmacros.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>
#include <ctype.h>
#include <reent.h>
#include <rtems/imfs.h>

#include <rtems.h>
#include <rtems/libio.h>

#include "primode.h"

const char rtems_test_name[] = "PSXFILE 1";

/* forward declarations to avoid warnings */
void test_case_reopen_append(void);
void dump_statbuf(struct stat *buf);
void stat_a_file(const char *file);
int test_main(void);

char test_write_buffer[ 1024 ];
rtems_filesystem_operations_table  IMFS_ops_no_evalformake;
rtems_filesystem_operations_table  IMFS_ops_no_rename;

/*
 *  File test support routines.
 */

void test_cat(
  char *file,
  int   offset_arg,
  int   length
);

void test_write(
  char   *file,
  off_t  offset,
  char  *buffer
);

void test_extend(
  char *file,
  off_t new_len
);

/*
 *  dump_statbuf
 */
void dump_statbuf( struct stat *buf )
{
  rtems_device_major_number major1;
  rtems_device_minor_number minor1;
  rtems_device_major_number major2;
  rtems_device_minor_number minor2;

  rtems_filesystem_split_dev_t( buf->st_dev, major1, minor1 );
  rtems_filesystem_split_dev_t( buf->st_rdev, major2, minor2 );

  printf( "....st_dev     (0x%" PRIx32 ":0x%" PRIx32 ")\n", major1, minor1 );
  printf( "....st_rdev    (0x%" PRIx32 ":0x%" PRIx32 ")\n", major2, minor2 );
  printf( "....st_ino     %" PRIxino_t "  may vary by small amount\n", buf->st_ino );
  printf( "....mode  = %08" PRIomode_t "\n", buf->st_mode );
  printf( "....nlink = %d\n", buf->st_nlink );

  printf( "....uid = %d\n", buf->st_uid );
  printf( "....gid = %d\n", buf->st_gid );

  printf( "....atime = %s", ctime(&buf->st_atime) );
  printf( "....mtime = %s", ctime(&buf->st_mtime) );
  printf( "....ctime = %s", ctime(&buf->st_ctime) );

  printf( "....st_blksize %" PRIxblksize_t "\n", buf->st_blksize );
  printf( "....st_blocks  %" PRIxblkcnt_t "\n", buf->st_blocks );
}

void stat_a_file(
  const char *file
)
{
  int         status;
  struct stat statbuf;

  rtems_test_assert( file );

  printf( "stat( %s ) returned ", file );

  status = stat( file, &statbuf );

  if ( status == -1 ) {
    printf( ": %s\n", strerror( errno ) );
  } else {
    puts("");
    dump_statbuf( &statbuf );
  }

}

/*
 *  Main entry point of the test
 */

#if defined(__rtems__)
int test_main(void)
#else
int main(
  int argc,
  char **argv
)
#endif
{
  int               status;
  size_t            max_size;
  int               fd;
  int               i;
  struct stat       buf;
  char              buffer[128];
  FILE             *file;
  time_t            atime1;
  time_t            mtime1;
  time_t            ctime1;
  time_t            atime2;
  time_t            mtime2;
  time_t            ctime2;
  rtems_status_code rtems_status;
  rtems_time_of_day time;

  TEST_BEGIN();

  /*
   *  Grab the maximum size of an in-memory file.
   */

  max_size = IMFS_MEMFILE_MAXIMUM_SIZE;

  build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
  rtems_status = rtems_clock_set( &time );
  directive_failed( rtems_status, "clock set" );

  /*
   *  Simple stat() of /dev/console.
   */

  puts( "stat of /dev/console" );
  status = stat( "/dev/console", &buf );
  rtems_test_assert( !status );

  dump_statbuf( &buf );

  /*
   *  Exercise mkdir() and some path evaluation.
   */

  puts( "" );
  puts( "mkdir /dev/tty" );
  status = mkdir( "/dev/tty", S_IRWXU );
  rtems_test_assert( !status );

  puts( "" );
  puts( "mkdir /usr" );
  status = mkdir( "/usr", S_IRWXU );
  rtems_test_assert( !status );
  puts( "mkdir /etc" );
  status = mkdir( "/etc", S_IRWXU );
  rtems_test_assert( !status );

  puts( "mkdir /tmp" );
  status = mkdir( "/tmp", S_IRWXU );
  rtems_test_assert( !status );

  /* this tests the ".." path in path name evaluation */
  puts( "mkdir /tmp/.." );
  status = mkdir( "/tmp/..", S_IRWXU );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EEXIST );

  /* now check out trailing separators */
  puts( "mkdir /tmp/" );
  status = mkdir( "/tmp/", S_IRWXU );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EEXIST );

  /* try to make a directory under a non-existent subdirectory */
  puts( "mkdir /j/j1" );
  status = mkdir( "/j/j1", S_IRWXU );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == ENOENT );

  /* this tests the ability to make a directory in the current one */
  puts( "mkdir tmp" );
  status = mkdir( "tmp", S_IRWXU );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EEXIST );

  /*
   *  Now switch gears and exercise rmdir().
   */

  puts( "" );
  puts( "rmdir /usr" );
  status = rmdir( "/usr" );
  rtems_test_assert( !status );

  puts( "rmdir /dev" );
  status = rmdir( "/dev" );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno ==  ENOTEMPTY);

  puts( "rmdir /fred" );
  status = rmdir ("/fred");
  rtems_test_assert (status == -1);
  rtems_test_assert( errno == ENOENT );

  puts( "rmdir /tmp/bha" );
  status = rmdir( "/tmp/bha" );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == ENOENT );

  puts( "mknod /dev/test_console" );
  status = mknod( "/dev/test_console", S_IFCHR, 0LL );
  rtems_test_assert( !status );

  puts( "mknod /dev/tty/S3" );
  status = mknod( "/dev/tty/S3", S_IFCHR, 0xFF00000080LL );
  rtems_test_assert( !status );

  puts ("mknod /etc/passwd");
  status = mknod( "/etc/passwd", (S_IFREG | S_IRWXU), 0LL );
  rtems_test_assert( !status );

  puts( "mkdir /tmp/my_dir");
  status = mkdir( "/tmp/my_dir", S_IRWXU );
  rtems_test_assert( status == 0 );

  puts("mkfifo /c/my_dir" );
  status = mkfifo( "/c/my_dir", S_IRWXU );
  rtems_test_assert( status == -1 );

  /*
   *  Try to make a directory under a file -- ERROR
   */

  puts( "mkdir /etc/passwd/j" );
  status = mkdir( "/etc/passwd/j", S_IRWXU );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == ENOTDIR );

  /*
   *  Simple open failure case on non-existent file
   */

  puts( "open /tmp/joel - should fail with ENOENT" );
  fd = open( "/tmp/joel", O_RDONLY );
  rtems_test_assert( fd == -1 );
  rtems_test_assert( errno == ENOENT );

  /*
   *  Simple open case where the file is created.
   */

  puts( "open /tmp/j" );
  fd = open( "/tmp/j", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
  rtems_test_assert( fd != -1 );
  printf( "open returned file descriptor %d\n", fd );

  puts( "close /tmp/j" );
  status = close( fd );
  rtems_test_assert( !status );

  puts( "close /tmp/j again" );
  status = close( fd );
  rtems_test_assert( status == -1 );

  puts( "unlink /tmp/j" );
  status = unlink( "/tmp/j" );
  rtems_test_assert( !status );

  puts( "unlink /tmp" );
  status = unlink( "/tmp" );
  rtems_test_assert( status );

  /*
   *  Simple open failure. Trying to create an existing file.
   */

  puts("create and close /tmp/tom");
  fd = open( "/tmp/tom", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
  rtems_test_assert( fd != -1 );
  status = close( fd );
  rtems_test_assert( status == 0 );

  puts("Attempt to recreate /tmp/tom");
  fd = open( "/tmp/tom", O_CREAT | O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO );
  rtems_test_assert( fd == -1 );
  rtems_test_assert( errno == EEXIST );

  puts("create /tmp/john");
  fd = open( "/tmp/john", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
  rtems_test_assert( fd != -1 );
  status = close( fd );
  rtems_test_assert( status == 0 );

  /*
   * Open a file in read-only mode and try to truncate
   */

  puts( "Attempt to create a file, open in read-only mode and truncate it" );
  fd = open( "/tmp/bha", O_CREAT | O_RDONLY | O_TRUNC, S_IRUSR );
  rtems_test_assert( fd == -1 );
  rtems_test_assert( errno == EINVAL );

  puts( "Exercise the reentrant version _link_r -- Expect ENOENT" );
  status = _link_r( NULL, "/tmp/notexist", "/tmp/cannotexist" );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == ENOENT );

  puts( "Unlink /tmp/bha using the reentrant version -- OK" );
  status = _unlink_r( NULL, "/tmp/bha" );
  rtems_test_assert( status == 0 );

  /*
   * Simple test case for mknod
   */

  puts( "mknod with bad type - expect EINVAL" );
  status = mknod( "/tmp/bha", 0, 0LL );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EINVAL );

  /*
   * Read from filedes opened for write
   */

  puts( "open /tmp/bha in write only mode -- OK" );
  fd = open( "/tmp/bha", O_CREAT | O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO );
  rtems_test_assert( fd != -1 );

  puts( "attempt fcntl on opened file -- OK" );
  status = fcntl( fd, F_SETFD, 0 );
  rtems_test_assert( status == 0 );

  puts( "attempt to read from /tmp/bha - expect EBADF" );
  status = read( fd, buffer, 10 );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EBADF );

  puts( "closing and unlinking /tmp/bha" );
  status = close( fd );
  status |= unlink( "/tmp/bha" );
  rtems_test_assert( status == 0 );

  puts( "open /tmp/bha in read only mode -- OK" );
  fd = open( "/tmp/bha", O_CREAT | O_RDONLY, S_IRWXU|S_IRWXG|S_IRWXO );
  rtems_test_assert( fd != -1 );

  puts( "attempt to read from /tmp/bha - expect EBADF" );
  status = write( fd, buffer, 10 );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EBADF );

  puts( "closing and unlinking /tmp/bha" );
  status = close( fd );
  status |= unlink( "/tmp/bha" );
  rtems_test_assert( status == 0 );

  /*
   * Read/write from an unopened filedes
   */
  puts( "attempt to read from an unopened filedes - expect EBADF" );
  status = read( 5, buffer, 10 );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EBADF );

  puts( "attempt to write to an unopened filedes - expect EBADF" );
  status = write( 5, buffer, 10 );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EBADF );

  /*
   *  Test simple write to a file at offset 0
   */

  puts( "mknod /tmp/joel" );
  status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL );
  test_write( "/tmp/joel", 0, "the first write!!!\n" );
  test_cat( "/tmp/joel", 0, 0 );

  /* Exercise _rename_r */

  /* Simple rename test */
  puts( "rename /tmp/joel to /tmp/drjoel");
  status = _rename_r(NULL,"/tmp/joel","/tmp/drjoel");
  rtems_test_assert(status == 0);

  /* Simple rename test */
  puts("rename /tmp/drjoel to /tmp/joel");
  status = _rename_r(NULL,"/tmp/drjoel","/tmp/joel");
  rtems_test_assert(status == 0);

  /* Invalid old path */
  puts("rename /tmp/drjoel to /tmp/joel - Should result in an error \
since old path is not valid");
  status = _rename_r(NULL,"/tmp/drjoel","/tmp/joel");
  rtems_test_assert(status == -1);

  /* Invalid new path */
  puts("rename /tmp/joel to /tmp/drjoel/joel - Should result in an error \
since new path is not valid");
  status = _rename_r(NULL,"/tmp/joel","/tmp/drjoel/joel");
  rtems_test_assert(status == -1);

  puts("changing dir to /tmp");
  status = chdir("/tmp/");
  rtems_test_assert(status == 0);

  puts("rename joel to drjoel");
  status = _rename_r(NULL,"joel","drjoel");
  rtems_test_assert(status == 0);

  puts("rename drjoel to joel");
  status = _rename_r(NULL,"drjoel","joel");
  rtems_test_assert(status == 0);

  /* Rename across file systems */
  puts("creating directory /imfs");
  status = mkdir("/imfs",0777);
  rtems_test_assert(status == 0);
  puts("creating directory /imfs/hidden_on_mount");
  status = mkdir("/imfs/hidden_on_mount",0777);
  rtems_test_assert(status == 0);

  puts("mounting filesystem with IMFS_ops at /imfs");
  status = mount("null", "/imfs", "imfs", RTEMS_FILESYSTEM_READ_WRITE, NULL);
  rtems_test_assert(status == 0);
  puts("creating directory /imfs/test (on newly mounted filesystem)");
  status = mkdir("/imfs/test", 0777);
  rtems_test_assert(status == 0);

  puts("attempt to rename directory joel to /imfs/test/joel - should fail with EXDEV");
  status = _rename_r(NULL, "joel", "/imfs/test/joel");
  rtems_test_assert(status == -1);
  rtems_test_assert(errno == EXDEV);

  puts("changing dir to /");
  status = chdir("/");
  rtems_test_assert(status == 0);

  puts("attempt to rename across filesystem, with old path having a parent node");
  puts("attempt to rename tmp/joel to /imfs/test/joel");
  status = _rename_r(NULL, "tmp/joel", "/imfs/test/joel");
  rtems_test_assert(status == -1);
  rtems_test_assert(errno == EXDEV);

  puts("End of _rename_r tests");

  /*
   *  Test simple write to a file at a non-0 offset in the first block
   */

  status = unlink( "/tmp/joel" );
  rtems_test_assert( !status );

  status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL );
  rtems_test_assert( !status );

  test_write( "/tmp/joel", 10, "the first write!!!\n" );
  test_cat( "/tmp/joel", 0, 0 );
  stat_a_file( "/tmp/joel" );

  /*
   *  Test simple write to a file at a non-0 offset in the second block.  Then
   *  try to read from various offsets and lengths.
   */

  puts("unlink /tmp/joel");
  status = unlink( "/tmp/joel" );
  rtems_test_assert( !status );

  /* Test a failure path */

  puts( "unlink /tmp/joel" );
  status = unlink( "/tmp/joel" );
  rtems_test_assert( status == -1 );

  puts( "mknod /tmp/joel");
  status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL );
  rtems_test_assert( !status );

  test_write( "/tmp/joel", 514, "the first write!!!\n" );
  test_write( "/tmp/joel", 1, test_write_buffer );
  test_write( "/tmp/joel", 63, test_write_buffer );
  test_cat( "/tmp/joel", 0, 1 );
  test_cat( "/tmp/joel", 1, 1 );
  test_cat( "/tmp/joel", 490, 1 );
  test_cat( "/tmp/joel", 512, 1 );
  test_cat( "/tmp/joel", 513, 1 );
  test_cat( "/tmp/joel", 514, 1 );
  test_cat( "/tmp/joel", 520, 1 );
  test_cat( "/tmp/joel", 1, 1024 );

  /*
   *  Read from a much longer file so we can descend into doubly and
   *  triply indirect blocks.
   */

  if ( max_size < (size_t) 300 * 1024 ) {
    test_extend( "/tmp/joel", max_size - 1 );
    test_cat( "/tmp/joel", max_size / 2, 1024 );
  } else {
    printf( "Skipping maximum file size test since max_size is %zu bytes\n", max_size );
    puts("That is likely to be bigger than the available RAM on many targets." );
  }

  stat_a_file( "/tmp/joel" );

  /*
   *  Now try to use a FILE * descriptor
   *
   *  /tmp/j should not exist at this point.
   */

  puts( "stat of /tmp/j" );
  errno = 0;
  status = stat( "/tmp/j", &buf );
  printf( "stat(/tmp/j) returned %d (errno=%d)\n", status, errno );
  dump_statbuf( &buf );

  puts( "fopen of /tmp/j" );
  file = fopen( "/tmp/j", "w+" );
  rtems_test_assert( file );

  puts( "fprintf to /tmp/j" );
  for (i=1 ; i<=5 ; i++) {
    status = fprintf( file, "This is call %d to fprintf\n", i );
    rtems_test_assert( status );
    printf( "(%d) %d characters written to the file\n", i, status );
  }

  fflush( file );

  status = stat( "/tmp/j", &buf );
  rtems_test_assert( !status );
  dump_statbuf( &buf );
  atime2 = buf.st_atime;
  mtime2 = buf.st_mtime;
  ctime2 = buf.st_ctime;


  status = rtems_task_wake_after( rtems_clock_get_ticks_per_second() );
  rewind( file );
  while ( fgets(buffer, 128, file) )
    printf( "%s", buffer );

  /*
   * Verify only atime changed for a read.
   */
  status = stat( "/tmp/j", &buf );
  rtems_test_assert( !status );
  dump_statbuf( &buf );
  atime1 = buf.st_atime;
  mtime1 = buf.st_mtime;
  ctime1 = buf.st_ctime;
  rtems_test_assert( atime1 != atime2);
  rtems_test_assert( mtime1 == mtime2);
  rtems_test_assert( ctime1 == ctime2);

  unlink( "/tmp/joel" );

  /*
   *  Now truncate a file
   */

  status = rtems_task_wake_after( rtems_clock_get_ticks_per_second() );
  puts( "truncate /tmp/j to length of 40" );
  status = truncate( "/tmp/j", 40 );
  rtems_test_assert( !status );

  /*
   * Verify truncate changed all except atime.
   */
  status = stat( "/tmp/j", &buf );
  rtems_test_assert( !status );
  dump_statbuf( &buf );
  atime2 = buf.st_atime;
  mtime2 = buf.st_mtime;
  ctime2 = buf.st_ctime;
  rtems_test_assert( atime1 == atime2);
  rtems_test_assert( mtime1 != mtime2);
  rtems_test_assert( ctime1 != ctime2);

  /* try to truncate the console and see what happens */
  errno = 0;
  status = truncate( "/dev/console", 40 );
  rtems_test_assert( status == 0 || ( status == -1 && errno == EINVAL ) );

  puts( "truncate /tmp/j to length of 0" );
  status = truncate( "/tmp/j", 0 );
  rtems_test_assert( !status );

  puts( "truncate /tmp to length of 0 should fail with EISDIR\n");
  status = truncate( "/tmp", 0 );
  rtems_test_assert( status == -1 );
  printf( "%d: %s\n", errno, strerror( errno ) );
  rtems_test_assert( errno == EISDIR );

  status = truncate( "/tmp/fred", 10 );
  rtems_test_assert( status == -1);

  rtems_status = rtems_io_register_name( "/dev/not_console", 0, 0 );
  directive_failed( rtems_status, "io register" );

  test_case_reopen_append();

  TEST_END();
  rtems_test_exit( 0 );
}

/*
 *  Open/Create a File and write to it
 *
 *  Test case submitted by Andrew Bythell <abythell@nortelnetworks.com>.
 *
 */

void test_file (char *filename, char *mode);

void test_case_reopen_append(void)
{
  printf ("Writing First File\n");
  test_file ("/one.txt", "a");
  test_file ("/one.txt", "a");

  /* but not the second time - this will insert junk.
     the number of ^@'s seems to equal the number of
     actual characters in the file */

  printf ("Writing Second File\n");
  test_file ("/two.txt", "a");
  test_file ("/two.txt", "a");

  test_cat( "/one.txt", 0, 1024 );
  test_cat( "/two.txt", 0, 1024 );
}

void test_file (char *filename, char *mode)
{
  FILE *fp;
  fp = fopen (filename, mode);
  if (!fp)
      perror ("fopen");
  fprintf (fp, "this is a test line\n");
  if (fclose (fp))
      perror ("fclose");
}