[comp.os.minix] help wanted

XBR2D78V%DDATHD21.BITNET@cunyvm.cuny.edu (MATHIAS GAERTNER) (06/15/89)

Hi folks,
I have a problem with MINIX-ST, recently with the file-system (as i guess).
In an old UNIX-book I found a little program to list the directory-structure
of an UNIX-system. Well, typing in was easy and compiling too but...
It brings out the wanted but with many errors inbetween. And my guess, besides
logical errors in the program, is that the fs is not properly implemented to
UNIX-style.
The program: It gets a filename and checks this file with stat(). If it is
a directory it will list it, otherwises it just prints the filename.
So should it be. In real it prints the directory (as wanted) and then an amount
of '/'es, than the error 'Directory not to open' and than all the files in it.
So anything with the S_IFDIR-mode went wrong...
I have no idea, what it could be, so if someone can help me?
PS.: There may be better ways to get the file-structure of UNIX but I want this.

The source-code:
------------------------------cut here-----------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dir.h>

#define P_GR 128               /* just a buffer-size */

extern char *strcpy();
void traverse();

void report(fileordir)
        char * fileordir;
{
  struct stat stbuf;

  if (stat(fileordir,&stbuf) != 0)
  {
     printf("Directory defekt\n");
     exit();
  }
  if ((stbuf.st_mode & S_IFMT) != S_IFDIR)
  {
     printf("%s;\n",fileordir);
     return;
  }
  else
  {
     printf("%s;<<<=== DIRECTORY\n",fileordir);
     traverse(fileordir,report);
  }
}

void traverse(name, func)
        char *name;
        int (*func)();
{
  struct direct dirbuf;
  char *nbp;
  char *nep;
  int i,fd;

  nbp=name + strlen(name);
  *nbp++ = '/';
  *nbp++ = '\0';
  if ((fd=open(name,0)) ==-1)
  {
     fprintf(stdout,"FEHLER/traverse(), Dir nicht lesbar%s\n",name);
     return;
  }

  while (read(fd,(char *)&dirbuf,sizeof(dirbuf))>0)
  {
     if (dirbuf.d_ino == 0)
         continue;

     if (strcmp(dirbuf.d_name,".") == 0 ||
         strcmp(dirbuf.d_name,"..") == 0)
            continue;

     if (nbp + strlen(dirbuf.d_name) +2 >= name + P_GR)
     {
        printf("Puffer-ueberlauf\n");
        exit();
     }
     for (i=0,nep=nbp;i<DIRSIZ;i++)
     {
       *nep++ = dirbuf.d_name[i];
     }
     *nep++ = '\0';
     (*func)(name);
   }
   close(fd);
   *nbp='\0';
   *--nbp='\0';
}

void main(argc,argv)
int argc;
char *argv[];
{
   char startdir[P_GR];

   if (argc!=2)
   {
      printf("Startdirectory angeben\n");
      exit();
   }

   strcpy(startdir,argv[1]);
   traverse(startdir,report);
}
-------------------------------cut here------------------------------------

                      M. Gaertner
                TH Darmstadt, W. Germany
                XBR2D78V@DDATHD21.BITNET

holm@handel.mpr.ca (Terrence W. Holm) (06/16/89)

In article <17736@louie.udel.EDU> (MATHIAS GAERTNER) writes:
>Hi folks,
>I have a problem with MINIX-ST, recently with the file-system (as i guess).
...
>  *nbp++ = '/';
>  *nbp++ = '\0';
...
>     for (i=0,nep=nbp;i<DIRSIZ;i++)
>     {
>       *nep++ = dirbuf.d_name[i];
>     }

Try changing the second assignment to *nbp = '\0';

maart@cs.vu.nl (Maarten Litmaath) (06/16/89)

XBR2D78V%DDATHD21.BITNET@cunyvm.cuny.edu (MATHIAS GAERTNER) writes:
\...
\void traverse(name, func)
\        char *name;
\        int (*func)();
\{
\  struct direct dirbuf;
\  char *nbp;
\  char *nep;
\  int i,fd;

\  nbp=name + strlen(name);
\  *nbp++ = '/';
\  *nbp++ = '\0';
       ^^
Change this line to:

	*nbp = '\0';
-- 
"I HATE arbitrary limits, especially when |Maarten Litmaath @ VU Amsterdam:
   they're small."  (Stephen Savitzky)    |maart@cs.vu.nl, mcvax!botter!maart