GLYLIB  0.3.0b
sslurp_directory_plus.c
Go to the documentation of this file.
00001 /* 
00002 \file slurp_directory.c 
00003 \addtogroup FILE_UTILS
00004 \brief  Read an entire directory into a structure to be processed
00005         later within the calling function.
00006 
00007         Argument: A directory path.
00008 
00009         Returns: Fileslurp structure containing each entry in the
00010                 directory except for "." and "..".
00011 
00012         Date started: 24th February 2010.
00013 
00014 \file glyopendir.c 
00015 \addtogroup FILE_UTILS
00016 \brief  Verifies if the path to a directory structure is valid,  and if the directory itself is a valid directory.
00017 
00018         Argument: A directory path.
00019 
00020         Returns: A pointer to the directory structure of interest. 
00021 
00022         Date started: 7th March 2010.
00023 
00024 
00025 Author: Anita K. Nivedha
00026         minor modifications by BLFoley on 20100225
00027 
00028 */
00029 
00030 #include<mylib.h>
00031 //char glyopendir(char *pathToDir);
00032 //fileslurp slurp_directory(char *pathToDir);
00033 
00034 char glyopendir(char *pathToDir)
00035 {
00036 DIR *__restrict__ dir;
00037 int errsv=0;
00038 dir=opendir(pathToDir);
00039         if(dir==NULL){
00040                 errsv = errno;
00041                 switch(errsv) {
00042                         case EACCES:
00043                                 printf("Search or Read Permission denied.\n");
00044                                 exit(1);
00045                                 break;
00046                         case ELOOP:
00047                                 printf("A loop exists in symbolic links encountered during resolution of the dirname argument.\n");
00048                                 exit(1);
00049                                 break;
00050                         case ENAMETOOLONG:
00051                                 printf("Length of directory name too long.\n");
00052                                 exit(1);
00053                                 break;
00054                         case ENOENT:
00055                                 printf("Invalid directory name in input.\n");
00056                                 exit(1);
00057                                 break;
00058                         case ENOTDIR:
00059                                 printf("A component of the input directory name or path does not exist.\n");
00060                                 exit(1);
00061                                 break;
00062                         case EMFILE:
00063                                 printf("File descriptors are currently open in the calling process.");
00064                                 exit(1);
00065                                 break;
00066                         case ENFILE:
00067                                 printf("Too many files are currently open in the system.\n")
00068                                 exit(1);
00069                                 break;
00070                              }
00071                         }
00072         else
00073                 {
00074                 return dir;
00075                 }       
00076 }
00077 
00078 
00079 fileslurp slurp_directory(char *pathToDir)
00080 {
00081 DIR *__restrict__ dir;
00082 struct dirent *__restrict__ entry;
00083 fileslurp S;
00084 int errsv=0;
00085 dir=glyopendir(pathToDir);  
00086 entry=readdir(dir);     //reading contents of the directory            
00087 S.n=0;
00088 S.L=(char**)calloc(1,sizeof(char*));
00089 while(entry!=NULL)
00090         {
00091         if((strcmp(entry[0].d_name,".")!=0)&&(strcmp(entry[0].d_name,"..")!=0))
00092                 {
00093                 S.n++;
00094                 S.L=(char**)realloc(S.L,S.n*sizeof(char*));
00095                 S.L[S.n-1]=strdup(entry[0].d_name);                     
00096                 }
00097         entry=readdir(dir);
00098         }
00099 return S;
00100 }
00101 
 All Classes Files Functions Variables Typedefs Defines