Repository navigation
Expand file tree
/
Copy pathminux.h
More file actions
971 lines (874 loc) · 46.1 KB
/
Copy pathminux.h
File metadata and controls
971 lines (874 loc) · 46.1 KB
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
#ifndef MINUX_H
#define MINUX_H
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
//──────────────────────────────────────────
// Filesystem constants/structures (keep in sync with mkfs.c)
//──────────────────────────────────────────
#define BSIZE 512 // Block size
#define NDIRECT 12 // Number of direct block pointers
#define NINDIRECT (BSIZE / sizeof(uint32_t)) // Entries per indirect block (512/4=128)
#define MAXFILE (NDIRECT + NINDIRECT + NINDIRECT * NINDIRECT)
#define ROOTINO 1 // i-node number of the root directory
#define T_DIR 1
#define T_FILE 2
#define T_SYMLINK 3
// mode bits (subset)
#ifndef S_ISUID
#define S_ISUID 04000
#endif
#ifndef S_ISGID
#define S_ISGID 02000
#endif
#ifndef S_ISVTX
#define S_ISVTX 01000
#endif
#define DIRSIZ 14 // Maximum length of a directory entry name
// ───────────────────────────────────────────────────────────────────────
// On-disk structures
// ────────────────────────────────────────────────────────────────────────
// Superblock (placed at block number 1)
struct superblock {
uint32_t size; // Total number of blocks
uint32_t nblocks; // Number of data blocks
uint32_t ninodes; // Number of inodes
uint32_t nlog; // Number of log blocks (0 if unused)
uint32_t logstart; // Starting block of the log area (0 if unused)
uint32_t inodestart; // Starting block of the inode area
uint32_t bmapstart; // Starting block of the bitmap
};
// On-disk i-node structure
struct dinode {
uint16_t type; // 0=free, 1=directory, 2=file, 3=symlink
uint16_t major; // Device major number (0 if unused)
uint16_t minor; // Device minor number (0 if unused)
uint16_t nlink; // Hard link count
uint32_t size; // File size in bytes
uint32_t mode; // Permission/type bits (simplified)
uint16_t uid; // Owner
uint16_t gid; // Group
uint32_t atime; // Last access time
uint32_t mtime; // Last modification time
uint32_t ctime; // Metadata change time
/* Direct pointers [0..NDIRECT-1], single indirect = addrs[NDIRECT], double indirect = addrs[NDIRECT+1] */
uint32_t addrs[NDIRECT + 2];
};
// Directory entry
struct dirent {
uint16_t inum; // File/directory i-node number
char name[DIRSIZ]; // File name (fixed 14 bytes, no NUL terminator)
};
// Simplified stat structure (shared by user and kernel)
struct stat {
uint16_t type; // T_DIR / T_FILE / T_SYMLINK
uint16_t nlink; // Hard link count
uint32_t size; // Byte count
uint32_t inum; // i-node number
uint32_t mode; // Permissions
uint16_t uid; // Owner
uint16_t gid; // Group
uint32_t atime; // Access time
uint32_t mtime; // Modification time
uint32_t ctime; // Change time
};
//──────────────────────────────────────────
// Helper function prototypes
//──────────────────────────────────────────
// Low-level block read (expected to be implemented externally)
// blockno: block number on the filesystem (starting at 0)
// buf: buffer allocated for BSIZE bytes
extern void read_block(uint32_t blockno, void *buf);
// Read the superblock
void read_superblock(void);
// Read an i-node
// inum: i-node number (1 through ninodes)
// dest: buffer for struct dinode (allocated by caller)
void read_inode(uint32_t inum, struct dinode *dest);
// Read file (or directory) data into a buffer
// inode: must be populated with read_inode beforehand
// offset, size: offset and number of bytes to read
// buf: destination buffer (allocate at least size bytes)
void read_data(struct dinode *inode, uint32_t offset, uint8_t *buf, uint32_t size);
// Lookup a filename from the root directory
// path: given as an absolute path like "/foo/bar.txt"
// return: inode number if found, otherwise 0
uint32_t path_lookup(const char *path);
// Find an entry inside a directory
// parent: dinode structure of the parent directory
// name: target filename (NUL-terminated string)
// return: inode number, or 0 if not found
uint32_t dir_lookup(struct dinode *parent, const char *name);
// Load an i-node and print its contents (for debugging)
void dump_inode(uint32_t inum);
// Initialize the virtio block device
void virtio_blk_init(void);
void bcache_init(void);
typedef int32_t ssize_t;
int fs_fstat_fd(int fd, struct stat* st);
long fs_lseek(int fd, long offset, int whence);
enum { SEEK_SET, SEEK_END, SEEK_CUR };
#ifndef assert
#define assert
#endif
typedef long time_t;
typedef long suseconds_t;
struct timeval { time_t tv_sec; suseconds_t tv_usec; };
struct tm { int tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst; };
typedef struct __minux_FILE {
int fd;
int flags; // 1=readable, 2=writable, 4=append
long pos;
int eof;
int err;
int have_push;
unsigned char push_ch;
// memory stream support (fd < 0 if memory stream)
int is_mem;
char **ms_bufp;
size_t *ms_sizep;
char *ms_buf; // internal buffer
size_t ms_cap; // capacity of ms_buf
size_t ms_len; // valid length
} FILE;
typedef struct {
size_t gl_pathc; // count of paths matched
char **gl_pathv; // NULL-terminated vector of strings
} glob_t;
extern FILE* stdin;
extern FILE* stdout;
extern FILE* stderr;
#define EOF (-1)
typedef int pid_t;
#define SYS_execve 100
#define SYS_execved 199
#define SYS_settimeofday 92
#define SYS_utimes 93
#define SYS_umask 94
#define SYS_gettimeofday 95
#define SYS_getuid 96
#define SYS_getgid 97
#define SYS_setuid 98
#define SYS_setgid 99
#define SYS_lseek 192
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1
#endif
#ifndef SEEK_END
#define SEEK_END 2
#endif
#define SYS_fstat 210
#define SYS_waitdirect 211
#define SYS_write 64
#define SYS_read 65
#define SYS_open 66
#define SYS_close 67
#define SYS_fork 68
#define SYS_execv 69
#define SYS_exit 70
#define SYS_wait 71
#define SYS_dup2 72
#define SYS_pipe 73
#define SYS_brk 74
#define SYS_clear 75
#define SYS_opendir 76
#define SYS_readdir 77
#define SYS_closedir 78
// add cwd-related syscalls
#define SYS_getcwd 79
#define SYS_chdir 80
#define SYS_mkdir 81
#define SYS_rmdir 82
#define SYS_unlink 83
#define SYS_rename 84
#define SYS_link 85
#define SYS_symlink 86
#define SYS_stat 87
#define SYS_readlink 88
#define SYS_lstat 89
#define SYS_chmod 90
#define SYS_chown 91
#define SYS_sleep 102
#define SYS_tcsetpgrp 103
#define SYS_getpid 105
#define SYS_realpath 101
#define SYS_isatty 104
extern int errno;
#define write(fd, buf, len) ({ \
long _ret; \
/* Put arguments into the corresponding registers */ \
register long _a0 asm("a0") = (long)(fd); \
register long _a1 asm("a1") = (long)(buf); \
register long _a2 asm("a2") = (long)(len); \
register long _a7 asm("a7") = SYS_write; \
/* Do ecall; return value comes back in a0 */ \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
/* Return it as the statement-expression result */ \
_ret = _a0; \
_ret; \
})
#define tcsetpgrp(fildes, pgid_id) ({ \
long _ret; \
/* Put arguments into the corresponding registers */ \
register long _a0 asm("a0") = (long)(fildes); \
register long _a1 asm("a1") = (long)(pgid_id); \
register long _a7 asm("a7") = SYS_tcsetpgrp; \
/* Do ecall; return value comes back in a0 */ \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
/* Return it as the statement-expression result */ \
_ret = _a0; \
_ret; \
})
#define read(fd, buf, len) ({ \
long _ret; \
/* Put arguments into the appropriate registers */ \
register long _a0 asm("a0") = (long)(fd); \
register long _a1 asm("a1") = (long)(buf); \
register long _a2 asm("a2") = (long)(len); \
register long _a7 asm("a7") = SYS_read; \
/* Execute ecall; return value comes back in a0 */ \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
/* Save a0 locally and use it as the statement-expression value */ \
_ret = _a0; \
_ret; \
})
#define open(path, flags, mode) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(flags); \
register long _a2 asm("a2") = (long)(mode); \
register long _a7 asm("a7") = SYS_open; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
/* Return value is placed in a0 */ \
(int)_a0; \
})
// open(2) flags (subset)
#ifndef O_RDONLY
#define O_RDONLY 0
#endif
#ifndef O_WRONLY
#define O_WRONLY 1
#endif
#ifndef O_RDWR
#define O_RDWR 2
#endif
#ifndef O_CREAT
#define O_CREAT (1<<9)
#endif
#ifndef O_TRUNC
#define O_TRUNC (1<<10)
#endif
#ifndef O_APPEND
#define O_APPEND (1<<11)
#endif
#define close(fd) ({ \
register long _a0 asm("a0") = (long)(fd); \
register long _a7 asm("a7") = SYS_close; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// opendir: open a directory and return fd (or -1)
#define opendir(path) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a7 asm("a7") = SYS_opendir; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// readdir: fetch up to max_entries dirents; returns number filled, 0 (EOF), or -1
#define readdir(fd, dirents_ptr, max_entries) ({ \
register long _a0 asm("a0") = (long)(fd); \
register long _a1 asm("a1") = (long)(dirents_ptr); \
register long _a2 asm("a2") = (long)(max_entries); \
register long _a7 asm("a7") = SYS_readdir; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// closedir: close directory fd; returns 0 or -1
#define closedir(fd) ({ \
register long _a0 asm("a0") = (long)(fd); \
register long _a7 asm("a7") = SYS_closedir; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// getcwd: write current directory into user buffer; returns length or -1
#define getcwd_user(buf, size) ({ \
register long _a0 asm("a0") = (long)(buf); \
register long _a1 asm("a1") = (long)(size); \
register long _a7 asm("a7") = SYS_getcwd; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// chdir: change current process directory; returns 0 or -1
#define chdir(path) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a7 asm("a7") = SYS_chdir; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// mkdir: create directory at path; returns 0 or -1
#define mkdir(path, mode) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(mode); \
register long _a7 asm("a7") = SYS_mkdir; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// rmdir: remove empty directory at path; returns 0 or -1
#define rmdir(path) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a7 asm("a7") = SYS_rmdir; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// unlink: remove a file (not directory); returns 0 or -1
#define unlink(path) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a7 asm("a7") = SYS_unlink; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// rename: move/rename file or directory
#define rename(oldp,newp) ({ \
register long _a0 asm("a0") = (long)(oldp); \
register long _a1 asm("a1") = (long)(newp); \
register long _a7 asm("a7") = SYS_rename; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// link: create a hard link newp pointing to oldp
#define link(oldp,newp) ({ \
register long _a0 asm("a0") = (long)(oldp); \
register long _a1 asm("a1") = (long)(newp); \
register long _a7 asm("a7") = SYS_link; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// symlink: create a symbolic link
#define symlink(target, linkpath) ({ \
register long _a0 asm("a0") = (long)(target); \
register long _a1 asm("a1") = (long)(linkpath); \
register long _a7 asm("a7") = SYS_symlink; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// stat: fill struct stat for path
#define stat(path, stptr) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(stptr); \
register long _a7 asm("a7") = SYS_stat; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// readlink: read symlink target into buf (no NUL), returns bytes copied
#define readlink(path, buf, size) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(buf); \
register long _a2 asm("a2") = (long)(size); \
register long _a7 asm("a7") = SYS_readlink; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// lstat: like stat but does not follow final symlink
#define lstat(path, stptr) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(stptr); \
register long _a7 asm("a7") = SYS_lstat; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// chmod: change file mode
#define chmod(path, mode) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(mode); \
register long _a7 asm("a7") = SYS_chmod; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// chown: change ownership
#define chown(path, uid, gid) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(uid); \
register long _a2 asm("a2") = (long)(gid); \
register long _a7 asm("a7") = SYS_chown; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define execv(path, argv) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(argv); \
register long _a7 asm("a7") = SYS_execv; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define execvp(path, argv) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(argv); \
register long _a7 asm("a7") = SYS_execv; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// execve: exec with environment (envp = array of "KEY=VAL" strings)
#define execve(path, argv, envp) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(argv); \
register long _a2 asm("a2") = (long)(envp); \
register long _a7 asm("a7") = SYS_execve; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// execve: exec with environment (envp = array of "KEY=VAL" strings)
#define execved(path, argv, envp) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(argv); \
register long _a2 asm("a2") = (long)(envp); \
register long _a7 asm("a7") = SYS_execved; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define _exit(status) __extension__ ({ \
register long _a0 asm("a0") = (long)(status); \
register long _a7 asm("a7") = SYS_exit; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
while(1); \
})
void exit(int status);
#define wait(status_ptr) ({ \
register long _a0 asm("a0") = (long)(status_ptr); \
register long _a7 asm("a7") = SYS_wait; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define waitdirect(status_ptr) ({ \
register long _a0 asm("a0") = (long)(status_ptr); \
register long _a7 asm("a7") = SYS_waitdirect; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define dup2(oldfd,newfd) ({ \
register long _a0 asm("a0") = (long)(oldfd); \
register long _a1 asm("a1") = (long)(newfd); \
register long _a7 asm("a7") = SYS_dup2; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define pipe(fds) ({ \
register long _a0 asm("a0") = (long)(fds); \
register long _a7 asm("a7") = SYS_pipe; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// Version that captures and returns the result
#define fork() \
({ \
register long _num asm("a7") = SYS_fork; \
register long _ret asm("a0"); \
asm volatile("ecall" \
: "=r"(_ret) \
: "r"(_num) \
: "memory"); \
_ret; \
})
#define clear_proc() \
({ \
register long _num asm("a7") = SYS_clear; \
register long _ret asm("a0"); \
asm volatile("ecall" \
: "=r"(_ret) \
: "r"(_num) \
: "memory"); \
_ret; \
})
#define brk(size) ({ \
register long _a0 asm("a0") = (long)(size); \
register long _a7 asm("a7") = SYS_brk; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// settimeofday: set base epoch seconds used by FS times
#define settimeofday(sec) ({ \
register long _a0 asm("a0") = (long)(sec); \
register long _a7 asm("a7") = SYS_settimeofday; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// utimes: set atime/mtime; pass 0xFFFFFFFF to set to 'now'
#define utimes(path, atime, mtime) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(atime); \
register long _a2 asm("a2") = (long)(mtime); \
register long _a7 asm("a7") = SYS_utimes; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// umask: set new umask, return old
#define umask(newmask) ({ \
register long _a0 asm("a0") = (long)(newmask); \
register long _a7 asm("a7") = SYS_umask; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// gettimeofday: returns epoch seconds
#define gettimeofday() ({ \
register long _a7 asm("a7") = SYS_gettimeofday; \
register long _a0 asm("a0") = 0; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// uid/gid syscalls
#define getuid() ({ \
register long _a7 asm("a7") = SYS_getuid; \
register long _a0 asm("a0") = 0; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define getgid() ({ \
register long _a7 asm("a7") = SYS_getgid; \
register long _a0 asm("a0") = 0; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define setuid(uid) ({ \
register long _a0 asm("a0") = (long)(uid); \
register long _a7 asm("a7") = SYS_setuid; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define setgid(gid) ({ \
register long _a0 asm("a0") = (long)(gid); \
register long _a7 asm("a7") = SYS_setgid; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// realpath: resolve to canonical absolute path; returns 0 on success
#define realpath_user(path, outbuf, outsz) ({ \
register long _a0 asm("a0") = (long)(path); \
register long _a1 asm("a1") = (long)(outbuf); \
register long _a2 asm("a2") = (long)(outsz); \
register long _a7 asm("a7") = SYS_realpath; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
// isatty: return 1 if fd refers to a TTY (stdin/stdout/stderr/tty), else 0
#define isatty(fd) ({ \
register long _a0 asm("a0") = (long)(fd); \
register long _a7 asm("a7") = SYS_isatty; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define lseek(fd, offset, whence) ({ \
register long _a0 asm("a0") = (long)(fd); \
register long _a1 asm("a1") = (long)(offset); \
register long _a2 asm("a2") = (long)(whence); \
register long _a7 asm("a7") = SYS_lseek; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a2), "r"(_a7) \
: "memory"); \
(long)_a0; \
})
// Additional minux.h helpers
// fstat: stat a file descriptor
#define fstat(fd, stptr) ({ \
register long _a0 asm("a0") = (long)(fd); \
register long _a1 asm("a1") = (long)(stptr); \
register long _a7 asm("a7") = SYS_fstat; \
asm volatile("ecall" \
: "+r"(_a0) \
: "r"(_a1), "r"(_a7) \
: "memory"); \
(int)_a0; \
})
#define getpid() ({ \
long _ret; \
/* Put arguments into the appropriate registers */ \
register long _a7 asm("a7") = SYS_getpid; \
/* Execute ecall; return value comes back in a0 */ \
asm volatile("ecall" \
: "=r"(_ret) \
: "r"(_a7) \
: "memory"); \
/* Save a0 locally and use it as the statement-expression value */ \
_ret; \
})
int printf(const char* fmt, ...);
int fflush(FILE* fp);
int ferror(FILE* fp);
FILE* open_memstream(char **bufp, size_t *sizep);
int fscanf(FILE* fp, const char* fmt, ...);
int fprintf(FILE* fp, const char* fmt, ...);
int vfprintf(FILE* fp, const char* fmt, va_list ap);
int fputs(const char* s, FILE* fp);
size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* fp);
size_t fread(void* ptr, size_t size, size_t nmemb, FILE* fp);
char* fgets(char* s, int size, FILE* fp);
int ungetc(int c, FILE* fp);
int fgetc(FILE* fp);
int fseek(FILE* fp, long offset, int whence);
long ftell(FILE* fp);
unsigned sleep(unsigned seconds);
void rewind(FILE* fp);
int fclose(FILE* fp);
FILE* fopen(const char* path, const char* mode);
FILE* tmpfile(void);
void putchar(char c);
int vsnprintf(char* out, unsigned long out_size, const char* fmt, va_list ap);
int snprintf(char* out, unsigned long out_size, const char* fmt, ...);
int vasprintf(char** out, const char* fmt, va_list ap);
char* strncat(char* dest, const char* src, size_t n);
void puts(const char *s);
char* strerror(int errnum);
int isalnum(int c);
int isdigit(int c);
int isalpha(int c);
int isspace(int c);
int isprint(int c);
void globfree(glob_t* pglob);
int glob(const char* pattern, int flags, void* errfunc, glob_t* pglob);
int mkstemp(char* template);
char* strndup(const char* s, size_t n);
char* strrchr(const char* s, int c);
char* strchr(const char* s, int c);
int strlen(const char *s);
char* strncpy(char *s, const char *t, int n);
int strncmp(const char *p, const char *q, unsigned int n);
void* memcpy(void *dst, const void *src, unsigned int n);
void* memmove(void *dst, const void *src, unsigned int n);
int memcmp(const void *v1, const void *v2, unsigned int n);
void* memset(void *dst, int c, unsigned int n);
char* strstr(const char* haystack, const char* needle);
int strcmp(const char* s1, const char* s2);
char* strdup(const char* s);
void *realloc(void *ptr, size_t size);
void *calloc(size_t nmemb, size_t size);
void free(void *ptr);
void *malloc(size_t size);
int fputc(int c, FILE* fp);
double strtod(const char* nptr, char** endptr);
unsigned long strtoul(const char* nptr, char** endptr, int base);
long strtol(const char* nptr, char** endptr, int base);
long long strtoll(const char* nptr, char** endptr, int base);
long atol(const char* nptr);
int atoi(const char* nptr);
int strncasecmp(const char* a, const char* b, size_t n);
time_t time(time_t* tloc);
struct tm* localtime(const time_t* timep);
char* ctime_r(const time_t* timep, char* buf);
char* strerror(int errnum);
int ispunct(int c);
char *strtok(char *s, const char *delim);
int isxdigit(int c);
char* readline(const char* prompt);
extern size_t linenumber;
//──────────────────────────────────────────
// Minimal curses-like interface for user programs
//──────────────────────────────────────────
typedef struct minux_window {
int rows;
int cols;
int beg_y;
int beg_x;
int cur_y;
int cur_x;
struct minux_window* parent;
int is_subwin;
int is_static;
} WINDOW;
extern WINDOW* stdscr;
WINDOW* initscr(void);
int endwin(void);
int refresh(void);
int wrefresh(WINDOW* win);
int wmove(WINDOW* win, int y, int x);
int mvwprintf(WINDOW* win, int y, int x, const char* fmt, ...);
int mvprintw(int y, int x, const char* fmt, ...);
WINDOW* newwin(int nlines, int ncols, int begin_y, int begin_x);
WINDOW* subwin(WINDOW* win, int nlines, int ncols, int begin_y, int begin_x);
int delwin(WINDOW* win);
struct __minux_FILE;
enum arg_kind {
ARG_KIND_LIT = 1,
ARG_KIND_FILE = 2,
ARG_KIND_END = 3,
};
struct arg_hdr {
enum arg_kind kind;
const char *shortopts;
const char *longopts;
const char *datatype;
const char *glossary;
int mincount;
int maxcount;
};
struct arg_lit {
struct arg_hdr hdr;
int count;
};
struct arg_file {
struct arg_hdr hdr;
int count;
const char **filename;
};
struct arg_error {
const char *msg;
const char *argval;
const struct arg_hdr *hdr;
};
struct arg_end {
struct arg_hdr hdr;
int count;
int maxerrors;
struct arg_error *errors;
};
struct arg_lit *arg_litn(const char *shortopts, const char *longopts,
int mincount, int maxcount, const char *glossary);
struct arg_file *arg_filen(const char *shortopts, const char *longopts,
const char *datatype, int mincount, int maxcount,
const char *glossary);
struct arg_end *arg_end(int maxerrors);
int arg_parse(int argc, char **argv, void **argtable);
void arg_print_syntax(struct __minux_FILE *fp, void **argtable,
const char *suffix);
void arg_print_glossary(struct __minux_FILE *fp, void **argtable,
const char *format);
void arg_print_errors(struct __minux_FILE *fp, struct arg_end *end,
const char *progname);
void arg_freetable(void **argtable, size_t n);
int sscanf(const char *str, const char *fmt, ...);
char *realpath(const char *path, char *resolved_path);
char *dirname(const char *path);
int fileno(FILE* fp);
int system(const char* command);
char* getenv(const char* str);
void* memchr(const void* s, int c, size_t n);
int vprintf(const char* fmt, va_list ap);
char* strcpy(char* dest, const char* src);
char* strcat(char* dest, const char* src);
void perror(char* msg);
int sprintf(char* out, const char* fmt, ...);
#endif