Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7982683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:44:41+00:00 2026-06-04T10:44:41+00:00

I am currently working with a code example that initially is designed to take

  • 0

I am currently working with a code example that initially is designed to take an argument, then search for that argument in the current directory, I’ve tried to make it search another directory (/dev/shm to exact) by replacing the “.” with “/dev/shm” but the code turns up nothing when i search for something* (notice the wildcard). The wild card search works fine in the current directory so I do not think it is the wild card that is the problem, If someone could help me out though I would really appreciate it, thanks!

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>


static void lookup(const char *arg)
{
    DIR *dirp;
    struct dirent *dp;


    if ((dirp = opendir(".")) == NULL) {
        perror("couldn't open '.'");
        return;
    }


    do {
        errno = 0;
        if ((dp = readdir(dirp)) != NULL) {
            if (strcmp(dp->d_name, arg) != 0)
                continue;


            (void) printf("found %s\n", arg);
            (void) closedir(dirp);
                return;


        }
    } while (dp != NULL);


    if (errno != 0)
        perror("error reading directory");
    else
        (void) printf("failed to find %s\n", arg);
    (void) closedir(dirp);
    return;
}


int main(int argc, char *argv[])
{
    int i;
    for (i = 1; i < argc; i++)
        lookup(argv[i]);
    return (0);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:44:43+00:00Added an answer on June 4, 2026 at 10:44 am

    opendir doesn’t handle wildcards. It expects a real directory path. I’m not sure what you mean when you say

    wildcard search works in the current directory

    If you mean it works in your shell, that’s to be expected. The shell will first expand the wildcard and then perform the command you typed.

    So how to solve this? Expand the wildcard yourself using glob before calling opendir.


    Edit: sorry, I thought you were trying to match the wildcard in the directory name. It looks like you want to match directory contents using the wildcard. In that case simply replace

    if (strcmp(dp->d_name, arg) != 0)
    

    with

    if (fnmatch(arg, dp->d_name, 0) != 0)
    

    You could also use glob for this. It will actually replace the call to opendir and the loop. Here is an example for using glob:

    #include <glob.h>
    #include <stdio.h>
    
    
    static void lookup(const char *root, const char *arg)
    {
        size_t n;
        glob_t res;
        char **p;
    
        chdir(root);
        glob(arg, 0, 0, &res);
    
        n = res.gl_pathc;
        if (n < 1) {
            printf("failed to find %s\n", arg);
        } else {
            for (p = res.gl_pathv; n; p++, n--) {
                printf("found %s\n", *p);
            }
        }
        globfree(&res);
    }
    
    
    int main(int argc, char *argv[])
    {
        int i;
        for (i = 2; i < argc; i++)
            lookup(argv[1], argv[i]);
        return (0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code I'm currently working with: http://jsfiddle.net/gEMCm/ I'm trying to make an image
I am currently working on Problem 62 I have tried the following code to
I'm currently working on a plug-in for Eclipse that translates some custom Java code
I'm currently working on an app that use ZXing to generate QR code as
The code I'm currently working on requires adding an NSNumber object to an array.
I am currently working on some Ajax heavy code and I am wondering how
I am currently working with the following pseudo code block to fix a regex
I'm currently working on a compiler for a language (an external byte-code), and am
Currently, I am working on restructuring an existing code base. I'm new to php
As a beginner in Ocaml, I have this current working code: ... let ch_in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.