TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Grr.. silly C programs



I figured I'd toss this one out to you guys before running to the
comp.sci.* newsgroups..  I'm working on a simple little GTK filebrowser,
in case you haven't heard before.  Unfortunately, there are some teensy
problems (probably more due to the fact that I don't know C very well
yet than anything else..)

Anyway, enough with the intro, here's my problem..

It seems that the Heisenberg uncertainty principle applies to computer
science..
(or maybe that's Schroedinger's cat...)

When I'm looking at the names of files in directories, none (or almost
none) of the resulting files end up being identified as directories!

I have the following code in my program:


int make_tree (char *dir, GtkWidget *tree)
{
  DIR *dp, *tempdir;
  struct dirent *entry;
  struct stat statbuf;
  GtkWidget *treeitem, *subtree;
  gchar *name;

  if ((dp = opendir ("/")) == NULL) {
    printf ("cannot open directory: %s\n", dir);
    return 1;
  }

  while ((entry = readdir (dp)) != NULL) {
    stat (entry->d_name, &statbuf);
    if ( (strncmp (".", entry->d_name, 1) == 0)) {    
      printf ("omit dir %s\n", entry->d_name);     
    } else {
      printf ("include dir %s\n", entry->d_name);
      if (S_ISDIR (statbuf.st_mode)) {
        printf ("Is dir!\n");
        /* found a directory, adding to tree... */
        treeitem = gtk_tree_item_new_with_label (entry->d_name);
        gtk_tree_append (GTK_TREE (tree), treeitem);
        gtk_widget_show (treeitem);
      }
    }  
  }  
  closedir (dp);
}


...which, to my great surprise, doesn't work..

You may notice that it's basically just a generic directory scanner with
a few extra debugging printf's and some GTK widget manipulation
functions...

It's supposed to omit the directories named "." and ".." (especially
since they mean very little when looking at the root directory!) while
still creating a list of the regular directories.  

On my system, the function outputs the following to stdout:

omit dir .
omit dir ..
include dir lost+found
Is dir!
include dir home
include dir usr
include dir dev
include dir etc
include dir tmp
include dir var
include dir bin
include dir boot
include dir lib
include dir mnt
include dir proc
include dir root
include dir sbin

which means that only the directory 'lost+found' filters through.  But
why don't any of the other directories make it?

I imagine that this explanation isn't that great, so if you want to try
picking at an actual file, try getting
http://www.tc.umn.edu/~hick0088/files/gtkbrowser/gtkbrowser-19981030.tar.gz
(a whopping 2 kilobytes!)

The function is in make_tree.c

Thanks for any help you guys can offer...
-- 
                    --== Mike Hicks ==--
http://umn.edu/~hick0088	mailto:hick0088@tc.umn.edu
                        ICQ:6883760
Linux User Since 1.2.13            Current Kernel: 2.1.125