source: rtems/c/src/tests/samples/fileio/init.c @ f27bdfc

Last change on this file since f27bdfc was a97ee41, checked in by cvs2git <rtems-devel@…>, on 08/06/03 at 19:20:53

This commit was manufactured by cvs2svn to create branch 'rtems-4-6-branch'.

Cherrypick from master 2003-08-06 19:20:52 UTC Jennifer Averett <Jennifer.Averett@…> '2003-08-06 Thomas Doerfler<Thomas.Doerfler@…>':

c/src/tests/samples/fileio/Makefile.am
c/src/tests/samples/fileio/fileio.doc
c/src/tests/samples/fileio/init.c
c/src/tests/samples/fileio/system.h
cpukit/libmisc/fsmount/Makefile.am
cpukit/libmisc/fsmount/README
cpukit/libmisc/fsmount/fsmount.c
cpukit/libmisc/fsmount/fsmount.h

Cherrypick from master 2003-06-18 15:15:48 UTC Ralf Corsepius <ralf.corsepius@…> '2003-06-18 Ralf Corsepius <corsepiu@…>':

bootstrap
config.sub

  • Property mode set to 100644
File size: 14.2 KB
Line 
1/*  Init
2 *
3 *  This routine is the initialization task for this test program.
4 *  It is called from init_exec and has the responsibility for creating
5 *  and starting the tasks that make up the test.  If the time of day
6 *  clock is required for the test, it should also be set to a known
7 *  value by this function.
8 *
9 *  Input parameters:  NONE
10 *
11 *  Output parameters:  NONE
12 *
13 *  COPYRIGHT (c) 1989-1999.
14 *  On-Line Applications Research Corporation (OAR).
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.OARcorp.com/rtems/license.html.
19 *
20 *  init.c,v 1.11 2000/06/12 15:00:12 joel Exp
21 */
22
23#define CONFIGURE_INIT
24#include "system.h"
25#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <errno.h>
30#include <rtems.h>
31#include <fcntl.h>
32#include <rtems/error.h>
33#include <dosfs.h>
34#include <ctype.h>
35#include <rtems/ide_part_table.h>
36#include <rtems/libcsupport.h>
37#include <rtems/fsmount.h>
38
39/*
40 * Table of FAT file systems that will be mounted
41 * with the "fsmount" function.
42 * See cpukit/libmisc/fsmount for definition of fields
43 */
44fstab_t fs_table[] = {
45  {
46    "/dev/hda1","/mnt/hda1",
47    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
48    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
49    0
50  },
51  {
52    "/dev/hda2","/mnt/hda2",
53    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
54    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
55    0
56  },
57  {
58    "/dev/hda3","/mnt/hda3",
59    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
60    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
61    0
62  },
63  {
64    "/dev/hda4","/mnt/hda4",
65    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
66    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
67    0
68  },
69  {
70    "/dev/hdc1","/mnt/hdc1",
71    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
72    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
73    0
74  },
75  {
76    "/dev/hdc2","/mnt/hdc2",
77    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
78    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
79    0
80  },
81  {
82    "/dev/hdc3","/mnt/hdc3",
83    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
84    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
85    0
86  },
87  {
88    "/dev/hdc4","/mnt/hdc4",
89    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
90    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
91    0
92  }
93};
94
95#ifndef MIN
96#define MIN(a,b) (((a) > (b)) ? (b) : (a))
97#endif
98
99#define USE_SHELL
100
101#ifdef USE_SHELL
102#include <rtems/shell.h>
103
104void fileio_start_shell(void)
105{
106  printf(" =========================\n");
107  printf(" starting shell\n");
108  printf(" =========================\n");
109  shell_init("SHLL",0,100,"/dev/console",
110             B9600 | CS8,
111             0);
112  rtems_task_suspend(RTEMS_SELF);
113}
114
115#endif /* USE_SHELL */
116
117void fileio_print_free_heap(void)
118{
119  printf("--- unused dynamic memory: %lu bytes ---\n",
120         (unsigned long) malloc_free_space());
121}
122
123
124void fileio_part_table_initialize(void)
125{
126  char devname[64];
127  rtems_status_code rc;
128
129  printf(" =========================\n");
130  printf(" Initialize partition table\n");
131  printf(" =========================\n");
132  fileio_print_free_heap();
133  printf(" Enter device to initialize ==>");
134  fgets(devname,sizeof(devname)-1,stdin);
135  while (devname[strlen(devname)-1] == '\n') {
136    devname[strlen(devname)-1] = '\0';
137  }
138  /*
139   * call function
140   */
141  rc = rtems_ide_part_table_initialize(devname);
142  printf("result = %d\n",rc);
143  fileio_print_free_heap();
144}
145
146void fileio_fsmount(void)
147{
148  rtems_status_code rc;
149
150  printf(" =========================\n");
151  printf(" Process fsmount table\n");
152  printf(" =========================\n");
153  fileio_print_free_heap();
154  /*
155   * call function
156   */
157  rc = rtems_fsmount( fs_table,
158                      sizeof(fs_table)/sizeof(fs_table[0]),
159                      NULL);
160  printf("result = %d\n",rc);
161  fileio_print_free_heap();
162}
163
164void fileio_list_file(void)
165{
166  char fname[1024];
167  char *buf_ptr = NULL;
168  unsigned32 flen = 0;
169  int fd = -1;
170  ssize_t n;
171  size_t buf_size = 100;
172
173  rtems_interval start_tick,curr_tick,ticks_per_sec;
174
175  printf(" =========================\n");
176  printf(" LIST FILE ...            \n");
177  printf(" =========================\n");
178  fileio_print_free_heap();
179  printf(" Enter filename to list ==>");
180  fgets(fname,sizeof(fname)-1,stdin);
181  while (fname[strlen(fname)-1] == '\n') {
182    fname[strlen(fname)-1] = '\0';
183  }
184  /*
185   * allocate buffer of given size
186   */
187  if (buf_size > 0) {
188    buf_ptr = malloc(buf_size);
189  }
190
191  if (buf_ptr != NULL) {
192    printf("\n Trying to open file \"%s\" for read\n",fname);
193    fd = open(fname,O_RDONLY);
194    if (fd < 0) {
195      printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno));
196    }
197  }
198
199  if (fd >= 0) {
200    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
201    do {
202      n = read(fd,buf_ptr,buf_size);
203      if (n > 0) {
204        write(1,buf_ptr,n);
205        flen += n;
206      }
207    } while (n > 0);
208   
209    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
210   
211    printf("\n ******** End of file reached, flen = %d\n",flen);
212    close(fd);
213   
214    rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
215    printf("time elapsed for read:  %g seconds\n",
216           ((double)curr_tick-start_tick)/ticks_per_sec);
217  }
218  /*
219   * free buffer
220   */
221  if (buf_ptr != NULL) {
222    free(buf_ptr);
223  }
224  fileio_print_free_heap();
225}
226
227/*
228 * convert a size string (like 34K or 12M) to actual byte count
229 */
230boolean fileio_str2size(const char *str,unsigned32 *res_ptr)
231{
232  boolean failed = FALSE;
233  unsigned long size;
234  char suffix = ' ';
235
236  if (1 > sscanf(str,"%lu%c",&size,&suffix)) {
237    failed = TRUE;
238  }
239  else if (toupper(suffix) == 'K') {
240    size *= 1024;
241  }
242  else if (toupper(suffix) == 'M') {
243    size *= 1024UL*1024UL;
244  }
245  else if (isalpha(suffix)) {
246    failed = TRUE;
247  }
248
249  if (!failed) {
250    *res_ptr = size;
251  }
252  return failed;
253}
254
255void fileio_write_file(void)
256{
257  char fname[1024];
258  char tmp_str[32];
259  unsigned32 file_size = 0;
260  unsigned32 buf_size  = 0;
261  size_t curr_pos,bytes_to_copy;
262  int fd = -1;
263  ssize_t n;
264  rtems_interval start_tick,curr_tick,ticks_per_sec;
265  char *bufptr = NULL;
266  boolean failed = FALSE;
267  static const char write_test_string[] =
268    "The quick brown fox jumps over the lazy dog\n";
269  static const char write_block_string[] =
270    "\n----- end of write buffer ------\n";
271 
272  printf(" =========================\n");
273  printf(" WRITE FILE ...           \n");
274  printf(" =========================\n");
275  fileio_print_free_heap();
276  /*
277   * get number of ticks per second
278   */
279  rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
280
281  /*
282   * get path to file to write
283   */
284  if (!failed) {
285    printf("Enter path/filename ==>");
286    fgets(fname,sizeof(fname)-1,stdin);
287    while (fname[strlen(fname)-1] == '\n') {
288      fname[strlen(fname)-1] = '\0';
289    }
290    if (0 == strlen(fname)) {
291      printf("*** no filename entered, aborted\n");
292      failed = TRUE;
293    }
294  }
295  /*
296   * get total file size to write
297   */
298  if (!failed) {
299    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
300           "Enter filesize to write ==>");
301    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
302    failed = fileio_str2size(tmp_str,&file_size);
303    if (failed) {
304      printf("*** illegal file size, aborted\n");
305    }
306  }
307  /*
308   * get block size to write
309   */
310  if (!failed) {
311    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
312           "Enter block size to use for write calls ==>");
313    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
314    failed = fileio_str2size(tmp_str,&buf_size);
315    if (failed) {
316      printf("*** illegal block size, aborted\n");
317    }
318  }
319
320  /*
321   * allocate buffer
322   */
323  if (!failed) {
324    printf("... allocating %lu bytes of buffer for write data\n",
325           (unsigned long)buf_size);
326    bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */
327    if (bufptr == NULL) {
328      printf("*** malloc failed, aborted\n");
329      failed = TRUE;
330    }
331  }
332  /*
333   * fill buffer with test pattern
334   */
335  if (!failed) {
336    printf("... filling buffer with write data\n");
337    curr_pos = 0;
338    /*
339     * fill buffer with test string
340     */
341    while (curr_pos < buf_size) {
342      bytes_to_copy = MIN(buf_size-curr_pos,
343                          sizeof(write_test_string)-1);
344      memcpy(bufptr+curr_pos,write_test_string,bytes_to_copy);
345      curr_pos += bytes_to_copy;
346    }
347    /*
348     * put "end" mark at end of buffer
349     */
350    bytes_to_copy = sizeof(write_block_string)-1;
351    if (buf_size >= bytes_to_copy) {
352      memcpy(bufptr+buf_size-bytes_to_copy,
353             write_block_string,
354             bytes_to_copy);
355    }
356  }
357  /*
358   * create file
359   */
360  if (!failed) {
361    printf("... creating file \"%s\"\n",fname);
362    fd = open(fname,O_WRONLY | O_CREAT | O_TRUNC,S_IREAD|S_IWRITE);
363    if (fd < 0) {
364      printf("*** file create failed, errno = %d(%s)\n",errno,strerror(errno));
365      failed = TRUE;
366    }
367  }
368  /*
369   * write file
370   */
371  if (!failed) {
372    printf("... writing to file\n");
373    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
374    curr_pos = 0;
375    do {     
376      bytes_to_copy = buf_size;
377      do {
378        n = write(fd,
379          bufptr + (buf_size-bytes_to_copy),
380                  MIN(bytes_to_copy,file_size-curr_pos));
381        if (n > 0) {
382          bytes_to_copy -= n;
383          curr_pos      += n;
384        }
385      } while ((bytes_to_copy > 0)  && (n > 0));
386    } while ((file_size > curr_pos) && (n > 0));
387    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
388    if (n < 0) {
389      failed = TRUE;
390      printf("*** file write failed, "
391             "%lu bytes written, "
392             "errno = %d(%s)\n",
393             (unsigned long)curr_pos,errno,strerror(errno));
394    }
395    else {
396      printf("time elapsed for write:  %g seconds\n",
397             ((double)curr_tick-start_tick)/ticks_per_sec);
398      printf("write data rate: %g KBytes/second\n",
399             (((double)file_size) / 1024.0 /
400              (((double)curr_tick-start_tick)/ticks_per_sec)));
401    }
402  }
403  if (fd >= 0) {
404    printf("... closing file\n");
405    close(fd);
406  }
407  if (bufptr != NULL) {
408    printf("... deallocating buffer\n");
409    free(bufptr);
410    bufptr = NULL;
411  }
412  printf("\n ******** End of file write\n");
413  fileio_print_free_heap();
414}
415
416void fileio_read_file(void)
417{
418  char fname[1024];
419  char tmp_str[32];
420  unsigned32 buf_size  = 0;
421  size_t curr_pos;
422  int fd = -1;
423  ssize_t n;
424  rtems_interval start_tick,curr_tick,ticks_per_sec;
425  char *bufptr = NULL;
426  boolean failed = FALSE;
427 
428  printf(" =========================\n");
429  printf(" READ FILE ...            \n");
430  printf(" =========================\n");
431  fileio_print_free_heap();
432  /*
433   * get number of ticks per second
434   */
435  rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
436
437  /*
438   * get path to file to read
439   */
440  if (!failed) {
441    printf("Enter path/filename ==>");
442    fgets(fname,sizeof(fname)-1,stdin);
443    while (fname[strlen(fname)-1] == '\n') {
444      fname[strlen(fname)-1] = '\0';
445    }
446    if (0 == strlen(fname)) {
447      printf("*** no filename entered, aborted\n");
448      failed = TRUE;
449    }
450  }
451  /*
452   * get block size to read
453   */
454  if (!failed) {
455    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
456           "Enter block size to use for read calls ==>");
457    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
458    failed = fileio_str2size(tmp_str,&buf_size);
459    if (failed) {
460      printf("*** illegal block size, aborted\n");
461    }
462  }
463
464  /*
465   * allocate buffer
466   */
467  if (!failed) {
468    printf("... allocating %lu bytes of buffer for write data\n",
469           (unsigned long)buf_size);
470    bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */
471    if (bufptr == NULL) {
472      printf("*** malloc failed, aborted\n");
473      failed = TRUE;
474    }
475  }
476  /*
477   * open file
478   */
479  if (!failed) {
480    printf("... opening file \"%s\"\n",fname);
481    fd = open(fname,O_RDONLY);
482    if (fd < 0) {
483      printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno));
484      failed = TRUE;
485    }
486  }
487  /*
488   * read file
489   */
490  if (!failed) {
491    printf("... reading from file\n");
492    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
493    curr_pos = 0;
494    do {     
495      n = read(fd,
496               bufptr,
497               buf_size);
498      if (n > 0) {
499        curr_pos      += n;
500      }
501    } while (n > 0);
502    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
503    if (n < 0) {
504      failed = TRUE;
505      printf("*** file read failed, "
506             "%lu bytes read, "
507             "errno = %d(%s)\n",
508             (unsigned long)curr_pos,errno,strerror(errno));
509    }
510    else {
511      printf("%lu bytes read\n",
512             (unsigned long)curr_pos);
513      printf("time elapsed for read:  %g seconds\n",
514             ((double)curr_tick-start_tick)/ticks_per_sec);
515      printf("read data rate: %g KBytes/second\n",
516             (((double)curr_pos) / 1024.0 /
517              (((double)curr_tick-start_tick)/ticks_per_sec)));
518    }
519  }
520  if (fd >= 0) {
521    printf("... closing file\n");
522    close(fd);
523  }
524  if (bufptr != NULL) {
525    printf("... deallocating buffer\n");
526    free(bufptr);
527    bufptr = NULL;
528  }
529  printf("\n ******** End of file read\n");
530  fileio_print_free_heap();
531
532}
533
534void fileio_menu (void)
535{
536  char inbuf[10];
537
538  /*
539   * Wait for characters from console terminal
540   */
541  for (;;) {
542    printf(" =========================\n");
543    printf(" RTEMS FILE I/O Test Menu \n");
544    printf(" =========================\n");
545    printf("   p -> part_table_initialize\n");
546    printf("   f -> mount all disks in fs_table\n");
547    printf("   l -> list  file\n");
548    printf("   r -> read  file\n");
549    printf("   w -> write file\n");
550#ifdef USE_SHELL
551    printf("   s -> start shell\n");
552#endif
553    printf("   Enter your selection ==>");
554
555    inbuf[0] = '\0';
556    fgets(inbuf,sizeof(inbuf),stdin);
557    switch (inbuf[0]) {
558    case 'l':
559      fileio_list_file ();                     
560      break;
561    case 'r':
562      fileio_read_file ();                     
563      break;
564    case 'w':
565      fileio_write_file ();                     
566      break;
567    case 'p':
568      fileio_part_table_initialize ();                 
569      break;
570    case 'f':
571      fileio_fsmount ();                       
572      break;
573#ifdef USE_SHELL
574    case 's':
575      fileio_start_shell ();                   
576      break;
577#endif
578    default:
579      printf("Selection `%c` not implemented\n",inbuf[0]);
580      break;
581    }
582   
583  }
584  exit (0);
585}
586
587int menu_tid;
588
589/*
590 * RTEMS Startup Task
591 */
592rtems_task
593Init (rtems_task_argument ignored)
594{
595  puts( "\n\n*** FILE I/O SAMPLE AND TEST ***" );
596
597  fileio_menu();
598}
599
600
Note: See TracBrowser for help on using the repository browser.