source: rtems/testsuites/psxtests/psxfile01/test.c

Last change on this file was ede29158, checked in by Joel Sherrill <joel@…>, on 04/07/22 at 15:04:32

testsuites/psxtests/psx[a-f]*: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 21.3 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 *  @file
5 *
6 *  Simple test program to exercise some of the basic functionality of
7 *  POSIX Files and Directories Support.
8 *
9 *  This test assumes that the file system is initialized with the
10 *  following directory structure:
11 *
12 *  XXXX fill this in.
13 *    /
14 *    /dev
15 *    /dev/XXX   [where XXX includes at least console]
16 */
17
18/*
19 *  COPYRIGHT (c) 1989-2012.
20 *  On-Line Applications Research Corporation (OAR).
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 *    notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 *    notice, this list of conditions and the following disclaimer in the
29 *    documentation and/or other materials provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
35 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 */
43
44#ifdef HAVE_CONFIG_H
45#include "config.h"
46#endif
47
48#include <stdio.h>
49
50#include <pmacros.h>
51#include <sys/types.h>
52#include <sys/stat.h>
53#include <fcntl.h>
54#include <unistd.h>
55#include <errno.h>
56#include <string.h>
57#include <inttypes.h>
58#include <ctype.h>
59#include <reent.h>
60#include <rtems/imfs.h>
61
62#include <rtems.h>
63#include <rtems/libio.h>
64
65#include "primode.h"
66
67const char rtems_test_name[] = "PSXFILE 1";
68
69/* forward declarations to avoid warnings */
70void test_case_reopen_append(void);
71void dump_statbuf(struct stat *buf);
72void stat_a_file(const char *file);
73int test_main(void);
74
75char test_write_buffer[ 1024 ];
76rtems_filesystem_operations_table  IMFS_ops_no_evalformake;
77rtems_filesystem_operations_table  IMFS_ops_no_rename;
78
79static const char somefile[] = "somefile";
80
81static const char somelink[] = "somelink";
82
83/*
84 *  File test support routines.
85 */
86
87void test_cat(
88  char *file,
89  int   offset_arg,
90  int   length
91);
92
93void test_write(
94  char   *file,
95  off_t  offset,
96  char  *buffer
97);
98
99void test_extend(
100  char *file,
101  off_t new_len
102);
103
104/*
105 *  dump_statbuf
106 */
107void dump_statbuf( struct stat *buf )
108{
109  rtems_device_major_number major1;
110  rtems_device_minor_number minor1;
111  rtems_device_major_number major2;
112  rtems_device_minor_number minor2;
113
114  rtems_filesystem_split_dev_t( buf->st_dev, major1, minor1 );
115  rtems_filesystem_split_dev_t( buf->st_rdev, major2, minor2 );
116
117  printf( "....st_dev     (0x%" PRIx32 ":0x%" PRIx32 ")\n", major1, minor1 );
118  printf( "....st_rdev    (0x%" PRIx32 ":0x%" PRIx32 ")\n", major2, minor2 );
119  printf( "....st_ino     %" PRIxino_t "  may vary by small amount\n", buf->st_ino );
120  printf( "....mode  = %08" PRIomode_t "\n", buf->st_mode );
121  printf( "....nlink = %d\n", buf->st_nlink );
122
123  printf( "....uid = %d\n", buf->st_uid );
124  printf( "....gid = %d\n", buf->st_gid );
125
126  printf( "....atime = %s", ctime(&buf->st_atime) );
127  printf( "....mtime = %s", ctime(&buf->st_mtime) );
128  printf( "....ctime = %s", ctime(&buf->st_ctime) );
129
130  printf( "....st_blksize %" PRIxblksize_t "\n", buf->st_blksize );
131  printf( "....st_blocks  %" PRIxblkcnt_t "\n", buf->st_blocks );
132}
133
134void stat_a_file(
135  const char *file
136)
137{
138  int         status;
139  struct stat statbuf;
140
141  rtems_test_assert( file );
142
143  printf( "stat( %s ) returned ", file );
144
145  status = stat( file, &statbuf );
146
147  if ( status == -1 ) {
148    printf( ": %s\n", strerror( errno ) );
149  } else {
150    puts("");
151    dump_statbuf( &statbuf );
152  }
153
154}
155
156static void test_open_directory(void)
157{
158  int status;
159  int fd;
160
161  fd = open( somefile, O_CREAT, S_IRWXU );
162  rtems_test_assert( fd >= 0 );
163
164  status = close( fd );
165  rtems_test_assert( status == 0 );
166
167#ifdef O_DIRECTORY
168  errno = 0;
169  fd = open( somefile, O_DIRECTORY, S_IRWXU );
170  rtems_test_assert( fd == -1 );
171  rtems_test_assert( errno == ENOTDIR );
172#endif
173
174  status = unlink( somefile );
175  rtems_test_assert( status == 0 );
176
177  errno = 0;
178  fd = open( somefile, O_RDONLY );
179  rtems_test_assert( fd == -1 );
180  rtems_test_assert( errno == ENOENT );
181}
182
183static void test_open_cloexec(void)
184{
185  int status;
186  int fd;
187  mode_t mode;
188
189  mode = O_CREAT;
190
191#ifdef O_CLOEXEC
192  mode |= O_CLOEXEC;
193#endif
194
195  fd = open( somefile, mode, S_IRWXU );
196  rtems_test_assert( fd >= 0 );
197
198  status = close( fd );
199  rtems_test_assert( status == 0 );
200
201  status = unlink( somefile );
202  rtems_test_assert( status == 0 );
203
204  errno = 0;
205  fd = open( somefile, O_RDONLY );
206  rtems_test_assert( fd == -1 );
207  rtems_test_assert( errno == ENOENT );
208}
209
210static void test_open_nofollow(void)
211{
212  int status;
213  int fd;
214  struct stat st;
215
216  fd = open( somefile, O_CREAT, S_IRWXU );
217  rtems_test_assert( fd >= 0 );
218
219  status = close( fd );
220  rtems_test_assert( status == 0 );
221
222  status = symlink( somefile, somelink );
223  rtems_test_assert( status == 0 );
224
225  fd = open( somelink, O_RDONLY );
226  rtems_test_assert( fd >= 0 );
227
228  status = fstat( fd, &st );
229  rtems_test_assert( status == 0 );
230  rtems_test_assert( S_ISREG( st.st_mode ) );
231
232  status = close( fd );
233  rtems_test_assert( status == 0 );
234
235#ifdef O_NOFOLLOW
236  fd = open( somelink, O_RDONLY | O_NOFOLLOW );
237  rtems_test_assert( fd >= 0 );
238
239  status = fstat( fd, &st );
240  rtems_test_assert( status == 0 );
241  rtems_test_assert( S_ISLNK( st.st_mode ) );
242
243  status = close( fd );
244  rtems_test_assert( status == 0 );
245#endif
246
247  status = unlink( somelink );
248  rtems_test_assert( status == 0 );
249
250  errno = 0;
251  fd = open( somelink, O_RDONLY );
252  rtems_test_assert( fd == -1 );
253  rtems_test_assert( errno == ENOENT );
254
255  status = unlink( somefile );
256  rtems_test_assert( status == 0 );
257
258  errno = 0;
259  fd = open( somefile, O_RDONLY );
260  rtems_test_assert( fd == -1 );
261  rtems_test_assert( errno == ENOENT );
262}
263
264/*
265 *  Main entry point of the test
266 */
267
268#if defined(__rtems__)
269int test_main(void)
270#else
271int main(
272  int argc,
273  char **argv
274)
275#endif
276{
277  int               status;
278  size_t            max_size;
279  int               fd;
280  int               i;
281  struct stat       buf;
282  char              buffer[128];
283  FILE             *file;
284  time_t            atime1;
285  time_t            mtime1;
286  time_t            ctime1;
287  time_t            atime2;
288  time_t            mtime2;
289  time_t            ctime2;
290  rtems_status_code rtems_status;
291  rtems_time_of_day time;
292
293  TEST_BEGIN();
294
295  test_open_directory();
296  test_open_cloexec();
297  test_open_nofollow();
298
299  /*
300   *  Grab the maximum size of an in-memory file.
301   */
302
303  max_size = IMFS_MEMFILE_MAXIMUM_SIZE;
304
305  build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
306  rtems_status = rtems_clock_set( &time );
307  directive_failed( rtems_status, "clock set" );
308
309  /*
310   *  Simple stat() of /dev/console.
311   */
312
313  puts( "stat of /dev/console" );
314  status = stat( "/dev/console", &buf );
315  rtems_test_assert( !status );
316
317  dump_statbuf( &buf );
318
319  /*
320   *  Exercise mkdir() and some path evaluation.
321   */
322
323  puts( "" );
324  puts( "mkdir /dev/tty" );
325  status = mkdir( "/dev/tty", S_IRWXU );
326  rtems_test_assert( !status );
327
328  puts( "" );
329  puts( "mkdir /usr" );
330  status = mkdir( "/usr", S_IRWXU );
331  rtems_test_assert( !status );
332  puts( "mkdir /etc" );
333  status = mkdir( "/etc", S_IRWXU );
334  rtems_test_assert( !status );
335
336  puts( "mkdir /tmp" );
337  status = mkdir( "/tmp", S_IRWXU );
338  rtems_test_assert( !status );
339
340  /* this tests the ".." path in path name evaluation */
341  puts( "mkdir /tmp/.." );
342  status = mkdir( "/tmp/..", S_IRWXU );
343  rtems_test_assert( status == -1 );
344  rtems_test_assert( errno == EEXIST );
345
346  /* now check out trailing separators */
347  puts( "mkdir /tmp/" );
348  status = mkdir( "/tmp/", S_IRWXU );
349  rtems_test_assert( status == -1 );
350  rtems_test_assert( errno == EEXIST );
351
352  /* try to make a directory under a non-existent subdirectory */
353  puts( "mkdir /j/j1" );
354  status = mkdir( "/j/j1", S_IRWXU );
355  rtems_test_assert( status == -1 );
356  rtems_test_assert( errno == ENOENT );
357
358  /* this tests the ability to make a directory in the current one */
359  puts( "mkdir tmp" );
360  status = mkdir( "tmp", S_IRWXU );
361  rtems_test_assert( status == -1 );
362  rtems_test_assert( errno == EEXIST );
363
364  /*
365   *  Now switch gears and exercise rmdir().
366   */
367
368  puts( "" );
369  puts( "rmdir /usr" );
370  status = rmdir( "/usr" );
371  rtems_test_assert( !status );
372
373  puts( "rmdir /dev" );
374  status = rmdir( "/dev" );
375  rtems_test_assert( status == -1 );
376  rtems_test_assert( errno ==  ENOTEMPTY);
377
378  puts( "rmdir /fred" );
379  status = rmdir ("/fred");
380  rtems_test_assert (status == -1);
381  rtems_test_assert( errno == ENOENT );
382
383  puts( "rmdir /tmp/bha" );
384  status = rmdir( "/tmp/bha" );
385  rtems_test_assert( status == -1 );
386  rtems_test_assert( errno == ENOENT );
387
388  puts( "mknod /dev/test_console" );
389  status = mknod( "/dev/test_console", S_IFCHR, 0LL );
390  rtems_test_assert( !status );
391
392  puts( "mknod /dev/tty/S3" );
393  status = mknod( "/dev/tty/S3", S_IFCHR, 0xFF00000080LL );
394  rtems_test_assert( !status );
395
396  puts ("mknod /etc/passwd");
397  status = mknod( "/etc/passwd", (S_IFREG | S_IRWXU), 0LL );
398  rtems_test_assert( !status );
399
400  puts( "mkdir /tmp/my_dir");
401  status = mkdir( "/tmp/my_dir", S_IRWXU );
402  rtems_test_assert( status == 0 );
403
404  puts("mkfifo /c/my_dir" );
405  status = mkfifo( "/c/my_dir", S_IRWXU );
406  rtems_test_assert( status == -1 );
407
408  /*
409   *  Try to make a directory under a file -- ERROR
410   */
411
412  puts( "mkdir /etc/passwd/j" );
413  status = mkdir( "/etc/passwd/j", S_IRWXU );
414  rtems_test_assert( status == -1 );
415  rtems_test_assert( errno == ENOTDIR );
416
417  /*
418   *  Simple open failure case on non-existent file
419   */
420
421  puts( "open /tmp/joel - should fail with ENOENT" );
422  fd = open( "/tmp/joel", O_RDONLY );
423  rtems_test_assert( fd == -1 );
424  rtems_test_assert( errno == ENOENT );
425
426  /*
427   *  Simple open case where the file is created.
428   */
429
430  puts( "open /tmp/j" );
431  fd = open( "/tmp/j", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
432  rtems_test_assert( fd != -1 );
433  printf( "open returned file descriptor %d\n", fd );
434
435  puts( "close /tmp/j" );
436  status = close( fd );
437  rtems_test_assert( !status );
438
439  puts( "close /tmp/j again" );
440  status = close( fd );
441  rtems_test_assert( status == -1 );
442
443  puts( "unlink /tmp/j" );
444  status = unlink( "/tmp/j" );
445  rtems_test_assert( !status );
446
447  puts( "unlink /tmp" );
448  status = unlink( "/tmp" );
449  rtems_test_assert( status );
450
451  /*
452   *  Simple open failure. Trying to create an existing file.
453   */
454
455  puts("create and close /tmp/tom");
456  fd = open( "/tmp/tom", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
457  rtems_test_assert( fd != -1 );
458  status = close( fd );
459  rtems_test_assert( status == 0 );
460
461  puts("Attempt to recreate /tmp/tom");
462  fd = open( "/tmp/tom", O_CREAT | O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO );
463  rtems_test_assert( fd == -1 );
464  rtems_test_assert( errno == EEXIST );
465
466  puts("create /tmp/john");
467  fd = open( "/tmp/john", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
468  rtems_test_assert( fd != -1 );
469  status = close( fd );
470  rtems_test_assert( status == 0 );
471
472  /*
473   * Open a file in read-only mode and try to truncate
474   */
475
476  puts( "Attempt to create a file, open in read-only mode and truncate it" );
477  fd = open( "/tmp/bha", O_CREAT | O_RDONLY | O_TRUNC, S_IRUSR );
478  rtems_test_assert( fd == -1 );
479  rtems_test_assert( errno == EINVAL );
480
481  puts( "Exercise the reentrant version _link_r -- Expect ENOENT" );
482  status = _link_r( NULL, "/tmp/notexist", "/tmp/cannotexist" );
483  rtems_test_assert( status == -1 );
484  rtems_test_assert( errno == ENOENT );
485
486  puts( "Unlink /tmp/bha using the reentrant version -- OK" );
487  status = _unlink_r( NULL, "/tmp/bha" );
488  rtems_test_assert( status == 0 );
489
490  /*
491   * Simple test case for mknod
492   */
493
494  puts( "mknod with bad type - expect EINVAL" );
495  status = mknod( "/tmp/bha", 0, 0LL );
496  rtems_test_assert( status == -1 );
497  rtems_test_assert( errno == EINVAL );
498
499  /*
500   * Read from filedes opened for write
501   */
502
503  puts( "open /tmp/bha in write only mode -- OK" );
504  fd = open( "/tmp/bha", O_CREAT | O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO );
505  rtems_test_assert( fd != -1 );
506
507  puts( "attempt fcntl on opened file -- OK" );
508  status = fcntl( fd, F_SETFD, 0 );
509  rtems_test_assert( status == 0 );
510
511  puts( "attempt to read from /tmp/bha - expect EBADF" );
512  status = read( fd, buffer, 10 );
513  rtems_test_assert( status == -1 );
514  rtems_test_assert( errno == EBADF );
515
516  puts( "closing and unlinking /tmp/bha" );
517  status = close( fd );
518  status |= unlink( "/tmp/bha" );
519  rtems_test_assert( status == 0 );
520
521  puts( "open /tmp/bha in read only mode -- OK" );
522  fd = open( "/tmp/bha", O_CREAT | O_RDONLY, S_IRWXU|S_IRWXG|S_IRWXO );
523  rtems_test_assert( fd != -1 );
524
525  puts( "attempt to read from /tmp/bha - expect EBADF" );
526  status = write( fd, buffer, 10 );
527  rtems_test_assert( status == -1 );
528  rtems_test_assert( errno == EBADF );
529
530  puts( "closing and unlinking /tmp/bha" );
531  status = close( fd );
532  status |= unlink( "/tmp/bha" );
533  rtems_test_assert( status == 0 );
534
535  /*
536   * Read/write from an unopened filedes
537   */
538  puts( "attempt to read from an unopened filedes - expect EBADF" );
539  status = read( 5, buffer, 10 );
540  rtems_test_assert( status == -1 );
541  rtems_test_assert( errno == EBADF );
542
543  puts( "attempt to write to an unopened filedes - expect EBADF" );
544  status = write( 5, buffer, 10 );
545  rtems_test_assert( status == -1 );
546  rtems_test_assert( errno == EBADF );
547
548  /*
549   *  Test simple write to a file at offset 0
550   */
551
552  puts( "mknod /tmp/joel" );
553  status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL );
554  test_write( "/tmp/joel", 0, "the first write!!!\n" );
555  test_cat( "/tmp/joel", 0, 0 );
556
557  /* Exercise _rename_r */
558
559  /* Simple rename test */
560  puts( "rename /tmp/joel to /tmp/drjoel");
561  status = _rename_r(NULL,"/tmp/joel","/tmp/drjoel");
562  rtems_test_assert(status == 0);
563
564  /* Simple rename test */
565  puts("rename /tmp/drjoel to /tmp/joel");
566  status = _rename_r(NULL,"/tmp/drjoel","/tmp/joel");
567  rtems_test_assert(status == 0);
568
569  /* Invalid old path */
570  puts("rename /tmp/drjoel to /tmp/joel - Should result in an error \
571since old path is not valid");
572  status = _rename_r(NULL,"/tmp/drjoel","/tmp/joel");
573  rtems_test_assert(status == -1);
574
575  /* Invalid new path */
576  puts("rename /tmp/joel to /tmp/drjoel/joel - Should result in an error \
577since new path is not valid");
578  status = _rename_r(NULL,"/tmp/joel","/tmp/drjoel/joel");
579  rtems_test_assert(status == -1);
580
581  puts("changing dir to /tmp");
582  status = chdir("/tmp/");
583  rtems_test_assert(status == 0);
584
585  puts("rename joel to drjoel");
586  status = _rename_r(NULL,"joel","drjoel");
587  rtems_test_assert(status == 0);
588
589  puts("rename drjoel to joel");
590  status = _rename_r(NULL,"drjoel","joel");
591  rtems_test_assert(status == 0);
592
593  /* Rename across file systems */
594  puts("creating directory /imfs");
595  status = mkdir("/imfs",0777);
596  rtems_test_assert(status == 0);
597  puts("creating directory /imfs/hidden_on_mount");
598  status = mkdir("/imfs/hidden_on_mount",0777);
599  rtems_test_assert(status == 0);
600
601  puts("mounting filesystem with IMFS_ops at /imfs");
602  status = mount("null", "/imfs", "imfs", RTEMS_FILESYSTEM_READ_WRITE, NULL);
603  rtems_test_assert(status == 0);
604  puts("creating directory /imfs/test (on newly mounted filesystem)");
605  status = mkdir("/imfs/test", 0777);
606  rtems_test_assert(status == 0);
607
608  puts("attempt to rename directory joel to /imfs/test/joel - should fail with EXDEV");
609  status = _rename_r(NULL, "joel", "/imfs/test/joel");
610  rtems_test_assert(status == -1);
611  rtems_test_assert(errno == EXDEV);
612
613  puts("changing dir to /");
614  status = chdir("/");
615  rtems_test_assert(status == 0);
616
617  puts("attempt to rename across filesystem, with old path having a parent node");
618  puts("attempt to rename tmp/joel to /imfs/test/joel");
619  status = _rename_r(NULL, "tmp/joel", "/imfs/test/joel");
620  rtems_test_assert(status == -1);
621  rtems_test_assert(errno == EXDEV);
622
623  puts("End of _rename_r tests");
624
625  /*
626   *  Test simple write to a file at a non-0 offset in the first block
627   */
628
629  status = unlink( "/tmp/joel" );
630  rtems_test_assert( !status );
631
632  status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL );
633  rtems_test_assert( !status );
634
635  test_write( "/tmp/joel", 10, "the first write!!!\n" );
636  test_cat( "/tmp/joel", 0, 0 );
637  stat_a_file( "/tmp/joel" );
638
639  /*
640   *  Test simple write to a file at a non-0 offset in the second block.  Then
641   *  try to read from various offsets and lengths.
642   */
643
644  puts("unlink /tmp/joel");
645  status = unlink( "/tmp/joel" );
646  rtems_test_assert( !status );
647
648  /* Test a failure path */
649
650  puts( "unlink /tmp/joel" );
651  status = unlink( "/tmp/joel" );
652  rtems_test_assert( status == -1 );
653
654  puts( "mknod /tmp/joel");
655  status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL );
656  rtems_test_assert( !status );
657
658  test_write( "/tmp/joel", 514, "the first write!!!\n" );
659  test_write( "/tmp/joel", 1, test_write_buffer );
660  test_write( "/tmp/joel", 63, test_write_buffer );
661  test_cat( "/tmp/joel", 0, 1 );
662  test_cat( "/tmp/joel", 1, 1 );
663  test_cat( "/tmp/joel", 490, 1 );
664  test_cat( "/tmp/joel", 512, 1 );
665  test_cat( "/tmp/joel", 513, 1 );
666  test_cat( "/tmp/joel", 514, 1 );
667  test_cat( "/tmp/joel", 520, 1 );
668  test_cat( "/tmp/joel", 1, 1024 );
669
670  /*
671   *  Read from a much longer file so we can descend into doubly and
672   *  triply indirect blocks.
673   */
674
675  if ( max_size < (size_t) 300 * 1024 ) {
676    test_extend( "/tmp/joel", max_size - 1 );
677    test_cat( "/tmp/joel", max_size / 2, 1024 );
678  } else {
679    printf( "Skipping maximum file size test since max_size is %zu bytes\n", max_size );
680    puts("That is likely to be bigger than the available RAM on many targets." );
681  }
682
683  stat_a_file( "/tmp/joel" );
684
685  /*
686   *  Now try to use a FILE * descriptor
687   *
688   *  /tmp/j should not exist at this point.
689   */
690
691  puts( "stat of /tmp/j" );
692  errno = 0;
693  status = stat( "/tmp/j", &buf );
694  printf( "stat(/tmp/j) returned %d (errno=%d)\n", status, errno );
695  dump_statbuf( &buf );
696
697  puts( "fopen of /tmp/j" );
698  file = fopen( "/tmp/j", "w+" );
699  rtems_test_assert( file );
700
701  puts( "fprintf to /tmp/j" );
702  for (i=1 ; i<=5 ; i++) {
703    status = fprintf( file, "This is call %d to fprintf\n", i );
704    rtems_test_assert( status );
705    printf( "(%d) %d characters written to the file\n", i, status );
706  }
707
708  fflush( file );
709
710  status = stat( "/tmp/j", &buf );
711  rtems_test_assert( !status );
712  dump_statbuf( &buf );
713  atime2 = buf.st_atime;
714  mtime2 = buf.st_mtime;
715  ctime2 = buf.st_ctime;
716
717
718  status = rtems_task_wake_after( rtems_clock_get_ticks_per_second() + 1);
719  rewind( file );
720  while ( fgets(buffer, 128, file) )
721    printf( "%s", buffer );
722
723  /*
724   * Verify only atime changed for a read.
725   */
726  status = stat( "/tmp/j", &buf );
727  rtems_test_assert( !status );
728  dump_statbuf( &buf );
729  atime1 = buf.st_atime;
730  mtime1 = buf.st_mtime;
731  ctime1 = buf.st_ctime;
732  rtems_test_assert( atime1 != atime2);
733  rtems_test_assert( mtime1 == mtime2);
734  rtems_test_assert( ctime1 == ctime2);
735
736  unlink( "/tmp/joel" );
737
738  /*
739   *  Now truncate a file
740   */
741
742  status = rtems_task_wake_after( rtems_clock_get_ticks_per_second() );
743  puts( "truncate /tmp/j to length of 40" );
744  status = truncate( "/tmp/j", 40 );
745  rtems_test_assert( !status );
746
747  /*
748   * Verify truncate changed all except atime.
749   */
750  status = stat( "/tmp/j", &buf );
751  rtems_test_assert( !status );
752  dump_statbuf( &buf );
753  atime2 = buf.st_atime;
754  mtime2 = buf.st_mtime;
755  ctime2 = buf.st_ctime;
756  rtems_test_assert( atime1 == atime2);
757  rtems_test_assert( mtime1 != mtime2);
758  rtems_test_assert( ctime1 != ctime2);
759
760  /* try to truncate the console and see what happens */
761  errno = 0;
762  status = truncate( "/dev/console", 40 );
763  rtems_test_assert( status == 0 || ( status == -1 && errno == EINVAL ) );
764
765  puts( "truncate /tmp/j to length of 0" );
766  status = truncate( "/tmp/j", 0 );
767  rtems_test_assert( !status );
768
769  puts( "truncate /tmp to length of 0 should fail with EISDIR\n");
770  status = truncate( "/tmp", 0 );
771  rtems_test_assert( status == -1 );
772  printf( "%d: %s\n", errno, strerror( errno ) );
773  rtems_test_assert( errno == EISDIR );
774
775  status = truncate( "/tmp/fred", 10 );
776  rtems_test_assert( status == -1);
777
778  rtems_status = rtems_io_register_name( "/dev/not_console", 0, 0 );
779  directive_failed( rtems_status, "io register" );
780
781  test_case_reopen_append();
782
783  TEST_END();
784  rtems_test_exit( 0 );
785}
786
787/*
788 *  Open/Create a File and write to it
789 *
790 *  Test case submitted by Andrew Bythell <abythell@nortelnetworks.com>.
791 *
792 */
793
794void test_file (char *filename, char *mode);
795
796void test_case_reopen_append(void)
797{
798  printf ("Writing First File\n");
799  test_file ("/one.txt", "a");
800  test_file ("/one.txt", "a");
801
802  /* but not the second time - this will insert junk.
803     the number of ^@'s seems to equal the number of
804     actual characters in the file */
805
806  printf ("Writing Second File\n");
807  test_file ("/two.txt", "a");
808  test_file ("/two.txt", "a");
809
810  test_cat( "/one.txt", 0, 1024 );
811  test_cat( "/two.txt", 0, 1024 );
812}
813
814void test_file (char *filename, char *mode)
815{
816  FILE *fp;
817  fp = fopen (filename, mode);
818  if (!fp)
819      perror ("fopen");
820  fprintf (fp, "this is a test line\n");
821  if (fclose (fp))
822      perror ("fclose");
823}
Note: See TracBrowser for help on using the repository browser.