linux源代码剖析之include~~其他

include~~

a.out.h
#ifndef _A_OUT_H
#define _A_OUT_H

#define GNU_EXEC_MACROS

struct exec {
unsigned long a_magic; /* Use macros N_MAGIC, etc for access /
unsigned a_text; /
length of text, in bytes /
unsigned a_data; /
length of data, in bytes /
unsigned a_bss; /
length of uninitialized data area for file, in bytes /
unsigned a_syms; /
length of symbol table data in file, in bytes /
unsigned a_entry; /
start address /
unsigned a_trsize; /
length of relocation info for text, in bytes /
unsigned a_drsize; /
length of relocation info for data, in bytes */
};

#ifndef N_MAGIC
#define N_MAGIC(exec) ((exec).a_magic)
#endif

#ifndef OMAGIC
/* Code indicating object file or impure executable. /
#define OMAGIC 0407
/
Code indicating pure executable. /
#define NMAGIC 0410
/
Code indicating demand-paged executable. /
#define ZMAGIC 0413
#endif /
not OMAGIC */

#ifndef N_BADMAG
#define N_BADMAG(x)
(N_MAGIC(x) != OMAGIC && N_MAGIC(x) != NMAGIC
&& N_MAGIC(x) != ZMAGIC)
#endif

#define _N_BADMAG(x)
(N_MAGIC(x) != OMAGIC && N_MAGIC(x) != NMAGIC
&& N_MAGIC(x) != ZMAGIC)

#define _N_HDROFF(x) (SEGMENT_SIZE - sizeof (struct exec))

#ifndef N_TXTOFF
#define N_TXTOFF(x)
(N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : sizeof (struct exec))
#endif

#ifndef N_DATOFF
#define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
#endif

#ifndef N_TRELOFF
#define N_TRELOFF(x) (N_DATOFF(x) + (x).a_data)
#endif

#ifndef N_DRELOFF
#define N_DRELOFF(x) (N_TRELOFF(x) + (x).a_trsize)
#endif

#ifndef N_SYMOFF
#define N_SYMOFF(x) (N_DRELOFF(x) + (x).a_drsize)
#endif

#ifndef N_STROFF
#define N_STROFF(x) (N_SYMOFF(x) + (x).a_syms)
#endif

/* Address of text segment in memory after it is loaded. */
#ifndef N_TXTADDR
#define N_TXTADDR(x) 0
#endif

/* Address of data segment in memory after it is loaded.
Note that it is up to you to define SEGMENT_SIZE
on machines not listed here. /
#if defined(vax) || defined(hp300) || defined(pyr)
#define SEGMENT_SIZE PAGE_SIZE
#endif
#ifdef hp300
#define PAGE_SIZE 4096
#endif
#ifdef sony
#define SEGMENT_SIZE 0x2000
#endif /
Sony. */
#ifdef is68k
#define SEGMENT_SIZE 0x20000
#endif
#if defined(m68k) && defined(PORTAR)
#define PAGE_SIZE 0x400
#define SEGMENT_SIZE PAGE_SIZE
#endif

#define PAGE_SIZE 4096
#define SEGMENT_SIZE 1024

#define _N_SEGMENT_ROUND(x) (((x) + SEGMENT_SIZE - 1) & ~(SEGMENT_SIZE - 1))

#define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)

#ifndef N_DATADDR
#define N_DATADDR(x)
(N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x))
: (_N_SEGMENT_ROUND (_N_TXTENDADDR(x))))
#endif

/* Address of bss segment in memory after it is loaded. */
#ifndef N_BSSADDR
#define N_BSSADDR(x) (N_DATADDR(x) + (x).a_data)
#endif

#ifndef N_NLIST_DECLARED
struct nlist {
union {
char *n_name;
struct nlist *n_next;
long n_strx;
} n_un;
unsigned char n_type;
char n_other;
short n_desc;
unsigned long n_value;
};
#endif

#ifndef N_UNDF
#define N_UNDF 0
#endif
#ifndef N_ABS
#define N_ABS 2
#endif
#ifndef N_TEXT
#define N_TEXT 4
#endif
#ifndef N_DATA
#define N_DATA 6
#endif
#ifndef N_BSS
#define N_BSS 8
#endif
#ifndef N_COMM
#define N_COMM 18
#endif
#ifndef N_FN
#define N_FN 15
#endif

#ifndef N_EXT
#define N_EXT 1
#endif
#ifndef N_TYPE
#define N_TYPE 036
#endif
#ifndef N_STAB
#define N_STAB 0340
#endif

/* The following type indicates the definition of a symbol as being
an indirect reference to another symbol. The other symbol
appears as an undefined reference, immediately following this symbol.

Indirection is asymmetrical. The other symbol’s value will be used
to satisfy requests for the indirect symbol, but not vice versa.
If the other symbol does not have a definition, libraries will
be searched to find a definition. */
#define N_INDR 0xa

