0

I'm trying to create a program where it would count the number of directories, or the number of readable/writable/executable files. The user would input only the name of the author and the letters "d", "r", "w", or "x". I tried to directly call "ls -l" in my program but that caused an error. How do you call UNIX commands within a C program?

3
  • 1
    look into system() Commented Jan 27, 2016 at 3:10
  • 1
    Although system() 'works', you'd have to manufacture the command line carefully; there's no way for the launching program to filter the output (in the ordinary course of events). You might need to use POSIX functions popen(), or maybe fork() and execvp() and related functions (pipe(), dup2(), …). Commented Jan 27, 2016 at 3:41
  • 1
    You might also want to investigate POSIX functions nftw(), opendir(), readdir(), stat(). Commented Jan 27, 2016 at 3:43

2 Answers 2

1

I tried to directly call "ls -l" in my program but that caused an error. How do you call UNIX commands within a C program?

You can se system in your C program, for example:

system( "ls -l" );

For that to work, you'll also need to #include <stdlib.h>

Sign up to request clarification or add additional context in comments.

2 Comments

I think the program output is necessary in this case too.
In that case use popen
0

Using the find command might work better, you could count directories with the command "find . -t d | wc -l" and do something similar for files with the appropriate flags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.