3
3
*/
4
4
#define min (a ,b ) ((a>b) ? b : a)
5
5
6
+ /* MAXPATHLEN - maximum length of a pathname we allow */
7
+ #define MAXPATHLEN 1024
8
+
6
9
/*
7
10
* machine variants which require cc -Dmachine: pdp11, z8000, pcxt
8
11
*/
@@ -373,7 +376,7 @@ count_int checkpoint = CHECK_GAP;
373
376
#define CLEAR 256 /* table clear output code */
374
377
375
378
int force = 0 ;
376
- char ofname [ 100 ];
379
+ char ofname [ MAXPATHLEN ];
377
380
#ifdef DEBUG
378
381
int verbose = 0 ;
379
382
#endif /* DEBUG */
@@ -386,6 +389,7 @@ void (*bgnd_flag)();
386
389
387
390
388
391
int do_decomp = 0 ;
392
+ struct stat statbuf , insbuf ;
389
393
390
394
/*****************************************************************
391
395
* TAG( main )
@@ -596,8 +600,7 @@ register int argc; char **argv;
596
600
comprexx (fileptr )
597
601
char * * fileptr ;
598
602
{
599
- struct stat statbuf ,insbuf ;
600
- char tempname [1024 ], * cp ;
603
+ char tempname [MAXPATHLEN ], * cp ;
601
604
602
605
strcpy (tempname ,* fileptr );
603
606
errno = 0 ;
@@ -647,6 +650,9 @@ char **fileptr;
647
650
case S_IFDIR : /* directory */
648
651
if (recursive )
649
652
compdir (tempname );
653
+ else if ( ! quiet )
654
+ fprintf (stderr ,"%s is a directory -- ignored\n" ,
655
+ tempname );
650
656
break ;
651
657
652
658
case S_IFREG : /* regular file */
@@ -697,9 +703,14 @@ char **fileptr;
697
703
else {
698
704
/* COMPRESSION */
699
705
if (strcmp (tempname + strlen (tempname ) - 2 , ".Z" ) == 0 ) {
700
- fprintf (stderr , "%s: already has .Z suffix -- no change\n" ,
701
- tempname );
702
- return ;
706
+ fprintf (stderr , "%s: already has .Z suffix -- no change\n" ,
707
+ tempname );
708
+ return ;
709
+ }
710
+ if (insbuf .st_nlink > 1 && (! force ) ) {
711
+ fprintf (stderr , "%s has %d other links: unchanged\n" ,
712
+ tempname ,insbuf .st_nlink - 1 );
713
+ return ;
703
714
}
704
715
/* Open input file */
705
716
if ((freopen (tempname , "r" , stdin )) == NULL ) {
@@ -787,8 +798,11 @@ char **fileptr;
787
798
if ((exit_stat == 1 ) || (!quiet ))
788
799
putc ('\n' , stderr );
789
800
}
801
+ break ;
790
802
default :
791
- break ;
803
+ fprintf (stderr ,"%s is not a directory or a regular file - ignored\n" ,
804
+ tempname );
805
+ break ;
792
806
} /* end switch */
793
807
return ;
794
808
} /* end comprexx */
@@ -802,22 +816,41 @@ char *dir;
802
816
#else
803
817
register struct dirent * dp ;
804
818
#endif
805
- char nbuf [1024 ];
819
+ char nbuf [MAXPATHLEN ];
806
820
char * nptr = nbuf ;
807
821
dirp = opendir (dir );
808
822
if (dirp == NULL ) {
809
823
printf ("%s unreadable\n" , dir ); /* not stderr! */
810
824
return ;
811
825
}
826
+ /*
827
+ ** WARNING: the following algorithm will occasionally cause
828
+ ** compress to produce error warnings of the form "<filename>.Z
829
+ ** already has .Z suffix - ignored". This occurs when the
830
+ ** .Z output file is inserted into the directory below
831
+ ** readdir's current pointer.
832
+ ** These warnings are harmless but annoying. The alternative
833
+ ** to allowing this would be to store the entire directory
834
+ ** list in memory, then compress the entries in the stored
835
+ ** list. Given the depth-first recursive algorithm used here,
836
+ ** this could use up a tremendous amount of memory. I don't
837
+ ** think it's worth it. -- Dave Mack
838
+ */
812
839
while (dp = readdir (dirp )) {
813
840
if (dp -> d_ino == 0 )
814
841
continue ;
815
- if (strcmp (dp -> d_name ,"." ) == 0 || strcmp (dp -> d_name ,".." ) == 0 )
842
+ if (strcmp (dp -> d_name ,"." ) == 0 ||
843
+ strcmp (dp -> d_name ,".." ) == 0 )
816
844
continue ;
817
- strcpy (nbuf ,dir );
818
- strcat (nbuf ,"/" );
819
- strcat (nbuf ,dp -> d_name );
820
- comprexx (& nptr );
845
+ if ( (strlen (dir )+ strlen (dp -> d_name )+ 1 ) < (MAXPATHLEN - 1 )){
846
+ strcpy (nbuf ,dir );
847
+ strcat (nbuf ,"/" );
848
+ strcat (nbuf ,dp -> d_name );
849
+ comprexx (& nptr );
850
+ }
851
+ else {
852
+ fprintf (stderr ,"Pathname too long: %s/%s\n" ,dir ,dp -> d_name );
853
+ }
821
854
}
822
855
closedir (dirp );
823
856
return ;
@@ -1407,42 +1440,27 @@ writeerr()
1407
1440
copystat (ifname , ofname )
1408
1441
char * ifname , * ofname ;
1409
1442
{
1410
- struct stat statbuf ;
1411
1443
int mode ;
1412
1444
time_t timep [2 ];
1413
1445
1414
1446
fclose (stdout );
1415
- if (stat (ifname , & statbuf )) { /* Get stat on input file */
1416
- perror (ifname );
1417
- return ;
1418
- }
1419
- if ((statbuf .st_mode & S_IFMT /*0170000*/ ) != S_IFREG /*0100000*/ ) {
1420
- if (quiet )
1421
- fprintf (stderr , "%s: " , ifname );
1422
- fprintf (stderr , " -- not a regular file: unchanged" );
1423
- exit_stat = 1 ;
1424
- } else if (statbuf .st_nlink > 1 && (! force ) ) {
1425
- if (quiet )
1426
- fprintf (stderr , "%s: " , ifname );
1427
- fprintf (stderr , " -- has %d other links: unchanged" ,
1428
- statbuf .st_nlink - 1 );
1429
- exit_stat = 1 ;
1430
- } else if (exit_stat == 2 && (!force )) { /* No compression: remove file.Z */
1447
+ if (exit_stat == 2 && (!force )) { /* No compression: remove file.Z */
1431
1448
if (!quiet )
1432
- fprintf (stderr , " -- file unchanged" );
1449
+ fprintf (stderr , "No compression -- %s unchanged" ,
1450
+ ifname );
1433
1451
} else { /* ***** Successful Compression ***** */
1434
1452
exit_stat = 0 ;
1435
- mode = statbuf .st_mode & 07777 ;
1453
+ mode = insbuf .st_mode & 07777 ;
1436
1454
if (chmod (ofname , mode )) /* Copy modes */
1437
1455
perror (ofname );
1438
- chown (ofname , statbuf .st_uid , statbuf .st_gid ); /* Copy ownership */
1439
- timep [0 ] = statbuf .st_atime ;
1440
- timep [1 ] = statbuf .st_mtime ;
1456
+ chown (ofname , insbuf .st_uid , insbuf .st_gid ); /* Copy ownership */
1457
+ timep [0 ] = insbuf .st_atime ;
1458
+ timep [1 ] = insbuf .st_mtime ;
1441
1459
utime (ofname , timep ); /* Update last accessed and modified times */
1442
1460
if (unlink (ifname )) /* Remove input file */
1443
1461
perror (ifname );
1444
1462
if (!quiet )
1445
- fprintf (stderr , " -- replaced with %s" , ofname );
1463
+ fprintf (stderr , " -- replaced with %s" ,ofname );
1446
1464
return ; /* Successful return */
1447
1465
}
1448
1466
0 commit comments