#ifndef _UNISTD_H #define _UNISTD_H //指出符合IEEE标志1003.1实现的版本 #define _POSIX_VERSION 19808L //chown和fchown的使用受限于进程的权限 #define _POSIX_CHOWN_RESTRICTRD //长于(name max)的路径名将产生错误,不会自动截断 #define _POSIX_NO_TRUNC //这个符号定义成字符值,该值将禁止终端对其的处理 #define _POSIX_VDISABLE '\0' #define STDIN_FILEN0 0 //标准输入文件句柄 #define STDOUT_DILENO 1 // 标准输出文件句柄 #define STDERR_FILENO 2 // 标准出错文件句柄 #ifndef NULL #define NULL ((void*)0) //空指针 #endif //access() #define F_OK 0 //检测文件是否存在 #define X_OK 1 //检测是否可执行 #define W_OK 2 // 检测是否可写 #define R_OK 4 //检测是否可读 //lseek() #define SEEK_SET 0 //将文件读写指针设置位偏移值 #define SEEK_CUR 1 //将文件读写指针设置位当前值加上偏移值 #define SEEK_END 2 //将文件读写指针设置位文件长度加上偏移值 #define _SC_ARG_MAX 1 //最大变量数 #define _SC_CHILD_MAX 2 //子进程最大数 #define _SC_CLOCKS_PER_SEC 3 //每秒滴答数 #define _SC_NGROUPS_MAX 4 //最大组数 #define _SC_OPEN_MAX 5 //最大打开文件数 #define _SC_JOB_CONTROL 6 //作业控制 #define _SC_SAVED_IDS 7 //保存的标识符 #define _SC_VERSION 8 // 版本 // #define _PC_LINK_MAX 1 // 连接最大数 #define _PC_MAX_CANON 2 //最大常规文件数 #define _PC_MAX_INPUT 3 //最大输入长度 #define _PC_NAME_MAX 4 //名称最大长度 #define _PC_PATH_MAX 5 //路径最大长度 #define _PC_PIPE_BUF 6 //管道缓冲大小 #define _PC_NO_TRUNC 7 //文件名不截断 #define _PC_VDISABLE 8 // #define _PC_CHOWN_RESTRICTED 9 //改变宿主受限 #include<sys/stat.h> #include<sys/times.h> #include<sys/utsname.h> #include<utime.h> #ifdef _LIBRARY_ #define _NR_setup 0 #define _NR_fork 1 #define _NR_exit 2 #define _NR_read 3 #define _NR_write 4 #define _NR_open 5 #define _NR_close 6 #define _NR_waitpid 7 #define _NR_creat 8 #define _NR_link 9 #define _NR_unlink 10 #define _NR_execve 11 #define _NR_chdir 12 #define _NR_time 13 #define _NR_mknod 14 #define _NR_chmod 15 #define _NR_chwon 16 #define _NR_break 17 #define _NR_stat 18 #define _NR_lseek 19 #define _NR_getpid 20 #define _NR_mount 21 #define _NR_umount 22 #define _NR_setuid 23 #define _NR_getuid 24 #define _NR_stime 25 #define _NR_ptrace 26 #define _NR_alarm 27 #define _NR_fstat 28 #define _NR_pause 29 #define _NR_utime 30 #define _NR_stty 31 #define _NR_gtty 32 #define _NR_access 33 #define _NR_nice 34 #define _NR_ftime 35 #define _NR_sync 36 #define _NR_kill 37 #define _NR_rename 38 #define _NR_mkdir 39 #define _NR_rmdir 40 #define _NR_dup 41 #define _NR_pipe 42 #define _NR_times 43 #define _NR_prof 44 #define _NR_brk 45 #define _NR_setgid 46 #define _NR_getgid 47 #define _NR_signal 48 #define _NR_geteuid 49 #define _NR_getegid 50 #define _NR_acct 51 #define _NR_phys 52 #define _NR_lock 53 #define _NR_ioctl 54 #define _NR_fcntl 55 #define _NR_mpx 56 #define _NR_setpgid 57 #define _NR_ulimit 58 #define _NR_uname 59 #define _NR_umask 60 #define _NR_chroot 61 #define _NR_ustat 62 #define _NR_dup2 63 #define _NR_getppid 64 #define _NR_getpgrp 65 #define _NR_setsid 66 #define _NR_sigaction 67 #define _NR_sgetmask 68 #define _NR_ssetmask 69 #define _NR_setreuid 70 #define _NR_setregid 71 /*定义系统调用嵌入式汇编宏函数 *不带参数的系统调用宏函数 type name(void) *%0-eax(res),%1-eax(_NR_##name).其中name是系统调用的名称,与上面_NR_组合形成上面的 *系统调用符号常数,从而用来对系统调用表中函数指针寻址 *返回:如果返回值大于等于0,则返回该值,否则置出错号errno,并返回-1 */ #define _syscall0(type,name) \ type name(void) \ { \ long _res; \ __asm__ volatile("int 0x80" \ :"=a(_res)" \ :""(_NR_##name);\ //输入系统中断调用号_NR_name ); \ if(_res>=0) \ return (type) _res; \ errno=-_res; \ return -1; \ } /*有一个参数的系统调用函数type name(atype a) *%0-eax(_res),%1-eax(_NR_name),%2-ebx(a) * * */ #define _syscall1(type,name,atype,a) \ type name(atype a) \ { \ long _res; \ _asm_ volaile( \ "int 0x80" \ :"=a"(_res) \ :""(_NR_##name),"b"((long)(a)) \ ); \ if(_res>0) \ return (type) _res; \ errno = -_res; \ return -1; } /*有两个参数的系统调用type name(atype a,btype b) *%0-eax(res),%1-eax(_NR_name),%2-ebx(a),%3-ecx(b) * */ #define _syscall2(type,name,atype a,btype b) \ type name(atype a,btype b) \ { \ long _res; \ _asm_ volaile( \ "int 0x80" \ :"=a"(_res) \ :""(_NR_name),"b"((long)(a)),"c"((long)(b))\ ); \ if(_res>=0) \ return (type) _res; \ errno =-_res; \ return -1; \ } /*有2个参数的系统调用宏函数type name(atype a,btype b,ctype c) *%0-eax(res),%1-eax(_NR_name),%2-ebx(a),%3-ecx(b),%4-edx(c) * */ #define _syscall3(type,name,atype a,btype b,ctypec) \ type name(atype a,btype b,ctype c) \ { \ long _res; \ _asm_ volaile( \ "int 0x80" \ :"=a"(_res) \ :""(_NR_name),"b"((long)(a)), \ "c"((long)(b)),"d"((long)(c)) \ ); \ if(_res>=0) \ return (type)_res; \ errno=-_res; \ return -1; \ } #endif extern int errno; //出错号,全局变量 //系统调用的函数原型定义 int access(const char*filename,mode_t mode); int acct(const char*filename); int alarm(int sec); int brk(void *end_data_segment); void *sbrk(ptrdiff_t increment); int chdir(const char*filenem); int chmod(const char*filenem,mode_t mode); int chown(const char*filenem,uid_t owner,gid_t group); int chroot(const char*filenem); int close(int fildes); int creat(const char*filenem,mode_t mode); int dup(int fildes); int execve(const char*filenamem,char**argv,char**envp); int ececv(const char*filename,char**argv); int execvp(const char*file,char**arg); int execl(const char*pathname,char*arg0...); int execlp(const char*file,char*arg0...); int execle(const char*pathname,char*arg0...); volatile void exit(int status); volatile void _exit(int status); int fcntl(int fildes,int cmd...); int fork(void); int getpid(void); int getuid(void); int geteuid(void); int getgid(void); int getegid(void); int ioctl(int fildes,int cmd...); int kill(pid_t pid,int signal); int link(const char*filenamel,const char*filename2); int lseek(int fildes,off_t offsets,int origin); int mknod(const char*filename,mode_t mode,dev_t dev); int mount(const char*specialfile,const char*dir,int reflag); int nice(int val); int open(const char*filename,int flag...); int pause(void); int pipe(int *fildes); int read(int fildes,char*buf,off_t count); int setpgrp(void); int setuid(uid_t uid); int setpgid(pid_t pid,pid_t pgid); int setgid(gid_t gid); void (*signal(int sig,void(*fn)(int)))(int); int stat(const char*filename,struct stat*stat_buf); int fstat(int fildes,struct stat* stat_buf); int stime(time_t *tptr); int sync(void); time_t time(time_t *tloc); time_t times(struct tms*buf); int ulimit(int cmd,long limit); mode_t umask(mode_t mask); int umount(const char*specialfile); int uname(struct utsname*name); int unlink(const char*filename); int ustat(dev_t dev,struct ustat*ubuf); int utime(const char*filename,struct utimbuf*times); pid_t waitpid(pid_t pid,int*wait,int options); pit_t wait(int *wait_stat); int write(int fildes,const char*buf,off_t count); int dup2(int oldfd,int newfd); int getppid(void); pid_t getpgrp(void); pid_t setsid(void); #endif