posix.listdir() and '.'&'..'

lance@fox.com
Thu, 14 Jul 94 15:57:39 PDT

I am the only one that has to code around posix.listdir()
returning '.' and '..' over and over and over again?

Well not anymore!

here are my diffs to Lib/packmail.py (where i saw this was needed)
and Modules/posixmodule.c. Basicly the changes to posixmodule.c
are very minimal.. what happens is this..
listdir() now can take an optional second parameter.
This optional second parameter is one of the following:

0 : Do NOT return '.' and '..' in the list.
anything else: Do return '.' and '..' in the list..
The default is still the same.. return them..

So here are the diffs...

===================================================================
RCS file: /u/lance/CVS/python/Lib/packmail.py,v
retrieving revision 1.1
diff -b -c -r1.1 packmail.py
*** 1.1 1994/07/11 15:04:14
--- Lib/packmail.py 1994/07/14 22:48:37
***************
*** 40,52 ****

# Pack all files from a directory
def packall(outfp, dirname):
! names = os.listdir(dirname)
names.sort()
packsome(outfp, dirname, names)

# Pack all files from a directory that are not older than a give one
def packnotolder(outfp, dirname, oldest):
! names = os.listdir(dirname)
oldest = os.path.join(dirname, oldest)
st = os.stat(oldest)
mtime = st[ST_MTIME]
--- 40,52 ----

# Pack all files from a directory
def packall(outfp, dirname):
! names = os.listdir(dirname,0)
names.sort()
packsome(outfp, dirname, names)

# Pack all files from a directory that are not older than a give one
def packnotolder(outfp, dirname, oldest):
! names = os.listdir(dirname,0)
oldest = os.path.join(dirname, oldest)
st = os.stat(oldest)
mtime = st[ST_MTIME]
***************
*** 66,72 ****
def packtree(outfp, dirname):
print 'packtree', dirname
outfp.write('mkdir ' + unixfix(dirname) + '\n')
! names = os.listdir(dirname)
subdirs = []
for name in names:
fullname = os.path.join(dirname, name)
--- 66,72 ----
def packtree(outfp, dirname):
print 'packtree', dirname
outfp.write('mkdir ' + unixfix(dirname) + '\n')
! names = os.listdir(dirname,0)
subdirs = []
for name in names:
fullname = os.path.join(dirname, name)
===================================================================
RCS file: /u/lance/CVS/python/Modules/posixmodule.c,v
retrieving revision 1.3
diff -b -c -r1.3 posixmodule.c
*** 1.3 1994/07/11 16:29:19
--- Modules/posixmodule.c 1994/07/14 22:44:46
***************
*** 123,128 ****
--- 123,135 ----
#endif /* NT */


+ #ifndef TRUE
+ #define TRUE 1
+ #endif
+ #ifndef FALSE
+ #define FALSE 0
+ #endif
+
/* Return a dictionary corresponding to the POSIX environment table */

extern char **environ;
***************
*** 327,338 ****
--- 334,347 ----
object *args;
{
char *name;
+ int dotdot = TRUE;
int len;
object *d, *v;
HANDLE hFindFile;
WIN32_FIND_DATA FileData;
char namebuf[MAX_PATH+5];

+ if (!getargs(args, "(s#i);directory, list_dotdot", &name, &len, &dotdot))
if (!getargs(args, "s#", &name, &len))
return NULL;
if (len >= MAX_PATH) {
***************
*** 353,358 ****
--- 362,372 ----
return posix_error();
}
do {
+ /* do we want '.' and '..' reported and is it one of them? */
+ if (dotdot == FALSE &&
+ ((strcmp(ep->d_name, '.')==0) ||
+ (strcmp(ep->d_name, '..')==0)))
+ continue;
v = newstringobject(FileData.cFileName);
if (v == NULL) {
DECREF(d);
***************
*** 382,390 ****
--- 396,407 ----
object *args;
{
char *name;
+ int dotdot = TRUE;
object *d, *v;
DIR *dirp;
struct dirent *ep;
+
+ if (!getargs(args, "(si);directory, list_dotdot", &name, &dotdot))
if (!getargs(args, "s", &name))
return NULL;
BGN_SAVE
***************
*** 398,403 ****
--- 415,425 ----
return NULL;
}
while ((ep = readdir(dirp)) != NULL) {
+ /* do we want '.' and '..' reported and is it one of them? */
+ if (dotdot == FALSE &&
+ ((strcmp(ep->d_name, ".")==0) ||
+ (strcmp(ep->d_name, "..")==0)))
+ continue;
v = newstringobject(ep->d_name);
if (v == NULL) {
DECREF(d);
***************
*** 1383,1388 ****
--- 1405,1415 ----
fatal("can't define nt.environ");
DECREF(v);

+ v = newintobject(0);
+ if (v == NULL || dictinsert(d,"NO_DOTDOT", v) != 0)
+ fatal("can't define nt.NO_DOTDOT");
+ DECREF(v);
+
/* Initialize nt.error exception */
PosixError = newstringobject("nt.error");
if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)
***************
*** 1403,1408 ****
--- 1430,1440 ----
fatal("can't define posix.environ");
DECREF(v);

+ v = newintobject(0);
+ if (v == NULL || dictinsert(d,"NO_DOTDOT", v) != 0)
+ fatal("can't define posix.NO_DOTDOT");
+ DECREF(v);
+
/* Initialize posix.error exception */
PosixError = newstringobject("posix.error");
if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)

--
Lance Ellinghouse                lance@fox.com