Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

dir.c

Go to the documentation of this file.
00001 /*! \file dir.c
00002     This file contains various utilities to manipulate directory.
00003  */
00004 
00005 #include <stdio.h>
00006 #include "my_dir.h"
00007 
00008 
00009 /*! \brief List the content of a directory (compatible SUN/LINUX).
00010     \param dir_name Pointer to a zero terminated string of characters that represents the name of the directory to list.
00011     \param flist Pointer to a 'file_list' structure that will be used to store directory's content.
00012     \return If the function succed, it returns a positive number that represents the number of entries in the directory.
00013             Oterwise, the function returns -1.
00014     \warning Some fields in the structure 'flist' has been allocated within the function. You must call free_file_list() 
00015              of 'flist' to free all the allocated memory.
00016  */
00017 
00018 int read_directory (char *dir_name, struct file_list *flist)
00019 {
00020   DIR           *dirp;
00021   struct dirent *dp;
00022   int           nb_file, total_file, fsize;
00023 
00024   flist->list        = NULL;
00025   flist->total_alloc = 0;
00026   flist->num         = 0;
00027 
00028   dirp = opendir(dir_name);
00029   if (dirp == NULL) { return -1; }
00030 
00031   total_file = FILE_NUMBER;
00032   flist->list = (char**)malloc(total_file*sizeof(char*));
00033   if (flist->list == NULL)
00034   {
00035     closedir(dirp);
00036     return -1;
00037   }
00038   flist->total_alloc = total_file;
00039 
00040 
00041   nb_file = 0;
00042   while ((dp = readdir(dirp)) != NULL)
00043   {
00044     if (nb_file >= total_file)
00045     {
00046       total_file *= 2;
00047       flist->list = (char**)realloc(flist->list, total_file*sizeof(char*));
00048       if (flist->list == NULL)
00049       {
00050         closedir(dirp);
00051         return -1;
00052       }
00053       flist->total_alloc = total_file;
00054     }
00055   
00056     fsize = strlen(dp->d_name) + 1 + strlen(dir_name); 
00057     (flist->list)[nb_file] = (char*)malloc((fsize+1)*sizeof(char));
00058     if ((flist->list)[nb_file] == NULL)
00059     {
00060       closedir(dirp);
00061       return -1;
00062     }
00063 
00064     sprintf ((flist->list)[nb_file], "%s/%s", dir_name, dp->d_name);
00065     nb_file++;
00066     flist->num = nb_file;
00067   }
00068 
00069   closedir(dirp);
00070   return nb_file; 
00071 }
00072 
00073 /*! \example test_dir.c
00074     this file shows how to use read_directory() and free_file_list().
00075  */
00076 
00077 /*! \brief List the content of a directory, and keeps only the directories.
00078     \param dir_name Pointer to a zero terminated string of characters that represents the name of the directory
00079            to list.
00080     \param flist_out Pointer to a 'file_list' structure that will be used to store directory's content.
00081     \return If the function succed, it returns a positive number that represents the number of entries in the
00082             directory. Oterwise, the function returns -1.
00083     \warning Some fields in the structure 'flist' has been allocated within the function. You must call
00084              free_file_list() of 'flist' to free all the allocated memory.
00085  */
00086 
00087 int read_directory_only (char *dir_name, struct file_list *flist_out)
00088 {
00089   int              nb_files, i, fsize;
00090   struct file_list flist;
00091   struct stat      stat_buf;
00092 
00093   /* ------------------------------------------------------- */
00094   /* Read all the entries of the firectory                   */
00095   /* ------------------------------------------------------- */
00096 
00097   nb_files = read_directory (dir_name, &flist);
00098   if (nb_files == -1) { return -1; }
00099 
00100   /* ------------------------------------------------------- */
00101   /* Allocate memory for 'flist_out'. We know for sure that  */
00102   /* we have no more that 'nb_files' directories.            */
00103   /* ------------------------------------------------------- */
00104 
00105   flist_out->list = (char**)malloc(nb_files*sizeof(char*));
00106   if (flist_out->list == NULL)
00107   {
00108     free_file_list (&flist);
00109     return -1;
00110   }
00111   flist_out->total_alloc = nb_files;
00112 
00113   /* ------------------------------------------------------- */
00114   /* Now look at each entry to keep only the directories     */
00115   /* ------------------------------------------------------- */
00116 
00117   flist_out->num = 0;
00118   for (i=0; i<flist.num; i++)
00119   {
00120     if (stat ((flist.list)[i], &stat_buf) == -1)
00121     {
00122       free_file_list (&flist);
00123       return -1;
00124     }
00125 
00126     if (S_ISDIR(stat_buf.st_mode))
00127     {
00128       fsize = strlen((flist.list)[i]+1);
00129       
00130       (flist_out->list)[flist_out->num] = (char*)malloc((fsize)*sizeof(char));
00131 
00132       if ((flist_out->list)[flist_out->num] == NULL)
00133       {
00134         free_file_list (&flist);
00135         return -1;
00136       }
00137 
00138       strcpy ((flist_out->list)[flist_out->num], (flist.list)[i]); 
00139 
00140       flist_out->num += 1;
00141     }
00142   }
00143 
00144   /* ------------------------------------------------------- */
00145   /* Free all allocated memory                               */ 
00146   /* ------------------------------------------------------- */
00147 
00148   free_file_list (&flist);
00149 
00150   return flist_out->num;
00151 }
00152 
00153 /*! \example test_dir.c
00154     this file shows how to use read_directory() and free_file_list().
00155  */
00156 
00157 /*! \brief Free all allocated memory after you called read_directory().
00158     \param flist Pointer to a structure 'file_list' that should have been filled by read_directory().
00159     \warning ALWAYS call this function after read_directory().
00160  */
00161 
00162 void free_file_list(struct file_list *flist)
00163 {
00164   int i;
00165 
00166   for (i=0; i<flist->num; i++)
00167   { free ((flist->list)[i]); }
00168   if (flist->total_alloc > 0) { free (flist->list); }
00169 }
00170 
00171 /*! \example test_dir.c
00172     this file shows how to use read_directory() and free_file_list().
00173  */
00174 
00175 
00176 /*! \brief Extract the directory base name from a file name.
00177     \param dirname Pointer to a NULL terminated that represents the file name.
00178     \param res Pointer to a pointer that will receive the directory base name.
00179     \return If the function found the directory base name, then it returns BASE_DIR_FOUND and in this case 'res'
00180             contains the directory base name. If the function can not extract the directory base name it returns
00181             BASE_DIR_NOT_FOUND and in this case 'res' is a NULL pointer. If the function returns BASE_DIR_NO_MEM, it
00182             means that the function can not allocate memory (in this case 'res' is NULL).
00183     \warning 'res' has been allocated within the function. You must delete it yoursef (using free()). But before 
00184              you free it, make sure to check that 'res' is not NULL (if (res != NULL) { free (res); }).
00185  */
00186 
00187 int base_dir_name (char *dirname, char **res)
00188 {
00189   char   *c, save;
00190   int    size;
00191 
00192   *res = NULL;
00193   c = rindex (dirname, '/');
00194   if (c == NULL) { return BASE_DIR_NOT_FOUND; }
00195 
00196   save = *c;
00197   *c = 0;
00198 
00199   size = strlen(dirname);
00200   *res = (char*)malloc((size+1)*sizeof(char));
00201   if (*res == NULL)
00202   {
00203     *c = save;
00204     return BASE_DIR_NO_MEM;
00205   }
00206 
00207   strcpy (*res, dirname);
00208   *c = save;
00209 
00210   return BASE_DIR_FOUND;
00211 }
00212 
00213 /*! \example test_basename.c
00214     This file shows how to use the function base_dir_name().
00215  */
00216 

Generated on Thu Apr 3 16:23:43 2003 for Common_C_libraries by doxygen1.3-rc1