Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's because you aren't stating the actual file. It's in a different directory. If you want the real filename, combine <code>argv[1]</code> and <code>direntp-&gt;d_name</code> with a '/' between them.</p> <p>Also, hungarian naming is icky, even the minor bit like 'p' on the end. If you have so many variables you need to keep track of their types in their names you're doing something wrong.</p> <p>Here is a revised version of your program:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;stddef.h&gt; #include &lt;string.h&gt; #include &lt;dirent.h&gt; #include &lt;sys/stat.h&gt; #include &lt;errno.h&gt; #include &lt;limits.h&gt; #include &lt;sys/param.h&gt; int main(int argc, char *argv[]) { DIR *dirp; struct dirent *direntp; struct stat stat_buf; char *str; char fullpath[MAXPATHLEN + 1]; size_t dirnamelen; if (argc != 2) { fprintf( stderr, "Usage: %s dir_name\n", argv[0]); exit(1); } strncpy(fullpath, argv[1], MAXPATHLEN - 1); /* account for trailing '/' */ fullpath[MAXPATHLEN - 1] = '\0'; dirnamelen = strlen(fullpath); if (strlen(argv[1]) &gt; dirnamelen) { fprintf( stderr, "Directory name is too long: %s", argv[1] ); exit(2); } fullpath[dirnamelen++] = '/'; fullpath[dirnamelen] = '\0'; if ((dirp = opendir( argv[1])) == NULL) { perror(argv[1]); exit(2); } while ((direntp = readdir( dirp)) != NULL) { fullpath[dirnamelen] = '\0'; if ((dirnamelen + strlen(direntp-&gt;d_name)) &gt; MAXPATHLEN) { fprintf(stderr, "File %s + directory %s is too long.", direntp-&gt;d_name, fullpath); continue; } else { /* strncpy is mild overkill because the if statement has verified that there's enough space. */ strncpy(fullpath + dirnamelen, direntp-&gt;d_name, MAXPATHLEN - dirnamelen); fullpath[MAXPATHLEN] = '\0'; } if (stat(fullpath, &amp;stat_buf)==-1) { perror("stat ERROR"); exit(3); } if (S_ISREG(stat_buf.st_mode)) str = "regular"; else if (S_ISDIR(stat_buf.st_mode)) str = "directory"; else str = "other"; printf("%-25s - %s\n", direntp-&gt;d_name, str); } closedir(dirp); exit(0); } </code></pre> <p>Note that I use <code>MAXPATHLEN</code> (from <code>&lt;limits.h&gt;</code>) and carefully check to make sure there aren't any buffer overflows. You should do the same in your code.</p> <p><strong>Edit:</strong> Changed code to use <code>strn</code> family functions for added safety.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload