home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 13.ddi / RTLDOS.ZIP / DOS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-28  |  15.7 KB  |  439 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Turbo Pascal Runtime Library                    }
  5. {       DOS Interface Unit                              }
  6. {                                                       }
  7. {       Copyright (C) 1988,92 Borland International     }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit Dos;
  12.  
  13. {$I-,O+,S-}
  14.  
  15. interface
  16.  
  17. const
  18.  
  19. { Flags bit masks }
  20.  
  21.   FCarry     = $0001;
  22.   FParity    = $0004;
  23.   FAuxiliary = $0010;
  24.   FZero      = $0040;
  25.   FSign      = $0080;
  26.   FOverflow  = $0800;
  27.  
  28. { File mode magic numbers }
  29.  
  30.   fmClosed = $D7B0;
  31.   fmInput  = $D7B1;
  32.   fmOutput = $D7B2;
  33.   fmInOut  = $D7B3;
  34.  
  35. { File attribute constants }
  36.  
  37.   ReadOnly  = $01;
  38.   Hidden    = $02;
  39.   SysFile   = $04;
  40.   VolumeID  = $08;
  41.   Directory = $10;
  42.   Archive   = $20;
  43.   AnyFile   = $3F;
  44.  
  45. type
  46.  
  47. { String types }
  48.  
  49.   ComStr  = string[127];        { Command line string }
  50.   PathStr = string[79];         { File pathname string }
  51.   DirStr  = string[67];         { Drive and directory string }
  52.   NameStr = string[8];          { File name string }
  53.   ExtStr  = string[4];          { File extension string }
  54.  
  55. { Registers record used by Intr and MsDos }
  56.  
  57.   Registers = record
  58.                 case Integer of
  59.                   0: (AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags: Word);
  60.                   1: (AL,AH,BL,BH,CL,CH,DL,DH: Byte);
  61.               end;
  62.  
  63. { Typed-file and untyped-file record }
  64.  
  65.   FileRec = record
  66.               Handle: Word;
  67.               Mode: Word;
  68.               RecSize: Word;
  69.               Private: array[1..26] of Byte;
  70.               UserData: array[1..16] of Byte;
  71.               Name: array[0..79] of Char;
  72.             end;
  73.  
  74. { Textfile record }
  75.  
  76.   TextBuf = array[0..127] of Char;
  77.   TextRec = record
  78.               Handle: Word;
  79.               Mode: Word;
  80.               BufSize: Word;
  81.               Private: Word;
  82.               BufPos: Word;
  83.               BufEnd: Word;
  84.               BufPtr: ^TextBuf;
  85.               OpenFunc: Pointer;
  86.               InOutFunc: Pointer;
  87.               FlushFunc: Pointer;
  88.               CloseFunc: Pointer;
  89.               UserData: array[1..16] of Byte;
  90.               Name: array[0..79] of Char;
  91.               Buffer: TextBuf;
  92.             end;
  93.  
  94. { Search record used by FindFirst and FindNext }
  95.  
  96.   SearchRec = record
  97.                 Fill: array[1..21] of Byte;
  98.                 Attr: Byte;
  99.                 Time: Longint;
  100.                 Size: Longint;
  101.                 Name: string[12];
  102.               end;
  103.  
  104. { Date and time record used by PackTime and UnpackTime }
  105.  
  106.   DateTime = record
  107.                Year,Month,Day,Hour,Min,Sec: Word;
  108.              end;
  109.  
  110. var
  111.  
  112. { Error status variable }
  113.  
  114.   DosError: Integer;
  115.  
  116. { DosVersion returns the DOS version number. The low byte of    }
  117. { the result is the major version number, and the high byte is  }
  118. { the minor version number. For example, DOS 3.20 returns 3 in  }
  119. { the low byte, and 20 in the high byte.                        }
  120.  
  121. function DosVersion: Word;
  122.  
  123. { Intr executes a specified software interrupt with a specified }
  124. { Registers package.                                            }
  125.  
  126. procedure Intr(IntNo: Byte; var Regs: Registers);
  127.  
  128. { MsDos invokes the DOS function call handler with a specified  }
  129. { Registers package.                                            }
  130.  
  131. procedure MsDos(var Regs: Registers);
  132.  
  133. { GetDate returns the current date set in the operating system. }
  134. { Ranges of the values returned are: Year 1980-2099, Month      }
  135. { 1-12, Day 1-31 and DayOfWeek 0-6 (0 corresponds to Sunday).   }
  136.  
  137. procedure GetDate(var Year,Month,Day,DayOfWeek: Word);
  138.  
  139. { SetDate sets the current date in the operating system. Valid  }
  140. { parameter ranges are: Year 1980-2099, Month 1-12 and Day      }
  141. { 1-31. If the date is not valid, the function call is ignored. }
  142.  
  143. procedure SetDate(Year,Month,Day: Word);
  144.  
  145. { GetTime returns the current time set in the operating system. }
  146. { Ranges of the values returned are: Hour 0-23, Minute 0-59,    }
  147. { Second 0-59 and Sec100 (hundredths of seconds) 0-99.          }
  148.  
  149. procedure GetTime(var Hour,Minute,Second,Sec100: Word);
  150.  
  151. { SetTime sets the time in the operating system. Valid          }
  152. { parameter ranges are: Hour 0-23, Minute 0-59, Second 0-59 and }
  153. { Sec100 (hundredths of seconds) 0-99. If the time is not       }
  154. { valid, the function call is ignored.                          }
  155.  
  156. procedure SetTime(Hour,Minute,Second,Sec100: Word);
  157.  
  158. { GetCBreak returns the state of Ctrl-Break checking in DOS.    }
  159. { When off (False), DOS only checks for Ctrl-Break during I/O   }
  160. { to console, printer, or communication devices. When on        }
  161. { (True), checks are made at every system call.                 }
  162.  
  163. procedure GetCBreak(var Break: Boolean);
  164.  
  165. { SetCBreak sets the state of Ctrl-Break checking in DOS.       }
  166.  
  167. procedure SetCBreak(Break: Boolean);
  168.  
  169. { GetVerify returns the state of the verify flag in DOS. When   }
  170. { off (False), disk writes are not verified. When on (True),    }
  171. { all disk writes are verified to insure proper writing.        }
  172.  
  173. procedure GetVerify(var Verify: Boolean);
  174.  
  175. { SetVerify sets the state of the verify flag in DOS.           }
  176.  
  177. procedure SetVerify(Verify: Boolean);
  178.  
  179. { DiskFree returns the number of free bytes on the specified    }
  180. { drive number (0=Default,1=A,2=B,..). DiskFree returns -1 if   }
  181. { the drive number is invalid.                                  }
  182.  
  183. function DiskFree(Drive: Byte): Longint;
  184.  
  185. { DiskSize returns the size in bytes of the specified drive     }
  186. { number (0=Default,1=A,2=B,..). DiskSize returns -1 if the     }
  187. { drive number is invalid.                                      }
  188.  
  189. function DiskSize(Drive: Byte): Longint;
  190.  
  191. { GetFAttr returns the attributes of a file. F must be a file   }
  192. { variable (typed, untyped or textfile) which has been assigned }
  193. { a name. The attributes are examined by ANDing with the        }
  194. { attribute masks defined as constants above. Errors are        }
  195. { reported in DosError.                                         }
  196.  
  197. procedure GetFAttr(var F; var Attr: Word);
  198.  
  199. { SetFAttr sets the attributes of a file. F must be a file      }
  200. { variable (typed, untyped or textfile) which has been assigned }
  201. { a name. The attribute value is formed by adding (or ORing)    }
  202. { the appropriate attribute masks defined as constants above.   }
  203. { Errors are reported in DosError.                              }
  204.  
  205. procedure SetFAttr(var F; Attr: Word);
  206.  
  207. { GetFTime returns the date and time a file was last written.   }
  208. { F must be a file variable (typed, untyped or textfile) which  }
  209. { has been assigned and opened. The Time parameter may be       }
  210. { unpacked throgh a call to UnpackTime. Errors are reported in  }
  211. { DosError.                                                     }
  212.  
  213. procedure GetFTime(var F; var Time: Longint);
  214.  
  215. { SetFTime sets the date and time a file was last written.      }
  216. { F must be a file variable (typed, untyped or textfile) which  }
  217. { has been assigned and opened. The Time parameter may be       }
  218. { created through a call to PackTime. Errors are reported in    }
  219. { DosError.                                                     }
  220.  
  221. procedure SetFTime(var F; Time: Longint);
  222.  
  223. { FindFirst searches the specified (or current) directory for   }
  224. { the first entry that matches the specified filename and       }
  225. { attributes. The result is returned in the specified search    }
  226. { record. Errors (and no files found) are reported in DosError. }
  227.  
  228. procedure FindFirst(Path: PathStr; Attr: Word; var F: SearchRec);
  229.  
  230. { FindNext returs the next entry that matches the name and      }
  231. { attributes specified in a previous call to FindFirst. The     }
  232. { search record must be one passed to FindFirst. Errors (and no }
  233. { more files) are reported in DosError.                         }
  234.  
  235. procedure FindNext(var F: SearchRec);
  236.  
  237. { UnpackTime converts a 4-byte packed date/time returned by     }
  238. { FindFirst, FindNext or GetFTime into a DateTime record.       }
  239.  
  240. procedure UnpackTime(P: Longint; var T: DateTime);
  241.  
  242. { PackTime converts a DateTime record into a 4-byte packed      }
  243. { date/time used by SetFTime.                                   }
  244.  
  245. procedure PackTime(var T: DateTime; var P: Longint);
  246.  
  247. { GetIntVec returns the address stored in the specified         }
  248. { interrupt vector.                                             }
  249.  
  250. procedure GetIntVec(IntNo: Byte; var Vector: Pointer);
  251.  
  252. { SetIntVec sets the address in the interrupt vector table for  }
  253. { the specified interrupt.                                      }
  254.  
  255. procedure SetIntVec(IntNo: Byte; Vector: Pointer);
  256.  
  257. { FSearch searches for the file given by Path in the list of    }
  258. { directories given by DirList. The directory paths in DirList  }
  259. { must be separated by semicolons. The search always starts     }
  260. { with the current directory of the current drive. The returned }
  261. { value is a concatenation of one of the directory paths and    }
  262. { the file name, or an empty string if the file could not be    }
  263. { located.                                                      }
  264.  
  265. function FSearch(Path: PathStr; DirList: String): PathStr;
  266.  
  267. { FExpand expands the file name in Path into a fully qualified  }
  268. { file name. The resulting name consists of a drive letter, a   }
  269. { colon, a root relative directory path, and a file name.       }
  270. { Embedded '.' and '..' directory references are removed.       }
  271.  
  272. function FExpand(Path: PathStr): PathStr;
  273.  
  274. { FSplit splits the file name specified by Path into its three  }
  275. { components. Dir is set to the drive and directory path with   }
  276. { any leading and trailing backslashes, Name is set to the file }
  277. { name, and Ext is set to the extension with a preceding dot.   }
  278. { Each of the component strings may possibly be empty, if Path  }
  279. { contains no such component.                                   }
  280.  
  281. procedure FSplit(Path: PathStr; var Dir: DirStr;
  282.   var Name: NameStr; var Ext: ExtStr);
  283.  
  284. { EnvCount returns the number of strings contained in the DOS   }
  285. { environment.                                                  }
  286.  
  287. function EnvCount: Integer;
  288.  
  289. { EnvStr returns a specified environment string. The returned   }
  290. { string is of the form "VAR=VALUE". The index of the first     }
  291. { string is one. If Index is less than one or greater than      }
  292. { EnvCount, EnvStr returns an empty string.                     }
  293.  
  294. function EnvStr(Index: Integer): String;
  295.  
  296. { GetEnv returns the value of a specified environment variable. }
  297. { The variable name can be in upper or lower case, but it must  }
  298. { not include the '=' character. If the specified environment   }
  299. { variable does not exist, GetEnv returns an empty string.      }
  300.  
  301. function GetEnv(EnvVar: String): String;
  302.  
  303. { SwapVectors swaps the contents of the SaveIntXX pointers in   }
  304. { the System unit with the current contents of the interrupt    }
  305. { vectors. SwapVectors is typically called just before and just }
  306. { after a call to Exec. This insures that the Exec'd process    }
  307. { does not use any interrupt handlers installed by the current  }
  308. { process, and vice versa.                                      }
  309.  
  310. procedure SwapVectors;
  311.  
  312. { Keep (or Terminate Stay Resident) terminates the program and  }
  313. { makes it stay in memory. The entire program stays in memory,  }
  314. { including data segment, stack segment, and heap. The ExitCode }
  315. { corresponds to the one passed to the Halt standard procedure. }
  316.  
  317. procedure Keep(ExitCode: Word);
  318.  
  319. { Exec executes another program. The program is specified by    }
  320. { the Path parameter, and the command line is specified by the  }
  321. { CmdLine parameter. To execute a DOS internal command, run     }
  322. { COMMAND.COM, e.g. "Exec('\COMMAND.COM','/C DIR *.PAS');".     }
  323. { Note the /C in front of the command. Errors are reported in   }
  324. { DosError. When compiling a program that uses Exec, be sure    }
  325. { to specify a maximum heap size as there will otherwise not be }
  326. { enough memory.                                                }
  327.  
  328. procedure Exec(Path: PathStr; ComLine: ComStr);
  329.  
  330. { DosExitCode returns the exit code of a sub-process. The low   }
  331. { byte is the code sent by the terminating process. The high    }
  332. { byte is zero for normal termination, 1 if terminated by       }
  333. { Ctrl-C, 2 if terminated due to a device error, or 3 if        }
  334. { terminated by the Keep procedure (function call 31 hex).      }
  335.  
  336. function DosExitCode: Word;
  337.  
  338. implementation
  339.  
  340. {$L VERS.OBJ}           { DOS version routine }
  341. {$L TIME.OBJ}           { Date and time routines }
  342. {$L CBRK.OBJ}           { Ctrl-Break flag handling }
  343. {$L VERF.OBJ}           { Verify flag handling }
  344. {$L DISK.OBJ}           { Disk status routines }
  345. {$L FATR.OBJ}           { File attribute routines }
  346. {$L FTIM.OBJ}           { File date and time routines }
  347. {$L FIND.OBJ}           { Directory search routines }
  348. {$L PTIM.OBJ}           { Time pack and unpack routines }
  349. {$L VECT.OBJ}           { Interrupt vector handling }
  350. {$L SRCH.OBJ}           { File search routine }
  351. {$L EXPN.OBJ}           { File name expansion routine }
  352. {$L SPLT.OBJ}           { File name split routine }
  353. {$L ENVS.OBJ}           { Environment string routines }
  354. {$L ENVV.OBJ}           { Environment variable routine }
  355. {$L KEEP.OBJ}           { TSR support routine }
  356. {$L EXEC.OBJ}           { Program execution routines }
  357.  
  358. {$IFDEF DPMI}
  359.  
  360. {$L INTR.OBP}           { Software interrupt routines }
  361. {$L SWAP.OBP}           { Interrupt vector swapping }
  362.  
  363. {$ELSE}
  364.  
  365. {$L INTR.OBJ}           { Software interrupt routines }
  366. {$L SWAP.OBJ}           { Interrupt vector swapping }
  367.  
  368. {$ENDIF}
  369.  
  370. function DosVersion: Word; external {VERS};
  371.  
  372. procedure Intr(IntNo: Byte; var Regs: Registers); external {INTR};
  373.  
  374. procedure MsDos(var Regs: Registers); external {INTR};
  375.  
  376. procedure GetDate(var Year,Month,Day,DayOfWeek: Word); external {TIME};
  377.  
  378. procedure SetDate(Year,Month,Day: Word); external {TIME};
  379.  
  380. procedure GetTime(var Hour,Minute,Second,Sec100: Word); external {TIME};
  381.  
  382. procedure SetTime(Hour,Minute,Second,Sec100: Word); external {TIME};
  383.  
  384. procedure GetCBreak(var Break: Boolean); external {CBRK};
  385.  
  386. procedure SetCBreak(Break: Boolean); external {CBRK};
  387.  
  388. procedure GetVerify(var Verify: Boolean); external {VERF};
  389.  
  390. procedure SetVerify(Verify: Boolean); external {VERF};
  391.  
  392. function DiskFree(Drive: Byte): Longint; external {DISK};
  393.  
  394. function DiskSize(Drive: Byte): Longint; external {DISK};
  395.  
  396. procedure GetFAttr(var F; var Attr: Word); external {FATR};
  397.  
  398. procedure SetFAttr(var F; Attr: Word); external {FATR};
  399.  
  400. procedure GetFTime(var F; var Time: Longint); external {FTIM};
  401.  
  402. procedure SetFTime(var F; Time: Longint); external {FTIM};
  403.  
  404. procedure FindFirst(Path: PathStr; Attr: Word; var F: SearchRec);
  405.   external {FIND};
  406.  
  407. procedure FindNext(var F: SearchRec); external {FIND};
  408.  
  409. procedure UnpackTime(P: Longint; var T: DateTime); external {PTIM};
  410.  
  411. procedure PackTime(var T: DateTime; var P: Longint); external {PTIM};
  412.  
  413. procedure GetIntVec(IntNo: Byte; var Vector: Pointer); external {VECT};
  414.  
  415. procedure SetIntVec(IntNo: Byte; Vector: Pointer); external {VECT};
  416.  
  417. function FSearch(Path: PathStr; DirList: String): PathStr; external {SRCH};
  418.  
  419. function FExpand(Path: PathStr): PathStr; external {EXPN};
  420.  
  421. procedure FSplit(Path: PathStr; var Dir: DirStr;
  422.   var Name: NameStr; var Ext: ExtStr); external {SPLT};
  423.  
  424. function EnvCount: Integer; external {ENVS};
  425.  
  426. function EnvStr(Index: Integer): String; external {ENVS};
  427.  
  428. function GetEnv(EnvVar: String): String; external {ENVV};
  429.  
  430. procedure SwapVectors; external {SWAP};
  431.  
  432. procedure Keep(ExitCode: Word); external {KEEP};
  433.  
  434. procedure Exec(Path: PathStr; ComLine: ComStr); external {EXEC};
  435.  
  436. function DosExitCode: Word; external {EXEC};
  437.  
  438. end.
  439.