File: hif.c

package info (click to toggle)
pm3 1.1.13-11
  • links: PTS
  • area: main
  • in suites: potato
  • size: 174,164 kB
  • ctags: 133,819
  • sloc: ansic: 982,617; modula3: 548,483; cpp: 57,119; exp: 21,673; sh: 17,053; lisp: 13,693; makefile: 13,492; asm: 11,795; yacc: 8,575; sed: 1,100; objc: 476; csh: 254; awk: 223; pascal: 95; fortran: 5
file content (1100 lines) | stat: -rw-r--r-- 29,085 bytes parent folder | download | duplicates (8)
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
static char _[] = "@(#)hif.c	5.19 93/10/26 11:33:19, Srini, AMD.";
/******************************************************************************
 * Copyright 1991 Advanced Micro Devices, Inc.
 *
 * This software is the property of Advanced Micro Devices, Inc  (AMD)  which
 * specifically  grants the user the right to modify, use and distribute this
 * software provided this notice is not removed or altered.  All other rights
 * are reserved by AMD.
 *
 * AMD MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS
 * SOFTWARE.  IN NO EVENT SHALL AMD BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL
 * DAMAGES IN CONNECTION WITH OR ARISING FROM THE FURNISHING, PERFORMANCE, OR
 * USE OF THIS SOFTWARE.
 *
 * So that all may benefit from your experience, please report  any  problems
 * or  suggestions about this software to the 29K Technical Support Center at
 * 800-29-29-AMD (800-292-9263) in the USA, or 0800-89-1131  in  the  UK,  or
 * 0031-11-1129 in Japan, toll free.  The direct dial number is 512-462-4118.
 *
 * Advanced Micro Devices, Inc.
 * 29K Support Products
 * Mail Stop 573
 * 5900 E. Ben White Blvd.
 * Austin, TX 78741
 * 800-292-9263
 *****************************************************************************
 *      Engineers: Srini Subramanian.
 *****************************************************************************
 * This module implements the part of the HIF kernel implemented using the
 * host computer's operating system. This is invoked when a HIF_CALL message
 * is received by MONTIP. This module uses the Debugger/Monitor on the 29K
 * target for certain HIF services.
 * The results are sent back to the 29K Target using a HIF_CALL_RTN message.
 * If the Debugger is invoked, a GO message is sent first to switch the
 * context from the Debugger to the HIF kernel, and then the HIF_CALL_RTN
 * message is sent. 
 *****************************************************************************
 */

#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <memory.h>
#include "types.h"
#include "udiproc.h"

#include "hif.h"

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif 
#ifdef HAVE_STRING_H
# include <string.h>
#else
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#ifdef	MSDOS
#include <io.h>
#include <sys\types.h>
#include <sys\timeb.h>
#else
#include <sys/types.h>
#include <sys/file.h>
#include <sys/timeb.h>
#endif

#include "mtip.h"	/* access to version numbers, etc. */
#include "messages.h"

INT32   UDI_write_string PARAMS((INT32, ADDR32, INT32, char *));
INT32   UDI_read_string PARAMS((INT32, ADDR32, INT32, char *));

INT32   hif_exit PARAMS((UINT32 lr2));
INT32   hif_open PARAMS((UINT32 lr2, UINT32 lr3, UINT32 lr4, UINT32 *gr96));
INT32   hif_close PARAMS((UINT32 lr2, UINT32 *gr96));
INT32   hif_read PARAMS((UINT32 lr2, UINT32 lr3, UINT32 lr4, UINT32 *gr96));
INT32   hif_write PARAMS((UINT32 lr2, UINT32 lr3, UINT32 lr4, UINT32 *gr96));
INT32   hif_lseek PARAMS((UINT32 lr2, UINT32 lr3, UINT32 lr4, UINT32 *gr96));
INT32   hif_remove PARAMS((UINT32 lr2, UINT32 *gr96));
INT32   hif_rename PARAMS((UINT32 lr2, UINT32 lr3, UINT32 *gr96));
INT32   hif_ioctl PARAMS((UINT32 lr2, UINT32 lr3));
INT32   hif_iowait PARAMS((UINT32 lr2, UINT32 lr3));
INT32   hif_iostat PARAMS((UINT32 lr2, UINT32 *gr96));
INT32   hif_tmpnam PARAMS((UINT32 lr2, UINT32 *gr96));
INT32   hif_time PARAMS((UINT32 *gr96));
INT32   hif_getenv PARAMS((UINT32 lr2, UINT32  lr3, UINT32 *gr96));
INT32   hif_gettz PARAMS((UINT32 *gr96, UINT32 *gr97));


