platform/unix.c
changeset 39 bc29e1ee7ef6
parent 28 529214f57d1b
child 41 ff60d39d0862
equal deleted inserted replaced
38:feb16343f44c 39:bc29e1ee7ef6
    33 #include <pthread.h>
    33 #include <pthread.h>
    34 #include <unistd.h>
    34 #include <unistd.h>
    35 #include <sys/types.h>
    35 #include <sys/types.h>
    36 #include <pwd.h>
    36 #include <pwd.h>
    37 #include <sys/stat.h>
    37 #include <sys/stat.h>
       
    38 #include <sys/param.h>
    38 #include <dirent.h>
    39 #include <dirent.h>
    39 #include <time.h>
    40 #include <time.h>
    40 #include <errno.h>
    41 #include <errno.h>
    41 
    42 
    42 
    43 
   301     {
   302     {
   302         ent = readdir(dir);
   303         ent = readdir(dir);
   303         if (ent == NULL)   /* we're done. */
   304         if (ent == NULL)   /* we're done. */
   304             break;
   305             break;
   305 
   306 
       
   307         if (strcmp(ent->d_name, ".") == 0)
       
   308             continue;
       
   309 
       
   310         if (strcmp(ent->d_name, "..") == 0)
       
   311             continue;
       
   312 
   306         l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
   313         l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
   307         if (l != NULL)
   314         if (l == NULL)
   308             break;
   315             break;
   309 
   316 
   310         l->str = (char *) malloc(strlen(ent->d_name) + 1);
   317         l->str = (char *) malloc(strlen(ent->d_name) + 1);
   311         if (l->str == NULL)
   318         if (l->str == NULL)
   312         {
   319         {
   313             free(l);
   320             free(l);
   314             break;
   321             break;
   315         } /* if */
   322         } /* if */
   316 
   323 
       
   324         strcpy(l->str, ent->d_name);
       
   325 
   317         if (retval == NULL)
   326         if (retval == NULL)
   318             retval = l;
   327             retval = l;
   319         else
   328         else
   320             prev->next = l;
   329             prev->next = l;
   321 
   330 
   335     BAIL_IF_MACRO(fstat(fileno(handle), &statbuf) == -1, strerror(errno), -1);
   344     BAIL_IF_MACRO(fstat(fileno(handle), &statbuf) == -1, strerror(errno), -1);
   336     return(statbuf.st_size);
   345     return(statbuf.st_size);
   337 } /* __PHYSFS_platformFileLength */
   346 } /* __PHYSFS_platformFileLength */
   338 
   347 
   339 
   348 
       
   349 char *__PHYSFS_platformCurrentDir(void)
       
   350 {
       
   351     int allocSize = 0;
       
   352     char *retval = NULL;
       
   353     char *ptr;
       
   354 
       
   355     do
       
   356     {
       
   357         allocSize += 100;
       
   358         ptr = (char *) realloc(retval, allocSize);
       
   359         if (ptr == NULL)
       
   360         {
       
   361             if (retval != NULL)
       
   362                 free(retval);
       
   363             BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
       
   364         } /* if */
       
   365 
       
   366         retval = ptr;
       
   367         ptr = getcwd(retval, allocSize);
       
   368     } while (ptr == NULL);
       
   369 
       
   370     return(retval);
       
   371 } /* __PHYSFS_platformCurrentDir */
       
   372 
       
   373 
       
   374 char *__PHYSFS_platformRealPath(const char *path)
       
   375 {
       
   376     char resolved_path[MAXPATHLEN];
       
   377     char *retval = NULL;
       
   378 
       
   379     errno = 0;
       
   380     BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
       
   381     retval = malloc(strlen(resolved_path) + 1);
       
   382     BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
       
   383     strcpy(retval, resolved_path);
       
   384     return(retval);
       
   385 } /* __PHYSFS_platformRealPath */
       
   386 
   340 /* end of unix.c ... */
   387 /* end of unix.c ... */
   341 
   388