File: os.adb

package info (click to toggle)
libtexttools 2.0.3-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,188 kB
  • ctags: 635
  • sloc: ada: 13,120; cpp: 1,679; ansic: 777; makefile: 156; sh: 2
file content (1226 lines) | stat: -rw-r--r-- 35,950 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
------------------------------------------------------------------------------
-- OS (package body)                                                        --
--                                                                          --
-- Part of TextTools                                                        --
-- Designed and Programmed by Ken O. Burtch                                 --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
--                 Copyright (C) 1999-2003 Ken O. Burtch                    --
--                                                                          --
-- This is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion.  This is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with this;  see file COPYING.  If not, write --
-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
-- MA 02111-1307, USA.                                                      --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable  to  be --
-- covered  by the  GNU  General  Public  License.  This exception does not --
-- however invalidate  any other reasons why  the executable file  might be --
-- covered by the  GNU Public License.                                      --
--                                                                          --
-- This is maintained at http://www.vaxxine.com/pegasoft                    --
--                                                                          --
------------------------------------------------------------------------------
-- C error numbers in /usr/include/asm/errno.h for linux.
--
-- much of this should probably be rewritten now that I know more about
-- kernel programming -- KB

pragma Optimize( Space );
--pragma Normalize_Scalars;
pragma suppress( range_check );
pragma suppress( index_check );

with Text_IO; use Text_IO;  -- for debugging
with Ada.Strings;
with Strings; use Strings;
with Interfaces.C;
with Gen_List;

package body os is

LockPath : constant string := "/home/ken/";
SessionLogPath : APathName;

ttyname : str255; 
IsLocaltty : boolean;  -- true if not client/server

---> C Interface
--
-- These are all from C_code/system.c.

procedure CTime;
pragma Import( C, CTime, "CTime" );

procedure CDate;
pragma Import( C, CDate, "CDate" );

procedure CLongDate;
pragma Import( C, CLongDate, "CLongDate" );

function CUNIX return integer;
pragma Import( C, CUNIX, "CUNIX" );

function CGetUNIX( temppath : string ) return integer;
pragma Import( C, CGetUNIX, "CGetUNIX" );

--Obsolete
--procedure GetTempName;
--pragma Import( C, GetTempName, "GetTempName" );

function CNotEmpty return integer;
pragma Import( C, CNotEmpty, "CNotEmpty" ); -- 0 = file not empty

function LockIt return integer;
pragma Import( C, LockIt, "LockIt" );

procedure UnlockIt;
pragma Import( C, UnlockIt, "UnlockIt" );

function CSetPath return integer;
pragma Import( C, CSetPath, "CSetPath" );

procedure CSync;
pragma Import( C, CSync, "CSync" );

procedure CAppend( s : string );
pragma Import( C, CAppend, "CAppend" );

function CUnlink( s : string ) return integer;
pragma Import( C, CUnlink, "CUnlink" );

procedure CClearSessionLog;
pragma Import( C, CClearSessionLog, "CClearSessionLog" );

procedure CAddSessionLog;
pragma Import( C, CAddSessionLog, "CAddSessionLog" );

procedure CUSleep( usec : long_integer );
pragma Import( C, CUSleep, "CUSleep" );

function CIsDirectory( s : string ) return integer;
pragma Import( C, CIsDirectory, "CIsDirectory" );

function CFileExists( s : string ) return boolean;
pragma Import( C, CFileExists, "CFileExists" );

function CRunIt( cmd, outfile, parm1, parm2, parm3 : string ) return integer;
pragma Import( C, CRunIt, "CRunIt" );


---> Linux Kernel Functions
--
-- Use "man <function>" to get more info.
--
-- Since Linux kernel functions use a lot of C strings (and pointers
-- to C strings), I've defined some C strings here for use with
-- the "Linux_" subprograms.  To convert to and from C strings, use
-- To_C(), To_Ada(), and To255().

type ACPath is new Interfaces.C.char_array( 1..1024 );
type CPathPtr is access all ACPath;

-- A C string, pointer to same, and a another pointer.

TempCPath     : aliased  ACPath;
TempCPathPtr  : constant CPathPtr := TempCPath'access;
TempCPtr      : CPathPtr;

-- Linux_GetCWD -- returns the current working directory

function Linux_GetCWD( buf : CPathPtr; size : long_integer )
  return CPathPtr;
pragma Import(C , Linux_GetCWD, "getcwd" ); -- from unistd.h

-- Linux_ChDir -- changes the current working directory

function Linux_ChDir( buf : CPathPtr ) return integer;
pragma Import(C, Linux_ChDir, "chdir" ); -- from unistd.h

-- Linux_GetEnv - get an environment variable's value

function Linux_GetEnv( env_var :  CPathPtr ) return CPathPtr;
pragma Import( C, Linux_GetEnv, "getenv" ); -- from stdlib.h