#ifdef	MSDOS
/* Stub out functions not available in MS-DOS */
int   ioctl(int x, int y);
int   ioctl(x, y) int x; int y; {return(0);}
void  kbd_raw()     {return;}
void  kbd_restore() {return;}
#endif

/*
** Globals
*/

#define	TMP_BUF_SIZE	1024

static	char tmp_buffer[TMP_BUF_SIZE];

static	int	com_error;


/*
** This function is the HIF service handler on the host. It receives
** the HIF system call message as its only parameter.  When
** complete, GR96, GR97 and GR121 should be set to appropriate
** values.  
**
*/

INT32
service_HIF(service_number, lr2, lr3, lr4, gr96, gr97, gr121)
UINT32		service_number;
UINT32		lr2, lr3, lr4;
UINT32		*gr96, *gr97, *gr121;
   {
   INT32  result;

   com_error  = 0;	/* reset */
   *gr121 = (UINT32) 0x80000000; /* success unless... */

   switch ((int) service_number) {
      case HIF_exit:   result = hif_exit(lr2);
                       break;
      case HIF_open:   result = hif_open(lr2, lr3, lr4, gr96);
                       break;
      case HIF_close:  result = hif_close(lr2, gr96);
                       break;
      case HIF_read:   result = hif_read(lr2, lr3, lr4, gr96);
                       break;
      case HIF_write:  result = hif_write(lr2, lr3, lr4, gr96);
                       break;
      case HIF_remove: result = hif_remove(lr2, gr96);
                       break;
      case HIF_lseek:  result = hif_lseek(lr2, lr3, lr4, gr96);
                       break;
      case HIF_rename: result = hif_rename(lr2, lr3, gr96);
                       break;
      case HIF_ioctl:  result = hif_ioctl(lr2, lr3);
                       break;
      case HIF_iowait: result = hif_iowait(lr2, lr3);
                       break;
      case HIF_iostat: result = hif_iostat(lr2, gr96);
                       break;
      case HIF_tmpnam: result = hif_tmpnam(lr2, gr96);
                       break;
      case HIF_time:   result = hif_time(gr96);
                       break;
      case HIF_getenv: result = hif_getenv(lr2, lr3, gr96);
                       break;
      case HIF_gettz:  result = hif_gettz(gr96, gr97);
                       break;
      default:         *gr121 = (UINT32) HIF_EHIFUNDEF;
			return (SUCCESS);
      }

   /* Set gr121 if HIF error received */
   if (result != (INT32) 0)
      *gr121 = (UINT32) result;

   if (com_error) 
     return ((INT32) -1);	/* FAILURE */
   return(SUCCESS);

   }  /* end service_HIF() */




/*
** HIF Services
*/


/*
** Service 1 - exit
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    1 (0x01)    Service number
**             lr2      exitcode    User-supplied exit code
**
** Returns:    gr96     undefined   Service does not return
**             gr121    undefined   Service does not return
**
*/

/* ARGSUSED */
INT32
hif_exit(lr2)
UINT32	lr2;
   {
   return (0);
   }  /* end hif_exit() */


/*
** Service 17 - open
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    17 (0x11)   Service number
**             lr2      pathname    Pointer to a filename
**             lr3      mode        See HIF Specification
**             lr4      pflag       See HIF Specification
**
** Returns:    gr96     fileno      Success >= 0 (file descriptor)
**                                  Failure <0
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
**
*/

