-
Notifications
You must be signed in to change notification settings - Fork 0
/
stprim.c
1423 lines (1308 loc) · 40.1 KB
/
stprim.c
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
/*********************************************************************
*
* Software Tools -- UNIX primitives
*
**********************************************************************
*
* SGETSTE -- get software tools environment
* SRELSTE -- release software tools environment
* SINTRPT -- select attention interrupt (break) processing
* SOPEN -- open a file, return file descriptor
* SCREATE -- create file, open it, return file descriptor
* SCLOSE -- close a file
* SREMOVE -- remove a file
* SRENAME -- rename a file
* SGETLIN -- get next line from file descriptor
* SGETCF -- get next character from file descriptor
* SPUTSTR -- put string to file descriptor
* SPUTCF -- put character to file descriptor
* SGETIDX -- return get/put next i/o index for later get
* SSEEK -- set position of next get
* SGETFDS -- get file descriptor status
* SPROMPT -- set standard input prompt string
* SGETINF -- return information according to index
* SSYSCMD -- execute command string
* SFFCOPY -- copy from one file descriptor to another (dummy)
* SLOGUSE -- log use of software tools application (dummy)
* STWAIT -- delay execution for a time
* SIOCTRL -- set state of opened file descriptor
* SSECURE -- secure disk copy of opened file. (dummy)
* SIXCOPY -- copy specified record from one fd to another
* SITOXPC -- internal (binary) to external (text) file pointer
* SXTOIPC -- external (text) to internal (binary) file pointer
* SMAILER -- interface to e-mail spooler (dummy)
* SREWIND -- rewind (read) or reset (write, append) file desc
*
*********************************************************************/
#include <stdio.h>
#include "st.typ.h"
#include "local.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#ifdef UTS
#include <time.h>
#else
#include <sys/time.h>
#endif
#include <errno.h>
#include <pwd.h>
#include <utmp.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#ifdef SYSV
#include <termio.h>
#endif
#include <sys/file.h>
#include "iso.h"
/*
** Character conversion tables from natural code to iso standard
** In some systems, these convert EBCDIC to ISO, here all that
** is required is to strip parity bits.
*/
static char isonat[] = {
NUL, CTLA, CTLB, CTLC, CTLD, CTLE, CTLF, CTLG,
CTLH, CTLI, CTLJ, CTLK, CTLL, CTLM, CTLN, CTLO,
CTLP, CTLQ, CTLR, CTLS, CTLT, CTLU, CTLV, CTLW,
CTLX, CTLY, CTLZ, ESC, 28, 29, 30, 31,
BLANK, EXCLAM, DQUOTE, SHARP, DOLLAR, PERCENT, AMPER, SQUOTE,
LPAREN, RPAREN, STAR, PLUS, COMMA, MINUS, PERIOD, SLASH,
DIG0, DIG1, DIG2, DIG3, DIG4, DIG5, DIG6, DIG7,
DIG8, DIG9, COLON, SEMICOL, LESS, EQUALS, GREATER, QUESTION,
ATSIGN, CAPA, CAPB, CAPC, CAPD, CAPE, CAPF, CAPG,
CAPH, CAPI, CAPJ, CAPK, CAPL, CAPM, CAPN, CAPO,
CAPP, CAPQ, CAPR, CAPS, CAPT, CAPU, CAPV, CAPW,
CAPX, CAPY, CAPZ, LBRACK, BAKSLASH,RBRACK, CFLEX, USCORE,
GRAVE, LETA, LETB, LETC, LETD, LETE, LETF, LETG,
LETH, LETI, LETJ, LETK, LETL, LETM, LETN, LETO,
LETP, LETQ, LETR, LETS, LETT, LETU, LETV, LETW,
LETX, LETY, LETZ, LBRACE, BAR, RBRACE, CTILDE, DEL,
NUL, CTLA, CTLB, CTLC, CTLD, CTLE, CTLF, CTLG,
CTLH, CTLI, CTLJ, CTLK, CTLL, CTLM, CTLN, CTLO,
CTLP, CTLQ, CTLR, CTLS, CTLT, CTLU, CTLV, CTLW,
CTLX, CTLY, CTLZ, ESC, 28, 29, 30, 31,
BLANK, EXCLAM, DQUOTE, SHARP, DOLLAR, PERCENT, AMPER, SQUOTE,
LPAREN, RPAREN, STAR, PLUS, COMMA, MINUS, PERIOD, SLASH,
DIG0, DIG1, DIG2, DIG3, DIG4, DIG5, DIG6, DIG7,
DIG8, DIG9, COLON, SEMICOL, LESS, EQUALS, GREATER, QUESTION,
ATSIGN, CAPA, CAPB, CAPC, CAPD, CAPE, CAPF, CAPG,
CAPH, CAPI, CAPJ, CAPK, CAPL, CAPM, CAPN, CAPO,
CAPP, CAPQ, CAPR, CAPS, CAPT, CAPU, CAPV, CAPW,
CAPX, CAPY, CAPZ, LBRACK, BAKSLASH,RBRACK, CFLEX, USCORE,
GRAVE, LETA, LETB, LETC, LETD, LETE, LETF, LETG,
LETH, LETI, LETJ, LETK, LETL, LETM, LETN, LETO,
LETP, LETQ, LETR, LETS, LETT, LETU, LETV, LETW,
LETX, LETY, LETZ, LBRACE, BAR, RBRACE, CTILDE, DEL,
};
static isochar natiso[] = {
GRAVE, CTLA, CTLB, CTLC, CTLD, CTLE, CTLF, CTLG,
CTLH, CTLI, CTLJ, CTLK, CTLL, CTLM, CTLN, CTLO,
CTLP, CTLQ, CTLR, CTLS, CTLT, CTLU, CTLV, CTLW,
CTLX, CTLY, CTLZ, ESC, 28, 29, 30, 31,
BLANK, EXCLAM, DQUOTE, SHARP, DOLLAR, PERCENT, AMPER, SQUOTE,
LPAREN, RPAREN, STAR, PLUS, COMMA, MINUS, PERIOD, SLASH,
DIG0, DIG1, DIG2, DIG3, DIG4, DIG5, DIG6, DIG7,
DIG8, DIG9, COLON, SEMICOL, LESS, EQUALS, GREATER, QUESTION,
ATSIGN, CAPA, CAPB, CAPC, CAPD, CAPE, CAPF, CAPG,
CAPH, CAPI, CAPJ, CAPK, CAPL, CAPM, CAPN, CAPO,
CAPP, CAPQ, CAPR, CAPS, CAPT, CAPU, CAPV, CAPW,
CAPX, CAPY, CAPZ, LBRACK, BAKSLASH,RBRACK, CFLEX, USCORE,
GRAVE, LETA, LETB, LETC, LETD, LETE, LETF, LETG,
LETH, LETI, LETJ, LETK, LETL, LETM, LETN, LETO,
LETP, LETQ, LETR, LETS, LETT, LETU, LETV, LETW,
LETX, LETY, LETZ, LBRACE, BAR, RBRACE, CTILDE, DEL,
GRAVE, CTLA, CTLB, CTLC, CTLD, CTLE, CTLF, CTLG,
CTLH, CTLI, CTLJ, CTLK, CTLL, CTLM, CTLN, CTLO,
CTLP, CTLQ, CTLR, CTLS, CTLT, CTLU, CTLV, CTLW,
CTLX, CTLY, CTLZ, ESC, 28, 29, 30, 31,
BLANK, EXCLAM, DQUOTE, SHARP, DOLLAR, PERCENT, AMPER, SQUOTE,
LPAREN, RPAREN, STAR, PLUS, COMMA, MINUS, PERIOD, SLASH,
DIG0, DIG1, DIG2, DIG3, DIG4, DIG5, DIG6, DIG7,
DIG8, DIG9, COLON, SEMICOL, LESS, EQUALS, GREATER, QUESTION,
ATSIGN, CAPA, CAPB, CAPC, CAPD, CAPE, CAPF, CAPG,
CAPH, CAPI, CAPJ, CAPK, CAPL, CAPM, CAPN, CAPO,
CAPP, CAPQ, CAPR, CAPS, CAPT, CAPU, CAPV, CAPW,
CAPX, CAPY, CAPZ, LBRACK, BAKSLASH,RBRACK, CFLEX, USCORE,
GRAVE, LETA, LETB, LETC, LETD, LETE, LETF, LETG,
LETH, LETI, LETJ, LETK, LETL, LETM, LETN, LETO,
LETP, LETQ, LETR, LETS, LETT, LETU, LETV, LETW,
LETX, LETY, LETZ, LBRACE, BAR, RBRACE, CTILDE, DEL,
};
/*********************************************************************
*
* Static variables for remembering our file status etc
*
*********************************************************************/
#define MAXIOLEN MAXSTR /* Less NEWLINE + ENDSTR */
#define P_LEN 20 /* Length of prompt string */
#define IOREAD 1 /* File open request codes */
#define IOWRITE 2
#define IOAPPEND 3
#define MAXINFO 64
#define IOMDDEFLT 0 /* Default file io mode */
#define IOMDASIS 1 /* SMP packets ready to write as is */
#define IOMDMSG 2 /* Message mode, CRLF modd off */
static int f_iostat[_NFILE]; /* IO MODE of an opened file */
static int f_stat[_NFILE]; /* Array of file statuses */
static int f_mode[_NFILE]; /* Array of open modes */
static FILE *fileptr[_NFILE] = {
stdin, stdout, stderr
}; /* Array of file pointers. Not needed in some
systems, but good for portibility */
static int ucount; /* No of times this user signed on */
static char prompt[MAXSTR+1]; /* Stored prompt string */
static char buf1[MAXSTR+1]; /* Buffers for string conversion */
static char buf2[MAXSTR+1];
static struct sgttyb sttymode; /* Mode to restore to terminal at exit */
static int fcntlflags; /* Flags to restore fcntl mode at exit
/* Definitions for the various offsets of info items for SGETINF */
#define GINSFPFX 1 /* Scratch filename prefix */
#define GINCMPAR 2 /* Command parameter string */
#define GINTDATE 3 /* Time and date, #822 format */
#define GINUNAME 4 /* Username */
#define GINMAXFN 5 /* SSMP maximum frames/packet */
#define GINSORTD 6 /* time and date, sortable format */
#define SPARE2 7 /* (not in use) */
#define GINLTFCH 8 /* Literal file character, if any */
#define GINFFCPY 9 /* Fast file copy provided? */
#define GINIXCPY 10 /* Indexed copy provided? */
#define GINCLEBF 11 /* CURLEW :: edit buffer filename */
#define GINCLCBF 12 /* CURLEW :: clone buffer filename */
#define GINCLPFN 13 /* CURLEW :: profile filename */
#define GINCLFN1 14 /* CURLEW :: first safety filename */
#define GINCLFN2 15 /* CURLEW :: second safety filename */
#define GINCLJFN 16 /* CURLEW :: journal filename */
#define GINCLHCM 17 /* CURLEW :: system help command */
#define GINCLHFN 18 /* CURLEW :: simple help filename */
#define GINCLLCL 19 /* CURLEW :: local site override */
#define GINCLRSV 20 /* CURLEW :: rename save file switch */
#define GINWIDCK 21 /* CURLEW :: check line width overflow */
#define GINFOMAX 21 /* Info items from 1 .. GINFOMAX are known */
static char * staticinfo[GINFOMAX + 1] = {
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0
};
static struct passwd *info_pw; /* The user's password file entry */
static int info_pid; /* The current process id */
/*********************************************************************
*
* definitions of objects from the system libraries
*
*********************************************************************/
extern int errno;
char *getenv();
struct tm *localtime();
long time();
char *malloc();
/*********************************************************************
*
* badend -- Restore the modes of the terminal,
* print a message and exit abnormally.
*
*********************************************************************/
badend(signum)
int signum;
{
tidyup();
fprintf(stderr,"\n\rError exit %d -- please a report fault\n\r",signum);
signal(SIGQUIT,SIG_DFL);
kill(getpid(),SIGQUIT);
}
/*********************************************************************
*
* goodend -- Restore the modes of the terminal, exit normally.
*
*********************************************************************/
goodend() {
tidyup();
exit(1);
}
/*********************************************************************
*
* tidyup -- Restore the modes of the terminal.
*
*********************************************************************/
tidyup() {
fcntl(0,F_SETFL,fcntlflags);
ioctl(1,TIOCSETP,&sttymode);
}
/*********************************************************************
*
* SGETSTE(envlocn, littoiso, isotolit)
*
* The first parameter is an integer. SGETSTE returns the
* address of the environment storage used by the primitives.
* This should be supplied by the caller as the first parameter
* of all of the other primitives. SGETSTE must be called only
* once, and before any other primitive.
*
* The second two parameters are 256-byte translate tables set up
* to allow the calling routine to perform literal (native code)
* to ISO 646 translations, and vice versa.
*
*********************************************************************/
SGETSTE(env,littoiso,isotolit)
int *env;
itable littoiso;
chtable isotolit;
{
register int i;
register int utfd;
struct utmp utbuf;
register struct utmp *utptr;
char *p;
char * gincmpar();
fcntl(0, F_GETFL, fcntlflags);
ioctl(1, TIOCGETP, &sttymode);
signal(SIGTERM, goodend); /* Normal termination signal */
signal(SIGHUP, goodend); /* Network disconnect */
signal(SIGINT, goodend); /* Interrupt */
signal(SIGQUIT, badend); /* Abnormal exit */
signal(SIGILL, badend); /* Illegal instruction */
signal(SIGFPE, badend); /* Floating point exception */
signal(SIGBUS, badend); /* Bus error - bad address */
signal(SIGSEGV, badend); /* Segmentation violation */
*env = 0;
for (i=0; i<256; i++) {
littoiso[i] = natiso[i & 0377];
isotolit[i] = isonat[i & 0377];
}
/*
** Initialise the file statuses.
*/
for (i=0; i<_NFILE; i++)
f_stat[i] = 0;
/*
** Save some useful info for SGETINF calls
*/
info_pw = getpwuid( getuid() );
info_pid = getpid();
if (staticinfo[GINSFPFX ] = malloc(13))
sprintf(staticinfo[GINSFPFX ], "/tmp/tk%05d",info_pid);
staticinfo[GINCMPAR ] = gincmpar();
if (staticinfo[GINUNAME ] = malloc(strlen(info_pw->pw_name) + 1 ) )
strcpy(staticinfo[GINUNAME ],info_pw->pw_name);
if (staticinfo[GINMAXFN ] = malloc(2))
strcpy(staticinfo[GINMAXFN ], "2");
if (staticinfo[GINIXCPY ] = malloc(2))
strcpy(staticinfo[GINIXCPY ], "y");
if (staticinfo[GINCLEBF ] = malloc(17))
sprintf(staticinfo[GINCLEBF ], "/tmp/cl%05.5debuf",info_pid);
if (staticinfo[GINCLCBF ] = malloc(17))
sprintf(staticinfo[GINCLCBF ], "/tmp/cl%05.5dclon",info_pid);
if (staticinfo[GINCLPFN ] = malloc(strlen(info_pw->pw_dir) + 11));
sprintf(staticinfo[GINCLPFN ], "%s/.curlewrc",info_pw->pw_dir);
if (staticinfo[GINCLFN1 ] = malloc(17))
sprintf(staticinfo[GINCLFN1 ], "/tmp/cl%05.5dsafe",info_pid);
if (staticinfo[GINCLFN2 ] = malloc(15))
sprintf(staticinfo[GINCLFN2 ], "./.cl%05.5dsafe",info_pid);
if (staticinfo[GINCLHFN ] = malloc(strlen(HELPDIR) + 13) )
sprintf(staticinfo[GINCLHFN], "%s/curlew.help", HELPDIR);
if (staticinfo[GINCLRSV ] = malloc(2))
strcpy(staticinfo[GINCLRSV ], "y");
/*
** To disable the line length check in Curlew's pascal, comment out next
** two lines.
*/
if (staticinfo[GINWIDCK ] = malloc(2))
strcpy(staticinfo[GINWIDCK ], "y");
/*
** Decide if it is ok to have a journal file. Some work here into
** checking if the user can use journaling - to allow it to be automatic
** most of the time.
**
** If the journal file already exists, the following must hold true, and
** automatic recovery will be attempted if the file is not empty:-
** - The user must only be signed on once.
** - The file is writeable by this user.
** - The directory containing the file is writeable.
**
** If the journal file does not exist, the following must hold true, and
** a journal file will be used for this edit.
** - The directory where the file will be placed is writeable.
**
** This is not a perfect set of tests, in particular race conditions can occur
** if the same user starts up two curlew sessions in the same directory
** at the same time.
*/
/*
** Create the proposed journal file name, and check if it exists.
*/
if (staticinfo[GINCLJFN ] = malloc(15))
sprintf(staticinfo[GINCLJFN ], "./.cl%05.5djrnl",info_pw->pw_uid);
else
staticinfo[GINCLJFN] = "";
if (access(staticinfo[GINCLJFN], F_OK) == 0) {
/*
** The file exists, be carefull not to mes up another session.
**
** Check how many times this user is signed on.
*/
utptr = &utbuf;
ucount = 0;
utfd = open("/etc/utmp",0);
while(read(utfd,utptr,sizeof utbuf) == sizeof utbuf)
if (strcmp(staticinfo[GINUNAME ],utptr->ut_name) == 0)
ucount++;
close(utfd);
if (ucount > 1)
staticinfo[GINCLJFN] = (char *) 0;
/*
** Check that the file is writeable by this user.
*/
else if (access(staticinfo[GINCLJFN], W_OK))
staticinfo[GINCLJFN] = (char *) 0;
}
/*
** Check that the directory containing the file is writeable.
*/
if (access(".", W_OK))
staticinfo[GINCLJFN] = (char *) 0;
}
/*********************************************************************
*
* SRELSTE(envlocn, action)
*
* Called to release resources acquired by SGETSTE and other
* primitives. No other primitive may be called after SRELSTE.
* If action=1, this is an emergency exit call :: don't return.
*
*********************************************************************/
SRELSTE(env,act)
int env;
int act;
{
tidyup();
if (act)
exit(act);
}
/*********************************************************************
*
* SINTRPT(envlocn, reqcode)
*
* Called to select the type of processing required when an
* interrupt signal is processed. The parameter reqcode
* specifies the action required:
*
* reqcode=0 default processing by operating system
* reqcode=1 mask interrupt; just continue execution
* reqcode=2 kill execution; return to o/s command state
*
*********************************************************************/
SINTRPT(env,req)
int env;
int req;
{
switch(req) {
case 0: /* Return to default (close files and die) */
case 2:
signal(SIGINT,goodend);
break;
case 1:
signal(SIGINT,SIG_IGN);
break;
}
}
/*********************************************************************
*
* SOPEN(envlocn, name, mode, fd)
*
* Open the file called name. If access mode is IOWRITE, the file
* will be emptied. Any error condition results in fd being
* returned with the value IOERROR.
*
*********************************************************************/
SOPEN(env,name,mode,fd)
int env;
chstring name;
int mode;
filedesc *fd;
{
struct stat junk;
FILE *fptr;
char * litname;
litname = buf1;
makelit(name, litname);
if (stat(litname,&junk) < 0) {
*fd = -1;
return;
}
switch(mode) {
case IOREAD:
fptr = fopen(litname,"r");
break;
case IOWRITE:
fptr = fopen(litname,"w");
break;
case IOAPPEND:
fptr = fopen(litname,"a");
break;
default:
*fd = -1;
return;
}
if (fptr == (FILE *) NUL)
*fd = -1;
else {
*fd = fileno(fptr);
fileptr[*fd] = fptr;
f_stat[*fd] = 0;
f_mode[*fd] = mode;
}
}
/*********************************************************************
*
* SCREATE(envlocn, name, mode, fd)
*
* Create the file called name if it doesn't already exit, then
* open it exactly as for SOPEN returning the file descriptor fd.
*
*********************************************************************/
SCREATE(env,name,mode,fd)
int env;
chstring name;
int mode;
filedesc *fd;
{
FILE *fptr;
char * litname;
litname = buf1;
makelit(name, litname);
switch(mode) {
case IOREAD:
fptr = fopen(litname,"r");
break;
case IOWRITE:
fptr = fopen(litname,"w");
break;
case IOAPPEND:
fptr = fopen(litname,"a");
break;
default:
*fd = -1;
return;
}
if (fptr == (FILE *) NUL)
*fd = -1;
else {
*fd = fileno(fptr);
fileptr[*fd] = fptr;
f_stat[*fd] = 0;
f_mode[*fd] = mode;
}
}
/*********************************************************************
*
* SCLOSE(envlocn, fd)
*
* The file associated with the file descriptor parameter is
* closed; that is it is no longer available for access by any of
* SGETLIN, SPUTSTR or SPUTCF as appropriate.
*
*********************************************************************/
SCLOSE(env,fd)
int env;
filedesc fd;
{
register FILE *f;
if (fd < 0 || fd >= _NFILE)
return;
f = fileptr[fd];
fclose(fileptr[fd]);
f_mode[fd] = f_stat[fd] = -1;
fileptr[fd] = (FILE *) 0;
}
/*********************************************************************
*
* SREMOVE(envlocn, name, rc)
*
* The file with the specified name is removed (destroyed).
* The return code rc is returned as 0 to mark success; any other
* value signifies an error.
*
*********************************************************************/
SREMOVE(env,name,rc)
int env;
chstring name;
int *rc;
{
char * litname;
litname = buf1;
makelit(name, litname);
*rc = unlink(litname);
}
/*********************************************************************
*
* SRENAME(envlocn, oldname, newname, rc)
*
* The file called oldname is renamed to be newname. The return
* code rc is returned as 0 to mark success; any other value
* signifies an error. Files with more than one link are renamed by
* copying, rather than moving, to avoid breaking the link. If the
* file newname already exists, its access mode is explicitly
* preserved.
*
*********************************************************************/
SRENAME(env,old,new,rc)
int env;
chstring old;
chstring new;
int *rc;
{
char * litold;
char * litnew;
char * cmdbuf;
struct stat statbuf;
*rc = -1;
litold = buf1;
litnew = buf2;
makelit(old,litold);
makelit(new,litnew);
if ( stat(litnew, &statbuf) < 0 ) {
statbuf.st_nlink = 0;
statbuf.st_mode = 0;
}
if ( statbuf.st_nlink < 2 ) {
if (rename(litold, litnew) == 0) {
*rc = 0;
}
}
if ( *rc != 0 ){
/* Else we'll have to do a copy and delete */
cmdbuf = malloc( strlen(litold) + strlen(litnew) + 21 );
if (cmdbuf != (char *) 0) {
sprintf(cmdbuf,"cp %s %s >/dev/null 2>&1", litold, litnew);
if ( system(cmdbuf) == 0 ) {
unlink(litold);
*rc = 0;
}
free(cmdbuf);
}
}
if ( *rc == 0 )
if (statbuf.st_mode)
chmod(litnew, statbuf.st_mode);
}
/*********************************************************************
*
* SGETLIN(envlocn, str, fd, maxlen, rc)
*
* Reads characters from the file until a NEWLINE is encountered.
* Not more than maxlen characters will be returned in str.
* On end of file, the string contains only an ENDFILE character
* (no NEWLINE) and rc will be set to 4. On success, rc is zero.
*
*********************************************************************/
SGETLIN(env, dest, fd, maxlen, rc)
int env;
chstring dest;
filedesc fd;
int maxlen;
int *rc;
{
register FILE *f;
register int c;
register char * s;
char *ret;
*rc = -1;
if (fd < 0 || fd >= _NFILE)
return;
f = fileptr[fd];
/*
** maxlen is the calling code's idea of a full line,
** MAXSTR is our idea of the same from buffer sizes etc.
** Both include space for a trailing newline/null (ie read 2 chars less
** than the greater of maxlen and MAXSTR
*/
if ( maxlen > (MAXSTR) )
maxlen = MAXSTR;
if ( (f == stdin) && (f_iostat[fd] != IOMDASIS) ) {
fputs(prompt, stderr);
fflush(stderr);
}
errno = 0;
ret = fgets(buf1, (maxlen-1), f);
#ifdef UTSBUGS
if ( errno == ENOTTY )
errno = 0;
#endif
if ( errno )
return;
s = buf1;
/* Check for end of file */
if ( ret == (char*) 0) {
*s++ = ENDFILE;
*s++ = ENDSTR;
*rc = 4;
} else {
*rc = 0;
/* Check for an overlength line and skip over the rest of it */
while ( *s != '\0')
s++;
if ( *(s-1) != '\n' ){
*s++ = '\n';
*s = '\0';
errno = 0;
do {
c = fgetc(f);
} while ( (c != '\n') && (c != EOF) && (errno == 0) );
}
}
if (f_iostat[fd] == IOMDASIS)
cvtmsg(buf1);
trim(buf1);
makeiso(buf1, dest);
}
/*********************************************************************
*
* SGETCF(envlocn, c, fd)
*
* Reads one character from the file. If end-of-file is reached,
* this character will be ENDFILE.
*
*********************************************************************/
SGETCF(env,cc,fd)
int env;
isochar *cc;
filedesc fd;
{
register FILE *f;
register int c;
if (fd < 0 || fd >= _NFILE) {
*cc = ENDFILE;
return;
}
f = fileptr[fd];
errno = 0;
c = fgetc(f);
#ifdef UTSBUGS
if ( errno == ENOTTY )
errno = 0;
#endif
if ( errno ) {
f_stat[fd] = errno;
*cc = ENDFILE;
return;
}
if ( c == EOF ) {
f_stat[fd] = -1;
c = ENDFILE;
}
*cc = natiso[c & 0377];
}
/*********************************************************************
*
* SPUTSTR(envlocn, str, fd)
*
* Write the characters from string str to the file associated
* with the file descriptor fd. Lines are terminated by NEWLINE.
*
*********************************************************************/
SPUTSTR(env,src,fd)
int env;
chstring src;
filedesc fd;
{
register FILE *f;
char * s;
s = buf1;
if (fd < 0 || fd >= _NFILE)
return;
f = fileptr[fd];
errno = 0;
makelit(src,s);
trim(s);
if (f_iostat[fd] == IOMDMSG)
cvtasis(s);
fputs(s, f);
#ifdef UTSBUGS
if ( errno == ENOTTY )
errno = 0;
#endif
fflush(f);
if (errno)
f_stat[fd] = errno;
}
/*********************************************************************
*
* SPUTCF(envlocn, c, fd)
*
* Outputs the character c to the file associated with fd.
* The action is identical to that for a call to SPUTSTR where
* the string contains only the single character c.
*
*********************************************************************/
SPUTCF(env,cc,fd)
int env;
isochar cc;
filedesc fd;
{
register FILE *f;
register int c;
c = cc;
if (fd < 0 || fd >= _NFILE)
return;
f = fileptr[fd];
errno = 0;
fputc(isonat[c & 0377 ],f);
#ifdef UTSBUGS
if ( errno == ENOTTY )
errno = 0;
#endif
fflush(f);
if (errno)
f_stat[fd] = errno;
}
/*********************************************************************
*
* SIOCTRL(envlocn, newstate, fd)
*
* This entry sets the io state of an opened file descriptor,
* depending on the value of newstate:-
* IOMDDEFLT - Default file mode (with echo, no translation)
* IOMDASIS - Packets ready to write "as is",
* reverse system provided CR/LF translation on
* output.
* IOMDMSG - message mode reverse CR/LF translation on input,
* no echo.
*
*********************************************************************/
SIOCTRL(envlocn, newstate, fd)
int envlocn;
int newstate;
filedesc fd;
{
struct sgttyb ttymode;
register FILE *f;
if (fd < 0 || fd >= _NFILE)
return;
f = fileptr[fd];
errno = 0;
fflush(f);
switch ( (f_iostat[fd] = newstate) ) {
case IOMDDEFLT: /* switch on echo again just in case */
ioctl(fd,TIOCGETP,&ttymode);
ttymode.sg_flags |= ECHO;
ioctl(fd,TIOCSETP,&ttymode);
break;
case IOMDMSG:
if ( (strcmp("x29",getenv("TERM")) != 0) && (fd == 0) ) {
/* Then switch off echo, we are a direct line and this is stdin */
ioctl(fd,TIOCGETP,&ttymode);
ttymode.sg_flags &= (~ECHO);
ioctl(fd,TIOCSETP,&ttymode);
}
break;
case IOMDASIS:
break;
}
}
/*********************************************************************
*
* SGETIDX(envlocn, index, fd, rc)
*
* This entry returns an index number suitable for use with SSEEK.
* If fd is open for write or append access, the index is that of
* the first character of next record to be written. For read
* access, the index is of the next record to be read; it is
* permissible to pre-fetch the record to determine the read index.
* The last parameter should be set to zero if index is usable.
*
*********************************************************************/
SGETIDX(env,num,fd,rc)
int env;
fdpointer *num;
filedesc fd;
int *rc;
{
register FILE *f;
if (fd < 0 || fd >= _NFILE) {
*rc = -1;
return;
}
f = fileptr[fd];
errno = 0;
*num = (fdpointer) ftell(f);
if (errno)
*rc = f_stat[fd] = errno;
else
*rc = 0;
}
/*********************************************************************
*
* SSEEK(envlocn, index, fd)
*
* For stream fd the read operation forced by SGETLIN or SGETCF
* will read the record numbered index rather than the next one
* in sequence. The index'th record is the one which ends with
* the index'th NEWLINE character.
*
*********************************************************************/
SSEEK(env,num,fd)
int env;
fdpointer num;
filedesc fd;
{
register FILE *f;
if (fd < 0 || fd >= _NFILE)
return;
f = fileptr[fd];
errno = 0;
fseek(f, ((long)num), 0);
if ( errno )
f_stat[fd] = errno;
}
/*********************************************************************
*
* SGETFDS(envlocn, fdstat, fd)
*
* This routine returns the status of the file descriptor fd in
* the integer fdstat. Values are:
*
* -1 End of file reached on input.
* 0 No input/output error since SOPEN, SCREATE or SGETFDS.
* >0 A serious error has occured (see definition of errno)
*
* Positive values are cleared to zero by the SGETFDS call, also
* -1 (end of file) for standard input.
*
*********************************************************************/
SGETFDS(env,fdstat,fd)
int env;
int *fdstat;
filedesc fd;
{
*fdstat = f_stat[fd];
if ( (f_stat[fd] == -1) && ( fileptr[fd] == stdin ) && isatty(fd) ) {
errno = 0;
f_stat[fd] = 0;
freopen("/dev/tty","r",stdin);
if (errno)
f_stat[fd] = errno;
}
else if (f_stat[fd] > 0)
f_stat[fd] = 0;
}
/*********************************************************************
*
* SPROMPT(envlocn, pstr)
*
* Sets the input line prompt string for reads from standard
* input (fd = 0) only. The default string is null.
*
*********************************************************************/
SPROMPT(env,pstr)
int env;
chstring pstr;
{
makelit(pstr,prompt);
}
/*********************************************************************
*
* SGETINF(envlocn, index, info, maxlen)
*
* The index parameter specifies the information required by the
* calling package. Up to maxlen bytes of this will be returned
* in the info parameter; maxlen may be asssumed >= 128.
*
*********************************************************************/
SGETINF(env,ind,info,maxlen)
int env;
int ind;
chstring info;
int maxlen;
{
char * gintdate();
char * gsortdate();
info[0] = ENDSTR;
if ( (ind > GINFOMAX) || (ind < 0) )
ind = 0; /* Guaranteed not to be defined */
if (staticinfo[ind])
makeiso(staticinfo[ind], info);
else switch(ind) {
case GINTDATE :
makeiso(gintdate(), info);