unixutil.pp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2013 by the Free Pascal development team
  4. DO NOT ADD ROUTINES TO THIS FILE!
  5. THE ROUTINES IN THIS FILE ARE INTERNAL AND NOT FOR END USER USAGE!
  6. Background: This unit contains leftovers from the unix restructure that
  7. shouldn't be in the interface of unit baseunix/unix, but are needed
  8. in these units. (at the time routines were still being moved
  9. from baseunix to unix, and unit baseunix couldn't depend on unix)
  10. The routines are fairly OS independent but can't move to
  11. OS independent because the lowlevel units baseunix/unix depend
  12. on them. If they need to be generally accessable, copy these
  13. functions to a general purpose, OS independent, supportable unit.
  14. See the file COPYING.FPC, included in this distribution,
  15. for details about the copyright.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. **********************************************************************}
  20. unit unixutil;
  21. interface
  22. var
  23. Tzseconds : Longint;
  24. Type
  25. ComStr = String[255] deprecated 'Clean up shortstring use, or use same type from unit dos.';
  26. PathStr = String[255] deprecated 'Clean up shortstring use, or use same type from unit dos.';
  27. DirStr = String[255] deprecated 'Clean up shortstring use, or use same type from unit dos.';
  28. NameStr = String[255] deprecated 'Clean up shortstring use, or use same type from unit dos.';
  29. ExtStr = String[255] deprecated 'Clean up shortstring use, or use same type from unit dos.';
  30. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  31. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  32. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  33. Function GetFS(var T:Text):longint; deprecated;
  34. Function GetFS(Var F:File):longint; deprecated; // use sysutils.getfilehandle
  35. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  36. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  37. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  38. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  39. implementation
  40. {$I textrec.inc}
  41. {$i filerec.inc}
  42. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  43. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  44. // Note: for internal use by skilled programmers only
  45. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  46. var p : ppchar;
  47. i : LongInt;
  48. begin
  49. if High(s)<Low(s) Then Exit(NIL);
  50. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  51. // for cmd
  52. if p=nil then
  53. begin
  54. {$ifdef xunix}
  55. fpseterrno(ESysEnomem);
  56. {$endif}
  57. exit(NIL);
  58. end;
  59. for i:=low(s) to high(s) do
  60. p[i+Reserveentries]:=pchar(s[i]);
  61. p[high(s)+1+Reserveentries]:=nil;
  62. ArrayStringToPPchar:=p;
  63. end;
  64. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  65. {
  66. Create a PPChar to structure of pchars which are the arguments specified
  67. in the string S. Especially useful for creating an ArgV for Exec-calls
  68. }
  69. begin
  70. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  71. end;
  72. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  73. var
  74. i,nr : longint;
  75. Buf : ^char;
  76. p : ppchar;
  77. begin
  78. buf:=s;
  79. nr:=1;
  80. while (buf^<>#0) do // count nr of args
  81. begin
  82. while (buf^ in [' ',#9,#10]) do // Kill separators.
  83. inc(buf);
  84. inc(nr);
  85. if buf^='"' Then // quotes argument?
  86. begin
  87. inc(buf);
  88. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  89. inc(buf);
  90. if buf^='"' then // skip closing quote.
  91. inc(buf);
  92. end
  93. else
  94. begin // else std
  95. while not (buf^ in [' ',#0,#9,#10]) do
  96. inc(buf);
  97. end;
  98. end;
  99. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  100. StringToPPChar:=p;
  101. if p=nil then
  102. begin
  103. {$ifdef xunix}
  104. fpseterrno(ESysEnomem);
  105. {$endif}
  106. exit;
  107. end;
  108. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  109. buf:=s;
  110. while (buf^<>#0) do
  111. begin
  112. while (buf^ in [' ',#9,#10]) do // Kill separators.
  113. begin
  114. buf^:=#0;
  115. inc(buf);
  116. end;
  117. if buf^='"' Then // quotes argument?
  118. begin
  119. inc(buf);
  120. p^:=buf;
  121. inc(p);
  122. p^:=nil;
  123. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  124. inc(buf);
  125. if buf^='"' then // skip closing quote.
  126. begin
  127. buf^:=#0;
  128. inc(buf);
  129. end;
  130. end
  131. else
  132. begin
  133. p^:=buf;
  134. inc(p);
  135. p^:=nil;
  136. while not (buf^ in [' ',#0,#9,#10]) do
  137. inc(buf);
  138. end;
  139. end;
  140. end;
  141. Function GetFS (var T:Text):longint;
  142. {
  143. Get File Descriptor of a text file.
  144. }
  145. begin
  146. if textrec(t).mode=fmclosed then
  147. exit(-1)
  148. else
  149. GETFS:=textrec(t).Handle
  150. end;
  151. Function GetFS(Var F:File):longint;
  152. {
  153. Get File Descriptor of an unTyped file.
  154. }
  155. begin
  156. { Handle and mode are on the same place in textrec and filerec. }
  157. if filerec(f).mode=fmclosed then
  158. exit(-1)
  159. else
  160. GETFS:=filerec(f).Handle
  161. end;
  162. Const
  163. {Date Translation}
  164. C1970=2440588;
  165. D0 = 1461;
  166. D1 = 146097;
  167. D2 =1721119;
  168. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  169. Var
  170. YYear,XYear,Temp,TempMonth : LongInt;
  171. Begin
  172. Temp:=((JulianDN-D2) shl 2)-1;
  173. JulianDN:=Temp Div D1;
  174. XYear:=(Temp Mod D1) or 3;
  175. YYear:=(XYear Div D0);
  176. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  177. Day:=((Temp Mod 153)+5) Div 5;
  178. TempMonth:=Temp Div 153;
  179. If TempMonth>=10 Then
  180. Begin
  181. inc(YYear);
  182. dec(TempMonth,12);
  183. End;
  184. inc(TempMonth,3);
  185. Month := TempMonth;
  186. Year:=YYear+(JulianDN*100);
  187. end;
  188. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  189. {
  190. Transforms Epoch time into local time (hour, minute,seconds)
  191. }
  192. Var
  193. DateNum: LongInt;
  194. Begin
  195. inc(Epoch,TZSeconds);
  196. Datenum:=(Epoch Div 86400) + c1970;
  197. JulianToGregorian(DateNum,Year,Month,day);
  198. Epoch:=Abs(Epoch Mod 86400);
  199. Hour:=Epoch Div 3600;
  200. Epoch:=Epoch Mod 3600;
  201. Minute:=Epoch Div 60;
  202. Second:=Epoch Mod 60;
  203. End;
  204. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  205. {
  206. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  207. (seconds since 00:00, january 1 1970, corrected for local time zone)
  208. }
  209. Begin
  210. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  211. (LongInt(Hour)*3600)+(Longint(Minute)*60)+Second-TZSeconds;
  212. End;
  213. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  214. Var
  215. Century,XYear: LongInt;
  216. Begin
  217. If Month<=2 Then
  218. Begin
  219. Dec(Year);
  220. Inc(Month,12);
  221. End;
  222. Dec(Month,3);
  223. Century:=(longint(Year Div 100)*D1) shr 2;
  224. XYear:=(longint(Year Mod 100)*D0) shr 2;
  225. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  226. End;
  227. end.