INT32
hif_open(lr2, lr3, lr4, gr96)
UINT32	lr2, lr3, lr4;
UINT32	*gr96;
   {
   INT32      result;
   int      hif_mode;
   int      host_mode;
   int      hif_pflag;
   int      fd;
   ADDR32   pathptr;


   pathptr = (ADDR32) lr2;
   hif_pflag = (int) lr4;
   hif_mode = (int) lr3;

   /*
   ** Note:  The pflag is the same in BSD and MS-DOS.
   ** Unfortunately, the O_* flags for HIF, UNIX and MS-DOS
   ** are not the same.  So we have to do the folowing:
   */

   host_mode = 0;
   if ((hif_mode & HIF_RDONLY) != 0)
      host_mode = host_mode | O_RDONLY;
   if ((hif_mode & HIF_WRONLY) != 0)
      host_mode = host_mode | O_WRONLY;
   if ((hif_mode & HIF_RDWR) != 0)
      host_mode = host_mode | O_RDWR;
   if ((hif_mode & HIF_APPEND) != 0)
      host_mode = host_mode | O_APPEND;
   if ((hif_mode & HIF_NDELAY) != 0)
      host_mode = host_mode | O_NDELAY;
   if ((hif_mode & HIF_CREAT) != 0)
      host_mode = host_mode | O_CREAT;
   if ((hif_mode & HIF_TRUNC) != 0)
      host_mode = host_mode | O_TRUNC;
   if ((hif_mode & HIF_EXCL) != 0)
      host_mode = host_mode | O_EXCL;
   if ((hif_mode & HIF_FORM) != 0)
      host_mode = host_mode | O_TEXT;
      else
         host_mode = host_mode | O_BINARY;


   result = UDI_read_string((INT32) UDI29KDRAMSpace, 
                        pathptr,
                        (INT32) MAX_FILENAME,
                        tmp_buffer);

   if (result != (INT32) 0) {
      *gr96 = (UINT32) -1;
      return((INT32) result);
   }

   fd = open(tmp_buffer, host_mode, hif_pflag);

   *gr96 = (UINT32) fd;

   if (fd < 0)  /* open failed on host */
      return((INT32) errno);

   return (0);
   }  /* end hif_open() */



/*
** Service 18 - close
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    18 (0x12)   Service number
**             lr2      fileno      File descriptor
**
** Returns:    gr96     retval      Success = 0
**                                  Failure < 0
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
*/

INT32
hif_close(lr2, gr96)
UINT32	lr2;
UINT32	*gr96;
   {
   int  file_no;
   int  retval;

   file_no = (int) lr2;


   /* Don't close stdin, stdout or stderr */
   if (file_no == 0 ||
       file_no == 1 ||
       file_no == 2)
      retval = 0;  /* Pretend we closed it */
      else
         retval = close(file_no);

   *gr96 = (UINT32) retval;

   if (retval < 0) 
      return((INT32) errno);

   return (0);
   }  /* end hif_close() */




/*
** Service 19 - read
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    19 (0x13)   Service number
**             lr2      fileno      File descriptor
**             lr3      buffptr     A pointer to buffer area
**             lr4      nbytes      Number of bytes to be read
**
** Returns:    gr96     count       * See HIF spec
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
*/

INT32
hif_read(lr2, lr3, lr4, gr96)
UINT32	lr2, lr3, lr4;
UINT32	*gr96;
   {
   int     bytes_read;
   int       file_no;
   ADDR32    buffptr;
   UINT32     nbytes;
   UINT32     copy_count;
   UINT32     total_bytes;
   INT32       result;

   file_no = (int) lr2;
   buffptr = (ADDR32) lr3;
   nbytes = (UINT32) lr4;
   total_bytes = (UINT32) 0;


   while (nbytes > 0) {

      copy_count = (nbytes < (UINT32) TMP_BUF_SIZE) ? nbytes : (UINT32) TMP_BUF_SIZE;

      bytes_read = (int) read(file_no,
                        (char *) tmp_buffer,
                        (int) copy_count);

      if (bytes_read == (int) -1) {
        *gr96 = total_bytes;
         return((INT32) errno);
      }

      /* Write data to target buffer */
      result = UDI_write_string((INT32) UDI29KDRAMSpace,
                            buffptr,
                            (INT32) bytes_read,
                            tmp_buffer);

      if (result != (INT32) 0) {
        *gr96 = total_bytes;
         return((INT32) result);
      }

      total_bytes = total_bytes + (UINT32) bytes_read;
      buffptr = buffptr + (UINT32) bytes_read;

      /* If bytes_read is the same a copy_count, keep reading */
      if ((UINT32) bytes_read == copy_count)
         nbytes = nbytes - (UINT32) bytes_read;
         else
            nbytes = 0;

   }  /* end while loop */

   *gr96 = total_bytes;
   return (0);

   }  /* end hif_read() */



