home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / help.jn / listfd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-19  |  3.6 KB  |  201 lines

  1. #ifndef OLDDIR
  2. #include <sys/types.h>
  3. #include <stdio.h>
  4. #include <sys/stat.h>
  5. #include <string.h>
  6. #ifdef SYSV
  7. #include <dirent.h>
  8. #endif
  9. #include <sys/dir.h>
  10. listfd(yesprint)
  11. /* list files only of type "file" in the current directory */
  12. /* the "yesprint" thing is a horrible kludge, added to correct
  13.  a horrible kludge when the filename-completion was added ...
  14.  ad nauseam */
  15. {
  16.     DIR *dirp;
  17.     char line[233],fnmine[833];
  18.     int k;
  19. #ifdef SYSV
  20.     struct dirent dir, *p, *readdir();
  21. #else
  22.     struct direct dir, *p, *readdir();
  23. #endif
  24.     char *fn, *fgets();
  25.     extern char *zork[300];
  26.     int i;
  27.     char *malloc();
  28.  
  29.  
  30.     p= &dir;
  31.     dirp=opendir(".");
  32.     if(dirp==NULL){
  33.         return(-1);
  34.     }
  35.     line[0]=0;
  36.     i=0;
  37.     zork[i]=(char * )0;
  38.     while(p=readdir(dirp)) 
  39.     {
  40.         fn=p->d_name;
  41.         /*        fprintf(stderr,"fn=%x,p=%x,p->d_name=%x, *fn=%c\n",*/
  42.         /*        fn,p,p->d_name,*fn);*/
  43.         /*        {int i;for(i=0;i<5;i++)fprintf(stderr,"%o ",fn[i]);}*/
  44.         if(*fn=='.')continue;
  45.         if(mymode(fn)&040000) /*it's a dir*/continue;
  46.         if((k=kindex(fn,".txt"))<0)continue;
  47.         (void)strcpy(fnmine,fn);
  48.         fnmine[k]=0;
  49.         /*        puts(fnmine);*/
  50.         zork[i]=malloc((unsigned)strlen(fnmine)+1);
  51.         if(!zork[i])exit(1);
  52.         (void)strcpy(zork[i],fnmine);
  53.         /*        puts(zork[i]);*/
  54.         i++;
  55.         zork[i]=(char * )0;
  56.     }
  57.     closedir(dirp);
  58.     /*    puts(line);*/
  59.     mydumbsort(zork);
  60.     line[0]=0;
  61.     for(i=0;zork[i];i++)
  62.     {
  63.         int jim;
  64.         strcat(line,zork[i]);
  65. /*        free(zork[i]);*/
  66.         strcat(line," ");
  67.         jim=strlen(line);
  68.         if(jim>65){
  69.             if(yesprint)puts(line);
  70.             line[0]=0;
  71.         }
  72.         jim=strlen(line);
  73.         while(  (jim%9) ){
  74.             strcat(line," "); 
  75.             jim++;
  76.         }
  77.         if(jim>65){
  78.             if(yesprint)puts(line);
  79.             line[0]=0;
  80.         }
  81.         /*    if((i%5)==4){*/
  82.         /**/
  83.         /*            puts(line);*/
  84.         /*            line[0]=0;*/
  85.         /*        }*/
  86.  
  87.     }
  88.     if(yesprint)puts(line);
  89.     return 1;    
  90. }
  91. mydumbsort(p)
  92. char **p;
  93. {
  94.     /*dumb bubble sort of pointers*/
  95.     int i,j,n;
  96.     char *t;
  97.     if(p[0]==(char*)0)return; /*no elements*/
  98.     if(p[1]==(char*)0)return; /* one element*/
  99.     for(i=0;p[i];i++)   ;
  100.     n=i;
  101.     for(i=0;i<n-1;i++)
  102.         for(j=i+1;j<n;j++)
  103.             /*            if(*p[i]< *p[j]){*/
  104.             if(strcmp(p[i],p[j])>0){
  105.                 t=p[i];
  106.                 p[i]=p[j];
  107.                 p[j]=t;
  108.  
  109.             }
  110.  
  111. }
  112. #else
  113. /* this is an entire replacement for listfd() for old-
  114. style 14-character name, 2-byte inode no. style directories */
  115. listfd(yesprint)
  116. /* list files only of type "file" in the current directory */
  117. {
  118.     char line[233],fnmine[833];
  119.     int k;
  120.     char *fn, *fgets();
  121.     extern char *zork[300];
  122.     int i,fd;
  123.     char *malloc();
  124.     char mumble[17];
  125.  
  126.  
  127.     fd=open(".",0);
  128.  
  129.     if(fd<0){
  130.         return(-1);
  131.     }
  132.     line[0]=0;
  133.     i=0;
  134.     zork[i]=(char * )0;
  135.     while( read(fd,mumble,16)==16) 
  136.     {
  137.         if(mumble[0]==0&&mumble[1]==0)continue;/*zeroed inode no.*/
  138.         fn= mumble+2;/*filename*/
  139.         mumble[16]=0;/*make sure null terminated*/
  140.         if(*fn=='.')continue;/*skip all dot files*/
  141.         if(mymode(fn)&040000) /*it's a dir*/continue;
  142.         if((k=kindex(fn,".txt"))<0)continue;
  143.         (void)strcpy(fnmine,fn);
  144.         fnmine[k]=0;
  145.         zork[i]=malloc((unsigned)strlen(fnmine)+1);
  146.         if(!zork[i])exit(1);
  147.         (void)strcpy(zork[i],fnmine);
  148.         i++;
  149.         zork[i]=(char * )0;
  150.     }/*end while*/
  151.     i=close(fd);
  152.     if(i<0)exit(7);
  153.     mydumbsort(zork);
  154.     line[0]=0;
  155.     for(i=0;zork[i];i++)
  156.     {
  157.         int jim;
  158.         strcat(line,zork[i]);
  159. /*        free(zork[i]);*/
  160.         strcat(line," ");
  161.         jim=strlen(line);
  162.         if(jim>65){
  163.             if(yesprint)puts(line);
  164.             line[0]=0;
  165.         }
  166.         jim=strlen(line);
  167.         while(  (jim%9) ){
  168.             strcat(line," "); 
  169.             jim++;
  170.         }
  171.         if(jim>65){
  172.             if(yesprint)puts(line);
  173.             line[0]=0;
  174.         }
  175.  
  176.     }
  177.     if(yesprint)puts(line);
  178.     return 1;    
  179. }
  180. mydumbsort(p)
  181. char **p;
  182. {
  183.     /*dumb bubble sort of pointers*/
  184.     int i,j,n;
  185.     char *t;
  186.     if(p[0]==(char*)0)return; /*no elements*/
  187.     if(p[1]==(char*)0)return; /* one element*/
  188.     for(i=0;p[i];i++)   ;
  189.     n=i;
  190.     for(i=0;i<n-1;i++)
  191.         for(j=i+1;j<n;j++)
  192.             if(strcmp(p[i],p[j])>0){
  193.                 t=p[i];
  194.                 p[i]=p[j];
  195.                 p[j]=t;
  196.  
  197.             }
  198.  
  199. }
  200. #endif
  201.