[LUNI] opening directory

Koji Yabumoto koji at yabumoto.com
Thu Aug 1 13:01:01 CDT 2002


Hi,
I'm new to Linux and system programming and thus need a little help.  I'm 
using C to do all of this by the way.  To get practice, I've been trying to 
write a watered down version of the ls command. ( just liist files of 
directories and such)  But to get a feel for doing basic things in the 
system, I wrote a little hex dump program:

int main (int argc, char *argv[]){
   int fd;
   int i,j;
   int offset;    // Distance from first byte of file
   int actual;
   int ch;
   char buff[16];

   if (argc <2){
     printf("Need file name\n");
     return 1;
   }

   fd = open(argv[1], O_RDONLY,0);
   if (fd < 0){
     printf("File open error\n");
     return 1;
   }

   for (i = 0;; i += actual, offset += actual){
     actual = read(fd,buff,16);
      /*       printf("actual = %d    errno = %d\n",actual, errno);  */
     if (actual <1)
       break;
     printf("%8x: ", offset);

     for (j = 0;j <actual; ++j){
       printf("%02x ",buff[j]);
     }

     while (++j < 17)
       printf("   ");
     printf("   ");

     for (j = 0; j<actual; ++j){
       ch = buff[j];
       if (ch <32)
         ch = '.';
       printf("%c", ch);

     }
     printf("\n");

   }
   close (fd);
   return 0;
}

This program works great for files.  The problem is when I give it a 
directory, nothing comes out.  I printed out errno variable to see if 
anything was going wrong and found it to give me a 21.  To be honest,  I 
really don't know what the hell that means.  I thought I would be able to 
treat directories the same as files but apparently not. I was wondering how 
I could get directories to work the same way.

Thanks for your time,
Koji Yabumoto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://luni.org/pipermail/luni/attachments/20020801/713f4adb/attachment.html 


More information about the luni mailing list