| 1 | /////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 2 | // File   : main.c   (for shell application) | 
|---|
| 3 | // Date   : february 2014 | 
|---|
| 4 | // author : Alain Greiner | 
|---|
| 5 | /////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 6 | // This single thread application plays with files to test the FAT32. | 
|---|
| 7 | /////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 8 |  | 
|---|
| 9 | #include "stdio.h" | 
|---|
| 10 | #include "malloc.h" | 
|---|
| 11 |  | 
|---|
| 12 | #define NLINES   256 | 
|---|
| 13 | #define NPIXELS  256 | 
|---|
| 14 |  | 
|---|
| 15 | unsigned char buf[NLINES*NPIXELS]; | 
|---|
| 16 |  | 
|---|
| 17 | ////////////////////////////////////////// | 
|---|
| 18 | __attribute__ ((constructor)) void main() | 
|---|
| 19 | ////////////////////////////////////////// | 
|---|
| 20 | { | 
|---|
| 21 | giet_tty_alloc(); | 
|---|
| 22 |  | 
|---|
| 23 | giet_tty_printf("\n[SHELL] Enter at cycle %d\n", giet_proctime() ); | 
|---|
| 24 |  | 
|---|
| 25 | /////////////// display disk content | 
|---|
| 26 | giet_fat_list( "/" ); | 
|---|
| 27 | giet_fat_list( "/misc" ); | 
|---|
| 28 | giet_fat_list( "/home" ); | 
|---|
| 29 |  | 
|---|
| 30 | /////////////// open lena_256.raw | 
|---|
| 31 | int fd_lena = giet_fat_open( "/misc/lena_256.raw" , O_RDONLY ); | 
|---|
| 32 | if (fd_lena < 0 ) | 
|---|
| 33 | { | 
|---|
| 34 | giet_exit("cannot open file lena_256.raw"); | 
|---|
| 35 | } | 
|---|
| 36 | else | 
|---|
| 37 | { | 
|---|
| 38 | giet_tty_printf("\n[SHELL] open lena_256.raw\n"); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | /////////////// open copy.raw | 
|---|
| 42 | int fd_copy = giet_fat_open( "/home/copy.raw" , O_CREATE ); | 
|---|
| 43 | if (fd_copy < 0 ) | 
|---|
| 44 | { | 
|---|
| 45 | giet_exit("cannot open file copy.raw"); | 
|---|
| 46 | } | 
|---|
| 47 | else | 
|---|
| 48 | { | 
|---|
| 49 | giet_tty_printf("\n[SHELL] open copy.raw\n"); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | //////////////// load lena_256.raw | 
|---|
| 53 | if ( giet_fat_lseek( fd_lena , 0 , SEEK_SET ) ) | 
|---|
| 54 | { | 
|---|
| 55 | giet_exit("cannot seek lena_256.raw"); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | if ( giet_fat_read( fd_lena , buf , NLINES*NPIXELS ) != NLINES*NPIXELS ) | 
|---|
| 59 | { | 
|---|
| 60 | giet_exit("cannot read lena_256.raw"); | 
|---|
| 61 | } | 
|---|
| 62 | else | 
|---|
| 63 | { | 
|---|
| 64 | giet_tty_printf("\n[SHELL] lena_256.raw loaded from device\n"); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | ////////////// store copy.raw | 
|---|
| 68 | if ( giet_fat_lseek( fd_copy , 0 , SEEK_SET ) ) | 
|---|
| 69 | { | 
|---|
| 70 | giet_exit("cannot seek copy.raw"); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | if ( giet_fat_write( fd_copy , buf , NLINES*NPIXELS ) != NLINES*NPIXELS ) | 
|---|
| 74 | { | 
|---|
| 75 | giet_exit("cannot write copy.raw"); | 
|---|
| 76 | } | 
|---|
| 77 | else | 
|---|
| 78 | { | 
|---|
| 79 | giet_tty_printf("\n[SHELL] copy.raw stored to device\n"); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | ///////////// close lena_256.raw | 
|---|
| 83 | if ( giet_fat_close( fd_lena ) ) | 
|---|
| 84 | { | 
|---|
| 85 | giet_exit("cannot close lena_256.raw"); | 
|---|
| 86 | } | 
|---|
| 87 | else | 
|---|
| 88 | { | 
|---|
| 89 | giet_tty_printf("\n[SHELL] lena_256.raw closed\n"); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | ///////////// close copy.raw | 
|---|
| 93 | if ( giet_fat_close( fd_copy ) ) | 
|---|
| 94 | { | 
|---|
| 95 | giet_exit("cannot close copy.raw"); | 
|---|
| 96 | } | 
|---|
| 97 | else | 
|---|
| 98 | { | 
|---|
| 99 | giet_tty_printf("\n[SHELL] copy.raw closed\n"); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | ////////////// display disk content | 
|---|
| 103 | giet_fat_list( "/" ); | 
|---|
| 104 | giet_fat_list( "/misc" ); | 
|---|
| 105 | giet_fat_list( "/home" ); | 
|---|
| 106 |  | 
|---|
| 107 | ///////////// rename copy.raw | 
|---|
| 108 | if ( giet_fat_rename( "/home/copy.raw" , "/misc/copy_lena.raw" ) ) | 
|---|
| 109 | { | 
|---|
| 110 | giet_exit("cannot rename /home/copy.raw"); | 
|---|
| 111 | } | 
|---|
| 112 | else | 
|---|
| 113 | { | 
|---|
| 114 | giet_tty_printf("\n[SHELL] /home/copy.raw renamed to /misc/copy_lena.raw\n"); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | ////////////// display disk content | 
|---|
| 118 | giet_fat_list( "/" ); | 
|---|
| 119 | giet_fat_list( "/misc" ); | 
|---|
| 120 | giet_fat_list( "/home" ); | 
|---|
| 121 |  | 
|---|
| 122 | ///////////// remove home | 
|---|
| 123 | if ( giet_fat_remove( "/home" , 1 ) ) | 
|---|
| 124 | { | 
|---|
| 125 | giet_exit("cannot remove /home"); | 
|---|
| 126 | } | 
|---|
| 127 | else | 
|---|
| 128 | { | 
|---|
| 129 | giet_tty_printf("\n[SHELL] /home removed from file system\n"); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | ////////////// display disk content | 
|---|
| 133 | giet_fat_list( "/" ); | 
|---|
| 134 | giet_fat_list( "/misc" ); | 
|---|
| 135 |  | 
|---|
| 136 | //////////// create new_home | 
|---|
| 137 | if ( giet_fat_mkdir( "/new_home" ) ) | 
|---|
| 138 | { | 
|---|
| 139 | giet_exit("cannot create /new_home"); | 
|---|
| 140 | } | 
|---|
| 141 | else | 
|---|
| 142 | { | 
|---|
| 143 | giet_tty_printf("\n[SHELL] /new_home created in file system\n"); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | ////////////// display disk content | 
|---|
| 147 | giet_fat_list( "/" ); | 
|---|
| 148 | giet_fat_list( "/misc" ); | 
|---|
| 149 | giet_fat_list( "/new_home" ); | 
|---|
| 150 |  | 
|---|
| 151 | giet_exit("Completed"); | 
|---|
| 152 |  | 
|---|
| 153 | } // end main() | 
|---|
| 154 |  | 
|---|
| 155 | // Local Variables: | 
|---|
| 156 | // tab-width: 3 | 
|---|
| 157 | // c-basic-offset: | 
|---|
| 158 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) | 
|---|
| 159 | // indent-tabs-mode: nil | 
|---|
| 160 | // End: | 
|---|
| 161 |  | 
|---|
| 162 | // vim: filetype=cpp:expandtab:shiftwidth=3:tabstop=3:softtabstop=3 | 
|---|
| 163 |  | 
|---|
| 164 |  | 
|---|
| 165 |  | 
|---|