platform/unix.c
changeset 41 ff60d39d0862
parent 39 bc29e1ee7ef6
child 63 cb9a4714b844
equal deleted inserted replaced
40:8cc16df4bada 41:ff60d39d0862
   284     napTime.tv_nsec = 10 * 1000 * 1000;  /* specified in nanoseconds. */
   284     napTime.tv_nsec = 10 * 1000 * 1000;  /* specified in nanoseconds. */
   285     nanosleep(&napTime, NULL);           /* don't care if it fails. */
   285     nanosleep(&napTime, NULL);           /* don't care if it fails. */
   286 } /* __PHYSFS_platformTimeslice */
   286 } /* __PHYSFS_platformTimeslice */
   287 
   287 
   288 
   288 
   289 LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname)
   289 LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
       
   290                                                   int omitSymLinks)
   290 {
   291 {
   291     LinkedStringList *retval = NULL;
   292     LinkedStringList *retval = NULL;
   292     LinkedStringList *l = NULL;
   293     LinkedStringList *l = NULL;
   293     LinkedStringList *prev = NULL;
   294     LinkedStringList *prev = NULL;
   294     DIR *dir;
   295     DIR *dir;
   295     struct dirent *ent;
   296     struct dirent *ent;
       
   297     int bufsize = 0;
       
   298     char *buf = NULL;
       
   299     int dlen = 0;
       
   300 
       
   301     if (omitSymLinks)
       
   302     {
       
   303         dlen = strlen(dirname);
       
   304         bufsize = dlen + 256;
       
   305         buf = malloc(bufsize);
       
   306         BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
       
   307         strcpy(buf, dirname);
       
   308         if (buf[dlen - 1] != '/')
       
   309         {
       
   310             buf[dlen++] = '/';
       
   311             buf[dlen] = '\0';
       
   312         } /* if */
       
   313     } /* if */
   296 
   314 
   297     errno = 0;
   315     errno = 0;
   298     dir = opendir(dirname);
   316     dir = opendir(dirname);
   299     BAIL_IF_MACRO(dir == NULL, strerror(errno), NULL);
   317     if (dir == NULL)
       
   318     {
       
   319         if (buf != NULL)
       
   320             free(buf);
       
   321         BAIL_IF_MACRO(1, strerror(errno), NULL);
       
   322     } /* if */
   300 
   323 
   301     while (1)
   324     while (1)
   302     {
   325     {
   303         ent = readdir(dir);
   326         ent = readdir(dir);
   304         if (ent == NULL)   /* we're done. */
   327         if (ent == NULL)   /* we're done. */
   307         if (strcmp(ent->d_name, ".") == 0)
   330         if (strcmp(ent->d_name, ".") == 0)
   308             continue;
   331             continue;
   309 
   332 
   310         if (strcmp(ent->d_name, "..") == 0)
   333         if (strcmp(ent->d_name, "..") == 0)
   311             continue;
   334             continue;
       
   335 
       
   336         if (omitSymLinks)
       
   337         {
       
   338             char *p;
       
   339             int len = strlen(ent->d_name) + dlen + 1;
       
   340             if (len > bufsize)
       
   341             {
       
   342                 p = realloc(buf, len);
       
   343                 if (p == NULL)
       
   344                     continue;
       
   345                 buf = p;
       
   346                 bufsize = len;
       
   347             } /* if */
       
   348 
       
   349             strcpy(buf + dlen, ent->d_name);
       
   350             if (__PHYSFS_platformIsSymLink(buf))
       
   351                 continue;
       
   352         } /* if */
   312 
   353 
   313         l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
   354         l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
   314         if (l == NULL)
   355         if (l == NULL)
   315             break;
   356             break;
   316 
   357 
   331         prev = l;
   372         prev = l;
   332         l->next = NULL;
   373         l->next = NULL;
   333     } /* while */
   374     } /* while */
   334 
   375 
   335     closedir(dir);
   376     closedir(dir);
       
   377     free(buf);
   336     return(retval);
   378     return(retval);
   337 } /* __PHYSFS_platformEnumerateFiles */
   379 } /* __PHYSFS_platformEnumerateFiles */
   338 
   380 
   339 
   381 
   340 int __PHYSFS_platformFileLength(FILE *handle)
   382 int __PHYSFS_platformFileLength(FILE *handle)