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 7644733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:45:42+00:00 2026-05-31T09:45:42+00:00

Program : List all C files in the current folder using execlp() system call:

  • 0

Program : List all C files in the current folder using execlp() system call:

#include <stdio.h>
#include <unistd.h>

int main()
{
    printf("Before Execl\n");
    execlp("ls","ls","*.c",NULL); // this should print all c files in the current folder.
    return 0;
}

Program output:

Before Execl
ls: cannot access *.c: No such file or directory

Whenever I use ‘*‘ in the search pattern, I am getting a similar kind of error. Please suggest some appropriate solution.

  • 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-05-31T09:45:44+00:00Added an answer on May 31, 2026 at 9:45 am

    If you want shell metacharacters expanded, invoke the shell to expand them, thus:

    execlp("sh", "sh", "-c", "ls *.c", (char *)0);
    fprintf(stderr, "Failed to exec /bin/sh (%d %s)\n", errno, strerror(errno));
    exit(EXIT_FAILURE);
    

    Note that if execl() or any of the exec* functions returned, it failed. You don’t need to test its status; it failed. You should not then do exit(0); (or return 0; in the main() function) as that indicates success. It is courteous to include an error message outlining what went wrong, and the message should be written to stderr, not stdout — as shown.

    You can do the metacharacter expansion yourself; there are functions in the POSIX library to assist (such as glob()). But it is a whole heap simpler to let the shell do it.

    (I’ve revised the code above to use execlp() to conform to the requirements of the question. If I were doing this unconstrained, I’d probably use execl() and specify "/bin/sh" as the first argument instead.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following program list me all files in a directory but how can I display
I'm working on a program, which should list all files and it's size(for now...).
I want to list all files of the current directory, so I have this
How do I list all programs installed on my computer? I've tried using the
class Program { static void Main(string[] args) { List<Book> books = new List<Book> {
I have a program that's watching for changes to certain files in a list
Consider the following VBScript which, when run, lists all the files in the current
I primarily program in Linux, using tcsh shell. By default, my current directory is
I wanted a program which could list all the contents available in a directory.
I want to run my program on all the files in a directory after

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.