Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[C] Since when do I have files of an unknown type??
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
incubator
Guru
Guru


Joined: 05 Jun 2003
Posts: 584
Location: Belgium

PostPosted: Tue Sep 16, 2003 11:26 pm    Post subject: [C] Since when do I have files of an unknown type?? Reply with quote

I have been trying to create something that deletes a directory and everything in it (as long as you have the permissions for it of course) but I am trying to detect wich element of a directory is a subdirectory or a regular file.
I have tried either by checking the d_type member variable of the dirent struct and check if it is equal to DT_DIR or DT_REG and I have also tried to use the S_ISDIR() macro that requires a stat struct. The first returns true on DT_UNKNOWN and the latter method returns -1 when executing the stat function.
Here is my code, (I cleaned up a ton of debug info for easier reading)

Code:

#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

void ClearDir(char* name);

int main(int argc, char* argv[])
{
    if (argc == 2) {
        char* dirname = argv[1];
        ClearDir(dirname);

    } else {
        printf("Usage: killdir <dirname>\n");
        printf("removes directory located in current working directory");
        exit(1);
    }

    return 0;
}

void ClearDir(char* name) {
    DIR* pDir = NULL;
    struct dirent* dirent_dir;

    char path[256];
   pDir = opendir(name);
    switch (errno) {
        case ENOENT :
            printf("Directory %s not found\n", name);
            exit(1);
        case ENOMEM :
            printf("Not enough memory");
            exit(1);
    }
   // while the read element of dirname is not NULL
    while ((dirent_dir = readdir(pDir)) != NULL) {
        if (strcmp(dirent_dir->d_name, ".") && strcmp(dirent_dir->d_name, "..")) {
            sprintf(path, "%s/%s", name, dirent_dir->d_name);
   
            if (dirent_dir->d_type == DT_DIR) {   // if dir, append dirname to path so this can be used for recursive function
      ClearDir(path);
       } else {
      unlink(path);
      switch (errno) {
         case EACCES :
            printf("Acces denied for file %s", path);
            exit(1);
         case ENOENT :
            printf("There is no such file or directory %s", path);
            exit(1);
         default: break;
      }  // end switch
       } // end if
        } // end if

    } // end while

    closedir(pDir);
    rmdir(name); // remove the dir you just emptied
    if (errno == ENOTEMPTY) {
   printf("directory %s is not empty", name);
   exit(1);
    }
}


has anyone else experienced problems with this? Or is there a fix for it?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum