source: rtems/testsuites/samples/fileio/init.c @ 05153a1

4.104.114.84.9
Last change on this file since 05153a1 was 05153a1, checked in by Ralf Corsepius <ralf.corsepius@…>, on Apr 1, 2004 at 10:14:40 AM

2004-04-01 Ralf Corsepius <ralf_corsepius@…>

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