Windows系统C语言代码一览
生活随笔
收集整理的這篇文章主要介紹了
Windows系统C语言代码一览
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者弄到了Windows系統的部分源代碼文件,今天把其中是c語言的文件給大家分享一下
/* cds utilities */ #include "types.h" #include "sysvar.h" #include "cds.h" #include "dpb.h" #include <dos.h>extern struct sysVarsType SysVars ;char fGetCDS(i, pLCDS) int i ; struct CDSType *pLCDS ; {struct CDSType far *cptr ;int j ;if (i >= 0 && i < SysVars.cCDS) {*(long *)(&cptr) = SysVars.pCDS + (i * sizeof(*pLCDS)) ;for (j=0 ; j <= sizeof(*pLCDS) ; j++)*((char *)pLCDS+j) = *((char far *)cptr+j) ;return TRUE ;} ;return FALSE ; }char fPutCDS(i, pLCDS) int i ; struct CDSType *pLCDS ; {struct CDSType far *cptr ;int j ;if (i >= 0 && i < SysVars.cCDS) {*(long *)(&cptr) = SysVars.pCDS + (i * sizeof(*pLCDS)) ;for (j=0 ; j <= sizeof(*pLCDS) ; j++)*((char far *)cptr+j) = *((char *)pLCDS+j) ;return TRUE ;} ;return FALSE ; }/* returns TRUE if drive i is a physical drive. Physical means that logical* drive n corresponds with physical drive n. This is the case ONLY if the* CDS is inuse and the DPB corresponding to the CDS has the physical drive* equal to the original drive.*/char fPhysical(i) int i ; {struct DPBType DPB ;struct DPBType *pd = &DPB ;struct DPBType far *dptr ;int j ;struct CDSType CDS ;if (!fGetCDS(i, &CDS))return FALSE ;if (TESTFLAG(CDS.flags,CDSNET | CDSSPLICE | CDSLOCAL))return FALSE ;*(long *)(&dptr) = CDS.pDPB ;for (j=0 ; j <= sizeof(DPB) ; j++)*((char *)pd+j) = *((char far *)dptr+j) ;return(i == DPB.drive) ; }/* return TRUE if the specified drive is a network drive. i is a 0-based* quantity*//* MODIFICATION HISTORY** M000 June 5/85 Barrys* Removed extra net check.*/char fNet(i) int i ; {union REGS ir ;register union REGS *iregs = &ir ; /* Used for DOS calls */struct CDSType CDS ;if (!fGetCDS(i, &CDS))return FALSE ;iregs->x.ax = IOCTL9 ; /* Function 0x4409 */iregs->x.bx = i + 1 ;intdos(iregs, iregs) ;/*** M000return(TESTFLAG(CDS.flags,CDSNET) || TESTFLAG(iregs->x.dx,0x1000)) ; ***/return(TESTFLAG(CDS.flags,CDSNET)) ; }/* return TRUE if the specified drive is a shared drive. i is a 0-based* quantity*/ char fShared(i) int i ; {struct CDSType CDS ;union REGS ir ;register union REGS *iregs = &ir ; /* Used for DOS calls */if (!fGetCDS(i, &CDS))return FALSE ;iregs->x.ax = IOCTL9 ; /* Function 0x4409 */iregs->x.bx = i + 1 ;intdos(iregs, iregs) ;return TESTFLAG(CDS.flags,CDSNET) || TESTFLAG(iregs->x.dx,0x0200) ; } /* dpb.c - retrieve DPB for physical drive */#include "types.h" #include "sysvar.h" #include "dpb.h" #include "cds.h"extern char NoMem[], ParmNum[], BadParm[] ; extern struct sysVarsType SysVars ;/* Walk the DPB list trying to find the appropriate DPB */long GetDPB(i) int i ; {struct DPBType DPB ;struct DPBType *pd = &DPB ;struct DPBType far *dptr ;int j ;*(long *)(&dptr) = DPB.nextDPB = SysVars.pDPB ;DPB.drive = -1 ;while (DPB.drive != i) {if ((int)DPB.nextDPB == -1)return -1L ;*(long *)(&dptr) = DPB.nextDPB ;for (j=0 ; j <= sizeof(DPB) ; j++)*((char *)pd+j) = *((char far *)dptr+j) ;} ;return (long)dptr ; } #include "types.h" #include "dpb.h" #include <dos.h>/* #define KANJI TRUE *//* return FALSE if drive is valid AND the path is not a prefix of a non-root* current directory.*/ char fPathErr(p) char *p ; {char buf[MAXARG] ;int d ; #ifdef KANJIchar *p1; #endifif (p[1] == ':')d = *p-'A'+1 ;elsed = 0 ;if (curdir(buf, d) == -1) /* drive is invalid => error */return(TRUE) ;if (strlen(buf) == 3) /* current directory is root => OK */return(FALSE) ;if (strpre(p, buf)) { #ifdef KANJIp1 = p;while (*p1 != NULL) {if(testkanj(*p1 & 0xFF))p1 += 2 ;elseif((*p1++ == '\\') && (*p1 == NULL))return(TRUE) ;} #elseif (p[strlen(p)-1] == '\\') /* prefix matched, prefix had...*/return(TRUE) ; /* ...trailing / => valid ... *//* ...prefix => ERROR */ #endifd = buf[strlen(p)] ;if (d == 0 || d == '\\') /* prefix matched,... */return(TRUE) ; /* ...prefix had no trailing /, *//* ...next char was / => ... *//* ...valid prefix => ERROR */} ;return(FALSE) ; /* drive letter good and not valid prefix => OK */ }strpre(pre, tot)char *pre;char *tot; {return(!strncmp(pre, tot, strlen(pre))); }Fatal(p) char *p ; {printf("%s\n", p) ;exit(1) ; }ffirst(pb, attr, pfbuf) char *pb ; int attr ; struct findType *pfbuf ; {union REGS regs ;/* set DMA to point to buffer */regs.h.ah = 0x1A ;regs.x.dx = (unsigned) pfbuf ;intdos (®s, ®s) ;/* perform system call */regs.h.ah = 0x4E ;regs.x.cx = attr ;regs.x.dx = (unsigned) pb ;intdos (®s, ®s) ;return (regs.x.cflag ? -1 : 0) ; }fnext (pfbuf) struct findType *pfbuf; {union REGS regs;/* set DMA to point to buffer */regs.h.ah = 0x1A;regs.x.dx = (unsigned) pfbuf;intdos (®s, ®s);/* perform system call */regs.h.ah = 0x4F;intdos (®s, ®s);return (regs.x.cflag ? -1 : 0) ; }char *strbscan(str, class) char *str ; char *class ; {char *p ;char *strpbrk() ;p = strpbrk(str, class) ;return((p == NULL) ? (str + strlen(str)) : p) ; }/* curdir.c - return text of current directory for a particular drive */curdir (dst, drive) char *dst ; int drive ; {union REGS regs ;*dst++ = PathChr ;regs.h.ah = 0x47 ;regs.h.dl = drive ;regs.x.si = (unsigned) dst ;intdos (®s, ®s) ;return(regs.x.cflag ? -1 : 0) ; }/*rootpath *//*** rootpath -- convert a pathname argument to root based cannonical form** rootpath determines the current directory, appends the path argument (which* may affect which disk the current directory is relative to), and qualifies* "." and ".." references. The result is a complete, simple, path name with* drive specifier.** If the relative path the user specifies does not include a drive spec., the* default drive will be used as the base. (The default drive will never be* changed.)** entry: relpath -- pointer to the pathname to be expanded* fullpath -- must point to a working buffer, see warning* exit: fullpath -- the full path which results* return: true if an error occurs, false otherwise** calls: curdir, getdrv** warning: fullpath must point to a working buffer large enough to hold the* longest possible relative path argument plus the longest possible* current directory path.**/ int rootpath(relpath, fullpath) char *relpath ; char *fullpath ; {int drivenum ;char tempchar, getdrv() ;register char *lead, *follow ;char *p1, *p2;/* extract drive spec */drivenum = getdrv() ;if ((*relpath != NULL) && (relpath[1] == COLON)) {drivenum = toupper(*relpath) - 'A' ;relpath += 2 ;}fullpath[0] = (char) ('A' + drivenum) ;fullpath[1] = COLON ;/* append relpath to fullpath/base */if (*relpath == PathChr) {/* relpath starts at base */strcpy(fullpath+2, relpath) ;}else {/* must get base path first */if (curdir(fullpath+2, drivenum+1))return(TRUE) ; /* terrible error */if ((*relpath != ASCNULL) && (strlen(fullpath) > 3))strcat(fullpath, "\\") ;strcat(fullpath, relpath) ;}/* convert path to cannonical form */lead = fullpath ;while(*lead != ASCNULL) {/* mark next path segment */follow = lead ;lead = (char *) strpbrk(follow+1, "\\") ;if (lead == NULL)lead = fullpath + strlen(fullpath) ;tempchar = *lead ;if (tempchar == PathChr)tempchar = BACKSLASH ; /* make breaks uniform */*lead = ASCNULL ;/* "." segment? */if (strcmp(follow+1, ".") == 0) {*lead = tempchar ;strcpy(follow, lead) ; /* remove "." segment */lead = follow ;}/* ".." segment? */else if (strcmp(follow+1, "..") == 0) {*lead = tempchar ;tempchar = *follow ;*follow = NULL ;p2 = fullpath - 1 ;while(*(p2=strbscan(p1=p2+1,"\\")) != NULL) ;/* p1 now points to the start of the previous element */*follow = tempchar ;if(p1 == fullpath)return(TRUE) ; /* tried to .. the root */follow = p1 - 1 ; /* follow points to path sep */strcpy(follow, lead) ; /* remove ".." segment */lead = follow ;}/* normal segment */else*lead = tempchar ;}if (strlen(fullpath) == 2) /* 'D:' or some such */strcat(fullpath, "\\") ;/* shift to upper case */strupr(fullpath) ;return(FALSE) ; }/* getdrv - return current drive as a character */char getdrv() {union REGS regs ;regs.h.ah = CURDISK ; /* Function 0x19 */intdos (®s, ®s) ;return(regs.h.al) ; } /*** MSDOS JOIN Utility Vers 4.0** This utility allows the splicing of a physical drive to a pathname* on another physical drive such that operations performed using the* pathname as an arguement take place on the physical drive.** MODIFICATION HISTORY** Converted to CMERGE 03/26/85 by Greg Tibbetts** M000 May 23/85 Barrys* Disallow splicing similar drives.** M001 May 24/85 Barrys* The original IBM version of JOIN allowed the delete splice switch* "/D" immediately after the drive specification. The argument parsing* code has been modified to allow this combination.** M002 June 5/85 Barrys* Changed low version check for specific 320.** M003 July 15/85 Barrys* Checked for any possible switch characters in the other operands.** M004 July 15/85 Barrys* Moved check for physical drive before check for NET and SHARED tests.** 33D0016 July 16/86 Rosemarie Gazzia* Put SHARED test on an equal basis with physical drive check.* Last fix (M004) erroneously allowed joining physical or local shared* drives. This is because it only performed the SHARED test if the drive* failed the physical test.*/#include "types.h" #include "versionc.h" #include "sysvar.h" #include "cds.h" #include <dos.h> #include <ctype.h>extern char NoMem[], ParmNum[], BadParm[], DirNEmp[], NetErr[], BadVer[] ; extern char *strchr(); /* M003 */struct sysVarsType SysVars ;/*** main - program entry point** Purpose:* To test arguements for validity and perform the splice** int main(int c, char *v[])** Args:* c - the number of command line arguements* v - pointer to pointers to the command line arguements** Links:* ERRTST.C - Drive and path validity testing functions* SYSVAR.C - Functions to get/set DOS System Variable structures* CDS.C - Functions to get/set DOS CDS structures** Returns:* Appropriate return code with error message to stdout if* necessary.**/main(c, v) int c ; char *v[] ; {char *strbscan() ;union REGS ir;register union REGS *iregs = &ir ; /* Used for DOS calls */struct findType findbuf ;char path [MAXPATHLEN],*p ;struct CDSType CDS ;int i ;int dstdrv; /* dest. drive number M000 */int delflag = FALSE; /* delete splice flag M001 */int arglen; /* length of argument M001 *//* check os version */iregs->h.ah = GETVERS ; /* Function 0x30 */intdos(iregs, iregs) ;if ( (iregs->h.al != expected_version_major) || (iregs->h.ah != expected_version_minor) )Fatal(BadVer);/* i = (iregs->h.al * 100) + iregs->h.ah; *//* if (i < LowVersion || i > HighVersion) *//* Fatal(BadVer) ; */SHIFT(c,v) ;for (i=0 ; i < c ; i++) /* Convert to upper case */strupr(v[i]) ;GetVars(&SysVars) ; /* Access to DOS data structures */if (c > 2) /* M001 */Fatal(ParmNum); /* M001 */if (c == 0)DoList() ; /* list splices */else {/* Process drive letter */i = **v - 'A' ;if ((*v)[1] != ':') {if (c == 1) {Fatal(ParmNum);}else {Fatal(BadParm) ;}}if (!fGetCDS(i, &CDS)) {Fatal(BadParm) ;}/* Accept arguments separate or mixed with drive spec M001 */arglen = strlen(*v);if (arglen != 2) {if ((*v)[2] != SwitChr) {Fatal(ParmNum);}if (arglen != 4) {if (c == 1) {Fatal(BadParm);}else {Fatal(ParmNum);}}/* Advance arg pointer to possible switches */(*v)++; (*v)++;}else {SHIFT(c,v) ;}/* Check for splice deletion switch */if (**v == SwitChr) {if ((*v)[1] == 'D')delflag = TRUE;else {Fatal(BadParm);}}if (delflag == TRUE) { /* Deassigning perhaps? */if (!TESTFLAG(CDS.flags, CDSSPLICE)) {Fatal(BadParm) ; /* If NOT spliced */}if (fPathErr(CDS.text)) {Fatal(BadParm) ; /* If prefix of curdir */}CDS.text[0] = i + 'A' ;CDS.text[1] = ':' ;CDS.text[2] = '\\' ;CDS.text[3] = 0 ;CDS.cbEnd = 2 ;if (i >= SysVars.cDrv)CDS.flags = FALSE ;elseCDS.flags = CDSINUSE ;GetVars(&SysVars) ;SysVars.fSplice-- ;PutVars(&SysVars) ;fPutCDS(i, &CDS) ;}else {/* Test if there are any other possible switches* in the operand M003*/if (strchr(v[0], SwitChr)) {Fatal(ParmNum);}if (TESTFLAG(CDS.flags,CDSSPLICE)) {Fatal(BadParm) ; /* If now spliced */}rootpath(*v, path) ; /* Get root path */strupr(path) ; /* Upper case *//* M004 Start */if (i == getdrv() || /* Can't mov curdrv */fPathErr(path) || /* or curdir prefix */*strbscan(path+3, "/\\") != 0 ||!fPhysical(i) ||fShared(i)) { /* 33D0016 RG *//* Determine if it was a NET error */if (fNet(i) || fShared(i)) {Fatal(NetErr) ;}Fatal(BadParm) ;}if (fNet(path[0] - 'A') || fNet(path[0] - 'A')) {Fatal(NetErr) ; /* Same for dest */}/* M004 End *//* Check src and dst drives are not same */dstdrv = *path - 'A'; /* M000 */if (i == dstdrv) /* M000 */Fatal (BadParm); /* M000 */if (mkdir(path) == -1) { /* If can't mkdir *//* or if no dir or *//* if node is file */if (ffirst(path, A_D, &findbuf) == -1 ||!TESTFLAG(findbuf.attr,A_D))Fatal(BadParm) ;p = path + strlen(path) ;strcat(p, "\\*.*") ;if (ffirst(path, 0, &findbuf) != -1)Fatal(DirNEmp) ; /* if dir *//* not empty */*p = 0 ;} ;strcpy(CDS.text, path) ;CDS.flags = CDSINUSE | CDSSPLICE ;fPutCDS(i, &CDS) ;GetVars(&SysVars) ;SysVars.fSplice++ ;PutVars(&SysVars) ;} ;}exit(0) ; }DoList() {int i ;struct CDSType CDS ;for (i=0 ; fGetCDS(i, &CDS) ; i++) {if (TESTFLAG(CDS.flags,CDSSPLICE))printf("%c: => %s\n", i+'A', CDS.text) ;} ; } #include "internat.h" #include <dos.h> #define NULL 0 #define TRUE 0xffff #define FALSE 0 #define KANJI TRUE char haveinttab = FALSE; /** ECS Support - This module provides support for international >7FH and * TWO-BYTE character sets. The toupper routine uses the DOS MAP_CASE call.* In addition, STRING.C contains a default_tab containing a default lead* byte table for two byte character sets. If single byte operation is* desired, modify this table as follows: ="\000". If this utility * is run on a DOS with Function 63H support, the default table will * be replaced by the table in the DOS. The lbtbl_ptr is the far ptr to* which ever table is in use. */ long lbtbl_ptr; char *default_tab="\201\237\340\374\000\000"; char have_lbtbl = FALSE;struct InterTbl Currtab;int toupper(c) int c; {union REGS regs ;if(!haveinttab) {regs.x.ax = 0x3800 ;regs.x.dx = (unsigned) &Currtab ;intdos (®s, ®s) ; /* INIT the table */haveinttab = TRUE;}return(IToupper(c,Currtab.casecall));}char *strupr(string) char *string; {register char *p1;p1 = string;while (*p1 != NULL) {/** A note about the following " & 0xFF" stuff. This is* to prevent the damn C compiler from converting bytes* to words with the CBW instruction which is NOT correct* for routines like toupper*/ #ifdef KANJIif(testkanj(*p1 & 0xFF))p1 += 2 ;else*p1++ = toupper(*p1 & 0xFF); #else*p1++ = toupper(*p1 & 0xFF); #endif}return(string); }char *strpbrk(string1,string2) char *string1; char *string2; {register char *p1;while (*string1 != NULL) {/** A note about the following " & 0xFF" stuff. This is* to prevent the damn C compiler from converting bytes* to words with the CBW instruction which is NOT correct* for routines like toupper*/ #ifdef KANJIif(testkanj(*string1 & 0xFF))string1 += 2 ;else { #endifp1 = string2;while (*p1 != NULL) {if(*p1++ == *string1)return(string1);}string1++; #ifdef KANJI} #endif}return(NULL); /* no matches found */ }#ifdef KANJI testkanj(c) unsigned char c; {long *p1;union REGS regs ;int i;p1 = &lbtbl_ptr ; if (!have_lbtbl ) {lbtbl_ptr = default_tab ; /* Load offset in pointer */get_lbtbl( p1 );have_lbtbl=TRUE;}if ( test_ecs( c, lbtbl_ptr )) return(TRUE);elsereturn(FALSE); } #endif #include "types.h" #include "internat.h" #include <dos.h>/* #define KANJI TRUE */char haveinttab = FALSE;struct InterTbl Currtab;int toupper(c) int c; {union REGS regs ;if(!haveinttab) {regs.x.ax = 0x3800 ;regs.x.dx = (unsigned) &Currtab ;intdos (®s, ®s) ; /* INIT the table */haveinttab = TRUE;}return(IToupper(c,Currtab.casecall));}char *strupr(string) char *string; {register char *p1;p1 = string;while (*p1 != NULL) {/** A note about the following " & 0xFF" stuff. This is* to prevent the damn C compiler from converting bytes* to words with the CBW instruction which is NOT correct* for routines like toupper*/ #ifdef KANJIif(testkanj(*p1 & 0xFF))p1 += 2 ;else*p1++ = toupper(*p1 & 0xFF); #else*p1++ = toupper(*p1 & 0xFF); #endif}return(string); }char *strpbrk(string1,string2) char *string1; char *string2; {register char *p1;while (*string1 != NULL) {/** A note about the following " & 0xFF" stuff. This is* to prevent the damn C compiler from converting bytes* to words with the CBW instruction which is NOT correct* for routines like toupper*/ #ifdef KANJIif(testkanj(*string1 & 0xFF))string1 += 2 ;else { #endifp1 = string2;while (*p1 != NULL) {if(*p1++ == *string1)return(string1);}string1++; #ifdef KANJI} #endif}return(NULL); /* no matches found */ }#ifdef KANJI testkanj(c) unsigned char c; {if((c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xFC))return(TRUE);elsereturn(FALSE); } #endif /* return the system variables in sysVars */#include "types.h" #include "sysvar.h" #include <dos.h>GetVars(pSVars) struct sysVarsType *pSVars ; {struct sysVarsType far *vptr ;int i ;union REGS ir ;register union REGS *iregs = &ir ; /* Used for DOS calls */struct SREGS syssegs ;iregs->h.ah = GETVARS ; /* Function 0x52 */intdosx(iregs, iregs, &syssegs) ;*(long *)(&vptr) = (((long)syssegs.es) << 16)+(iregs->x.bx & 0xffffL) ;for (i=0 ; i <= sizeof(*pSVars) ; i++)*((char *)pSVars+i) = *((char far *)vptr+i) ;}PutVars(pSVars) struct sysVarsType *pSVars ; {struct sysVarsType far *vptr ;int i ;union REGS ir ;register union REGS *iregs = &ir ; /* Used for DOS calls */struct SREGS syssegs ;iregs->h.ah = GETVARS ; /* Function 0x52 */intdosx(iregs, iregs, &syssegs) ;*(long *)(&vptr) = (((long)syssegs.es) << 16)+(iregs->x.bx & 0xffffL) ;for (i=0 ; i <= sizeof(*pSVars) ; i++)*((char far *)vptr+i) = *((char *)pSVars+i) ;}我分享的內容就到這里,若還想要Windows源代碼中的其他文件請加微信wangtaohan0714
總結
以上是生活随笔為你收集整理的Windows系统C语言代码一览的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: K210识别数字(0~9)并与单片机通信
- 下一篇: C语言学习笔记:switch语句、循环语