-
Notifications
You must be signed in to change notification settings - Fork 3
/
ckufio.c
2097 lines (1848 loc) · 48.8 KB
/
ckufio.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
#ifndef NOICP
char *ckzv = " File IO, 4G(097)";
#endif /* ifndef NOICP */
/* C K U F I O -- Kermit file system support for UNIX systems */
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Copyright (c) 2021, 2022, 2023 Jeffrey H. Johnson <trnsz@pobox.com>
*
* Copyright (c) 1981-2011,
* Trustees of Columbia University in the City of New York.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* - Neither the name of Columbia University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ckcdeb.h" /* Typedefs, debug formats, etc */
#include "ckcker.h" /* Kermit definitions */
#include <ctype.h> /* Character types */
#include <errno.h> /* Error number information */
#include <pwd.h> /* Password file for shell name */
#include <stdio.h> /* Standard i/o */
#include <sys/dir.h> /* Directory structure */
#include <sys/types.h> /* Data types */
/*
* File date
* material
*/
#ifdef BSD4
#define TIMESTAMP
#include <sys/timeb.h>
#include <time.h>
#ifdef TIMEZONE
static long timezone;
#endif /* ifdef TIMEZONE */
#endif /* ifdef BSD4 */
#ifdef UXIII
#define TIMESTAMP
#include <time.h>
#ifdef TIMEZONE
void tzset();
extern long timezone;
#endif /* ifdef TIMEZONE */
#endif /* ifdef UXIII */
#ifdef __linux__
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#endif /* ifdef __linux__ */
#define leap(y) \
((( y ) % 4 == 0 && ( y ) % 100 != 0 ) || ( y ) % 400 == 0 )
#define nleap(y) \
((( y ) - 1969 ) / 4 - (( y ) - 1901 ) / 100 + (( y ) - 1601 ) / 400 )
#ifdef TIMEZONE
static char monlens[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
#endif /* ifdef TIMEZONE */
#include <sys/stat.h> /* File status */
/*
* Support for tilde-expansion
* in file and directory names
*/
#ifdef BSD4
#define NAMEENV "USER" /* Environment variable for tilde */
#endif /* ifdef BSD4 */
#ifdef UXIII
#define NAMEENV "LOGNAME" /* Environment variable for tilde */
#endif /* ifdef UXIII */
#ifdef BSD4
#ifdef MAXNAMLEN
#define BSD42
#ifdef BSD43
char *ckzsys = " 4.3 BSD";
#else /* ifdef BSD43 */
#ifdef SUNOS4
char *ckzsys = " SUNOS 4.x";
#else /* ifdef SUNOS4 */
#ifdef ultrix
char *ckzsys = " VAX/Ultrix";
#else /* ifdef ultrix */
char *ckzsys = " 4.2 BSD";
#endif /* ifdef ultrix */
#endif /* ifdef SUNOS4 */
#endif /* ifdef BSD43 */
#else /* ifdef MAXNAMLEN */
#define BSD41
char *ckzsys = " 4.1 BSD";
#endif /* ifdef MAXNAMLEN */
#endif /* ifdef BSD4 */
#ifdef BSD29
char *ckzsys = " 2.9 BSD";
#endif /* ifdef BSD29 */
#ifdef V7
char *ckzsys = " Version 7 UNIX";
#endif /* ifdef V7 */
#ifdef UXIII
#ifdef XENIX
#ifdef M_I386
char *ckzsys = " Xenix/386";
#else /* ifdef M_I386 */
#ifdef M_I286
char *ckzsys = " Xenix/286";
#else /* ifdef M_I286 */
char *ckzsys = " Xenix/86";
#endif /* ifdef M_I286 */
#endif /* ifdef M_I386 */
#else /* ifdef XENIX */
#ifdef ISIII
char *ckzsys = " ISC System III";
#else /* ifdef ISIII */
#ifdef __linux__
char *ckzsys = " GNU/Linux";
#else /* ifdef __linux__ */
char *ckzsys = " AT&T System III/System V";
#endif /* ifdef __linux__ */
#endif /* ifdef ISIII */
#endif /* ifdef XENIX */
#endif /* ifdef UXIII */
/*
* Definitions for some
* UNIX system commands
*/
char *DELCMD = "rm -f "; /* For file deletion */
#ifndef NOICP
char *PWDCMD = "pwd "; /* For saying where I am */
#endif /* ifndef NOICP */
char *TYPCMD = "cat "; /* For typing a file */
char *DIRCMD = "ls -l "; /* For directory listing */
#ifdef BSD4
char *SPACMD = "pwd ; quota ; df ."; /* Space/quota of current directory */
#else /* ifdef BSD4 */
char *SPACMD = "df ";
#endif /* ifdef BSD4 */
char *SPACM2 = "df "; /* For space in specified directory */
#ifdef BSD4
char *WHOCMD = "finger "; /* For seeing who's logged in */
#else /* ifdef BSD4 */
char *WHOCMD = "who "; /* For seeing who's logged in */
#endif /* ifdef BSD4 */
/*
* Functions (n is one of the predefined file numbers from ckermi.h):
*
* zopeni(n,name) - Opens an existing file for input
* zopeno(n,name) - Opens a new file for output
* zclose(n) - Closes a file
* zsout(n,s) - Write a null-terminated string to output file, buffered
* zsoutl(n,s) - Like zsout, but appends a line terminator
* zsoutx(n,s,x) - Write x characters to output file, unbuffered
* zchout(n,c) - Add a character to an output file, unbuffered
* zchki(name) - Check if named file exists and is readable, return size
* zchko(name) - Check if named file can be created
* znewn(name,s) - Make a new unique file name based on the given name
* zdelet(name) - Delete the named file
* zxpand(string) - Expands the given wildcard string into a list of files
* znext(string) - Returns the next file from the list in "string"
* zxcmd(cmd) - Execute the command in a lower fork
* zclosf() - Close input file associated with zxcmd()'s lower fork
* zrtol(n1,n2) - Convert remote filename into local form
* zltor(n1,n2) - Convert local filename into remote form
* zchdir(dirnaml) - Change working directory
* zhome() - Return pointer to home directory name string
* zkself() - Kill self, log out own job
* zsattr(
* struc zattr *) - Return attributes for file which is being sent
*/
/*
* Which systems include
* <sys/file.h>...
*/
#ifndef XENIX
#ifndef unos
/*
* Watch out, some versions of Xenix might
* need to do this include, but reportedly
* SCO Xenix 2.2 on an 80x86 system does not
*/
#include <sys/file.h> /* File access */
#endif /* ifndef unos */
#endif /* ifndef XENIX */
/*
* Some systems define these symbols in
* include files, others don't...
*/
#ifndef R_OK
#define R_OK 4 /* For access */
#endif /* ifndef R_OK */
#ifndef W_OK
#define W_OK 2
#endif /* ifndef W_OK */
#ifdef UXIII
#include <fcntl.h>
#ifndef MAXNAMLEN
#define MAXNAMLEN DIRSIZ
#endif /* ifndef MAXNAMLEN */
#endif /* ifdef UXIII */
#ifndef O_RDONLY
#define O_RDONLY 000
#endif /* ifndef O_RDONLY */
#ifdef MINBUF
#undef MAXNAMLEN
#define MAXNAMLEN 99 /* 99 max filename length for minbuf */
#endif /* ifdef MINBUF */
#ifdef __linux__
#include <limits.h>
#ifdef NAM_MAX
#define MAXNAMLEN NAM_MAX
#endif /* ifdef NAM_MAX */
#ifndef MAXNAMLEN
#define MAXNAMLEN 255
#else /* ifdef __linux__ */
#define MAXNAMLEN 14 /* If still not defined... */
#endif /* ifdef __linux__ */
#endif /* ifndef MAXNAMLEN */
#ifdef BSD29
#define MAXWLD 50 /* Maximum wildcard filenames */
#else /* ifdef BSD29 */
#ifdef MINBUF
#define MAXWLD 20 /* XXX(jhj): 20 wildcard files */
#else /* ifdef MINBUF */
#define MAXWLD 100 /* XXX(jhj): 100 wildcard files */
#endif /* ifdef MINBUF */
#endif /* ifdef BSD29 */
FILE *fp[ZNFILS] = { /* File pointers */
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
/*
* External definitions of things
* used in buffered file input and output
*/
extern CHAR zinbuffer[], zoutbuffer[];
extern CHAR *zinptr, *zoutptr;
extern int zincnt, zoutcnt;
static long iflen = -1; /* Input file length */
/* static long oflen = -1; */ /* Output file length */
static int pid = 0; /* pid of child fork */
static int fcount; /* Number of files in wild group */
static char nambuf[MAXNAMLEN + 2]; /* Buffer for a filename */
static char zmbuf[200]; /* For mail, remote print strings */
#ifndef __linux__
char *malloc();
#endif /* ifndef __linux__ */
char *getenv(), *strcpy(); /* System functions */
extern int errno; /* System error code */
static char *mtchs[MAXWLD], /* Matches found for filename */
**mtchptr; /* Pointer to current match */
/* Z K S E L F -- Kill Self: log out own job, if possible */
/*
* Note: This should get current pid,
* but if your system doesn't have
* getppid(), just do kill(0,9)...
*/
int
zkself()
{ /* For "bye", but, no guarantees! */
#ifdef V7
return ( kill(0, 9));
#else /* ifdef V7 */
return ( kill(getppid(), 1));
#endif /* ifdef V7 */
}
/* Z O P E N I -- Open an existing file for input */
int
zopeni(n, name)
int n;
char *name;
{
debug(F111, " zopeni", name, n);
/* debug(F101, " fp", "", (int)fp[n]); */
if (chkfn(n) != 0)
{
return ( 0 );
}
zincnt = 0; /* Reset input buffer */
#ifndef NOPUSH
if (n == ZSYSFN) /* Input from a system function? */
{
debug(F110, " invoking zxcmd", name, 0);
*nambuf = '\0'; /* No filename this time... */
return ( zxcmd(name)); /* Try to fork the command */
}
#endif /* ifndef NOPUSH */
if (n == ZSTDIO) /* Standard input? */
{
if (isatty(0))
{
#ifdef NODOHLP
ermsg("Not on a tty");
#else /* ifdef NODOHLP */
ermsg("Terminal input not allowed");
#endif /* ifdef NODOHLP */
debug(F110, "zopeni: attempts input from unredirected stdin", "", 0);
return ( 0 );
}
fp[ZIFILE] = stdin;
return ( 1 );
}
fp[n] = fopen(name, "r"); /* Real file. */
/* debug(F111, " zopeni", name, (int)fp[n]); */
if (fp[n] == NULL)
{
perror("zopeni");
}
return (( fp[n] != NULL ) ? 1 : 0 );
}
/* Z O P E N O -- Open a new file for output */
int
zopeno(n, name)
int n;
char *name;
{
/*
* This suid stuff commented out probably
* needs work, to apply to all the UNIX
* variants supported by this program.
* Maybe use setreuid()? The code shown
* allegedly works in 4.xBSD, Ultrix, etc.
*/
/* int uid, euid; */ /* suid variables... */
/* if (n != ZDFILE)
* debug(F111, " zopeno", name, n); */
if (chkfn(n) != 0)
{
return ( 0 );
}
if (( n == ZCTERM ) || \
( n == ZSTDIO )) /* Terminal or standard output */
{
fp[ZOFILE] = stdout;
/* if (n != ZDFILE)
* debug(F101, " fp[]=stdout", "", (int)fp[n]); */
zoutcnt = 0;
zoutptr = zoutbuffer;
return ( 1 );
}
/* uid = getuid(); euid = geteuid(); */ /* In case running suid to uucp */
/* seteuid(uid); */ /* etc, get user's own id. */
fp[n] = fopen(name, "w"); /* A real file, try to open. */
/* seteuid(uid); */ /* Put back program's suid. */
if (fp[n] == NULL)
{
perror("zopeno can't open");
}
else
{
chown(name, getuid(), getgid()); /* In case set[gu]id */
if (n == ZDFILE)
{
setbuf(fp[n], NULL); /* Debugging file unbuffered */
}
}
zoutcnt = 0; /* (PWP) reset output buffer */
zoutptr = zoutbuffer;
/* if (n != ZDFILE)
* debug(F101, " fp[n]", "", (int)fp[n]); */
return (( fp[n] != NULL ) ? 1 : 0 );
}
/* Z C L O S E -- Close the given file */
/*
* Returns 0 if arg out of
* range, 1 if successful,
* -1 if close failed.
*/
int
zclose(n)
int n;
{
int x, x2;
if (chkfn(n) < 1)
{
return ( 0 ); /* Check range of n */
}
if (( n == ZOFILE ) && ( zoutcnt > 0 )) /* (PWP) output leftovers */
{
x2 = zoutdump();
}
else
{
x2 = 0;
}
x = 0; /* Initialize return code */
#ifndef NOPUSH
if (( n == ZIFILE ) && fp[ZSYSFN]) /* If system function */
{
x = zclosf(); /* do it specially */
}
else
{
#endif /* ifndef NOPUSH */
if (( fp[n] != stdout ) && ( fp[n] != stdin ))
{
x = fclose(fp[n]);
}
fp[n] = NULL;
#ifndef NOPUSH
}
#endif /* ifndef NOPUSH */
iflen = -1; /* Invalidate file length */
if (x == EOF) /* if we got a close error */
{
return ( -1 );
}
else if (x2 < 0) /* or error flushing the buffer */
{
return ( -1 ); /* then return an error */
}
else
{
return ( 1 );
}
}
/* Z C H I N -- Get a character from the input file */
/*
* Returns -1 if EOF, 0
* otherwise with character
* returned in argument
*/
#ifdef COMMENT
zchin(n, c)
int n;
char *c;
{
int a;
/*
* (PWP) Just in case this gets
* called when it shoudn't
*/
if (n == ZIFILE)
{
return ( zminchar());
}
/* if (chkfn(n) < 1) return(-1); */
a = getc(fp[n]);
if (a == EOF)
{
return ( -1 );
}
*c = a & 0377;
return ( 0 );
}
#endif /* ifdef COMMENT */
/*
* (Re)fill the buffered input buffer with data.
* All file input should go through this routine, usually
* by calling the zminchar() macro.
*/
int
zinfill()
{
zincnt = fread(zinbuffer, sizeof ( char ), INBUFSIZE, fp[ZIFILE]);
if (zincnt == 0)
{
return ( -1 ); /* end of file */
}
zinptr = zinbuffer; /* set pointer to beginning, (== &zinbuffer[0]) */
zincnt--; /* one less char in buffer */
return \
((int)( *zinptr++ ) \
& 0377 ); /* because we return the first */
}
/* Z S O U T -- Write a string out to the given file, buffered */
int
zsout(n, s)
int n;
char *s;
{
if (chkfn(n) < 1)
{
return ( -1 ); /* Keep this here, prevents memory faults */
}
fputs(s, fp[n]);
return ( 0 );
}
/* Z S O U T L -- Write string to file, with line terminator, buffered */
int
zsoutl(n, s)
int n;
char *s;
{
/* if (chkfn(n) < 1) return(-1); */
fputs(s, fp[n]);
fputs("\n", fp[n]);
return ( 0 );
}
/* Z S O U T X -- Write x characters to file, unbuffered */
int
zsoutx(n, s, x)
int n, x;
char *s;
{
/* if (chkfn(n) < 1) return(-1); */
/* return(write(fp[n]->_file,s,x)); */
return ( write(fileno(fp[n]), s, x));
}
/* Z C H O U T -- Add a character to the given file */
/*
* Should return 0 or greater on success,
* -1 on failures (e.g. disk full)
*/
int
zchout(n, c)
/* register */
int n;
char c;
{
/* if (chkfn(n) < 1) return(-1); */
if (n == ZSFILE)
{
return ( write(fileno(fp[n]),
&c, 1)); /* Use unbuffered for session log */
}
else /* Buffered for everything else */
{
if (putc(c, fp[n]) == EOF) /* If true maybe there's an error */
{
return ( ferror(fp[n]) ? -1 : 0 ); /* Check to make sure */
}
else /* Otherwise... */
{
return ( 0 ); /* There was no error. */
}
}
}
/*
* Buffered character output
* routine to speed up file IO
*/
int
zoutdump()
{
int x;
zoutptr = zoutbuffer; /* reset buffer pointer in all cases */
debug(F101, "zoutdump chars", "", zoutcnt);
if (zoutcnt == 0) /* nothing to output */
{
return ( 0 );
}
else if (zoutcnt < 0) /* unexpected negative value */
{
zoutcnt = 0; /* reset output buffer count */
return ( -1 ); /* and fail. */
}
x = fwrite(zoutbuffer, 1, zoutcnt, fp[ZOFILE]);
if (x)
{
debug(F101, "zoutdump fwrite wrote", "", x);
zoutcnt = 0; /* reset output buffer count */
return ( 0 ); /* things worked OK */
}
else
{
zoutcnt = 0; /* reset output buffer count */
x = ferror(fp[ZOFILE]); /* get error code */
debug(F101, "zoutdump fwrite error", "", x);
return ( x ? -1 : 0 ); /* return failure if error */
}
}
/* C H K F N -- Internal function to verify file number is ok */
/*
* Returns:
* -1: File number n is out of range
* 0: n is in range, but file is not open
* 1: n in range and file is open
*/
int
chkfn(n)
int n;
{
switch (n)
{
case ZCTERM:
case ZSTDIO:
case ZIFILE:
case ZOFILE:
case ZDFILE:
case ZTFILE:
case ZPFILE:
case ZSFILE:
#ifndef NOPUSH
case ZSYSFN:
break;
#endif /* ifndef NOPUSH */
default:
debug(F101, "chkfn: file number out of range", "", n);
#ifndef NODOHLP
fprintf(stderr, "?File number out of range - %d\n", n);
#else /* ifndef NODOHLP */
fprintf(stderr, "?chkfn fail - %d\n", n);
#endif /* ifndef NODOHLP */
return ( -1 );
}
return (( fp[n] == NULL ) ? 0 : 1 );
}
/* Z C H K I -- Check if input file exists and is readable */
/*
* Returns:
* >= 0 if the file can be read (returns the size).
* -1 if file doesn't exist or can't be accessed,
* -2 if file exists but is not readable (e.g. a directory file).
* -3 if file exists but protected against read access.
*/
/*
* For Berkeley UNIX, a file must be of type "regular" to be
* readable. Directory files, special files, and symbolic
* links are not readable.
*/
long
zchki(name)
char *name;
{
struct stat buf;
int x = 0;
long y = 0;
(void)y;
x = stat(name, &buf);
if (x < 0)
{
debug(F111, "zchki stat fails", name, errno);
return ( -1 );
}
x = buf.st_mode & S_IFMT; /* Isolate file format field */
if (( x != 0 ) && ( x != S_IFREG ))
{
debug(F111, "zchki skipping:", name, x);
return ( -2 );
}
debug(F111, "zchki stat ok:", name, x);
x = access(name, R_OK);
if (x < 0) /* Is the file accessible? */
{
debug(F111, " access failed:", name, x); /* No */
return ( -3 );
}
else
{
iflen = buf.st_size; /* Yes, remember size */
strncpy(nambuf, name, MAXNAMLEN); /* and name globally. */
debug(F111, " access ok:", name, (int)iflen);
return (( iflen > -1 ) ? iflen : 0 );
}
}
/* Z C H K O -- Check if output file can be created */
/*
* Returns -1 if write permission for
* the file would be denied, 0 otherwise.
*/
#ifndef NOICP
int
zchko(name)
char *name;
{
int i, x;
char s[50], *sp;
sp = s; /* Make a copy, get length */
x = 0;
while (( *sp++ = *name++ ) != '\0')
{
x++;
}
if (x == 0)
{
return ( -1 ); /* If no filename, fail. */
}
debug(F101, " length", "", x);
for (i = x; i > 0; i--) /* Strip filename. */
{
if (s[i - 1] == '/')
{
break;
}
}
debug(F101, " i", "", i);
if (i == 0) /* If no path, use current */
{
strcpy(s, "./");
}
else /* Otherwise, use given one. */
{
s[i] = '\0';
}
x = access(s, W_OK); /* Check access of path. */
if (x < 0)
{
debug(F111, "zchko access failed:", s, errno);
return ( -1 );
}
else
{
debug(F111, "zchko access ok:", s, x);
return ( 0 );
}
}
#endif /* ifndef NOICP */
/* Z D E L E T -- Delete the named file */
void
zdelet(name)
char *name;
{
unlink(name);
}
/* Z R T O L -- Convert remote filename into local form */
/*
* For UNIX, this means changing
* uppercase letters to lowercase.
*/
void
zrtol(name, name2)
char *name, *name2;
{
for (; *name != '\0'; name++)
{
*name2++ = isupper(*name) ? tolower(*name) : *name;
}
*name2 = '\0';
debug(F110, "zrtol:", name2, 0);
}
/* Z L T O R -- Local TO Remote */
/*
* Convert filename from a local
* format to common (remote) form
*/
void
zltor(name, name2)
char *name, *name2;
{
char work[100], *cp, *pp;
int dc = 0;
debug(F110, "zltor", name, 0);
pp = work;
for (cp = name; *cp != '\0'; cp++) /* strip path name */
{
if (*cp == '/')
{
dc = 0;
pp = work;
}
else if (islower(*cp))
{
*pp++ = toupper(*cp); /* Uppercase letters */
}
else if (*cp == '~')
{
*pp++ = 'X'; /* Change tilde to 'X' */
}
else if (*cp == '#')
{
*pp++ = 'X'; /* Change number sign to 'X' */
}
else if (( *cp == '.' ) && ( ++dc > 1 ))
{
*pp++ = 'X'; /* & extra dots */
}
else
{
*pp++ = *cp;
}
}
*pp = '\0'; /* Tie it off. */
cp = name2; /* If nothing before dot, */
if (*work == '.')
{
*cp++ = 'X'; /* insert 'X' */
}
strcpy(cp, work);
debug(F110, " name2", name2, 0);
}
/* Z C H D I R -- Change directory */
int
zchdir(dirnam)
char *dirnam;
{
char *hd;
if (*dirnam == '\0')
{
hd = getenv("HOME");
}
else
{
hd = dirnam;
}
return (( chdir(hd) == 0 ) ? 1 : 0 );
}
/* Z H O M E -- Return pointer to user's home directory */
#ifndef NOICP
char *
zhome()
{
return ( getenv("HOME"));
}
#endif /* ifndef NOICP */
/* Z G T D I R -- Return pointer to user's current directory */
char *
zgtdir()
{
#ifdef MAXPATHLEN
#define CWDBL MAXPATHLEN
#else /* ifdef MAXPATHLEN */
#define CWDBL 100
#endif /* ifdef MAXPATHLEN */
#ifdef UXIII
char cwdbuf[CWDBL + 1];
char *buf;
char *getcwd();
buf = cwdbuf;
return ( getcwd(buf, CWDBL));
#else /* ifdef UXIII */
#ifdef BSD4
char cwdbuf[CWDBL + 1];
char *buf;
char *getwd();
buf = cwdbuf;
return ( getwd(buf));
#else /* ifdef BSD4 */
return ( "(directory unknown)" );
#endif /* ifdef BSD4 */
#endif /* ifdef UXIII */
}
/* Z X C M D -- Run system command so its output can be read like a file */
#ifndef NOPUSH
int
zxcmd(comand)
char *comand;
{
int pipes[2];
if (pipe(pipes) != 0)
{
debug(F100, "zxcmd pipe failure", "", 0);
return ( 0 ); /* can't make pipe, fail */
}
if (( pid = fork()) == 0) /* child */
/*
* #if BSD4 Code from Dave Tweten@AMES-NAS,
* readapted to use getpwuid to find login
* shell, bu H. Fischer.