pike.git / src / modules / _Stdio / efuns.c

version» Context lines:

pike.git/src/modules/_Stdio/efuns.c:1080:      /*! @decl array(string) get_dir(void|string dirname)    *!    *! Returns an array of all filenames in the directory @[dirname], or    *! @expr{0@} (zero) if the directory does not exist. When no    *! @[dirname] is given, current work directory is used.    *!    *! @seealso    *! @[mkdir()], @[cd()]    */ + #ifdef __NT__   void f_get_dir(INT32 args)   { - #ifdef __NT__ +     HANDLE dir;    WIN32_FIND_DATAW d;    struct string_builder sb; - #else /* !__NT__ */ - #ifdef USE_FDOPENDIR -  int dir_fd; - #endif -  DIR *dir = NULL; - #ifdef HAVE_READDIR_R -  ptrdiff_t name_max = -1; - #endif - #endif /* __NT__ */ +     struct pike_string *str=0;       VALID_FILE_IO("get_dir","read");    - #ifdef __NT__ +     get_all_args("get_dir",args,".%T",&str); - #else /* !__NT__ */ -  get_all_args("get_dir",args,".%N",&str); - #endif /* __NT__ */ +        if(!str) { - #if defined(__amigaos4__) -  push_empty_string(); - #else +     push_constant_text("."); - #endif +     str = Pike_sp[-1].u.string;    args++;    }    - #ifdef __NT__ -  +     if (str->size_shift == 2) {    /* Filenames that are too wide are not supported. */    errno = ENOENT;    pop_n_elems(args);    push_int(0);    return;    }       init_string_builder_alloc(&sb, str->len+2, 1);   
pike.git/src/modules/_Stdio/efuns.c:1215: Inside #if defined(__NT__)
      stack_pop_n_elems_keep_top(args);       if (err != ERROR_SUCCESS && err != ERROR_NO_MORE_FILES) {    set_errno_from_win32_error (err);    pop_stack();    push_int (0);    return;    }    } + }      #else /* !__NT__ */    -  if (string_has_null(str)) { -  /* Filenames with NUL are not supported. */ -  errno = ENOENT; -  pop_n_elems(args); -  push_int(0); -  return; -  } -  -  THREADS_ALLOW_UID(); - #ifdef USE_FDOPENDIR -  dir_fd = open(str->str, O_RDONLY); -  if (dir_fd != -1) { - #ifdef USE_FPATHCONF -  name_max = fpathconf(dir_fd, _PC_NAME_MAX); - #endif /* USE_FPATHCONF */ -  dir = fdopendir(dir_fd); -  if (!dir) close(dir_fd); -  } - #else -  dir = opendir(str->str); - #ifdef USE_FPATHCONF -  if (dir) { -  name_max = fpathconf(dirfd(dir), _PC_NAME_MAX); -  } - #endif - #endif /* !HAVE_FDOPENDIR */ - #ifdef USE_PATHCONF -  name_max = pathconf(str->str, _PC_NAME_MAX); - #endif -  THREADS_DISALLOW_UID(); -  if(dir) + static void low_get_dir(DIR *dir, ptrdiff_t name_max)   { -  +  if(dir) {    struct dirent *d;    struct dirent *tmp = NULL;   #if defined(_REENTRANT) && defined(HAVE_READDIR_R)   #define FPR 1024    char buffer[MAXPATHLEN * 4];    char *ptrs[FPR];    ptrdiff_t lens[FPR];      #ifndef NAME_MAX   #define NAME_MAX 1024
pike.git/src/modules/_Stdio/efuns.c:1325: Inside #if defined(_REENTRANT) && defined(HAVE_READDIR_R)
   d = tmp;    }   #elif defined(HAVE_POSIX_READDIR_R)    /* POSIX readdir_r returns 0 on success, and ERRNO on failure.    * at end of dir it sets the third arg to NULL.    */    d = NULL;    errno = 0;    if ((err = readdir_r(dir, tmp, &d)) || !d) {   #ifdef READDIR_DEBUG -  fprintf(stderr, "POSIX readdir_r(\"%s\") => err %d\n", -  str->str, err); -  fprintf(stderr, "POSIX readdir_r(), d= 0x%08x\n", -  (unsigned int)d); +  fprintf(stderr, "POSIX readdir_r() => err %d\n", err); +  fprintf(stderr, "POSIX readdir_r(), d= 0x%08x\n", (unsigned int)d);   #endif /* READDIR_DEBUG */    if (err == -1) {    /* Solaris readdir_r returns -1, and sets errno. */    err = errno;    }   #ifdef READDIR_DEBUG -  fprintf(stderr, "POSIX readdir_r(\"%s\") => errno %d\n", -  str->str, err); +  fprintf(stderr, "POSIX readdir_r() => errno %d\n", err);   #endif /* READDIR_DEBUG */    /* Solaris readdir_r seems to set errno to ENOENT sometimes.    *    * AIX readdir_r seems to set errno to EBADF at end of dir.    */    if ((err == ENOENT) || (err == EBADF)) {    err = 0;    }    break;    }   #ifdef READDIR_DEBUG -  fprintf(stderr, "POSIX readdir_r(\"%s\") => \"%s\"\n", -  str->str, d->d_name); +  fprintf(stderr, "POSIX readdir_r() => \"%s\"\n", d->d_name);   #endif /* READDIR_DEBUG */   #else   #error Unknown readdir_r variant   #endif    /* Filter "." and ".." from the list. */    if(d->d_name[0]=='.')    {    if(NAMLEN(d)==1) continue;    if(d->d_name[1]=='.' && NAMLEN(d)==2) continue;    }
