Re: A Q for C gurus

pmeric@nospam.vnet.IBM.COM
Thu, 19 Jan 95 20:13:26 EST

From: Peter Meric +61-2-353-3889 DP13
Project Assistant - Application Solutions Consulting
ISSC Australia
Subject: Re: A Q for C gurus

What about the following adjustment.....

Instea of using S_ISDIR(), which may or may not exist on particular
compilers, such as Turbo C, let's do this....

if ((buf.st_mode & S_IFDIR) == S_IFDIR) {
/* is a dir*/
...

This is possibly a little more portable - I couldn't find S_ISDIR()
in Borland C or IBM CSet++.

Also, you may see in the Turbo C examples that the bitwise AND is
performed thus :

if (buf.st_mode & S_IFDIR) {

...but I just happen to like knowing that you are actually getting
your expected result. Some will say "well, when would it ever be a
problem?". Better to always be safe than sorry.

Also, one should note that the precedence of the bitwise AND operator
('&') is LOWER than that of the equality operator ('=='). Thus,

if (buf.st_mode & S_IFDIR == S_IFDIR) {

... which looks reasonable, will produce unexpected results (it may
only give a run-time error, not compile time).

Peter
SYDVM1(PMERIC) PMERIC@nospam.VNET.IBM.COM
*** Forwarding note from SMTP2 --IINUS1 01/20/95 11:51 ***
=========================================================================
Received: from lucy by vnet.IBM.COM (IBM VM SMTP V2R2) with TCP;
Thu, 19 Jan 95 19:51:33 EST
Received: from ftoomsh.socs.uts.EDU.AU by lucy with SMTP id AA18916
(5.67a/IDA-1.4.4 for <pmeric@nospam.socs.uts.edu.au>); Fri, 20 Jan 1995 11:51:31
+1100
Received: (from daemon@nospam.localhost) by ftoomsh.socs.uts.EDU.AU (8.6.9/8.6.9) id
LAA01616 for progsoc-outgoing; Fri, 20 Jan 1995 11:47:24 +1100
Received: from amalfi.trl.OZ.AU (amalfi.trl.OZ.AU [137.147.99.99]) by
ftoomsh.socs.uts.EDU.AU (8.6.9/8.6.9) with ESMTP id LAA01611 for
<progsoc@nospam.ftoomsh.socs.uts.EDU.AU>; Fri, 20 Jan 1995 11:47:12 +1100
Received: from otcgpo.isg.otc.com.au ([134.159.16.100]) by amalfi.trl.OZ.AU
(8.6.9/8.6.9) with SMTP id LAA00127 for <progsoc@nospam.ftoomsh.socs.uts.EDU.AU>;
Fri, 20 Jan 1995 11:38:08 +1100
Received: from nmspad1.pad.otc.com.au by otcgpo.isg.otc.com.au
(4.1/OTC_GPO.2.2)
id AA22569; Fri, 20 Jan 95 00:48:26 GMT
Received: from ra.pad.otc.com.au by nmspad1.pad.otc.com.au (4.1/OTC_Gateway2.0)
id AA21610; Fri, 20 Jan 95 00:47:38 GMT
Received: from bag_end.pad.otc.com.au by ra.pad.otc.com.au (4.1/OTC_Peer_1.7)
id AA19126; Fri, 20 Jan 95 00:47:35 GMT
From: Colin.Panisset@nospam.nms.otc.com.au (Colin Panisset)
Message-Id: <9501200047.AA19126@nospam.ra.pad.otc.com.au>
Subject: Re: A Q for C gurus
To: progsoc@nospam.ftoomsh.socs.uts.edu.au
Date: Fri, 20 Jan 1995 11:47:33 +1100 (EST)
In-Reply-To: <199501200001.LAA01454@nospam.ftoomsh.socs.uts.EDU.AU> from "Anthony
Young" at Jan 20, 95 11:01:05 am
X-Face:
&CLkzYIu_[/8({Bw{"^u!tK@nospam.e08b[vX~l@nospam.3_QWfcQ%tDn[N3Iz{lIyRi^yQEo+KZE|;EGz7

1wKC&fkhTG29rln]x<,s1H}Ul@nospam.RGjI#m_ik*}O([Y||c#C*_)k%FKc*FrpH.Q-\ug%:`ocCQ+xr0NX
rTyMippLo)bD8s8w(os&Pe=p`y*T#.np0&u9s
X-Headline:
Joan Rivers Slaughters Ninety-Five Chinese Physicists in drunken rampage.
X-Mailer: ELM [version 2.4 PL20]
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Length: 1063
Sender: owner-progsoc@nospam.ftoomsh.socs.uts.edu.au
Precedence: bulk
Reply-To: progsoc@nospam.ftoomsh.socs.uts.edu.au

Once upon a time, Anthony Young was heard to say:

}
} Hi guys,
}
} What I'm in the process of doing at the moment, is writing a shell for DOS
} that implements unix style substitution and completion. (Just to see if I
} can :D). I've basically got it working, except for one thing.
}
} I need to be able to search through a directory, and check to see if an
} entry is a subdirectory or not. I'm using Turbo C, and I've gone through all
} the functions I could see in the header files, but couldn't find much to help
} me. Is there someone around who knows of any functions I could use, or who
can
} give me an idea on the logic to use, to check for directory status?
}
} Any help is appreciated.
} Thanx in advance :)
}

Given a file handle [int fh], and a stat buffer [struct stat buf], try

---- cut 8X ----

#include <sys/types.h>
#include <sys/stat.h>

...

if ((rtn = fstat(fh, &buf)) < 0) {
perror("fstat");
} else {
if (S_ISDIR(buf.st_mode)) {
/* is a directory */
} else {
/* isn't a directory */
}
}

---- cut 8X ----

-- Colin.