psmile_io_scandir.c

Go to the documentation of this file.
00001 #define __SYSV
00002 #include <sys/types.h>
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <string.h>
00006 
00007 #if defined(__SYSV) || defined(__Linux)
00008 #include <dirent.h>
00009 #else
00010 #include <sys/dir.h>
00011 #endif
00012 #include "PSMILe_f2c.h"
00013 
00014 #if defined (__MYALPHASORT)
00015 
00016 #define __MYALPHASORT myalphasort
00017 
00018 #if defined(__SYSV) || defined(__Linux)
00019 int myalphasort(struct dirent **d1, struct dirent **d2)
00020 {
00021         return(strcmp((*d1)->d_name, (*d2)->d_name));
00022 }
00023 #else
00024 int myalphasort(struct direct **d1, struct direct **d2)
00025 {
00026         return(strcmp((*d1)->d_name, (*d2)->d_name));
00027 }
00028 #endif
00029 
00030 #else
00031 
00032 #define __MYALPHASORT alphasort
00033 
00034 #endif
00035 
00036 /* ------------------------------------------------------------------- */
00037 /* Copyright 2006-2010, SGI Germany, Munich, Germany. */
00038 /* All rights reserved. Use is subject to OASIS4 license terms. */
00039 /*--------------------------------------------------------------------
00040 //BOP
00041 !  !ROUTINE: __psmile_io_scandir
00042 
00043    !INTERFACE:
00044 */
00045        void __psmile_io_scandir(char *basename
00046                           , int len_of_basename
00047                           , char *file_list
00048                           , int len_of_list_entry
00049                           , int len_of_list
00050                           , int *no_of_files
00051                           , int *ierror){
00052 /*
00053   !LOCAL VARIABLES:
00054 #if defined(__SYSV) || defined(__Linux)
00055            struct dirent **namelist;
00056 #else
00057            struct direct **namelist;
00058 #endif
00059            int n, entry=0;
00060            int offset=len_of_list_entry;
00061            size_t len=len_of_basename;
00062 
00063   !DESCRIPTION:
00064    Routine lists all files of a given basename in the current directory.
00065 
00066   !REVISION HISTORY:
00067 
00068     Date      Programmer   Description
00069   ----------  ----------   -----------
00070   31.10.03    R. Vogelsang created
00071   
00072 //EOP
00073 */
00074            static char mycvs[80]="$Id: psmile_io_scandir.c 2325 2010-04-21 15:00:07Z valcke $";
00075 
00076 #if defined(__SYSV) || defined(__Linux)
00077            struct dirent **namelist;
00078 #else
00079            struct direct **namelist;
00080 #endif
00081            int n, entry=0;
00082            int offset=len_of_list_entry;
00083            size_t len=len_of_basename;
00084 
00085            printf("%s %d %d %d \n",basename ,len_of_basename,len_of_list_entry,len_of_list);
00086            
00087 
00088 #if defined(__SYSV) || defined(__Linux)
00089            n = scandir(".", &namelist, 0, __MYALPHASORT);
00090 #else
00091            n = scandir(".", &namelist, 0, __MYALPHASORT);
00092 #endif
00093            if (n < 0) {
00094                perror("psmile_io_scandir: The directory contains no files!");
00095                *ierror=999;
00096            } else {
00097                while(n--) {
00098                    if( ! strncmp(basename,namelist[n]->d_name,len)){
00099 /*          printf("%s\n", namelist[n]->d_name);*/
00100                         if( entry < len_of_list ) {
00101                           strncpy(file_list + entry * offset
00102                                  ,namelist[n]->d_name
00103                                  ,strlen(namelist[n]->d_name));
00104                         }
00105                         entry++;
00106                     }       
00107                    free(namelist[n]);
00108                }
00109                free(namelist);
00110            }
00111            *no_of_files=entry;
00112        }
00113        void __psmile_io_scandir_no_of_files(char *basename
00114                           , int len_of_basename
00115                           , int *no_of_files
00116                           , int *ierror){
00117 #if defined(__SYSV) || defined(__Linux)
00118            struct dirent **namelist;
00119 #else
00120            struct direct **namelist;
00121 #endif
00122            int n, entry=0;
00123            size_t len=len_of_basename;
00124            printf("%s %d \n",basename ,len_of_basename);
00125            
00126 
00127 #if defined(__SYSV) || defined(__Linux)
00128            n = scandir(".", &namelist, 0, __MYALPHASORT);
00129 #else
00130            n = scandir(".", &namelist, 0, __MYALPHASORT);
00131 #endif
00132            if (n < 0) {
00133                perror("psmile_io_scandir: The directory contains no files!");
00134                *ierror=999;
00135            } else {
00136                while(n--) {
00137                    if( ! strncmp(basename,namelist[n]->d_name,len)){
00138 /*          printf("%s\n", namelist[n]->d_name);*/
00139                         entry++;
00140                     }       
00141                    free(namelist[n]);
00142                }
00143                free(namelist);
00144            }
00145            *no_of_files=entry;
00146        }
00147 
00148 /* Fortran interface */
00149        void psmile_io_scandir(char *basename
00150                            ,char *file_list
00151                            ,int *len_of_list
00152                            ,int *no_of_files
00153                            ,int *ierror
00154                            ,int len_of_basename
00155                            ,int len_of_list_entry){
00156 
00157             __psmile_io_scandir( basename
00158                               , len_of_basename
00159                               , file_list
00160                   , len_of_list_entry
00161                   ,*len_of_list
00162                   , no_of_files
00163                   , ierror);
00164 
00165        }
00166        void psmile_io_scandir_no_of_files(char *basename
00167                            ,int *no_of_files
00168                            ,int *ierror
00169                            ,int len_of_basename){
00170 
00171             __psmile_io_scandir_no_of_files( basename
00172                               , len_of_basename
00173                   , no_of_files
00174                   , ierror);
00175 
00176        }
00177 /*
00178        main(){
00179        char basename[]="foo";
00180        int  lenb=3;
00181        int  lenf=100;
00182        int  lene=64;
00183        int no;
00184        int error;
00185        int i;
00186 
00187        char *fl;
00188 
00189        fl=(char*)malloc(lene*lenf*sizeof(char));
00190        
00191        psmile_io_scandir(basename,lenb,fl,lene,lenf,&no,&error);
00192 
00193        for(i=0;i< no;i++)
00194        printf("%s\n",fl+i*lene);
00195        
00196        
00197        }
00198 */

Generated on 18 Mar 2011 for Oasis4 by  doxygen 1.6.1