/* The following symbols refer to set elements.
All the N_SET[ATDB] symbols with the same name form one set.
Space is allocated for the set in the text section, and each set
element’s value is stored into one word of the space.
The first word of the space is the length of the set (number of elements).

The address of the set is made into an N_SETV symbol
whose name is the same as the name of the set.
This symbol acts like a N_DATA global symbol
in that it can satisfy undefined external references. */

/* These appear as input to LD, in a .o file. /
#define N_SETA 0x14 /
Absolute set element symbol /
#define N_SETT 0x16 /
Text set element symbol /
#define N_SETD 0x18 /
Data set element symbol /
#define N_SETB 0x1A /
Bss set element symbol */

/* This is output from LD. /
#define N_SETV 0x1C /
Pointer to set vector in data area. */

#ifndef N_RELOCATION_INFO_DECLARED

/* This structure describes a single relocation to be performed.
The text-relocation section of the file is a vector of these structures,
all of which apply to the text section.
Likewise, the data-relocation section applies to the data section. */

struct relocation_info
{
/* Address (within segment) to be relocated. /
int r_address;
/
The meaning of r_symbolnum depends on r_extern. /
unsigned int r_symbolnum:24;
/
Nonzero means value is a pc-relative offset
and it should be relocated for changes in its own address
as well as for changes in the symbol or section specified. /
unsigned int r_pcrel:1;
/
Length (as exponent of 2) of the field to be relocated.
Thus, a value of 2 indicates 1<<2 bytes. /
unsigned int r_length:2;
/
1 => relocate with value of symbol.
r_symbolnum is the index of the symbol
in file’s the symbol table.
0 => relocate with the address of a segment.
r_symbolnum is N_TEXT, N_DATA, N_BSS or N_ABS
(the N_EXT bit may be set also, but signifies nothing). /
unsigned int r_extern:1;
/
Four bits that aren’t used, but when writing an object file
it is desirable to clear them. /
unsigned int r_pad:4;
};
#endif /
no N_RELOCATION_INFO_DECLARED. */

#endif /* A_OUT_GNU_H */



const.h
#ifndef _CONST_H
#define _CONST_H

#define BUFFER_END 0x200000

#define I_TYPE 0170000
#define I_DIRECTORY 0040000
#define I_REGULAR 0100000
#define I_BLOCK_SPECIAL 0060000
#define I_CHAR_SPECIAL 0020000
#define I_NAMED_PIPE 0010000
#define I_SET_UID_BIT 0004000
#define I_SET_GID_BIT 0002000

#endif



ctype.h
#ifndef _CTYPE_H
#define _CTYPE_H

#define _U 0x01 /* upper /
#define _L 0x02 /
lower /
#define _D 0x04 /
digit /
#define _C 0x08 /
cntrl /
#define _P 0x10 /
punct /
#define _S 0x20 /
white space (space/lf/tab) /
#define _X 0x40 /
hex digit /
#define _SP 0x80 /
hard space (0x20) */

extern unsigned char _ctype[];
extern char _ctmp;

#define isalnum© ((_ctype+1)[c]&(_U|_L|_D))
#define isalpha© ((_ctype+1)[c]&(_U|_L))
#define iscntrl© ((_ctype+1)[c]&(_C))
#define isdigit© ((_ctype+1)[c]&(_D))
#define isgraph© ((_ctype+1)[c]&(_P|_U|_L|_D))
#define islower© ((_ctype+1)[c]&(_L))
#define isprint© ((_ctype+1)[c]&(_P|_U|_L|_D|_SP))
#define ispunct© ((_ctype+1)[c]&(_P))
#define isspace© ((_ctype+1)[c]&(_S))
#define isupper© ((_ctype+1)[c]&(_U))
#define isxdigit© ((_ctype+1)[c]&(_D|_X))

#define isascii© (((unsigned) c)<=0x7f)
#define toascii© (((unsigned) c)&0x7f)

#define tolower© (_ctmp=c,isupper(_ctmp)?_ctmp+(‘a’+‘A’):_ctmp)
#define toupper© (_ctmp=c,islower(_ctmp)?_ctmp+(‘A’-‘a’):_ctmp)

#endif



errno.h
#ifndef _ERRNO_H
#define _ERRNO_H

/*

  • ok, as I hadn’t got any other source of information about
  • possible error numbers, I was forced to use the same numbers
  • as minix.
  • Hopefully these are posix or something. I wouldn’t know (and posix
  • isn’t telling me - they want $$$ for their f***ing standard).
  • We don’t use the _SIGN cludge of minix, so kernel returns must
  • see to the sign by themselves.
  • NOTE! Remember to change strerror() if you change this file!
    */

extern int errno;

#define ERROR 99
#define EPERM 1
#define ENOENT 2
#define ESRCH 3
#define EINTR 4
#define EIO 5
#define ENXIO 6
#define E2BIG 7
#define ENOEXEC 8
#define EBADF 9
#define ECHILD 10
#define EAGAIN 11
#define ENOMEM 12
#define EACCES 13
#define EFAULT 14
#define ENOTBLK 15
#define EBUSY 16
#define EEXIST 17
#define EXDEV 18
#define ENODEV 19
#define ENOTDIR 20
#define EISDIR 21
#define EINVAL 22
#define ENFILE 23
#define EMFILE 24
#define ENOTTY 25
#define ETXTBSY 26
#define EFBIG 27
#define ENOSPC 28
#define ESPIPE 29
#define EROFS 30
#define EMLINK 31
#define EPIPE 32
#define EDOM 33
#define ERANGE 34
#define EDEADLK 35
#define ENAMETOOLONG 36
#define ENOLCK 37
#define ENOSYS 38
#define ENOTEMPTY 39

#endif



fcntl.h
#ifndef _FCNTL_H
#define _FCNTL_H

#include <sys/types.h>

/* open/fcntl - NOCTTY, NDELAY isn’t implemented yet /
#define O_ACCMODE 00003
#define O_RDONLY 00
#define O_WRONLY 01
#define O_RDWR 02
#define O_CREAT 00100 /
not fcntl /
#define O_EXCL 00200 /
not fcntl /
#define O_NOCTTY 00400 /
not fcntl /
#define O_TRUNC 01000 /
not fcntl /
#define O_APPEND 02000
#define O_NONBLOCK 04000 /
not fcntl */
#define O_NDELAY O_NONBLOCK

/* Defines for fcntl-commands. Note that currently

  • locking isn’t supported, and other things aren’t really
  • tested.
    /
    #define F_DUPFD 0 /
    dup /
    #define F_GETFD 1 /
    get f_flags /
    #define F_SETFD 2 /
    set f_flags /
    #define F_GETFL 3 /
    more flags (cloexec) /
    #define F_SETFL 4
    #define F_GETLK 5 /
    not implemented */
    #define F_SETLK 6
    #define F_SETLKW 7

/* for F_[GET|SET]FL /
#define FD_CLOEXEC 1 /
actually anything with low bit set goes */

/* Ok, these are locking features, and aren’t implemented at any

  • level. POSIX wants them.
    */
    #define F_RDLCK 0
    #define F_WRLCK 1
    #define F_UNLCK 2

/* Once again - not implemented, but … */
struct flock {
short l_type;
short l_whence;
off_t l_start;
off_t l_len;
pid_t l_pid;
};

extern int creat(const char * filename,mode_t mode);
extern int fcntl(int fildes,int cmd, …);
extern int open(const char * filename, int flags, …);

#endif



signal.h
#ifndef _SIGNAL_H
#define _SIGNAL_H

#include <sys/types.h>

typedef int sig_atomic_t;
typedef unsigned int sigset_t; /* 32 bits */

#define _NSIG 32
#define NSIG _NSIG

#define SIGHUP 1
#define SIGINT 2
#define SIGQUIT 3
#define SIGILL 4
#define SIGTRAP 5
#define SIGABRT 6
#define SIGIOT 6
#define SIGUNUSED 7
#define SIGFPE 8
#define SIGKILL 9
#define SIGUSR1 10
#define SIGSEGV 11
#define SIGUSR2 12
#define SIGPIPE 13
#define SIGALRM 14
#define SIGTERM 15
#define SIGSTKFLT 16
#define SIGCHLD 17
#define SIGCONT 18
#define SIGSTOP 19
#define SIGTSTP 20
#define SIGTTIN 21
#define SIGTTOU 22

/* Ok, I haven’t implemented sigactions, but trying to keep headers POSIX */
#define SA_NOCLDSTOP 1

#define SIG_BLOCK 0 /* for blocking signals /
#define SIG_UNBLOCK 1 /
for unblocking signals /
#define SIG_SETMASK 2 /
for setting the signal mask */

#define SIG_DFL ((void ()(int))0) / default signal handling /
#define SIG_IGN ((void (
)(int))1) /* ignore signal */