-- Linux_SetEnv - set an environment variable's value

procedure Linux_SetEnv( env_var : CPathPtr; var_val : CPathPtr;
  overwrite : integer );
pragma Import( C, Linux_SetEnv, "setenv" ); -- from stdlib.h

function CMkTemp( template : string) return CPathPtr;
pragma Import( C, CMkTemp, "mktemp" );

-- Linux_ErrNo -- error returned from last Linux operation

Linux_ErrNo : integer;
pragma Import( C, Linux_ErrNo, "errno" ); -- from errno.h

--->
--
-- Lintel Pathnames
--
-- The pathnames are kept in a list of strings
-- was PathList : array( APathNumber ) of APathName := (others => NullStr255);

-- This should probably be a controlled object so it can be deleted
-- automatically.

type APathListRecord is record
   alias : str255;
   path  : APathName;
end record;

function ">=" (left, right : APathListRecord ) return boolean is
begin
  return left.alias >= right.alias;
end ">=";

package PathList is new Gen_List( APathListRecord, ">=" );

Paths : PathList.List;

--- Interface_String -- null-terminated "system" command string

Interface_String : array(0..255) of character;
pragma Import( C, Interface_String, "Interface_String" );

-- SessionLogName

SessionLogName : array(0..80) of character;
pragma Import( C, SessionLogName, "SessionLogName" );

procedure SetInterface( s : str255 ) is
begin
  if length( s ) < Interface_String'last then
     for i in 1..length(s) loop
         Interface_String(i-1) := Element( s, (i) );
     end loop;
     Interface_String( length( s ) ) := character'val(0);
  end if;
end SetInterface;


---> Housekeeping