/*
** Service 20 - write
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    20 (0x14)   Service number
**             lr2      fileno      File descriptor
**             lr3      buffptr     A pointer to buffer area
**             lr4      nbytes      Number of bytes to be read
**
** Returns:    gr96     count       * See HIF spec
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
*/

INT32
hif_write(lr2, lr3, lr4, gr96)
UINT32	lr2, lr3, lr4;
UINT32	*gr96;
    {
    int     file_no;
    ADDR32  buffptr;
    UINT32   nbytes;
    UINT32   copy_count;
    UINT32   total_bytes;
    int     bytes_written;
    INT32     result;

   file_no = (int) lr2;
   buffptr = (ADDR32) lr3;
   nbytes = (UINT32) lr4;
   total_bytes = (UINT32) 0;


   /*   If we are writing channel 0, which is stdin
	this is an error  */

   while (nbytes > (UINT32) 0) {

      copy_count = (nbytes < (UINT32) TMP_BUF_SIZE) ? nbytes : (UINT32) TMP_BUF_SIZE;

      result = UDI_read_string((UINT32) UDI29KDRAMSpace,
                            buffptr,
                            copy_count,
                            tmp_buffer);

      if (result != (INT32) 0) {
        *gr96 = total_bytes;
         return(result);
      }

      /* Write copy_count bytes to file_no */
      bytes_written = write(file_no, tmp_buffer, (int) copy_count);

      if (bytes_written < 0) {
        *gr96 = total_bytes;
         return(errno);
      }

      total_bytes = total_bytes + bytes_written;
      buffptr = buffptr + bytes_written;
      nbytes = nbytes - bytes_written;

   }  /* end while loop */


   *gr96 = total_bytes;

   return (0);
   }  /* end hif_write() */



/*
** Service 21 - lseek
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    21 (0x15)   Service number
**             lr2      file_no      File descriptor
**             lr3      offset      Number of bytes offset from orig
**             lr4      orig        A code number indication the point
**                                  within the file from which the
**                                  offset is measured
**
** Returns:    gr96     where       * See HIF spec
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
**
** Notes:  The defined constants for orig are different in MS-DOS
**         and UNIX, but there is a 1-1 correspondence as follows:
**
**       Code  UNIX    MS-DOS    Meaning
**       ----  ----    ------    -------
**         0   L_SET   SEEK_SET  Beginning of file
**         1   L_INCR  SEEK_CUR  Current file position
**         2   L_XTNX  SEEK_END  End of file
**
*/

INT32
hif_lseek(lr2, lr3, lr4, gr96)
UINT32	lr2, lr3, lr4;
UINT32	*gr96;
   {
   int    file_no;
   off_t  offset;
   int    orig;
   long   new_offset;

   file_no = (int) lr2;
   offset = (off_t) lr3;
#ifdef HAVE_UNISTD_H
   if (lr4 == (UINT32) 0)
     orig = SEEK_SET;
   else if (lr4 == (UINT32) 1)
     orig = SEEK_CUR;
   else if (lr4 == (UINT32) 2)
     orig = SEEK_END;
#else
   if (lr4 == (UINT32) 0)
     orig = L_SET;
   else if (lr4 == (UINT32) 1)
     orig = L_INCR;
   else if (lr4 == (UINT32) 2)
     orig = L_XTND;
#endif

   new_offset = lseek(file_no, offset, orig);

   *gr96 = (UINT32) new_offset;

   if (new_offset == (UINT32) -1)  /* failed */
      return(errno);

   return (0);
   }  /* end hif_lseek() */