pike.git/src/modules/_Stdio/efuns.c:1371:    lens[num_files]=NAMLEN(d);    if(bufptr+lens[num_files] >= buffer+sizeof(buffer)) break;    MEMCPY(bufptr, d->d_name, lens[num_files]);    ptrs[num_files]=bufptr;    bufptr+=lens[num_files];    num_files++;    }    THREADS_DISALLOW();    if ((!d) && err) {    free(tmp); -  Pike_error("get_dir(): readdir_r(\"%S\") failed: %d\n", str, err); +  Pike_error("get_dir(): readdir_r() failed: %d\n", err);    }   #ifdef READDIR_DEBUG    fprintf(stderr, "Pushing %d filenames...\n", num_files);   #endif /* READDIR_DEBUG */    for(e=0;e<num_files;e++)    {    push_string(make_shared_binary_string(ptrs[e],lens[e]));    }    if(d)    push_string(make_shared_binary_string(d->d_name,NAMLEN(d)));    else    break;    DO_AGGREGATE_ARRAY(120);    }       free(tmp);   #else    for(d=readdir(dir); d; d=readdir(dir))    { -  + #ifdef READDIR_DEBUG +  fprintf(stderr, "readdir(): %s\n", d->d_name); + #endif /* READDIR_DEBUG */    /* Filter "." and ".." from the list. */    if(d->d_name[0]=='.')    {    if(NAMLEN(d)==1) continue;    if(d->d_name[1]=='.' && NAMLEN(d)==2) continue;    }    push_string(make_shared_binary_string(d->d_name, NAMLEN(d)));       DO_AGGREGATE_ARRAY(120);    }   #endif    closedir(dir);       END_AGGREGATE_ARRAY; -  stack_pop_n_elems_keep_top(args); +     } else { -  +  push_int(0); +  } + } +  + void f_get_dir(INT32 args) + { + #ifdef USE_FDOPENDIR +  int dir_fd; + #endif +  DIR *dir = NULL; + #ifdef HAVE_READDIR_R +  ptrdiff_t name_max = -1; + #endif +  struct pike_string *str=0; +  +  VALID_FILE_IO("get_dir","read"); +  +  get_all_args("get_dir",args,".%N",&str); +  +  if(!str) { + #if defined(__amigaos4__) +  push_empty_string(); + #else +  push_constant_text("."); + #endif +  str = Pike_sp[-1].u.string; +  args++; +  } +  +  if (string_has_null(str)) { +  /* Filenames with NUL are not supported. */ +  errno = ENOENT;    pop_n_elems(args);    push_int(0); -  +  return;    } - #endif /* __NT__ */ +  +  THREADS_ALLOW_UID(); + #ifdef USE_FDOPENDIR +  dir_fd = open(str->str, O_RDONLY); +  if (dir_fd != -1) { + #ifdef USE_FPATHCONF +  name_max = fpathconf(dir_fd, _PC_NAME_MAX); + #endif /* USE_FPATHCONF */ +  dir = fdopendir(dir_fd); +  if (!dir) close(dir_fd);    } -  + #else +  dir = opendir(str->str); + #ifdef USE_FPATHCONF +  if (dir) { +  name_max = fpathconf(dirfd(dir), _PC_NAME_MAX); +  } + #endif + #endif /* !HAVE_FDOPENDIR */ + #ifdef USE_PATHCONF +  name_max = pathconf(str->str, _PC_NAME_MAX); + #endif    -  +  THREADS_DISALLOW_UID(); +  +  low_get_dir(dir, name_max); +  stack_pop_n_elems_keep_top(args); + } + #endif /* __NT__ */ +    /*! @decl int cd(string s)    *!    *! Change the current directory for the whole Pike process.    *!    *! @returns    *! Returns @expr{1@} for success, @expr{0@} (zero) otherwise.    *!    *! @seealso    *! @[getcwd()]    */