procedure StartupOS is
  WasRaising : boolean;

  procedure InitializeSessionLog is
  begin
    if length( SessionLogPath ) < SessionLogName'last then
       for i in 1..length( SessionLogPath ) loop
           SessionLogName(i-1) := Element( SessionLogPath, (i) );
       end loop;
       Interface_String( length( SessionLogPath ) ) := character'val(0);
    end if;
    CClearSessionLog;
    SessionLog( "StartupOS: New " & ProgramName & " session log started" );
    SessionLog( "StartupOS: " & GetLongDate & " @ " & GetTime );
    if LastError /= TT_OK then
       Put_Line( "StartupOS: Unable to write to session_log; error " &
          AnErrorCode'image( LastError ) );
       return; -- no log to write to (yet)
    end if;
  end InitializeSessionLog;

  procedure CheckOSServices is
    TempFile : str255;
  begin
    null;
    --MakeTempFilename( TempFile );
    --UNIX( ToString( "zoo a " & TempFile & " /etc/passwd > /dev/null" ) );
    --if LastError /= CoreOK then -- wierdness when I used if UNIX(zoo)...
    --   SessionLog( "StartupOS: Unable to find zoo command", LastError );
    --end if;
    --Erase( TempFile & ".zoo" );
  end CheckOSServices;

begin
  NoError;
  WasRaising := TrapErrors;
  TempCPath(1) := 'T';
  TempCPath(2) := 'M';
  TempCPath(3) := 'P';
  TempCPath(4) := 'D';
  TempCPath(5) := 'I';
  TempCPath(6) := 'R';
  TempCPath(7) := Interfaces.C.char( ASCII.NUL );
  TempCPtr := Linux_GetEnv( TempCPathPtr );
  if TempCPtr /= null then
     PathAlias( To255( "tmp" ), To255( To_Ada( TempCPtr.all )));
  else
     PathAlias( To255( "tmp" ), To255( "/tmp" ) );
  end if;

  -- Lookup HOME environment variable and make it an alias
  -- (Had problems with To_C, so made the C string manually
  TempCPath(1) := 'H';
  TempCPath(2) := 'O';
  TempCPath(3) := 'M';
  TempCPath(4) := 'E';
  TempCPath(5) := Interfaces.C.char( ASCII.NUL );
  TempCPtr := Linux_GetEnv( TempCPathPtr );
  if TempCPtr /= null then
     PathAlias( To255( "home" ), To255( To_Ada( TempCPtr.all )));
  end if;

  -- Make the $SYS alias by adding ShortProgramName to $HOME
  if TempCPtr = null then
     SessionLogPath := NullStr255;
  else
     PathAlias( To255( "sys" ), To_Ada( TempCPtr.all ) & "/" &
        ShortProgramName );
     SessionLogPath := ExpandPath( To255( "$SYS/session_log" ) );
  end if;

  InitializeSessionLog;
  if LastError = TT_OK then
     ttyname := UNIX( "tty" );
     IsLocaltty := (ttyname >= To255( "/dev/tty1" ) and
         ttyname <= To255( "/dev/tty9" )) and length( ttyname ) = 9;
     if LastError = TT_OK then
        CheckOSServices;
     end if;
     if LastError /= TT_OK then
        LastError := TT_OSService;
        null; --Put_Line( Standard_Error, "StartupOS: See session log for error details" );
     end if;
  end if;

  RestoreRaising( WasRaising );
  -- load system parameters
  -- load user defaults
end StartupOS;

procedure IdleOS( IdlePeriod : ATimeStamp ) is
begin
  NoError;
  --UNIX( "sync" ); -- should call sync() by C
  --should really check and remove an old file from the .Trash
  --directory, once per call
end IdleOS;

procedure ShutdownOS is
begin
  NoError;
  SessionLog( "ShutdownOS: End of session log" );
  PathList.Clear( Paths );
end ShutdownOS;


---> OS Interfacing

function UNIX( s : str255 ) return boolean is
  result : integer;
begin
  NoError;
  if length( s ) <= Interface_String'last then
     SetInterface( s );
     result := CUNIX;
     return result = 0;
  else
     New_Line;
     Put_Line( standard_error, "UNIX: TT_ParamError -- string too long" );
     Error( TT_ParamError );
     return false;
  end if;
end UNIX;

procedure UNIX( s : str255 ) is
  result : integer;
begin
  NoError;
  if length( s ) <= Interface_String'last then
     SetInterface( s );
     result := CUNIX;
     if result /= 0 then
        Error( TT_SystemError );
     end if;
  else
     Error( TT_ParamError );
  end if;
end UNIX;

function UNIX( s : str255 ) return str255 is
  Output       : str255;
  i            : Natural;
  temppath     : str255;
begin
  NoError;
  MakeTempFileName( temppath );
  Output := NullStr255;
  if length( s ) <= Interface_String'last - 50 then
     SetInterface( s );
     if CGetUNIX( ToString( temppath ) & ASCII.NUL ) /= 0 then
        Error( TT_SystemError );
     else
        i := 0;
        while Interface_String(i) /= character'val(10) and then
              Interface_String(i) /= character'val(0) loop -- line feed
           Output := Append( Output, Interface_String(i) );
           i := i + 1;
        end loop;
     end if;
  else
     Error( TT_ParamError );
  end if;
  return Output;
end UNIX;

function UNIX( s : string ) return boolean is
begin
  return UNIX( To255( s ) );
end UNIX;

procedure UNIX( s : string ) is
begin
  UNIX( To255( s ) );
end UNIX;

function UNIX( s : string ) return str255 is
begin
  return UNIX( To255( s ) );
end UNIX;

procedure RunIt( cmd : string;
                 parm1, parm2, parm3 : string := "";
                 Results : in out Str255List.List ) is
  Status : integer;
  TempFile : Str255;
begin
  NoError;
  MakeTempFileName( TempFile );
  Str255List.Clear( Results );
  Status := CRunIt( cmd & ASCII.NUL,
                    ToString( TempFile ) & ASCII.NUL,
                    parm1 & ASCII.NUL,
                    parm2 & ASCII.NUL,
                    parm3 & ASCII.NUL );
  if IsFile( TempFile ) then
     LoadList( TempFile, Results );
     Erase( TempFile );
  end if;
  if Status = 0 then
     Error( TT_OK );
  else
     Error( TT_SystemError );
  end if;
end RunIt;

function NotEmpty( s : in Str255 ) return boolean is
  s2 : str255;
begin
  NoError;
  s2 := ExpandPath( s );
  if length( s2 ) <= Interface_String'last then
     SetInterface( s2 );
     return CNotEmpty = 0;
  else
     SessionLog( "NotEmpty: TT_ParamError -- command too long" );
     Error( TT_ParamError );
     return false; -- dummy value
  end if;
end NotEmpty;

function IsDirectory( s : APathName ) return boolean is
-- should return error if file doesn't exist
  TempStr : str255;
begin
  NoError;
  TempStr := ExpandPath( s );
  return CIsDirectory( ToString( TempStr ) & ASCII.NUL ) /= 0;
end IsDirectory;

function IsFile( s : APathName ) return boolean is
  TempStr : str255;
begin
  NoError;
  TempStr := ExpandPath( s );
  return CFileExists( ToString( TempStr ) & ASCII.NUL );
end IsFile;

procedure MakeTempFileName( s : in out str255 ) is
  --i : natural;
begin
  TempCPtr := CMkTemp( ToString( ExpandPath( To255(
     "$TMP/texttoolsXXXXXX" ) ) ) & ASCII.NUL );
  -- probably a cleaner way to do this
  s := To255( To_Ada( TempCPtr.all ) );  
  --GetTempName;
  --s := NullStr255;
  --i := 0;
  --while Interface_String(i) /= character'val(0) loop
  --    s := Append(s, Interface_String(i) );
  --    i := i + 1;
  --end loop;
end MakeTempFileName;

function Lock( file : APathName ) return boolean is
  lockname : str255;
  temp     : str255;
begin
  lockname := Append( LockPath, ExpandPath( file ) );
  SetInterface( lockname );
  return LockIt = 0;
end Lock;

procedure Unlock( file : APathName ) is
  lockname : str255;
begin
  lockname := Append( LockPath, ExpandPath( file ) );
  SetInterface( lockname );
  UnlockIt;
end Unlock;

procedure ValidateFilename( fs : AFileSystem; oldfn : APathname;
  newfn : in out APathname; errmsg : in out str255 ) is

  ValidFilename : str255;
  changed : boolean;

  procedure ValidateUNIX is -- hastily assembled
    ch : character;
    s  : string(1..1);
  begin
    -- length OK
    -- leading character: no special requirements
    for i in 1..length( oldfn ) loop
        ch := Element( oldfn, i );
        if ch < ' ' then -- control character?
           ch := '_';
           Changed := true;
        elsif ch > '~' then -- control character?
           ch := '_';
           Changed := true;
        --elsif ch <= 'A' then -- special character?
        --    ch := '_';
        --    Changed := true;
        --elsif ch = '~' then
        --    ch := '-';
        --    Changed := true;
        end if;
        s(1) := ch;
        ValidFilename := ValidFilename & s;
    end loop;
    if Changed then
       newfn := ValidFilename;
       ErrMsg := To255( "bad characters for UNIX filesystem" );
    end if;
  end ValidateUNIX;

  procedure ValidateUNIX14 is
  begin
    if length( oldfn ) > 14 then
       newfn := Head( oldfn, 14 );
       Changed := true;
       ErrMsg := To255( "too many characters for old UNIX filesystem" );
    else
       ValidateUNIX;
    end if;
  end ValidateUNIX14;

  procedure ValidateDOS is
    ch : character;
    s  : string(1..1);
  begin
    -- length checks no longer applicable with vfat
    --if length(oldfn)>8 and then Element( oldfn,9 ) /= '.' then
    --   NewFn := OldFn;
    --   Insert( NewFn, 9, "." );
    --   ErrMsg := To255( "too many characters for DOS" );
    --elsif length( oldfn ) > 12 then
    --   newfn := Head( oldfn, 12 );
    --   Changed := true;
    --   ErrMsg := To255( "too many characters for DOS" );
    --else
       for i in 1..length(oldfn) loop
           ch := Element( oldfn, i );
           if ch <= ' ' then
              ch := '_';
              Changed := true;
           end if;
           s(1) := ch;
           ValidFilename := ValidFilename & s;
       end loop;
       if Changed then
          NewFn := ValidFilename;
          ErrMsg := To255( "bad characters for DOS" );
       end if;
    --end if;
  end ValidateDOS;

  procedure ValidateOS2 is
  begin
    ValidateUNIX; -- at least, for now
  end ValidateOS2;

begin
  ValidFilename := NullStr255;
  ErrMsg := NullStr255;
  Changed := false;
  if length( oldfn ) = 0 then
     Newfn := To255( "untitled" );
     ErrMsg := To255( "No filename" );
  else
     case fs is
     when UNIXFS => ValidateUNIX;
     when UNIX14FS => ValidateUNIX14;
     when DOSFS => ValidateDOS;
     when OS2FS => ValidateOS2;
     when NONE => null;
     when OTHERS => null;
     end case;
  end if;
end ValidateFilename;

procedure ValidatePathname( fs : AFileSystem; oldfn : APathname;
  newfn : in out APathname; errmsg : in out str255 ) is
  SepChar : character;
  SepCharAsString : string( 1..1 );
  SepPos1 : integer;
  SepPos2 : integer;
  CorrectedFile : str255;
  Piece2Validate : str255;
  thefs : AFileSystem;
  LastErrMsg : str255;
begin
  newfn := NullStr255;
  LastErrMsg := NullStr255;
  ErrMsg := NullStr255;
  case fs is
  when UNIXFS =>   SepChar := '/';
                   thefs := UnixFS;
  when UNIX14FS => SepChar := '/';
                   thefs := Unix14FS;
  when DOSFS =>    SepChar := '\';
                   thefs := DosFS;
  when OS2FS =>    SepChar := ':'; -- Is this right for OS/2?
                   thefs := OS2FS;
  when NONE => -- guess at separator
      if Index( oldfn, "/" ) > 0 then
         SepChar := '/';
         thefs := UnixFS;
      elsif Index( oldfn, "\" ) > 0 then
         SepChar := '\';
         thefs := DosFS;
      elsif Index( oldfn, ":" ) > 0 then
         SepChar := ':';
         thefs := OS2FS;
      else
         SepChar := '/'; -- guess UNIX by default
         thefs := UnixFS;
      end if;
  end case;
  SepCharAsString(1) := SepChar;
  SepPos1 := Index( oldfn, SepCharAsString );
  if SepPos1 = 0 then
     ValidateFilename( fs, oldfn, newfn, Errmsg );
  else
    loop
      SepPos2 := length( oldfn );
      for i in SepPos1+1..length( oldfn ) loop
          if Element( oldfn, i ) = SepChar then
             SepPos2 := i;
             exit;
          end if;
      end loop;
      Piece2Validate := To255( Slice( oldfn, SepPos1+1, SepPos2-1 ) );
      ValidateFilename( thefs, Piece2Validate, CorrectedFile, Lasterrmsg );
      if length( CorrectedFile ) /= 0 then
         newfn := newfn & SepChar & CorrectedFile;
         if length( LastErrMsg ) > 0 then
            ErrMsg := LastErrMsg;
         end if;
      else
         newfn := newfn & SepChar & Piece2Validate;
      end if;
      exit when SepPos2 = length( oldfn );
      SepPos1 := SepPos2;
    end loop;
    if length( ErrMsg ) = 0 then -- no errors? no changes
       newfn := NullStr255;
    end if;
  end if;
end ValidatePathname;

procedure Erase( file : APathName ) is
  CErr : integer;
begin
  NoError;
  CErr := CUnlink( ToString( ExpandPath( file ) ) & ASCII.NUL );
  if CErr /= 0 then
     SessionLog( "Erase: C error " & integer'image( CErr ) );
     case CErr is
     when 1      => Error( TT_FileAccess );    -- EPERM
     when 2      => Error( TT_FileExistance ); -- ENOENT
     when 13     => Error( TT_FileAccess );    -- EACCESS
     when 20     => Error( TT_PathExistance ); -- ENOTDIR
     when 30     => Error( TT_VolAccess );     -- EROFS
     when others => Error( TT_SystemError );   -- EFAULT, etc.
     end case;
  end if; 
end Erase;

procedure Trash( file : APathName ) is
-- remove a file to the trash can, erasing if necessary
  trashstr : str255;
  WasRaising : boolean;
begin
  --NoError called in UNIX
  WasRaising := RaisingErrors;
  trashstr := To255( "mv " );
  trashstr := Append( trashstr, ToString( ExpandPath( file ) ) );
  trashstr := Append( trashstr, " $HOME/.Trash 2> /dev/null" );
  TrapErrors;
  UNIX( trashstr );
  if WasRaising then
     RaiseErrors;
  end if;
  if LastError /= TT_OK then
     Erase( file );
  end if;
end Trash;

procedure EmptyTrash is
begin
  --NoError called in UNIX
  UNIX( "find $HOME/.Trash -type f -mtime +3 -exec rm {} \;" );
end EmptyTrash;

procedure Move( file1, file2 : APathName ) is
  movestr : str255;
begin
  --NoError called in UNIX
  movestr := To255( "mv " );
  movestr := Append( movestr, ToString( ExpandPath( file1 ) ) );
  movestr := Append( movestr, " " );
  movestr := Append( movestr, ToString( ExpandPath( file2 ) ) );
  movestr := Append( movestr, " 2> /dev/null" );
  UNIX( movestr );
end Move;

function Shrink( file : APathName ) return APathName is
  shrinkstr : str255;
begin
  --NoError called in UNIX
  shrinkstr := To255( "zoo aPq " );
  shrinkstr := Append( shrinkstr, ToString( ExpandPath( file ) ) );
  shrinkstr := Append( shrinkstr, " " );
  shrinkstr := Append( shrinkstr, ToString( ExpandPath( file ) ) );
  UNIX( shrinkstr );
  if LastError = 0 then
     shrinkstr := file;
     shrinkstr := Append( shrinkstr, ".bak" );
     Erase( shrinkstr );
  end if;
  if LastError = 0 then
     return Append( file, ".zoo" );
  else
     return NullStr255;
  end if;
end Shrink;

function Expand( file : APathName ) return APathName is
  expandstr : str255;
begin
  --NoError called in UNIX
  expandstr := To255( "zoo x//qO " );
  expandstr := Append( expandstr, ToString( ExpandPath( file ) ) );
  UNIX( expandstr );
  if LastError = 0 then
     return Head( file, length( file ) - 4 );
  else
     return NullStr255;
  end if;
end Expand;

procedure Archive( arch, file : APathName ) is
  ArchPath, FilePath, Cmd : str255;
  -- note possibility of overflow here!
begin
  --NoError called in UNIX
  ArchPath := ExpandPath( arch );
  FilePath := ExpandPath( file );
  if not NotEmpty( FilePath ) then -- should really be not exists
     Error( TT_FileExistance );
     return;
  end if;
  Cmd := To255( "zoo aunqP " ) & ArchPath & To255(" " ) & FilePath;
  UNIX( Cmd );
end Archive;

procedure Extract( arch, file : APathName ) is
  ArchPath, FilePath, Cmd : str255;
begin
  -- NoError called in UNIX
  ArchPath := ExpandPath( arch );
  FilePath := ExpandPath( file );
  if not NotEmpty( ArchPath ) then -- should really be not exists
     Error( TT_FileExistance );
     return;
  end if;
  Cmd := ( To255( "zoo xqO ") & ArchPath & To255(" ") ) & ( FilePath
      & " > /dev/null" );
  UNIX( Cmd );
end Extract;

procedure Armour( file : APathName; keyfile : APathName := NullStr255 ) is
begin
  null;
end Armour;

procedure Disarmour( file : APathName; keyfile : APathName := NullStr255 ) is
begin
  null;
end Disarmour;

procedure Usage( file : APathName; me : AFileUsage := Normal;
                                   us : AFileUsage := ReadOnly;
                             everyone : AFileUsage := ReadOnly ) is
  accessstr : str255;
  AccessCodeMe : character;
  AccessCodeUs : character;
  AccessCodeEveryone : character;
  tempstr   : APathName;
begin
  --NoError called in UNIX
  if me = ReadOnly or me = None then
     AccessCodeMe := '4';
  elsif me = Normal then
     AccessCodeMe := '6';
  elsif me = Run then
     AccessCodeMe := '7';
  else
     AccessCodeMe := '0';
  end if;
  if us = ReadOnly then
     AccessCodeUs := '4';
  elsif us = Normal then
     AccessCodeUs := '6';
  elsif us = Run then
     AccessCodeUs := '7';
  else
     AccessCodeUs := '0';
  end if;
  if everyone = ReadOnly then
     AccessCodeEveryone := '4';
  elsif everyone = Normal then
     AccessCodeEveryone := '6';
  elsif everyone = Run then
     AccessCodeEveryone := '7';
  else
     AccessCodeEveryone := '0';
  end if;
  accessstr := To255( "chmod " );
  accessstr := accessstr & AccessCodeMe & AccessCodeUs & AccessCodeEveryone;
  TempStr := " " & ExpandPath( file );
  accessstr := Append( accessstr, ToString( tempStr ), Ada.Strings.Right );
  UNIX( accessstr );
end Usage;

procedure BeginSession is
begin
  null;
end BeginSession;

procedure EndSession is
begin
  null; -- sync
end EndSession;


---> Directory Utilities

function SpaceUsed( dir : APathName ) return long_integer is
  expandstr : str255;
begin
  --NoError called in UNIX
  expandstr := To255( "du -fs " );
  expandStr := expandStr & ExpandPath( dir );
  return ToLongInteger( UNIX( expandStr ) );
end SpaceUsed;


---> Device Utilities

function SpaceFree( dev : APathName ) return long_integer is
begin
  return 1; -- NYI
end SpaceFree;

function TotalSpace( dev : APathName ) return long_integer is
begin
  return 1;
end TotalSpace;

function EntriesFree( dev : APathName ) return long_integer is
begin
  return 1;
end EntriesFree;

function TotalEntries( dev : APathname ) return long_integer is
begin
  return 1;
end TotalEntries;

function OnDevice( path : APathName ) return APathname is
begin
  return NullStr255;
end OnDevice;


--->

function GetFreeClusterHost return str255 is
begin
  return UNIX( "uname -n" );
end GetFreeClusterHost;


---> Str255Lists

procedure LoadList( path : APathName; StrList : in out Str255List.List ) is
-- load a string list to a file
   Expandedpath : APathName;
   f : file_type;
   s : string(1..255);
   -- 254 & EOL (won't take 255!)  When I expanded it to 256,
   -- it wouldn't load anything
   l : natural;
begin
  ExpandedPath := ExpandPath( path );
  --  SessionLog( ToString( ExpandPath( path ) ) ); ---
  Open( f, in_file, ToString( ExpandedPath ) );
  while not End_of_File( f ) loop
        Get_Line( f, s, l );
        Str255List.Queue( StrList, Head( To255( s ), l ) );
  end loop;
  Close( f );
  exception -- translate Text_IO errors into core errors
    when Status_Error     => Error( TT_FileLocking );
    when Name_Error       => Error( TT_FileExistance );
    when Constraint_Error => raise; -- shouldn't happen, but...
    when Storage_Error    => Str255List.Clear( StrList );
                             Error( TT_LowMemory );
    when others           => Error( TT_IOError );
end LoadList;

procedure SaveList( path : APathName; StrList : in out Str255List.List ) is
-- save a string list to a file
   f : file_type;
   s : Str255;
   Lines : Str255List.AListIndex;
   TruePath : APathName;
begin
  TruePath := ExpandPath( Path );
  UNIX( "touch " & TruePath ); -- file must exist
  Open( f, out_file, ToString( TruePath ) );
  Lines := Str255List.length( StrList );
  for i in 1..Lines loop
      Str255List.Find( StrList, i, s );
      Put_Line( f, ToString( s ) );
  end loop;
  Close( f );
  exception -- translate Text_IO errors into core errors
    when Status_Error     => Error( TT_FileLocking );
    when Name_Error       => Error( TT_FileExistance );
    when Constraint_Error => raise; -- shouldn't happen, but...
    when others           => Error( TT_IOError );
end SaveList;

function IsLocal return boolean is
begin
  return IsLocaltty;
end IsLocal;

procedure SetPath( s : APathName ) is
  NewPath : APathname;
begin
  NewPath := ExpandPath( s );
  NoError;
  if length( NewPath ) <= Interface_String'last then
     SetInterface( NewPath );
     if CSetPath /= 0 then
        SessionLog( "SetPath: can't change path to " & s );
        Put_line( standard_error, "SetPath: TT_SystemError -- can't change path" );
        Error( TT_SystemError );
     end if;
  else
     New_Line;
     Put_Line( standard_error, "SetPath: CoreParamError -- command too long" );
     Error( TT_ParamError );
  end if;
end SetPath;

function GetPath return APathName is
  Dummy : CPathPtr;
begin
  -- execute Linux getcwd (Get Current Working Directory) function
  Dummy := Linux_getcwd( TempCPathPtr, TempCPath'length );
  -- convert to a APathName and return result
  return To255( To_Ada( TempCPath ) );
end GetPath;

--procedure SetPath( pn : APathNumber; path : APathName ) is
--  temp : APathName;
--begin
--  temp := path;
--  if Element( temp, Length( temp ) ) = '/' then
--     Delete( temp, Length( temp ), Length( temp ) );
--  end if;
--  PathList( pn ) := temp;
--end SetPath;

procedure PathAlias( alias : str255; path : APathName ) is
  PathRec    : APathListRecord;
  Result     : PathList.AListIndex;
  IsNewAlias : boolean := true;
begin
  NoError;
  -- check to make sure that alias/path doesn't exist
  if not PathList.IsEmpty( Paths ) then
     -- locate first record with same alias (if any)
     PathRec.alias := ToUpper( alias );
     PathRec.path := NullStr255;
     Result := 0;
     for i in 1..PathList.length( Paths ) loop
        -- pull record and make sure it's a different path
        PathList.Find( Paths, i, PathRec );
        if PathRec.Alias = ToUpper( alias ) then
           IsNewAlias := false; -- alias exists
           Result := i;
           exit;
        elsif PathRec.Alias > alias then
           exit; -- missed it alphabetically, so it's new
        end if;
     end loop;
  end if;
  -- old entry? then delete it first
  if not IsNewAlias then
     PathList.Clear( Paths, Result );
  end if;
  -- add new entry (alphabetically) to path list
  PathRec.alias := ToUpper( alias );
  PathRec.path := path;
  PathList.Insert( Paths, PathRec );
end PathAlias;

function ExpandPath( path : in APathName ) return APathName is
-- check for leading number and convert to a path
  posn : integer;
  result : APathName;
  Alias  : APathName;
  PathRec : APathListRecord;
begin
  Result := path;
  if Length( Result ) > 0 then
     if Element( Result, 1 ) = '$' then
        posn := 1;
        while Element( Result, posn ) /= '/' loop
          posn := posn+1;
          exit when posn > Length( path );
        end loop;
        if posn > Length( path ) then
           Alias := Delete( ToUpper( path ), 1, 1 );
           Result := NullStr255;
        else
           Alias := ToUpper( To255( Slice( Result, 2, posn-1 ) ) );
           Delete( Result, 1, posn-1 );
        end if;
        for i in 1..PathList.length( Paths ) loop
            PathList.Find( Paths, i, PathRec );
            exit when PathRec.Alias > Alias;
            if PathRec.Alias = Alias then
               Result := PathRec.Path & Result;
            end if;
        end loop;
     end if;
  end if;
  return Result;
end ExpandPath;

procedure SplitPath( path : Str255;
   dir : out Str255; file : out Str255 ) is
-- split path into directory and file
   SlashPos : natural := 0;
begin
  for i in reverse 1..length( path ) loop
      if Element( path, i ) = '/' then
         SlashPos := i;
         exit;
      end if;
  end loop;
  if SlashPos > 0 then
     Dir := To255( Slice( path, 1, SlashPos ) );
     File := To255( Slice( path, SlashPos+1, length( path ) ) );
  else
     Dir := NullStr255;
     File := NullStr255;
  end if;
end SplitPath;

procedure DecomposePath( path : APathname; PathType : out APathType;
  Host : out Str255; Filepath : out APathname ) is
  SlashPos : natural;
  TempPath : APathname;
begin
  TempPath := ExpandPath( Path );
  --
  -- Check for a standard path--process and bail out if is one
  --
  if Index( TempPath, "://" ) = 0 then       -- no ://?
     PathType := File;
     Host := To255( "localhost" );           --  then it's this machine
     FilePath := TempPath;                   --  and it's the pathname
     return;                                 --  and bail out early
  end if;
  --
  -- Must be a URL.
  -- Check for the leading transfer method in the URL.
  -- slice vs. head: slice returns an Ada string and saves a conversion.
  --
  if Slice( TempPath, 1, 7 ) = "file://" then -- is it a file://?
     Delete( TempPath, 1, 7 );                -- then remove file:// part
     PathType := File;                        -- we know it's a file URL
  elsif Slice( Temppath, 1, 7 ) = "http://" then
     Delete( TempPath, 1, 7 );
     PathType := http;
  elsif Slice( Temppath, 1, 6 ) = "ftp://" then
     Delete( TempPath, 1, 6 );
     PathType := ftp;
  elsif Slice( Temppath, 1, 9 ) = "window://" then
     Delete( TempPath, 1, 9 );
     PathType := window;
  elsif Slice( Temppath, 1, 6 ) = "run://" then
     Delete( TempPath, 1, 6 );
     PathType := runnable;
  else
     SlashPos := Index( TempPath, "://" );    -- we know there is one
     Delete( TempPath, 1, SlashPos + 2 );     -- scrap unknown URL prefix
     PathType := Unknown;                     -- and mark it as unknown
  end if;                                     -- and try to process anyway
  --
  -- all URL's are host and optional path
  --
  if Element( TempPath, length( TempPath ) ) = '/' then -- ending slash?
     Delete( TempPath, length( TempPath), length( TempPath ) ); -- delete it
  end if;
text_io.put_line( "checking " & ToString( TempPath ) );
  SlashPos := Index( TempPath, "/" );      -- where's the next slash?
  if SlashPos > 0 then                     -- if there is one
text_io.put_line("has path" );
     Host := Head( TempPath, SlashPos-1 ); --   the address is before it
text_io.put_line("path = " & ToString( TempPath ) );
     Delete( TempPath, 1, SlashPos );      --   removing it and slash
text_io.put_line("after host removal, path = " & ToString( TempPath ) );
     FilePath := TempPath;                 --   leaves the path
  else                                     -- but if there isn't a slash
text_io.put_line("has no path" );
     Host := TempPath;                     --   then it's just the address
     FilePath := NullStr255;               --   with no path
  end if;
end DecomposePath;

---> Calandar Systems

function GetDate return str255 is
  TempStr : Str255;
begin
  CDate;
  for i in 0..Interface_String'length loop
      exit when Interface_String(i) = ASCII.NUL;
      TempStr := TempStr & Interface_String(i);
  end loop;
  return TempStr;
end GetDate;

function GetTime return str255 is
  TempStr : Str255;
begin
  Ctime;
  for i in 0..Interface_String'length loop
      exit when Interface_String(i) = ASCII.NUL;
      TempStr := TempStr & Interface_String(i);
  end loop;
  return TempStr;
end GetTime;

function GetTimeStamp return ATimeStamp is
  Time     : ATime;
  TimeZone : ATimeZone;
  TheStamp : ATimeStamp;
begin
  GetClock( Time, TimeZone );
  TheStamp := long_long_integer( time.seconds ) * 1_000_000 +
              long_long_integer( time.microseconds );
  return TheStamp;
end GetTimeStamp;

-- probably need to define comparision overloads

function GetLongDate return str255 is
  TempStr : Str255;
begin
  Clongdate;
  for i in 0..Interface_String'length loop
      exit when Interface_String(i) = ASCII.NUL;
      TempStr := TempStr & Interface_String(i);
  end loop;
  return TempStr;
end GetLongDate;

---> Append for Text File

procedure AddFile( file, text : str255 ) is
begin
  SetInterface( text );
  CAppend( ToString( file ) & character'val(0) );
end AddFile;

---> Logging

procedure SessionLog( message : str255 ) is
  WasRaising : boolean;
begin
  NoError;
  WasRaising := TrapErrors;
  SetInterface( message );
  CAddSessionLog;
  RestoreRaising( WasRaising );
end SessionLog;

procedure SessionLog( ada_message : string ) is
begin
  --NoError implied
  SessionLog( message => To255( ada_message ) );
end SessionLog;

procedure SessionLog( message : str255; ErrorCode : AnErrorCode ) is
begin
  --NoError implied
  SessionLog( message => message & " (Error Code" &
    AnErrorCode'image( ErrorCode ) & ")" );
end SessionLog;

procedure SessionLog( ada_message : string; ErrorCode : AnErrorCode ) is
begin
  --NoError implied
  SessionLog( To255( ada_message ), ErrorCode );
end SessionLog;

procedure Wait( seconds : float ) is
begin
  -- no range check
  CUSleep( long_integer( seconds * 1_000_000.0 ) );
end Wait;

end os;