unixutil.pp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  41. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  42. // Note: for internal use by skilled programmers only
  43. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  44. var p : ppchar;
  45. i : LongInt;
  46. begin
  47. if High(s)<Low(s) Then Exit(NIL);
  48. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  49. // for cmd
  50. if p=nil then
  51. begin
  52. {$ifdef xunix}
  53. fpseterrno(ESysEnomem);
  54. {$endif}
  55. exit(NIL);
  56. end;
  57. for i:=low(s) to high(s) do
  58. p[i+Reserveentries]:=pchar(s[i]);
  59. p[high(s)+1+Reserveentries]:=nil;
  60. ArrayStringToPPchar:=p;
  61. end;
  62. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  63. {
  64. Create a PPChar to structure of pchars which are the arguments specified
  65. in the string S. Especially useful for creating an ArgV for Exec-calls
  66. }
  67. begin
  68. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  69. end;
  70. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  71. var
  72. i,nr : longint;
  73. Buf : ^char;
  74. p : ppchar;
  75. begin
  76. buf:=s;
  77. nr:=1;
  78. while (buf^<>#0) do // count nr of args
  79. begin
  80. while (buf^ in [' ',#9,#10]) do // Kill separators.
  81. inc(buf);
  82. inc(nr);
  83. if buf^='"' Then // quotes argument?
  84. begin
  85. inc(buf);
  86. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  87. inc(buf);
  88. if buf^='"' then // skip closing quote.
  89. inc(buf);
  90. end
  91. else
  92. begin // else std
  93. while not (buf^ in [' ',#0,#9,#10]) do
  94. inc(buf);
  95. end;
  96. end;
  97. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  98. StringToPPChar:=p;
  99. if p=nil then
  100. begin
  101. {$ifdef xunix}
  102. fpseterrno(ESysEnomem);
  103. {$endif}
  104. exit;
  105. end;
  106. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  107. buf:=s;
  108. while (buf^<>#0) do
  109. begin
  110. while (buf^ in [' ',#9,#10]) do // Kill separators.
  111. begin
  112. buf^:=#0;
  113. inc(buf);
  114. end;
  115. if buf^='"' Then // quotes argument?
  116. begin
  117. inc(buf);
  118. p^:=buf;
  119. inc(p);
  120. p^:=nil;
  121. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  122. inc(buf);
  123. if buf^='"' then // skip closing quote.
  124. begin
  125. buf^:=#0;
  126. inc(buf);
  127. end;
  128. end
  129. else
  130. begin
  131. p^:=buf;
  132. inc(p);
  133. p^:=nil;
  134. while not (buf^ in [' ',#0,#9,#10]) do
  135. inc(buf);
  136. end;
  137. end;
  138. end;
  139. Function GetFS (var T:Text):longint;
  140. {
  141. Get File Descriptor of a text file.
  142. }
  143. begin
  144. if textrec(t).mode=fmclosed then
  145. exit(-1)
  146. else
  147. GETFS:=textrec(t).Handle
  148. end;
  149. Function GetFS(Var F:File):longint;
  150. {
  151. Get File Descriptor of an unTyped file.
  152. }
  153. begin
  154. { Handle and mode are on the same place in textrec and filerec. }
  155. if filerec(f).mode=fmclosed then
  156. exit(-1)
  157. else
  158. GETFS:=filerec(f).Handle
  159. end;
  160. Const
  161. {Date Translation}
  162. C1970=2440588;
  163. D0 = 1461;
  164. D1 = 146097;
  165. D2 =1721119;
  166. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  167. Var
  168. YYear,XYear,Temp,TempMonth : LongInt;
  169. Begin
  170. Temp:=((JulianDN-D2) shl 2)-1;
  171. JulianDN:=Temp Div D1;
  172. XYear:=(Temp Mod D1) or 3;
  173. YYear:=(XYear Div D0);
  174. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  175. Day:=((Temp Mod 153)+5) Div 5;
  176. TempMonth:=Temp Div 153;
  177. If TempMonth>=10 Then
  178. Begin
  179. inc(YYear);
  180. dec(TempMonth,12);
  181. End;
  182. inc(TempMonth,3);
  183. Month := TempMonth;
  184. Year:=YYear+(JulianDN*100);
  185. end;
  186. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  187. {
  188. Transforms Epoch time into local time (hour, minute,seconds)
  189. }
  190. Var
  191. DateNum: LongInt;
  192. Begin
  193. inc(Epoch,TZSeconds);
  194. Datenum:=(Epoch Div 86400) + c1970;
  195. JulianToGregorian(DateNum,Year,Month,day);
  196. Epoch:=Abs(Epoch Mod 86400);
  197. Hour:=Epoch Div 3600;
  198. Epoch:=Epoch Mod 3600;
  199. Minute:=Epoch Div 60;
  200. Second:=Epoch Mod 60;
  201. End;
  202. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  203. {
  204. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  205. (seconds since 00:00, january 1 1970, corrected for local time zone)
  206. }
  207. Begin
  208. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  209. (LongInt(Hour)*3600)+(Longint(Minute)*60)+Second-TZSeconds;
  210. End;
  211. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  212. Var
  213. Century,XYear: LongInt;
  214. Begin
  215. If Month<=2 Then
  216. Begin
  217. Dec(Year);
  218. Inc(Month,12);
  219. End;
  220. Dec(Month,3);
  221. Century:=(longint(Year Div 100)*D1) shr 2;
  222. XYear:=(longint(Year Mod 100)*D0) shr 2;
  223. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  224. End;
  225. end.