char *stringNDup(const char *str, unsigned long n) { char *tempString = NULL; if (!str) return NULL; tempString = memMgrChunkNew(n + 1); if (!tempString) return NULL; stringNCopy(tempString, str, n); tempString[n] = '\0'; return tempString; }
/* * readdir * * Return a pointer to a dirent structure filled with the information on the * next entry in the directory. */ struct dirent * readdir (DIR * dirp) { errno = 0; /* Check for valid DIR struct. */ if (!dirp) { errno = EFAULT; return NULL; } if (dirp->dd_dir.d_name != dirp->dd_dta.name) { /* The structure does not seem to be set up correctly. */ errno = EINVAL; return NULL; } if (dirp->dd_stat < 0) { /* We have already returned all files in the directory * (or the structure has an invalid dd_stat). */ return NULL; } else if (dirp->dd_stat == 0) { /* We haven't started the search yet. */ /* Start the search */ dirp->dd_handle = CmdFindFirst (dirp->dd_name, &(dirp->dd_dta)); if (dirp->dd_handle == -1) { /* Whoops! Seems there are no files in that * directory. */ dirp->dd_stat = -1; } else { dirp->dd_stat = 1; } } else { /* Get the next search entry. */ if (CmdFindNext (dirp->dd_handle, &(dirp->dd_dta))) { /* We are off the end or otherwise error. */ _findclose (dirp->dd_handle); dirp->dd_handle = -1; dirp->dd_stat = -1; } else { /* Update the status to indicate the correct * number. */ dirp->dd_stat++; } } if (dirp->dd_stat > 0) { /* Successfully got an entry. Everything about the file is * already appropriately filled in except the length of the * file name. */ dirp->dd_dir.d_namlen = stringLength (dirp->dd_dir.d_name); if ( dirp->dd_dir.d_name[0] == '.' && (dirp->dd_dir.d_name[1] == 0 || (dirp->dd_dir.d_name[1] == '.' && dirp->dd_dir.d_name[2] == 0))) return readdir(dirp); struct _stat buf; char_t buffer[CL_MAX_DIR]; int bl = stringLength(dirp->dd_name)-stringLength(DIRENT_SEARCH_SUFFIX); stringNCopy(buffer,dirp->dd_name,bl); buffer[bl]=0; stringCat(buffer, dirp->dd_dir.d_name); if ( Cmd_Stat(buffer,&buf) == -1 ) return readdir(dirp); return &dirp->dd_dir; } return NULL; }