/*
** Service 22 - remove
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    22 (0x16)   Service number
**             lr2      pathname    A pointer to string that contains
**                                  the pathname of the file
**
** Returns:    gr96     retval      Success = 0
**                                  Failure < 0
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
*/

INT32
hif_remove(lr2, gr96)
UINT32	lr2;
UINT32	*gr96;
   {
   int      retval;
   INT32      result;


   result = UDI_read_string((INT32) UDI29KDRAMSpace,
                        (ADDR32) lr2,
                        (INT32) MAX_FILENAME,
                        tmp_buffer);

   if (result != (INT32) 0) {
     *gr96 = (UINT32) -1;
      return(result);
   }

   retval = unlink(tmp_buffer);

   if (retval != 0) {
     *gr96 = (UINT32) -1;
      return(errno);
   }

   *gr96 = (UINT32) 0;
   return (0);

   }  /* end hif_remove() */



/*
** Service 23 - rename
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    23 (0x17)   Service number
**             lr2      oldfile     A pointer to string containing
**                                  the old pathname of the file
**             lr3      newfile     A pointer to string containing
**                                  the new pathname of the file
**
** Returns:    gr96     retval      Success = 0
**                                  Failure < 0
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
*/

INT32
hif_rename(lr2, lr3, gr96)
UINT32	lr2, lr3;
UINT32	*gr96;
   {
   char     oldname[MAX_FILENAME];
   int      retval;
   INT32      result;


   /* Get old filename */
   result = UDI_read_string((INT32) UDI29KDRAMSpace,
                        (ADDR32) lr2,
                        (INT32) MAX_FILENAME,
                        oldname);

   if (result != (INT32) 0) {
      *gr96 = (UINT32) -1;
      return(result);
   }

   /* Get new filename */
   result = UDI_read_string((INT32) UDI29KDRAMSpace,
                        (ADDR32) lr3,
                        (INT32) MAX_FILENAME,
                        tmp_buffer);

   if (result != (INT32) 0) {
      *gr96 = (UINT32) -1;
      return(result);
   }

   retval = rename(oldname, tmp_buffer);

   *gr96 = (UINT32) retval;

   if (retval < 0) {
      return(errno);
   }


   return (0);

   }  /* end hif_rename() */



/*
** Service 24 - ioctl
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    24 (0x18)   Service number
**             lr2      fileno      File descriptor number to be tested
**             lr3      mode        Operating mode
**
** Returns:    gr96     retval      Success = 0
**                                  Failure < 0
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
**
** Note:  There is no equivalent to ioctl() in MS-DOS.  It is
**        stubbed to return a zero.
*/

INT32
hif_ioctl(lr2, lr3)
UINT32	lr2, lr3;
   {
   int   des;
   int   request;
   int   result;


   des = (int) lr2;
   request = (int) lr3;

   result = ioctl(des, request);

   if (result == -1)
      return(errno);

   return (0);

   }  /* end hif_ioctl() */



/*
** Service 25 - iowait
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    25 (0x19)   Service number
**             lr2      fileno      File descriptor number to be tested
**             lr3      mode        1 = non-blocking completion test
**                                  2 = wait until read operation complete
**
** Returns:    gr96     count       * see HIF spec
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
**
** Note:  As with ioctl(), there is no equivalent to iowait() in
**        MS-DOS.  It is stubbed to return a zero.
*/

/* ARGSUSED */
INT32
hif_iowait(lr2, lr3)
UINT32	lr2, lr3;
   {
   return (HIF_EHIFNOTAVAIL);
   }  /* end hif_iowait() */