struct sigaction {
void (*sa_handler)(int);
sigset_t sa_mask;
int sa_flags;
};

void (signal(int _sig, void (_func)(int)))(int);
int raise(int sig);
int kill(pid_t pid, int sig);
int sigaddset(sigset_t *mask, int signo);
int sigdelset(sigset_t *mask, int signo);
int sigemptyset(sigset_t *mask);
int sigfillset(sigset_t *mask);
int sigismember(sigset_t mask, int signo); / 1 - is, 0 - not, -1 error */
int sigpending(sigset_t *set);
int sigprocmask(int how, sigset_t *set, sigset_t *oldset);
int sigsuspend(sigset_t *sigmask);
int sigaction(int sig, struct sigaction *act, struct sigaction *oldact);

#endif /* _SIGNAL_H */



stdarg.h
#ifndef _STDARG_H
#define _STDARG_H

typedef char *va_list;

/* Amount of space required in an argument list for an arg of type TYPE.
TYPE may alternatively be an expression whose type is used. */

#define __va_rounded_size(TYPE)
(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))

#ifndef sparc
#define va_start(AP, LASTARG)
(AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
#else
#define va_start(AP, LASTARG)
(__builtin_saveregs (),
AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
#endif

void va_end (va_list); /* Defined in gnulib */
#define va_end(AP)

#define va_arg(AP, TYPE)
(AP += __va_rounded_size (TYPE),
*((TYPE *) (AP - __va_rounded_size (TYPE))))

#endif /* _STDARG_H */



stddef.h
#ifndef _STDDEF_H
#define _STDDEF_H

#ifndef _PTRDIFF_T
#define _PTRDIFF_T
typedef long ptrdiff_t;
#endif

#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned long size_t;
#endif

#undef NULL
#define NULL ((void *)0)

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

#endif



string.h
#ifndef STRING_H
#define STRING_H

#ifndef NULL
#define NULL ((void *) 0)
#endif

#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned int size_t;
#endif

extern char * strerror(int errno);

/*

  • This string-include defines all string functions as inline
  • functions. Use gcc. It also assumes ds=es=data space, this should be
  • normal. Most of the string-functions are rather heavily hand-optimized,
  • see especially strtok,strstr,str[c]spn. They should work, but are not
  • very easy to understand. Everything is done entirely within the register
  • set, making the functions fast and clean. String instructions have been
  • used through-out, making for “slightly” unclear code 😃
  •  (C) 1991 Linus Torvalds
    

*/

extern inline char * strcpy(char * dest,const char *src)
{
asm(“cld\n”
“1:\tlodsb\n\t”
“stosb\n\t”
“testb %%al,%%al\n\t”
“jne 1b”
::“S” (src),“D” (dest):“si”,“di”,“ax”);
return dest;
}

extern inline char * strncpy(char * dest,const char *src,int count)
{
asm(“cld\n”
“1:\tdecl %2\n\t”
“js 2f\n\t”
“lodsb\n\t”
“stosb\n\t”
“testb %%al,%%al\n\t”
“jne 1b\n\t”
“rep\n\t”
“stosb\n”
“2:”
::“S” (src),“D” (dest),“c” (count):“si”,“di”,“ax”,“cx”);
return dest;
}

extern inline char * strcat(char * dest,const char * src)
{
asm(“cld\n\t”
“repne\n\t”
“scasb\n\t”
“decl %1\n”
“1:\tlodsb\n\t”
“stosb\n\t”
“testb %%al,%%al\n\t”
“jne 1b”
::“S” (src),“D” (dest),“a” (0),“c” (0xffffffff):“si”,“di”,“ax”,“cx”);
return dest;
}

extern inline char * strncat(char * dest,const char * src,int count)
{
asm(“cld\n\t”
“repne\n\t”
“scasb\n\t”
“decl %1\n\t”
“movl %4,%3\n”
“1:\tdecl %3\n\t”
“js 2f\n\t”
“lodsb\n\t”
“stosb\n\t”
“testb %%al,%%al\n\t”
“jne 1b\n”
“2:\txorl %2,%2\n\t”
“stosb”
::“S” (src),“D” (dest),“a” (0),“c” (0xffffffff),“g” (count)
:“si”,“di”,“ax”,“cx”);
return dest;
}

extern inline int strcmp(const char * cs,const char * ct)
{
register int __res asm(“ax”);
asm(“cld\n”
“1:\tlodsb\n\t”
“scasb\n\t”
“jne 2f\n\t”
“testb %%al,%%al\n\t”
“jne 1b\n\t”
“xorl %%eax,%%eax\n\t”
“jmp 3f\n”
“2:\tmovl $1,%%eax\n\t”
“jl 3f\n\t”
“negl %%eax\n”
“3:”
:"=a" (__res):“D” (cs),“S” (ct):“si”,“di”);
return __res;
}

extern inline int strncmp(const char * cs,const char * ct,int count)
{
register int __res asm(“ax”);
asm(“cld\n”
“1:\tdecl %3\n\t”
“js 2f\n\t”
“lodsb\n\t”
“scasb\n\t”
“jne 3f\n\t”
“testb %%al,%%al\n\t”
“jne 1b\n”
“2:\txorl %%eax,%%eax\n\t”
“jmp 4f\n”
“3:\tmovl $1,%%eax\n\t”
“jl 4f\n\t”
“negl %%eax\n”
“4:”
:"=a" (__res):“D” (cs),“S” (ct),“c” (count):“si”,“di”,“cx”);
return __res;
}

extern inline char * strchr(const char * s,char c)
{
register char * __res asm(“ax”);
asm(“cld\n\t”
“movb %%al,%%ah\n”
“1:\tlodsb\n\t”
“cmpb %%ah,%%al\n\t”
“je 2f\n\t”
“testb %%al,%%al\n\t”
“jne 1b\n\t”
“movl $1,%1\n”
“2:\tmovl %1,%0\n\t”
“decl %0”
:"=a" (__res):“S” (s),“0” ©:“si”);
return __res;
}

extern inline char * strrchr(const char * s,char c)
{
register char * __res asm(“dx”);
asm(“cld\n\t”
“movb %%al,%%ah\n”
“1:\tlodsb\n\t”
“cmpb %%ah,%%al\n\t”
“jne 2f\n\t”
“movl %%esi,%0\n\t”
“decl %0\n”
“2:\ttestb %%al,%%al\n\t”
“jne 1b”
:"=d" (__res):“0” (0),“S” (s),“a” ©:“ax”,“si”);
return __res;
}

extern inline int strspn(const char * cs, const char * ct)
{
register char * __res asm(“si”);
asm(“cld\n\t”
“movl %4,%%edi\n\t”
“repne\n\t”
“scasb\n\t”
“notl %%ecx\n\t”
“decl %%ecx\n\t”
“movl %%ecx,%%edx\n”
“1:\tlodsb\n\t”
“testb %%al,%%al\n\t”
“je 2f\n\t”
“movl %4,%%edi\n\t”
“movl %%edx,%%ecx\n\t”
“repne\n\t”
“scasb\n\t”
“je 1b\n”
“2:\tdecl %0”
:"=S" (__res):“a” (0),“c” (0xffffffff),“0” (cs),“g” (ct)
:“ax”,“cx”,“dx”,“di”);
return __res-cs;
}

extern inline int strcspn(const char * cs, const char * ct)
{
register char * __res asm(“si”);
asm(“cld\n\t”
“movl %4,%%edi\n\t”
“repne\n\t”
“scasb\n\t”
“notl %%ecx\n\t”
“decl %%ecx\n\t”
“movl %%ecx,%%edx\n”
“1:\tlodsb\n\t”
“testb %%al,%%al\n\t”
“je 2f\n\t”
“movl %4,%%edi\n\t”
“movl %%edx,%%ecx\n\t”
“repne\n\t”
“scasb\n\t”
“jne 1b\n”
“2:\tdecl %0”
:"=S" (__res):“a” (0),“c” (0xffffffff),“0” (cs),“g” (ct)
:“ax”,“cx”,“dx”,“di”);
return __res-cs;
}

extern inline char * strpbrk(const char * cs,const char * ct)
{
register char * __res asm(“si”);
asm(“cld\n\t”
“movl %4,%%edi\n\t”
“repne\n\t”
“scasb\n\t”
“notl %%ecx\n\t”
“decl %%ecx\n\t”
“movl %%ecx,%%edx\n”
“1:\tlodsb\n\t”
“testb %%al,%%al\n\t”
“je 2f\n\t”
“movl %4,%%edi\n\t”
“movl %%edx,%%ecx\n\t”
“repne\n\t”
“scasb\n\t”
“jne 1b\n\t”
“decl %0\n\t”
“jmp 3f\n”
“2:\txorl %0,%0\n”
“3:”
:"=S" (__res):“a” (0),“c” (0xffffffff),“0” (cs),“g” (ct)
:“ax”,“cx”,“dx”,“di”);
return __res;
}

extern inline char * strstr(const char * cs,const char * ct)
{
register char * __res asm(“ax”);
asm(“cld\n\t”
“movl %4,%%edi\n\t”
“repne\n\t”
“scasb\n\t”
“notl %%ecx\n\t”
“decl %%ecx\n\t” /* NOTE! This also sets Z if searchstring=’’ /
“movl %%ecx,%%edx\n”
“1:\tmovl %4,%%edi\n\t”
“movl %%esi,%%eax\n\t”
“movl %%edx,%%ecx\n\t”
“repe\n\t”
“cmpsb\n\t”
“je 2f\n\t” /
also works for empty string, see above */
“xchgl %%eax,%%esi\n\t”
“incl %%esi\n\t”
“cmpb $0,-1(%%eax)\n\t”
“jne 1b\n\t”
“xorl %%eax,%%eax\n\t”
“2:”
:"=a" (__res):“0” (0),“c” (0xffffffff),“S” (cs),“g” (ct)
:“cx”,“dx”,“di”,“si”);
return __res;
}

extern inline int strlen(const char * s)
{
register int __res asm(“cx”);
asm(“cld\n\t”
“repne\n\t”
“scasb\n\t”
“notl %0\n\t”
“decl %0”
:"=c" (__res):“D” (s),“a” (0),“0” (0xffffffff):“di”);
return __res;
}

extern char * ___strtok;

extern inline char * strtok(char * s,const char * ct)
{
register char * __res asm(“si”);
asm(“testl %1,%1\n\t”
“jne 1f\n\t”
“testl %0,%0\n\t”
“je 8f\n\t”
“movl %0,%1\n”
“1:\txorl %0,%0\n\t”
“movl $-1,%%ecx\n\t”
“xorl %%eax,%%eax\n\t”
“cld\n\t”
“movl %4,%%edi\n\t”
“repne\n\t”
“scasb\n\t”
“notl %%ecx\n\t”
“decl %%ecx\n\t”
“je 7f\n\t” /* empty delimeter-string */
“movl %%ecx,%%edx\n”
“2:\tlodsb\n\t”
“testb %%al,%%al\n\t”
“je 7f\n\t”
“movl %4,%%edi\n\t”
“movl %%edx,%%ecx\n\t”
“repne\n\t”
“scasb\n\t”
“je 2b\n\t”
“decl %1\n\t”
“cmpb $0,(%1)\n\t”
“je 7f\n\t”
“movl %1,%0\n”
“3:\tlodsb\n\t”
“testb %%al,%%al\n\t”
“je 5f\n\t”
“movl %4,%%edi\n\t”
“movl %%edx,%%ecx\n\t”
“repne\n\t”
“scasb\n\t”
“jne 3b\n\t”
“decl %1\n\t”
“cmpb $0,(%1)\n\t”
“je 5f\n\t”
“movb $0,(%1)\n\t”
“incl %1\n\t”
“jmp 6f\n”
“5:\txorl %1,%1\n”
“6:\tcmpb $0,(%0)\n\t”
“jne 7f\n\t”
“xorl %0,%0\n”
“7:\ttestl %0,%0\n\t”
“jne 8f\n\t”
“movl %0,%1\n”
“8:”
:"=b" (__res),"=S" (___strtok)
:“0” (___strtok),“1” (s),“g” (ct)
:“ax”,“cx”,“dx”,“di”);
return __res;
}

extern inline void * memcpy(void * dest,const void * src, int n)
{
asm(“cld\n\t”
“rep\n\t”
“movsb”
::“c” (n),“S” (src),“D” (dest)
:“cx”,“si”,“di”);
return dest;
}

extern inline void * memmove(void * dest,const void * src, int n)
{
if (dest<src)
asm(“cld\n\t”
“rep\n\t”
“movsb”
::“c” (n),“S” (src),“D” (dest)
:“cx”,“si”,“di”);
else
asm(“std\n\t”
“rep\n\t”
“movsb”
::“c” (n),“S” (src+n-1),“D” (dest+n-1)
:“cx”,“si”,“di”);
return dest;
}

extern inline int memcmp(const void * cs,const void * ct,int count)
{
register int __res asm(“ax”);
asm(“cld\n\t”
“repe\n\t”
“cmpsb\n\t”
“je 1f\n\t”
“movl $1,%%eax\n\t”
“jl 1f\n\t”
“negl %%eax\n”
“1:”
:"=a" (__res):“0” (0),“D” (cs),“S” (ct),“c” (count)
:“si”,“di”,“cx”);
return __res;
}

extern inline void * memchr(const void * cs,char c,int count)
{
register void * __res asm(“di”);
if (!count)
return NULL;
asm(“cld\n\t”
“repne\n\t”
“scasb\n\t”
“je 1f\n\t”
“movl $1,%0\n”
“1:\tdecl %0”
:"=D" (__res):“a” ©,“D” (cs),“c” (count)
:“cx”);
return __res;
}

extern inline void * memset(void * s,char c,int count)
{
asm(“cld\n\t”
“rep\n\t”
“stosb”
::“a” ©,“D” (s),“c” (count)
:“cx”,“di”);
return s;
}

#endif



termios.h
#ifndef _TERMIOS_H
#define _TERMIOS_H

#define TTY_BUF_SIZE 1024

/* 0x54 is just a magic number to make these relatively uniqe (‘T’) */

#define TCGETS 0x5401
#define TCSETS 0x5402
#define TCSETSW 0x5403
#define TCSETSF 0x5404
#define TCGETA 0x5405
#define TCSETA 0x5406
#define TCSETAW 0x5407
#define TCSETAF 0x5408
#define TCSBRK 0x5409
#define TCXONC 0x540A
#define TCFLSH 0x540B
#define TIOCEXCL 0x540C
#define TIOCNXCL 0x540D
#define TIOCSCTTY 0x540E
#define TIOCGPGRP 0x540F
#define TIOCSPGRP 0x5410
#define TIOCOUTQ 0x5411
#define TIOCSTI 0x5412
#define TIOCGWINSZ 0x5413
#define TIOCSWINSZ 0x5414
#define TIOCMGET 0x5415
#define TIOCMBIS 0x5416
#define TIOCMBIC 0x5417
#define TIOCMSET 0x5418
#define TIOCGSOFTCAR 0x5419
#define TIOCSSOFTCAR 0x541A

struct winsize {
unsigned short ws_row;
unsigned short ws_col;
unsigned short ws_xpixel;
unsigned short ws_ypixel;
};

#define NCC 8
struct termio {
unsigned short c_iflag; /* input mode flags /
unsigned short c_oflag; /
output mode flags /
unsigned short c_cflag; /
control mode flags /
unsigned short c_lflag; /
local mode flags /
unsigned char c_line; /
line discipline /
unsigned char c_cc[NCC]; /
control characters */
};

#define NCCS 17
struct termios {
unsigned long c_iflag; /* input mode flags /
unsigned long c_oflag; /
output mode flags /
unsigned long c_cflag; /
control mode flags /
unsigned long c_lflag; /
local mode flags /
unsigned char c_line; /
line discipline /
unsigned char c_cc[NCCS]; /
control characters */
};

/* c_cc characters */
#define VINTR 0
#define VQUIT 1
#define VERASE 2
#define VKILL 3
#define VEOF 4
#define VTIME 5
#define VMIN 6
#define VSWTC 7
#define VSTART 8
#define VSTOP 9
#define VSUSP 10
#define VEOL 11
#define VREPRINT 12
#define VDISCARD 13
#define VWERASE 14
#define VLNEXT 15
#define VEOL2 16

/* c_iflag bits */
#define IGNBRK 0000001
#define BRKINT 0000002
#define IGNPAR 0000004
#define PARMRK 0000010
#define INPCK 0000020
#define ISTRIP 0000040
#define INLCR 0000100
#define IGNCR 0000200
#define ICRNL 0000400
#define IUCLC 0001000
#define IXON 0002000
#define IXANY 0004000
#define IXOFF 0010000
#define IMAXBEL 0020000

/* c_oflag bits */
#define OPOST 0000001
#define OLCUC 0000002
#define ONLCR 0000004
#define OCRNL 0000010
#define ONOCR 0000020
#define ONLRET 0000040
#define OFILL 0000100
#define OFDEL 0000200
#define NLDLY 0000400
#define NL0 0000000
#define NL1 0000400
#define CRDLY 0003000
#define CR0 0000000
#define CR1 0001000
#define CR2 0002000
#define CR3 0003000
#define TABDLY 0014000
#define TAB0 0000000
#define TAB1 0004000
#define TAB2 0010000
#define TAB3 0014000
#define XTABS 0014000
#define BSDLY 0020000
#define BS0 0000000
#define BS1 0020000
#define VTDLY 0040000
#define VT0 0000000
#define VT1 0040000
#define FFDLY 0040000
#define FF0 0000000
#define FF1 0040000

/* c_cflag bit meaning /
#define CBAUD 0000017
#define B0 0000000 /
hang up /
#define B50 0000001
#define B75 0000002
#define B110 0000003
#define B134 0000004
#define B150 0000005
#define B200 0000006
#define B300 0000007
#define B600 0000010
#define B1200 0000011
#define B1800 0000012
#define B2400 0000013
#define B4800 0000014
#define B9600 0000015
#define B19200 0000016
#define B38400 0000017
#define CSIZE 0000060
#define CS5 0000000
#define CS6 0000020
#define CS7 0000040
#define CS8 0000060
#define CSTOPB 0000100
#define CREAD 0000200
#define CPARENB 0000400
#define CPARODD 0001000
#define HUPCL 0002000
#define CLOCAL 0004000
#define CIBAUD 03600000 /
input baud rate (not used) /
#define CRTSCTS 020000000000 /
flow control */

/* c_lflag bits */
#define ISIG 0000001
#define ICANON 0000002
#define XCASE 0000004
#define ECHO 0000010
#define ECHOE 0000020
#define ECHOK 0000040
#define ECHONL 0000100
#define NOFLSH 0000200
#define TOSTOP 0000400
#define ECHOCTL 0001000
#define ECHOPRT 0002000
#define ECHOKE 0004000
#define FLUSHO 0010000
#define PENDIN 0040000
#define IEXTEN 0100000

/* modem lines */
#define TIOCM_LE 0x001
#define TIOCM_DTR 0x002
#define TIOCM_RTS 0x004
#define TIOCM_ST 0x008
#define TIOCM_SR 0x010
#define TIOCM_CTS 0x020
#define TIOCM_CAR 0x040
#define TIOCM_RNG 0x080
#define TIOCM_DSR 0x100
#define TIOCM_CD TIOCM_CAR
#define TIOCM_RI TIOCM_RNG

/* tcflow() and TCXONC use these */
#define TCOOFF 0
#define TCOON 1
#define TCIOFF 2
#define TCION 3

/* tcflush() and TCFLSH use these */
#define TCIFLUSH 0
#define TCOFLUSH 1
#define TCIOFLUSH 2

/* tcsetattr uses these */
#define TCSANOW 0
#define TCSADRAIN 1
#define TCSAFLUSH 2

typedef int speed_t;

extern speed_t cfgetispeed(struct termios *termios_p);
extern speed_t cfgetospeed(struct termios *termios_p);
extern int cfsetispeed(struct termios *termios_p, speed_t speed);
extern int cfsetospeed(struct termios *termios_p, speed_t speed);
extern int tcdrain(int fildes);
extern int tcflow(int fildes, int action);
extern int tcflush(int fildes, int queue_selector);
extern int tcgetattr(int fildes, struct termios *termios_p);
extern int tcsendbreak(int fildes, int duration);
extern int tcsetattr(int fildes, int optional_actions,
struct termios *termios_p);

#endif



time.h
#ifndef _TIME_H
#define _TIME_H

#ifndef _TIME_T
#define _TIME_T
typedef long time_t;
#endif

#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned int size_t;
#endif

#define CLOCKS_PER_SEC 100

typedef long clock_t;

struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};

clock_t clock(void);
time_t time(time_t * tp);
double difftime(time_t time2, time_t time1);
time_t mktime(struct tm * tp);

char * asctime(const struct tm * tp);
char * ctime(const time_t * tp);
struct tm * gmtime(const time_t *tp);
struct tm *localtime(const time_t * tp);
size_t strftime(char * s, size_t smax, const char * fmt, const struct tm * tp);
void tzset(void);

#endif



unistd.h
#ifndef _UNISTD_H
#define _UNISTD_H

/* ok, this may be a joke, but I’m working on it */
#define _POSIX_VERSION 198808L

#define _POSIX_CHOWN_RESTRICTED /* only root can do a chown (I think…) /
/
#define _POSIX_NO_TRUNC*/ /* pathname truncation (but see in kernel) /
#define _POSIX_VDISABLE ‘\0’ /
character to disable things like ^C /
/
#define _POSIX_SAVED_IDS / / we’ll get to this yet /
/
#define _POSIX_JOB_CONTROL / / we aren’t there quite yet. Soon hopefully */

#define STDIN_FILENO 0
#define STDOUT_FILENO 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

/* _SC stands for System Configuration. We don’t use them much */
#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

/* more (possibly) configurable things - now pathnames */
#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 /* used only by init, to get system going */
#define __NR_exit 1
#define __NR_fork 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_chown 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 _syscall0(type,name)
type name(void)
{
type __res;
asm volatile (“int $0x80”
: “=a” (__res)
: “0” (_NR##name));
if (__res >= 0)
return __res;
errno = -__res;
return -1;
}

#define _syscall1(type,name,atype,a)
type name(atype a)
{
type __res;
asm volatile (“int $0x80”
: “=a” (__res)
: “0” (_NR##name),“b” (a));
if (__res >= 0)
return __res;
errno = -__res;
return -1;
}

#define _syscall2(type,name,atype,a,btype,b)
type name(atype a,btype b)
{
type __res;
asm volatile (“int $0x80”
: “=a” (__res)
: “0” (_NR##name),“b” (a),“c” (b));
if (__res >= 0)
return __res;
errno = -__res;
return -1;
}

#define _syscall3(type,name,atype,a,btype,b,ctype,c)
type name(atype a,btype b,ctype c)
{
type __res;
asm volatile (“int $0x80”
: “=a” (__res)
: “0” (_NR##name),“b” (a),“c” (b),“d” ©);
if (__res<0)
errno=-__res , __res = -1;
return __res;
}

#endif /* LIBRARY */

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 * filename);
int chmod(const char * filename, mode_t mode);
int chown(const char * filename, uid_t owner, gid_t group);
int chroot(const char * filename);
int close(int fildes);
int creat(const char * filename, mode_t mode);
int dup(int fildes);
int execve(const char * filename, char ** argv, char ** envp);
int execv(const char * pathname, char ** argv);
int execvp(const char * file, char ** argv);
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 * filename1, const char * filename2);
int lseek(int fildes, off_t offset, int origin);
int mknod(const char * filename, mode_t mode, dev_t dev);
int mount(const char * specialfile, const char * dir, int rwflag);
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 setpgid(pid_t pid,pid_t pgid);
int setuid(uid_t uid);
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 * tbuf);
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_stat,int options);
pid_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



utime.h
#ifndef _UTIME_H
#define _UTIME_H

#include <sys/types.h> /* I know - shouldn’t do this, but … */

struct utimbuf {
time_t actime;
time_t modtime;
};

extern int utime(const char *filename, struct utimbuf *times);

#endif



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

低调的小哥哥

你的关注就是我为你服务的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值