-
Notifications
You must be signed in to change notification settings - Fork 108
/
system.c
1407 lines (1270 loc) · 33.5 KB
/
system.c
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file system.c
* @brief newlib Interface Hooks
* @ingroup system
*/
#include <_ansi.h>
#include <_syslist.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include "system.h"
#include "n64sys.h"
#undef errno
/**
* @defgroup system newlib Interface Hooks
* @brief System hooks to provide low level threading and filesystem functionality to newlib.
*
* newlib provides all of the standard C libraries for homebrew development.
* In addition to standard C libraries, newlib provides some additional bridging
* functionality to allow POSIX function calls to be tied into libdragon.
* Currently this is used only for filesystems. The newlib interface hooks here
* are mostly stubs that allow homebrew applications to compile.
*
* The sbrk function is responsible for allowing newlib to find the next chunk
* of free space for use with malloc calls. This is made somewhat complicated
* on the N64 by the fact that precompiled code doesn't know in advance if
* expanded memory is available. libdragon attempts to determine if this additional
* memory is available and return accordingly but can only do so if it knows what
* type of CIC/bootcode was used. If you are using a 6102, this has been set for
* you already. If you are using a 6105 for some reason, you will need to use
* #sys_set_boot_cic to notify libdragon or malloc will not work properly!
*
* libdragon has defined a custom callback structure for filesystems to use.
* Providing relevant hooks for calls that your filesystem supports and passing
* the resulting structure to #attach_filesystem will hook your filesystem into
* newlib. Calls to POSIX file operations will be passed on to your filesystem
* code if the file prefix matches, allowing code to make use of your filesystyem
* without being rewritten.
*
* For example, your filesystem provides libdragon an interface to access a
* homebrew SD card interface. You register a filesystem with "sd:/" as the prefix
* and then attempt to open "sd://directory/file.txt". The open callback for your
* filesystem will be passed the file "/directory/file.txt". The file handle returned
* will be passed into all subsequent calls to your filesystem until the file is
* closed.
* @{
*/
/**
* @name STDIN/STDOUT/STDERR definitions from unistd.h
*
* We can't just include unistd.h as it redefines several of the functions
* here that we are attempting to replace.
*
* @{
*/
/** @brief Standard input file descriptor */
#define STDIN_FILENO 0
/** @brief Standard output file descriptor */
#define STDOUT_FILENO 1
/** @brief Standard error file descriptor */
#define STDERR_FILENO 2
/** @} */
/**
* @brief Stack size
*
* This is the maximum stack size for the purpose of malloc. Any malloc
* call that tries to allocate data will not allocate within this range.
* However, there is no guarantee that user code won't blow the stack and
* cause heap corruption. Use this as loose protection at best.
*/
#define STACK_SIZE 0x10000
/**
* @brief Write to the MESS debug register
*
* @param[in] x
* 32-bit value to write to the MESS debug register
*/
#define DEBUG_OUT( x ) ((uint32_t *)0xA4400044)[0] = ((uint32_t)(x))
/**
* @brief Environment variables
*/
char *__env[1] = { 0 };
/**
* @brief Definition of errno, as it's defined as extern across stdlib
*/
int errno __attribute__((weak));
/**
* @brief Assert function pointer (initialized at startup)
*/
void (*__assert_func_ptr)(const char *file, int line, const char *func, const char *failedexpr) = 0;
/* Externs from libdragon */
extern int __bootcic;
extern void enable_interrupts();
extern void disable_interrupts();
/**
* @brief Filesystem mapping structure
*
* This is used to look up what filesystem to use when passed
* a generic path.
*/
typedef struct
{
/** @brief Filesystem prefix
*
* This controls what filesystem prefix should link to this filesystem
* (eg. 'rom:/' or 'cf:/')
*/
char *prefix;
/** @brief Filesystem callback pointers */
filesystem_t *fs;
} fs_mapping_t;
/**
* @brief Filesystem open handle structure
*
* This is used to look up the correct filesystem function to call
* when working with an open file handle
*/
typedef struct
{
/** @brief Index into `filesystems` array. */
int fs_mapping;
/** @brief The handle assigned to this open file as returned by the
* filesystem code called to handle the open operation. Will
* be passed to all subsequent file operations on the file. */
void *handle;
/** @brief The handle assigned by the filesystem code that will be returned
* to newlib. All subsequent newlib calls will use this handle which
* will be used to look up the internal reference. */
int fileno;
} fs_handle_t;
/** @brief Array of filesystems registered */
static fs_mapping_t filesystems[MAX_FILESYSTEMS] = { { 0 } };
/** @brief Array of open handles tracked */
static fs_handle_t handles[MAX_OPEN_HANDLES] = { { 0 } };
/** @brief Current stdio hook structure */
static stdio_t stdio_hooks = { 0 };
/** @brief Function to provide the current time */
time_t (*time_hook)( void ) = NULL;
/* Forward definitions */
int close( int fildes );
/**
* @brief Simple implementation of strlen
*
* @note We can't link against regular libraries, so this is reimplemented
*
* @param[in] str
* Pointer to null-terminated string
*
* @return Length of string
*/
static int __strlen( const char * const str )
{
if( !str ) { return 0; }
int len = 0;
while( str[len] != 0 )
{
len++;
}
return len;
}
/**
* @brief Simple implementation of memcpy
*
* @note We can't link against regular libraries, so this is reimplemented
*
* @param[out] a
* Destination pointer to copy to
* @param[in] b
* Source pointer to copy from
* @param[in] len
* Length in bytes to copy
*/
static void __memcpy( char * const a, const char * const b, int len )
{
for( int i = 0; i < len; i++ )
{
a[i] = b[i];
}
}
/**
* @brief Simple implementation of strdup
*
* @note We can't link against regular libraries, so this is reimplemented
*
* @param[in] in
* String to duplicate
*
* @return Pointer to newly allocated memory containing a copy of the input string
*/
static char *__strdup( const char * const in )
{
if( !in ) { return 0; }
char *ret = malloc( __strlen( in ) + 1 );
__memcpy( ret, in, __strlen( in ) + 1 );
return ret;
}
/**
* @brief Simple implementation of strncmp
*
* @note We can't link against regular libraries, so this is reimplemented
*
* @param[in] a
* First string to compare against
* @param[in] b
* Second string to compare against
* @param[in] len
* Number of relevant characters. Specify -1 for infinite
*
* @return 0 if the two strings match or nonzero otherwise
*
* @note different from the standard strncmp
*/
static int __strncmp( const char * const a, const char * const b, int len )
{
if( !a || !b ) { return 0; }
int cur = 0;
while( cur != len )
{
/* Only care about equality */
if( a[cur] != b[cur] ) { return 1; }
if( a[cur] == 0 && b[cur] == 0 ) { return 0; }
if( a[cur] == 0 || b[cur] == 0 ) { return 1; }
/* Next please */
cur++;
}
/* Must have worked */
return 0;
}
/**
* @brief Simple implementation of strcmp
*
* @note We can't link against regular libraries, so this is reimplemented
*
* @param[in] a
* First string to compare against
* @param[in] b
* Second string to compare against
*
* @return 0 if the two strings match or nonzero otherwise
*
* @note different from the standard strcmp
*/
static int __strcmp( const char * const a, const char * const b )
{
return __strncmp( a, b, -1 );
}
/**
* @brief Return a unique filesystem handle
*
* @return A unique 32-bit value usable as a filesystem handle
*/
static int __get_new_handle()
{
/* Start past STDIN, STDOUT, STDERR file handles */
static int handle = 3;
int newhandle;
disable_interrupts();
/* Always give out a nonzero handle unique to the system */
newhandle = handle++;
enable_interrupts();
return newhandle;
}
/**
* @brief Register a filesystem with newlib
*
* This function will take a prefix in the form of 'prefix:/' and a pointer
* to a filesystem structure of relevant callbacks and register it with newlib.
* Any standard open/fopen calls with the registered prefix will be passed
* to this filesystem. Userspace code does not need to know the underlying
* filesystem, only the prefix that it has been registered under.
*
* The filesystem pointer passed in to this function should not go out of scope
* for the lifetime of the filesystem.
*
* @param[in] prefix
* Prefix of the filesystem
* @param[in] filesystem
* Structure of callbacks for various functions in the filesystem.
* If the registered filesystem doesn't support an operation, it
* should leave the callback null.
*
* @retval -1 if the parameters are invalid
* @retval -2 if the prefix is already in use
* @retval -3 if there are no more slots for filesystems
* @retval 0 if the filesystem was registered successfully
*/
int attach_filesystem( const char * const prefix, filesystem_t *filesystem )
{
/* Sanity checking */
if( !prefix || !filesystem )
{
errno = EINVAL;
return -1;
}
/* Make sure prefix is valid */
int len = __strlen( prefix );
if( len < 3 || prefix[len - 1] != '/' || prefix[len - 2] != ':' )
{
errno = EINVAL;
return -1;
}
/* Make sure the prefix doesn't match one thats already inserted */
for( int i = 0; i < MAX_FILESYSTEMS; i++ )
{
if( filesystems[i].prefix )
{
if( __strcmp( filesystems[i].prefix, prefix ) == 0 )
{
/* Filesystem has already been inserted */
errno = EPERM;
return -2;
}
}
}
/* Find an open handle */
int handle = -1;
for( int i = 0; i < MAX_FILESYSTEMS; i++ )
{
if( !filesystems[i].prefix )
{
/* Found one */
handle = i;
break;
}
}
if( handle == -1 )
{
/* No more filesystem handles available */
errno = ENOMEM;
return -3;
}
/* Attach the prefix */
filesystems[handle].prefix = __strdup( prefix );
/* Attach the inputted filesystem */
filesystems[handle].fs = filesystem;
/* All went well */
return 0;
}
/**
* @brief Unregister a filesystem from newlib
*
* @note This function will make sure all files are closed before unregistering
* the filesystem.
*
* @param[in] prefix
* The prefix that was used to register the filesystem
*
* @retval -1 if the parameters were invalid
* @retval -2 if the filesystem couldn't be found
* @retval 0 if the filesystem was successfully unregistered
*/
int detach_filesystem( const char * const prefix )
{
/* Sanity checking */
if( !prefix )
{
errno = EINVAL;
return -1;
}
for( int i = 0; i < MAX_FILESYSTEMS; i++ )
{
if( filesystems[i].prefix )
{
if( __strcmp( filesystems[i].prefix, prefix ) == 0 )
{
/* We found the filesystem, now go through and close every open file handle */
for( int j = 0; j < MAX_OPEN_HANDLES; j++ )
{
if( handles[j].fileno > 0 && handles[j].fs_mapping == i )
{
close( handles[j].fileno );
}
}
/* Now free the memory associated with the prefix and zero out the filesystem */
free( filesystems[i].prefix );
filesystems[i].prefix = 0;
filesystems[i].fs = 0;
/* All went well */
return 0;
}
}
}
/* Couldn't find the filesystem to free */
errno = EPERM;
return -2;
}
/**
* @brief Get a filesystem pointer by handle
*
* Given a file handle, return the filesystem callback structure.
*
* @param[in] fileno
* File handle
*
* @return Pointer to a filesystem callback structure or null if not found.
*/
static filesystem_t *__get_fs_pointer_by_handle( int fileno )
{
/* Invalid */
if( fileno <= 0 )
{
return 0;
}
for( int i = 0; i < MAX_OPEN_HANDLES; i++ )
{
if( handles[i].fileno == fileno )
{
/* Found it */
return filesystems[handles[i].fs_mapping].fs;
}
}
/* Couldn't find it */
return 0;
}
/**
* @brief Get the index of the registered filesystem based on fully qualified filename
*
* @param[in] name
* The filename of the file being opened including the prefix
*
* @return The index of the registered filesystem if found or -1 if not found.
*/
static int __get_fs_link_by_name( const char * const name )
{
/* Invalid */
if( !name )
{
return -1;
}
for( int i = 0; i < MAX_FILESYSTEMS; i++ )
{
if( filesystems[i].prefix )
{
if( __strncmp( filesystems[i].prefix, name, __strlen( filesystems[i].prefix ) ) == 0 )
{
/* Found it */
return i;
}
}
}
/* Couldn't find it */
return -1;
}
/**
* @brief Get the filesystem callback structure based on a fully qualified filename
*
* @param[in] name
* The filename of the file being opened including the prefix
*
* @return Pointer to a filesystem callback structure or null if not found.
*/
static filesystem_t *__get_fs_pointer_by_name( const char * const name )
{
int fs = __get_fs_link_by_name( name );
if( fs >= 0 )
{
return filesystems[fs].fs;
}
else
{
return 0;
}
}
/**
* @brief Look up the internal handle of a file given a file handle
*
* @param[in] fileno
* File handle
*
* @return The internal file handle to be passed to the filesystem function or null if not found.
*/
static void *__get_fs_handle( int fileno )
{
/* Invalid */
if( fileno <= 0 )
{
return 0;
}
for( int i = 0; i < MAX_OPEN_HANDLES; i++ )
{
if( handles[i].fileno == fileno )
{
/* Found it */
return handles[i].handle;
}
}
/* Couldn't find it */
return 0;
}
/**
* @brief Change ownership on a file or directory
*
* @note Not supported in libdragon
*
* @param[in] path
* Path of the file or directory to operate on
* @param[in] owner
* New owner of the file
* @param[in] group
* New group of the file
*
* @return 0 on success or a negative value on error.
*/
int chown( const char *path, uid_t owner, gid_t group )
{
/* No permissions support in libdragon */
errno = ENOSYS;
return -1;
}
/**
* @brief Close a file
*
* @param[in] fildes
* File handle of the file to close
*
* @return 0 on success or a negative value on error.
*/
int close( int fildes )
{
filesystem_t *fs = __get_fs_pointer_by_handle( fildes );
void *handle = __get_fs_handle( fildes );
if( fs == 0 )
{
errno = EINVAL;
return -1;
}
/* Free the open file handle */
for( int i = 0; i < MAX_OPEN_HANDLES; i++)
{
if( handles[i].fileno == fildes )
{
handles[i].fs_mapping = 0;
handles[i].handle = NULL;
handles[i].fileno = 0;
}
}
if( fs->close == 0 )
{
/* Filesystem doesn't support close */
errno = ENOSYS;
return -1;
}
/* Tell the filesystem to close the file */
return fs->close( handle );
}
/**
* @brief Load and execute an executable given a path
*
* @note Not supported in libdragon
*
* @param[in] name
* Filename of the executable
* @param[in] argv
* Array of pointers to arguments
* @param[in] env
* Array of pointers to environment variables
*
* @return 0 on success or a negative value on error.
*/
int execve( char *name, char **argv, char **env )
{
/* No threads (yet??) */
errno = ENOSYS;
return -1;
}
/**
* @brief End execution on current thread
*
* @note This function does not exit. If this is the last thread, the
* system will hang.
*
* @param[in] rc
* Return value of the exiting program
*/
void _exit( int rc )
{
/* Loop infinitely. */
for( ;; );
}
/**
* @brief Fork execution into two threads
*
* @note Not supported in libdragon.
*
* @return PID of child process if parent, 0 if child, negative value on failure.
*/
int fork( void )
{
/* No threads (yet??) */
errno = ENOSYS;
return -1;
}
/**
* @brief Return stats on an open file handle
*
* @param[in] fildes
* File handle
* @param[out] st
* Pointer to stat struct to be filled
*
* @return 0 on success or a negative value on error.
*/
int fstat( int fildes, struct stat *st )
{
if( st == NULL )
{
errno = EINVAL;
return -1;
}
else
{
filesystem_t *fs = __get_fs_pointer_by_handle( fildes );
void *handle = __get_fs_handle( fildes );
if( fs == 0 )
{
errno = EINVAL;
return -1;
}
if( fs->fstat == 0 )
{
/* Filesystem doesn't support fstat */
errno = ENOSYS;
return -1;
}
return fs->fstat( handle, st );
}
}
/**
* @brief Return the PID of the current thread
*
* @note Not supported in libdragon.
*
* @return The PID on success or a negative value on error.
*/
int getpid( void )
{
/* No threads (yet??) */
errno = ENOSYS;
return -1;
}
/**
* @brief Return the current time
*
* @param[out] ptimeval
* Time structure to be filled with current time.
* @param[out] ptimezone
* Timezone information to be filled. (Not supported)
*
* @return 0 on success or a negative value on error.
*/
int gettimeofday( struct timeval *ptimeval, void *ptimezone )
{
if( time_hook != NULL )
{
time_t time = time_hook();
if( time != -1 )
{
ptimeval->tv_sec = time;
ptimeval->tv_usec = 0;
return 0;
}
}
errno = ENOSYS;
return -1;
}
/**
* @brief Return whether a file is a TTY or a regular file
*
* @param[in] file
* File handle
*
* @return 1 if the file is a TTY, or 0 if not.
*/
int isatty( int file )
{
if( file == STDIN_FILENO ||
file == STDOUT_FILENO ||
file == STDERR_FILENO )
{
/* This is a TTY for libdragon's purposes */
return 1;
}
else
{
/* Not a TTY */
errno = EBADF;
return 0;
}
}
/**
* @brief Send a signal to a PID
*
* @note Not supported in libdragon.
*
* @param[in] pid
* The PID of the process
* @param[in] sig
* The signal to send
*
* @return 0 on success or a negative value on error.
*/
int kill( int pid, int sig )
{
/* No threads (yet??) */
errno = ENOSYS;
return -1;
}
/**
* @brief Link a new file to an existing file
*
* @note Not supported in libdragon.
*
* @param[in] existing
* The path of the existing file
* @param[in] new
* The path of the new file
*
* @return 0 on success or a negative value on error.
*/
int link( char *existing, char *new )
{
/* No symbolic links in libdragon */
errno = ENOSYS;
return -1;
}
/**
* @brief Seek to a location in a file
*
* @param[in] file
* The file handle of the file to seek
* @param[in] ptr
* The offset in bytes to seek to, given the direction in dir
* @param[in] dir
* The direction to seek. Use SEEK_SET to start from the beginning.
* Use SEEK_CUR to seek based on the current offset. Use SEEK_END
* to seek starting at the end of the file.
*
* @return The new location of the file in bytes or -1 on error.
*/
int lseek( int file, int ptr, int dir )
{
filesystem_t *fs = __get_fs_pointer_by_handle( file );
void *handle = __get_fs_handle( file );
if( fs == 0 )
{
errno = EINVAL;
return -1;
}
if( fs->lseek == 0 )
{
/* Filesystem doesn't support lseek */
errno = ENOSYS;
return -1;
}
return fs->lseek( handle, ptr, dir );
}
/**
* @brief Open a file given a path
*
* @param[in] file
* File name of the file to open
* @param[in] flags
* Flags specifying open flags, such as binary, append.
* @param[in] ... mode
* Mode of the file (currently ignored).
*
* @return File handle to refer to this file on success, or a negative value on error.
*/
int open( const char *file, int flags, ... )
{
filesystem_t *fs = __get_fs_pointer_by_name( file );
if( fs == 0 )
{
errno = EINVAL;
return -1;
}
if( fs->open == 0 )
{
/* Filesystem doesn't support open */
errno = ENOSYS;
return -1;
}
/* Use this to get the mode argument if needed (for O_CREAT and O_TMPFILE). */
if(0)
{
__attribute__((unused)) int mode;
va_list ap;
va_start (ap, flags);
mode = va_arg (ap, int);
va_end (ap);
}
/* Do we have room for a new file? */
for( int i = 0; i < MAX_OPEN_HANDLES; i++ )
{
if( handles[i].fileno == 0 )
{
/* Yes, we have room, try the open */
int mapping = __get_fs_link_by_name( file );
if( mapping < 0 )
{
errno = ENOMEM;
return -1;
}
/* Cast away const from the file name.
open used to mistakenly take a char* instead of a const char*,
and we don't want to break existing code for filesystem_t.open,
so filesystem_t.open still takes char* */
void *ptr = fs->open( (char *)( file + __strlen( filesystems[mapping].prefix ) ), flags );
if( ptr )
{
/* Create new internal handle */
handles[i].fileno = __get_new_handle();
handles[i].handle = ptr;
handles[i].fs_mapping = mapping;
/* Return our own handle */
return handles[i].fileno;
}
else
{
/* Couldn't open for some reason */
errno = EPERM;
return -1;
}
}
}
/* No file handles available */
errno = ENOMEM;
return -1;
}
/**
* @brief Read data from a file
*
* @param[in] file
* File handle
* @param[out] ptr
* Data pointer to read data to
* @param[in] len
* Length in bytes of data to read
*
* @return Actual number of bytes read or a negative value on error.
*/
int read( int file, char *ptr, int len )
{
if( file == STDIN_FILENO )
{
if( stdio_hooks.stdin_read )
{
return stdio_hooks.stdin_read( ptr, len );
}
else
{
/* No hook for this */
errno = EBADF;
return -1;
}
}
else if( file == STDOUT_FILENO ||
file == STDERR_FILENO )
{
/* Can't read from output buffers */
errno = EBADF;
return -1;
}
else
{
/* Read from file */
filesystem_t *fs = __get_fs_pointer_by_handle( file );
void *handle = __get_fs_handle( file );
if( fs == 0 )
{
errno = EINVAL;
return -1;
}
if( fs->read == 0 )
{
/* Filesystem doesn't support read */
errno = ENOSYS;
return -1;
}
return fs->read( handle, (uint8_t *)ptr, len );
}
}
/**
* @brief Read a link
*
* @note Not supported in libdragon.
*
* @param[in] path
* Path of the link
* @param[in] buf
* Buffer to read the link into
* @param[in] bufsize
* Size of the buffer
*
* @return 0 on success or a negative value on error.
*/
int readlink( const char *path, char *buf, size_t bufsize )