📄 syscalls.c
字号:
//////////////////////////////////////////////////////////////////////////////// syscalls.c /////////////////////////////////////////////////////////////////////////////////* system calls remove syscalls.o from libc.a: ar.exe x arm-elf\lib\libc.a syscalls.o ar.exe d arm-elf\lib\libc.a syscalls.o*///////////////////////////////////////////////////////////////////////////////// Includes ////////////////////////////////////////////////////////////////////////////////#include <stdio.h>#include <string.h>#include <sys/unistd.h>#include <sys/time.h>#include <sys/times.h>#include <sys/stat.h>#include <sys/fcntl.h>#include "smf_fat.h"//////////////////////////////////////////////////////////////////////////////// Defines ////////////////////////////////////////////////////////////////////////////////#define SM_PATH_SIZE 256//////////////////////////////////////////////////////////////////////////////// Variables ////////////////////////////////////////////////////////////////////////////////const char smRootPath[] = "dev0:\\";char smCurrentPath[SM_PATH_SIZE] = "dev0:\\"; // must end with '\'//struct _reent *reent; // dummyregister char * stack_ptr asm ("sp");//////////////////////////////////////////////////////////////////////////////// smMakePath ////////////////////////////////////////////////////////////////////////////////int smMakePath(const char *path, char *smPath){ // copy root or current directory { const char *p; if ((*path == '/') || (*path == '\\')) { path++; p = smRootPath; } else p = smCurrentPath; while (*p) *smPath++ = *p++; } // add filenames/directories. remove "." & ".." do { switch (*path) { case 0: case '/': case '\\': if (*(smPath-1) == '\\') { // already ends with '\' } else if ((*(smPath-1) == '.') && (*(smPath-2) == '\\')) { smPath--; // remove '.' and end with '\' } else if ((*(smPath-1) == '.') && (*(smPath-2) == '.') && (*(smPath-3) == '\\')) { smPath -= 3; // remove "\.." if (*(smPath-1) == ':') *smPath++ = '\\'; // "dev0:" -> "dev0:\" else while (*(smPath-1) != '\\') smPath--; // remove one directory and end with '\' } else { *smPath++ = '\\'; // just add '\' } break; default: *smPath++ = *path; break; } } while (*path++); *--smPath = 0; // remove last '\' and null-terminate return 0;}//////////////////////////////////////////////////////////////////////////////// read ////////////////////////////////////////////////////////////////////////////////// returns number of characters readint _read(int fd, void *buf, size_t size)//_ssize_t _read_r(struct _reent *r, int fd, void *buf, size_t size){ unsigned long read_count = 0; ERR_CODE errCode = smReadFile(fd, buf, size, &read_count); if (errCode == ERR_EOF) errCode = SM_OK; if (errCode != SM_OK) return -1; return read_count; // bytes read}//int _read(int fd, void *buf, size_t size) { return _read_r(reent, fd, buf, size); }//_ssize_t _read_r(struct _reent *r, int fd, void *buf, size_t size) return _read(fd, buf, size);//////////////////////////////////////////////////////////////////////////////// write ////////////////////////////////////////////////////////////////////////////////// returns number of characters writtenint _write(int fd, const void *buf, size_t size)//_ssize_t _write_r(struct _reent *r, int fd, const void *buf, size_t size){ ERR_CODE errCode = smWriteFile(fd, buf, size); if (errCode) return -1; return size; // bytes written}//int _write(int fd, const void *buf, size_t size) { return _write_r(reent, fd, buf, size); }//////////////////////////////////////////////////////////////////////////////// open ////////////////////////////////////////////////////////////////////////////////int _open(const char *name, int flags, ...)//int _open_r(struct _reent *r, const char *name, int flags){ F_HANDLE handle; char smPath[SM_PATH_SIZE]; unsigned long fopen_mode = 0; if (smMakePath(name, smPath)) return -1; // convert flags if (flags & O_RDWR) fopen_mode |= OPEN_W; if (flags & O_APPEND) fopen_mode |= OPEN_W; if (flags & O_CREAT) { ERR_CODE errCode; errCode = smCreateFile(smPath, (flags & O_TRUNC) ? ALWAYS_CREATE : NOT_IF_EXIST, &handle); if (errCode) return -1; } else { long old_offset; ERR_CODE errCode; fopen_mode |= OPEN_R; errCode = smOpenFile(smPath, fopen_mode, &handle); if (errCode) return -1; if (flags & O_APPEND) smSeekFile(handle, FROM_END, 0, &old_offset); } return handle;}//int _open(const char *name, int flags, ...) { return _open_r(reent, name, flags); }//////////////////////////////////////////////////////////////////////////////// close ////////////////////////////////////////////////////////////////////////////////int _close(int fd)//int _close_r(struct _reent *r, int fd){ ERR_CODE errCode; errCode = smCloseFile(fd); return errCode ? -1 : 0;}//int _close(int fd) { return _close_r(reent, fd); }//////////////////////////////////////////////////////////////////////////////// seek ////////////////////////////////////////////////////////////////////////////////off_t _lseek(int fd, off_t offset, int whence)//off_t _lseek_r(struct _reent *r, int fd, off_t offset, int whence){ unsigned long seek_mode; long sm_offset; ERR_CODE errCode; if (whence == SEEK_SET) seek_mode = FROM_BEGIN; else if (whence == SEEK_CUR) seek_mode = FROM_CURRENT; else if (whence == SEEK_END) seek_mode = FROM_END; else return -1; errCode = smSeekFile(fd, seek_mode, offset, &sm_offset); if (errCode) return -1; errCode = smSeekFile(fd, FROM_CURRENT, 0, &sm_offset); return sm_offset; // return current offset}//off_t _lseek(int fd, off_t offset, int whence) { return _lseek_r(reent, fd, offset, whence); }//////////////////////////////////////////////////////////////////////////////// link ////////////////////////////////////////////////////////////////////////////////int _link(const char *oldname, const char *newname)//int _link_r(struct _reent *r, const char *oldname, const char *newname){ ERR_CODE errCode; char smPath1[SM_PATH_SIZE]; char smPath2[SM_PATH_SIZE]; if (smMakePath(oldname, smPath1)) return -1; if (smMakePath(newname, smPath2)) return -1; errCode = smRenameFile(smPath1, smPath2); return errCode ? -1 : 0;}//int _link(const char *oldname, const char *newname) { return _link_r(reent, oldname, newname); }//////////////////////////////////////////////////////////////////////////////// unlink ////////////////////////////////////////////////////////////////////////////////int _unlink(const char *name)//int _unlink_r(struct _reent *r, const char *name){ ERR_CODE errCode; char smPath[SM_PATH_SIZE]; if (smMakePath(name, smPath)) return -1; errCode = smRemoveFile(smPath); return errCode ? -1 : 0;}//int _unlink(const char *name) { return _unlink_r(reent, name); }//////////////////////////////////////////////////////////////////////////////// fstat ////////////////////////////////////////////////////////////////////////////////// ???????int _fstat(int fd, struct stat *st)//int _fstat_r(struct _reent *r, int fd, struct stat *st){ memset(st, 0, sizeof(*st)); long gp_offset, gp_offset2; ERR_CODE err; err = smSeekFile(fd, FROM_END, 0, &gp_offset); if (err) return -1; err = smSeekFile(fd, FROM_BEGIN, gp_offset, &gp_offset2); if (err) return -1; st->st_mode = S_IREAD | S_IWRITE | S_IEXEC; st->st_blksize = S_BLKSIZE; st->st_size = gp_offset2; st->st_nlink = 1; return 0;}//int _fstat(int fd, struct stat *st) { return _fstat_r(reent, fd, st); }//////////////////////////////////////////////////////////////////////////////// stat ////////////////////////////////////////////////////////////////////////////////int _stat(const char *path, struct stat *st)//int _stat_r(struct _reent *r, const char *path, struct stat *st){ ERR_CODE errCode; sFILE_STAT stat; struct tm tm_; char smPath[SM_PATH_SIZE]; if (smMakePath(path, smPath)) return -1; errCode = smReadStat(smPath, &stat); if (errCode) return -1;/* * 7-6 Reserved. Must be 0's. * 5: 1 = ARCHIVE file was modified * 4: 1 = DIRECTORY, 0 = file * 3: 1 = VOLUME label * 2: 1 = SYSTEM file or directory * 1: 1 = HIDDEN file or directory * 0: 1 = READONLY file */ memset(st, 0, sizeof(*st)); st->st_mode = S_IEXEC + S_IREAD + ((stat.attr & 0x01) ? 0 : S_IWRITE) + // readonly ((stat.attr & 0x02) ? 0 : 0) + // hidden ((stat.attr & 0x04) ? 0 : 0) + // system ((stat.attr & 0x08) ? 0 : 0) + // volume label ((stat.attr & 0x10) ? S_IFDIR : 0) + // directory ((stat.attr & 0x20) ? 0 : 0); // archive st->st_size = stat.size; st->st_ino = stat.cluster; st->st_nlink = 1; tm_.tm_sec = stat.time.sec; tm_.tm_min = stat.time.min; tm_.tm_hour = stat.time.hour; tm_.tm_mday = stat.time.day; tm_.tm_mon = stat.time.month; tm_.tm_year = stat.time.year; tm_.tm_wday = 0; tm_.tm_yday = 0; tm_.tm_isdst = 0; st->st_atime = mktime(&tm_); st->st_blksize = S_BLKSIZE; return 0;}//int _stat(const char *path, struct stat *st) { return _stat_r(reent, path, st); }//////////////////////////////////////////////////////////////////////////////// exit ////////////////////////////////////////////////////////////////////////////////void _exit(int n){ while (1);}//////////////////////////////////////////////////////////////////////////////// kill ////////////////////////////////////////////////////////////////////////////////int _kill(int n, int m){ return 0;}//////////////////////////////////////////////////////////////////////////////// getpid ////////////////////////////////////////////////////////////////////////////////int _getpid(){ return 1;}//////////////////////////////////////////////////////////////////////////////// raise ////////////////////////////////////////////////////////////////////////////////void _raise(void){ return;}//////////////////////////////////////////////////////////////////////////////// gettimeofday ////////////////////////////////////////////////////////////////////////////////int _gettimeofday(struct timeval *tp, struct timezone *tzp){ if (tp) { /* Ask the host for the seconds since the Unix epoch. */ tp->tv_sec = 0; tp->tv_usec = 0; } /* Return fixed data for the timezone. */ if (tzp) { tzp->tz_minuteswest = 0; tzp->tz_dsttime = 0; } return 0;}//////////////////////////////////////////////////////////////////////////////// times ////////////////////////////////////////////////////////////////////////////////clock_t _times(struct tms * tp){ clock_t timeval = 0; if (tp) { tp->tms_utime = timeval; /* user time */ tp->tms_stime = 0; /* system time */ tp->tms_cutime = 0; /* user time, children */ tp->tms_cstime = 0; /* system time, children */ } return timeval;};//////////////////////////////////////////////////////////////////////////////// isatty ////////////////////////////////////////////////////////////////////////////////int isatty(int fd){ return 1;}//////////////////////////////////////////////////////////////////////////////// sbrk ////////////////////////////////////////////////////////////////////////////////void *_sbrk(size_t incr){ extern char end asm ("end"); /* Defined by the linker. */ static char * heap_end; char * prev_heap_end; if (heap_end == NULL) heap_end = & end; prev_heap_end = heap_end; if (heap_end + incr > stack_ptr) { _write (1, "_sbrk: Heap and stack collision\n", 32); abort (); } heap_end += incr; return (caddr_t) prev_heap_end;}//////////////////////////////////////////////////////////////////////////////// chdir ////////////////////////////////////////////////////////////////////////////////int chdir(const char *__path){ ERR_CODE errCode; sFILE_STAT stat; char smPath[SM_PATH_SIZE]; if (smMakePath(__path, smPath)) return -1; if (smPath[5]) // != "dev0:", no stats on root { errCode = smReadStat(smPath, &stat); if (errCode) return -1; } strcpy(smCurrentPath, smPath); strcat(smCurrentPath, "\\"); // must end with '\' return 0;}//////////////////////////////////////////////////////////////////////////////// rmdir ////////////////////////////////////////////////////////////////////////////////int rmdir(const char *__path){ ERR_CODE errCode; char smPath[SM_PATH_SIZE]; if (smMakePath(__path, smPath)) return -1; errCode = smRemoveDir(smPath, NOT_IF_NOT_EMPTY/*ALWAYS_DELETE*/); if (errCode) return -1; return 0;}//////////////////////////////////////////////////////////////////////////////// mkdir ////////////////////////////////////////////////////////////////////////////////int mkdir(const char *_path, mode_t __mode){ ERR_CODE errCode; char smPath[SM_PATH_SIZE]; if (smMakePath(_path, smPath)) return -1; errCode = smCreateDir(smPath, 0); if (errCode) return -1; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -