C Program to search and read all named pipes in current directory

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

Write a C program to search the current directory for all pipes.

  1.  It will print the pipe names, one per line.
    
  2.  Print the number of pipes found.
    
  3.  Search for all pipes whose contents contain your ID and print the entire contents of that/those pipe\(s\).
    
  4. Relevant commands, code, scripts, algorithms:

Running inside a bash shell, compiling with g++, professor provides a directory of pre-existing pipes and pipe writers to prevent the block that you get from waiting for a pipe read or write to start

  1. The attempts at a solution (include all code and scripts):

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

int main()
{
FILE *read_fp;
char buffer[BUFSIZ + 1];
int chars_read;
memset(buffer, '\0', sizeof(buffer));
read_fp = popen("uname -a", "r");
if (read_fp != NULL) {
chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
if (chars_read > 0) {
printf("Output was:-\n%s\n", buffer);
}
pclose(read_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    University of North Texas, Denton, Texas, USA
    Course is 3600, "Systems" taught by Professor Goodman
    www . cse . unt . edu /~goodman/CS3600/cs3600.html

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

I don't have a problem reading from the pipe. I can specify a named pipe with the popen command and read from a named pipe. The above code can do that. The problem I have is that I am given the task to read from all the named pipes in the directory using a C program. I can easily write a bash script like "for all files in *" do blah. I know that I can do a system call and I can make a system call for the pwd to figure some things out but I don't know how to isolate pipes from the general files inside the C program. In a bash script I can use "test" to do that but is there a way to suck a list of named pipes into a C program in Unix? Finding named pipe assistance seems to be quite difficult on google so I'm trying out here.

Any ideas how to pull in the list of pipes into the C program?

Some hints:

  • popen is not "open a pipe", it is more "open a process"
  • Use opendir(3) to open a directory for reading and readdir(3) to read entries.
  • stat(3) to determine filetypes.
  • Once you determine that you have a named pipe, you will have to open it for reading, etc etc.

Hope this helps.

1 Like

Thank you very much, I was able to figure it out from there.