/*
** Service 26 - iostat
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    26 (0x20)   Service number
**             lr2      fileno      File descriptor number to be tested
**
** Returns:    gr96     iostat      input status
**                                  0x0001 = RDREADY
**                                  0x0002 = ISATTY
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
**
** Note:  Currently RDREADY is always returned as set.  This is
**        ok for MS-DOS, but may cause problems in BSD UNIX.
**
*/

/* ARGSUSED */
INT32
hif_iostat(lr2, gr96)
UINT32	lr2;
UINT32	*gr96;
   {
   UDIError  result;
   UINT32  file_no;


   *gr96 = (UINT32) RDREADY;

   file_no = lr2;

   result = (UDIError) isatty((int) file_no);

   if (result == (UDIError) 0)
      *gr96 = (UINT32) (*gr96 | ISATTY);

   return (0);

   }  /* end hif_iostat() */


/*
** Service 33 - tmpnam
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    33 (0x21)   Service number
**             lr2      addrptr     Pointer into which filename is
**                                  to be stored
**
** Returns:    gr96     filename    Success: pointer to temporary
**                                  filename string.  This will be
**                                  the same as lr2 on entry unless
**                                  an error occurred
**                                  Failure: = 0 (NULL pointer)
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
**
** Warnings:  This function does not check environment variables
**            such as TMP when creating a temporary filename.
**
**            Also, an input parameter of NULL is not accepted.  This
**            would require allocation of a temporary buffer on the
**            target for storage of the temporary file name.  The
**            target must necessarily specify a buffer address for the
**            temporary filename.
**
*/

INT32
hif_tmpnam(lr2, gr96)
UINT32	lr2;
UINT32	*gr96;
   {
   ADDR32  addrptr;
   char   *filename;
   INT32     result;


   /*
   ** If addrptr is zero, there is supposed to be a temporary
   ** buffer allocated on the target.  Since we can't allocate
   ** memory on the target we have to return an error.  This
   ** should be fixed.
   */

   addrptr = lr2;

   if (addrptr == (UINT32) 0) {
     *gr96 = (UINT32) 0;
      return(HIF_EACCESS);
   }

   filename = tmpnam(tmp_buffer);

   if (filename == NULL) {
     *gr96 = (UINT32) 0;
      return(HIF_EACCESS);
   }

   result = UDI_write_string((INT32) UDI29KDRAMSpace,
                         addrptr,
                         (INT32) (strlen(filename) + 1),
                         filename);
   if (result != (INT32) 0) {
      *gr96 = (UINT32) 0;
      return(result);
   }

   *gr96 = (UINT32) addrptr;

   return (0);

   }  /* end hif_tmpnam() */



/*
** Service 49 - time
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    49 (0x31)   Service number
**
** Returns:    gr96     secs        Success != 0 (time in seconds)
**                                  Failure = 0
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
*/

/* ARGSUSED */
INT32
hif_time(gr96)
UINT32	*gr96;
   {
   time_t  secs;


   secs = time((time_t *) 0);

   *gr96 = (UINT32) secs;

   return (0);

   }  /* end hif_time() */



/*
** Service 65 - getenv
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    65 (0x41)   Service number
**             lr2      name        A pointer to symbol name
**		lr3	destination - given by OS.
**
** Returns:    gr96     addrptr     Success: pointer to symbol name string
**                                  Failure = 0 (NULL pointer)
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
**
** Note:  Since this service requires writing to a buffer on the
**        target, an extra parameter has been passed in lr3.
**        This parameter points to a buffer which can be used
**        by getenv.
*/

INT32
hif_getenv(lr2, lr3, gr96)
UINT32	lr2;
UINT32	lr3;
UINT32	*gr96;
   {
   char   *varval;
   INT32     result;

   result = UDI_read_string((INT32) UDI29KDRAMSpace,
                        (ADDR32) lr2,
                        (INT32) MAX_ENV,
                        tmp_buffer);

   if (result != (INT32) 0) {
      *gr96 = (UINT32) 0;
      return(result);
   }

   varval = (char *) getenv(tmp_buffer);

   if (varval == NULL)
     result = UDI_write_string((INT32) UDI29KDRAMSpace,
                         (ADDR32) lr3,
                         (INT32) 4,
                         "\0\0\0\0");
   else
     result = UDI_write_string((INT32) UDI29KDRAMSpace,
                         (ADDR32) lr3,
                         (INT32) (strlen(varval) + 1),
                         varval);

   if (result != (INT32) 0) {
      *gr96 = (UINT32) 0;
      return(result);
   }

   *gr96 = lr3;

   return (0);

   }  /* end hif_getenv() */




