<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: c code help on determining an existing directory in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213403#M169002</link>
    <description>I'll try to bit a little more clear:&lt;BR /&gt;&lt;BR /&gt;The stat() system call, if sucessful, returns&lt;BR /&gt;a 0 if ok or -1 if not and sets the global variable errno to indicate the error. This is the standard convention for all UNIX system calls. If the result is zero, a data structure containing all the metadata (modification, change, and last access times, owner, group, mode, file length, and others) is filled in and then we need to examine the fields within this structure for more detailed knowledge. Specifically, we need to examine the st_mode field which encodes the file type (what you are interested in), the setuid, setgid, and "sticky" bits, as well as the more familiar permissions for owner, group, and other.&lt;BR /&gt;</description>
    <pubDate>Tue, 09 Mar 2004 14:40:35 GMT</pubDate>
    <dc:creator>A. Clay Stephenson</dc:creator>
    <dc:date>2004-03-09T14:40:35Z</dc:date>
    <item>
      <title>c code help on determining an existing directory</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213395#M168994</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I need to determine if there is directory existed on the system, /a/b/c/d, if yes, I would set the variable xyz="yes", if not, set it to "no".&lt;BR /&gt;&lt;BR /&gt;It has to be achieved by c code. Can anybody please help me the detail code?&lt;BR /&gt;&lt;BR /&gt;Thanks,</description>
      <pubDate>Tue, 09 Mar 2004 09:35:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213395#M168994</guid>
      <dc:creator>Hanry Zhou</dc:creator>
      <dc:date>2004-03-09T09:35:39Z</dc:date>
    </item>
    <item>
      <title>Re: c code help on determining an existing directory</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213396#M168995</link>
      <description>This is actually quite easy. The ket is the stat() system call + some macro's defined in stat.h. Man stat for details.&lt;BR /&gt;&lt;BR /&gt;The isdir function returns 0 if the file/directory exists AND it sets a boolean to a non-zero value if the file is indeed a directory. &lt;BR /&gt;&lt;BR /&gt;-----------------------------------------&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;SYS&gt;&lt;BR /&gt;#include &lt;SYS&gt;&lt;BR /&gt;&lt;BR /&gt;extern int errno;&lt;BR /&gt;&lt;BR /&gt;#define assign_errno(x)  ((errno != 0) ? errno : (x))&lt;BR /&gt;&lt;BR /&gt;#ifndef FALSE&lt;BR /&gt;#define FALSE  (0)&lt;BR /&gt;#endif&lt;BR /&gt;&lt;BR /&gt;int isdir(char *fname, int *is_dir)&lt;BR /&gt;{&lt;BR /&gt;  int cc = 0;&lt;BR /&gt;  struct stat s;&lt;BR /&gt;&lt;BR /&gt;  *is_dir = FALSE;&lt;BR /&gt;  cc = stat(fname,&amp;amp;s);&lt;BR /&gt;  if (cc == 0)&lt;BR /&gt;    {&lt;BR /&gt;      *is_dir = (S_ISDIR(s.st_mode));&lt;BR /&gt;    }&lt;BR /&gt;  else&lt;BR /&gt;    {&lt;BR /&gt;      cc = assign_errno(-1);&lt;BR /&gt;      (void) fprintf(stderr,"Can't stat %s (%d)\n",fname,cc);&lt;BR /&gt;    }&lt;BR /&gt;  return(cc);&lt;BR /&gt;} /* isdir */&lt;BR /&gt;&lt;BR /&gt;int main(int argc, char *argv[])&lt;BR /&gt;{&lt;BR /&gt;  int cc = 0,i = 1,isadir = FALSE;&lt;BR /&gt;&lt;BR /&gt;  while (i &amp;lt; argc)&lt;BR /&gt;    {&lt;BR /&gt;      cc = isdir(argv[i],&amp;amp;isadir);&lt;BR /&gt;      (void) printf("File: %s cc = %d isadir = %d\n",argv[i],cc,isadir);&lt;BR /&gt;      ++i;&lt;BR /&gt;    }&lt;BR /&gt;  return(cc);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;------------------------------------------&lt;BR /&gt;Note the use of the S_ISDIR macro. If I have made any typo's this should do it for you.&lt;BR /&gt;&lt;BR /&gt;&lt;/SYS&gt;&lt;/SYS&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Tue, 09 Mar 2004 10:09:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213396#M168995</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2004-03-09T10:09:59Z</dc:date>
    </item>
    <item>
      <title>Re: c code help on determining an existing directory</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213397#M168996</link>
      <description>Hi, &lt;BR /&gt;&lt;BR /&gt;Thanks for the code.&lt;BR /&gt;&lt;BR /&gt;The first question came to my mind is following:&lt;BR /&gt;&lt;BR /&gt;The code only can determine if it is a file or not, but can not determine the directory.&lt;BR /&gt;let say, I have /a/b/c/d, if "d" is a file, I will get "0" return, if "d" is directory, I will get non-zero return. Is my statement correctly?&lt;BR /&gt;&lt;BR /&gt;Also, if I will get non-zero return when it is directory, then that is not good to determine it is directory or not. I mean, is there a way to see if it is directory or not?</description>
      <pubDate>Tue, 09 Mar 2004 11:53:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213397#M168996</guid>
      <dc:creator>Hanry Zhou</dc:creator>
      <dc:date>2004-03-09T11:53:03Z</dc:date>
    </item>
    <item>
      <title>Re: c code help on determining an existing directory</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213398#M168997</link>
      <description>No, it does both things.&lt;BR /&gt;&lt;BR /&gt;int cc = 0,isadir = 0;&lt;BR /&gt;&lt;BR /&gt;cc = isdir(fname,&amp;amp;isadir);&lt;BR /&gt;&lt;BR /&gt;Note that we pass in two arguments: the pathname and a pointer to an int. The convention for C functions is to return 0 for success. Your function might fail for several reasons: e.g. file/dir not found, you might not have permission to follow a path, filename too long to name just a few.&lt;BR /&gt;If the stat command fails for any reason, a meaningful (errno) non-zero result is returned. Only when the return result is 0 do you now check to see if the second argument (isadir) has been set to a non-zero status indicating that the file is in fact a directory. Remember, in UNIX speak, directories (along with device nodes, pipes, and symbolic links) are files too. The correct way to phrase your question is "Is a/b/c/d a REGULAR file or a directory?".&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;You could simply do this:&lt;BR /&gt;(void) isadir("/a/b/c/d",&amp;amp;isdir);&lt;BR /&gt;if (isdir) printf("Is a directory\n");&lt;BR /&gt;else printf("Not a directory\n");&lt;BR /&gt;&lt;BR /&gt;The problem with that approach is that all you know is whether or not it is a directory but if not, you know nothing else. Was it an IO failure? Permissions? or simply a file instead of a directory?</description>
      <pubDate>Tue, 09 Mar 2004 12:19:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213398#M168997</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2004-03-09T12:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: c code help on determining an existing directory</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213399#M168998</link>
      <description>Oops, my last example would be better if I kept the function/varible names consistant:&lt;BR /&gt;&lt;BR /&gt;You could simply do this:&lt;BR /&gt;(void) isadir("/a/b/c/d",&amp;amp;isdir);&lt;BR /&gt;if (isdir) printf("Is a directory\n");&lt;BR /&gt;else printf("Not a directory\n");&lt;BR /&gt;&lt;BR /&gt;SHOULD BE:&lt;BR /&gt;&lt;BR /&gt;You could simply do this:&lt;BR /&gt;(void) isdir("/a/b/c/d",&amp;amp;isadir);&lt;BR /&gt;if (isadir) printf("Is a directory\n");&lt;BR /&gt;else printf("Not a directory\n");&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Mar 2004 12:34:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213399#M168998</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2004-03-09T12:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: c code help on determining an existing directory</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213400#M168999</link>
      <description>Thank you so much for the detail message.&lt;BR /&gt;but, Sorry, I'm a slow man, still confused.&lt;BR /&gt;&lt;BR /&gt;1. As you said, non-zero return will be indicating an error, so how can you conculude that if (isadir) has been set to a non-zero status indicating that the file is in fact a directory?&lt;BR /&gt;&lt;BR /&gt;2. *is_dir = (S_ISDIR(s.st_mode));&lt;BR /&gt;does this line actually tell you if the "d" is directory or not.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Mar 2004 13:07:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213400#M168999</guid>
      <dc:creator>Hanry Zhou</dc:creator>
      <dc:date>2004-03-09T13:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: c code help on determining an existing directory</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213401#M169000</link>
      <description>If I understand you correctly in the case of S_ISDIR(...), since it is macro, so if it returns a non-zero, then that will indicate that it is directory. Right?&lt;BR /&gt;&lt;BR /&gt;Also, for "cc = stat(fname,&amp;amp;s);", it will retrun zero if it is either a regular file or directory, and that is why we need have "&lt;BR /&gt;*is_dir = (S_ISDIR(s.st_mode));". Right?</description>
      <pubDate>Tue, 09 Mar 2004 13:16:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213401#M169000</guid>
      <dc:creator>Hanry Zhou</dc:creator>
      <dc:date>2004-03-09T13:16:47Z</dc:date>
    </item>
    <item>
      <title>Re: c code help on determining an existing directory</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213402#M169001</link>
      <description>Yes, you have it now. You could actually determine if the file were a directory after doing a stat by looking at some bits in the "mode" field.&lt;BR /&gt;&lt;BR /&gt;int md;&lt;BR /&gt;&lt;BR /&gt;md = (s.st_mode &amp;amp; 0170000) &amp;gt;&amp;gt; 12;&lt;BR /&gt;switch(md)&lt;BR /&gt;  {&lt;BR /&gt;    case 012:&lt;BR /&gt;       (void) printf("Symbolic link\n"):&lt;BR /&gt;        break;&lt;BR /&gt;    case 010:&lt;BR /&gt;      (void) printf("Regular file\n");&lt;BR /&gt;       break;&lt;BR /&gt;    case 006:&lt;BR /&gt;      (void) printf("Block special\n");&lt;BR /&gt;      break;&lt;BR /&gt;    case 004:&lt;BR /&gt;      (void) print("Directory\n");&lt;BR /&gt;      break;&lt;BR /&gt;    case 002:&lt;BR /&gt;      (void) printf("Character Special\n");&lt;BR /&gt;       break;&lt;BR /&gt;    case 001:&lt;BR /&gt;      (void) printf("FIFO/Pipe\n");&lt;BR /&gt;      break;&lt;BR /&gt;    default :&lt;BR /&gt;       (void) printf("Unknown\n");&lt;BR /&gt;       break;&lt;BR /&gt;  } /* switch */&lt;BR /&gt;&lt;BR /&gt;The S_ISDIR() macro is a bit easier to use but it's essentially:&lt;BR /&gt;((s.st_mode &amp;amp; 0170000) == 040000)&lt;BR /&gt;If that expression is non-zero then the file is a directory.&lt;BR /&gt;&lt;BR /&gt;NOTE: All of these constants are octal values/&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Mar 2004 14:19:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213402#M169001</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2004-03-09T14:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: c code help on determining an existing directory</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213403#M169002</link>
      <description>I'll try to bit a little more clear:&lt;BR /&gt;&lt;BR /&gt;The stat() system call, if sucessful, returns&lt;BR /&gt;a 0 if ok or -1 if not and sets the global variable errno to indicate the error. This is the standard convention for all UNIX system calls. If the result is zero, a data structure containing all the metadata (modification, change, and last access times, owner, group, mode, file length, and others) is filled in and then we need to examine the fields within this structure for more detailed knowledge. Specifically, we need to examine the st_mode field which encodes the file type (what you are interested in), the setuid, setgid, and "sticky" bits, as well as the more familiar permissions for owner, group, and other.&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Mar 2004 14:40:35 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-code-help-on-determining-an-existing-directory/m-p/3213403#M169002</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2004-03-09T14:40:35Z</dc:date>
    </item>
  </channel>
</rss>

