����15-8 linux/lib/open.c


  1 /*

  2  *  linux/lib/open.c

  3  *

  4  *  (C) 1991  Linus Torvalds

  5  */

  6

  7 #define __LIBRARY__

  8 #include <unistd.h>       // Linux��׼ͷ�ļ��������˸��ַ��ų��������ͣ��������˸��ֺ�����

                              // �綨����__LIBRARY__���򻹺�ϵͳ���úź���Ƕ���_syscall0()�ȡ�

  9 #include <stdarg.h>       // ��׼����ͷ�ļ����Ժ����ʽ������������б�����Ҫ˵����-��

                              // ����(va_list)��������(va_start, va_arg��va_end)������

                              // vsprintf��vprintf��vfprintf������

 10

    //// ���ļ�������

    // �򿪲��п��ܴ���һ���ļ���

    // ������filename - �ļ�����flag - �ļ��򿪱�־��...

    // ���أ��ļ������������������ó����룬������-1��

    // ��13�ж�����һ���Ĵ�������res���ñ�������������һ���Ĵ����У��Ա��ڸ�Ч���ʺͲ�����

    // ����ָ����ŵļĴ���������eax������ô���԰Ѹþ�д����register int res asm("ax");����

 11 int open(const char * filename, int flag, ...)

 12 {

 13         register int res;

 14         va_list arg;

 15

    // ����va_start()�꺯����ȡ��flag���������ָ�룬Ȼ�����ϵͳ�ж�int 0x80������open����

    // �ļ��򿪲�����

    // %0 - eax(���ص��������������)��%1 - eax(ϵͳ�жϵ��ù��ܺ�__NR_open)��

    // %2 - ebx(�ļ���filename)��%3 - ecx(���ļ���־flag)��%4 - edx(��������ļ�����mode)��

 16         va_start(arg,flag);

 17         __asm__("int $0x80"

 18                 :"=a" (res)

 19                 :"" (__NR_open),"b" (filename),"c" (flag),

 20                 "d" (va_arg(arg,int)));

    // ϵͳ�жϵ��÷���ֵ���ڻ����0����ʾ��һ���ļ�����������ֱ�ӷ���֮��

 21         if (res>=0)

 22                 return res;

    // ����˵������ֵС��0�������һ�������롣���øó����벢����-1��

 23         errno = -res;

 24         return -1;

 25 }

 26