/*
** Service 66 - gettz
**
** Type         Regs    Contents    Description
** ----         ----    --------    -----------
** Calling:    gr121    66 (0x42)   Service number
**
** Returns:    gr96     zonecode    Success >= 0 (minutes west of GMT)
**                                  Failure < 0 (or information
**                                               unavailable)
**             gr97     dstcode     Success = 1 (Daylight Savings Time
**                                               in effect)
**                                          = 0 (Daylight Savings Time
**                                               not in effect)
**             gr121    0x80000000  Logical TRUE, service successful
**                      errcode     error number, service not
**                                  successful
**
*/

/* ARGSUSED */
INT32
hif_gettz(gr96, gr97)
UINT32	*gr96;
UINT32	*gr97;
   {
   struct timeb timeptr;


   (void) ftime(&timeptr);

   *gr96 = (UINT32) timeptr.timezone;
   *gr97 = (UINT32) timeptr.dstflag;

   return (0);

   }  /* end hif_gettz() */



/*
** This function is used to read data from the target.
** This function returns zero if successful, and an
** error code otherwise.
**
** Note that this function permits reading any
** arbitrary sized buffer on the target into a
** buffer on the host.
*/

INT32
UDI_read_string(memory_space, address, byte_count, data)
   INT32   memory_space;
   ADDR32  address;
   INT32   byte_count;
   char   *data;
   {
   UDIResource	from;
   UDICount	count_done;
   UDIError	UDIretval;

   from.Offset = address;
   from.Space = (CPUSpace) memory_space;

   if ((UDIretval = UDIRead (from,
			     (UDIHostMemPtr) data,
			     (UDICount) byte_count,
			     (size_t) 1,
			     &count_done,
			     (UDIBool) 0)) != UDINoError) {
      com_error = 1;
      return (UDIretval);
   };
   if ((tip_target_config.os_version & 0xf) > 4) { /* new HIF kernel */
     /* 
      * Examine UDIretval and send a GO to switch the Debugger context
      * back to HIF kernel.
      */
      Mini_build_go_msg();
      if (Mini_msg_send() != SUCCESS) {
	com_error = 1;
        return (-1);	/* FAILURE */
      }
   } else { /* old HIF kernel */
   }
    return(0); /* SUCCESS */
   }  /* end read_string() */


/*
** This function is used to write a buffer of data to
** the target.  This function returns zero if successful,
** and an error code otherwise.
**
** Note that this function permits writing any
** arbitrary sized buffer on the target from a
** buffer on the host.
*/

INT32
UDI_write_string(memory_space, address, byte_count, data)
   INT32   memory_space;
   ADDR32  address;
   INT32   byte_count;
   char   *data;
   {
   UDIResource	to;
   UDICount	count_done;
   UDIError	UDIretval;

   to.Offset = address;
   to.Space = (CPUSpace) memory_space;

   if ((UDIretval = UDIWrite ((UDIHostMemPtr) data,
			      to,
			      (UDICount) byte_count,
			      (size_t) 1,
			      &count_done,
			      (UDIBool) 0)) != UDINoError) {
      com_error = 1;
      return (UDIretval);
   }
   if ((tip_target_config.os_version & 0xf) > 4) { /* new HIF kernel */
     /* 
      * Examine UDIretval and send a GO to switch the Debugger context
      * back to HIF kernel.
      */
      Mini_build_go_msg();
      if (Mini_msg_send() != SUCCESS) {
	com_error = 1;
        return (-1);	/* FAILURE */
      }
   } else { /* old HIF kernel */
   }
    return(0); /* SUCCESS */
   }  /* end UDI_write_string() */