-4
int filter(int m, int readfd, int writefd)

I have a function named filter that takes three arguments: the filter value m, a file descriptor readfd from which integers are received, and a file descriptor writefd to which integers are written. Its purpose is to remove (filter) any integers from the data stream that are multiples of m. The function returns 0 if it completes without encountering an error and 1 otherwise.

How do I make a file descriptor for input readfd?

  • 1
    Use the function [`open`](https://linux.die.net/man/3/open) to open a file. The function returns an integer which is the file handle. You should try to make the title of your question congruent with the description. What you are basically asking seems to be ‘how do I open a file’. This is a question for which there is certainly an answer on this site. – Paul Rooney Mar 18 '18 at 00:41
  • 3
    What has this anything to do with bash? – Pablo Mar 18 '18 at 00:47
  • 1
    **Bash has no foreign function interface. You cannot directly call an arbitrary C function from bash.** Either write a `main` -- which is purely a C question, nothing to do with bash -- or use another language which *does* have a FFI interface. – Charles Duffy Mar 18 '18 at 00:51
  • ...oh, you just want to make a file descriptor? `exec 3< <(echo "input for readfd")` will do. Or run your program with `3< file.in` as a redirection, and pass `3` as your `readfd`. Or pipe your content on stdin (which is FD 0), and pass 0 as readfd and 1 (stdout) as writefd; the existing answer from @Simon is doing the latter. Thus, `./test out` will read from `in` and write to `out` with their answer as currently given, since `<` redirects stdin (FD 0) by default, and `>` redirects stdout (FD 1) by default. – Charles Duffy Mar 18 '18 at 01:01
  • @CharlesDuffy: bash has *loadable builtins*, but, judging from the question, that goes way beyond what the OP is likely to want. I've played with them but found them tricky to build. In the bash source tree, see `examples/loadables`. There are better ways :-) – cdarke Mar 18 '18 at 08:06
  • @cdarke, sure -- that's why I said you couldn't run an *arbitrary* C function. If you're going to write a wrapper to make something a loadable builtin, you might as well write a `main` to make it `exec`able. – Charles Duffy Mar 18 '18 at 17:04

2 Answers2

1

What you should do is put your file inside a script for testing (or separate script) or use an interpreter.

say testfile.h contains your function.

test.c:

#include "testfile.h"

int main() {

    int x = filter(2, 0, 1);
    if (x == 0) {
        //Do stuff
    }
    else if (x == 1) {
        // Do another thing if there was an error
    else {
        // Do something else
    }
    return 0;
}

compile your function into an executable using terminal:

gcc test.c -o test.o
ld test.o test

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • My question is how to test this specific program, i know how to compile a c program. – heskinreaper Mar 18 '18 at 00:42
  • @heskinreaper Sorry. Please see my updated answer. – Xantium Mar 18 '18 at 00:50
  • 1
    BTW, there *are* interpreters for C available, so "cannot" is a little strong. See https://stackoverflow.com/questions/584714/is-there-an-interpreter-for-c -- or, for a compiler that can be used to build an execute via a shebang, see https://bellard.org/tcc/ – Charles Duffy Mar 18 '18 at 00:53
1

The open function will return you a file descriptor. Now, you can use the fdopen function to get the same stream associated with the file descriptor.

int main()
{
    int readfd,writefd,m=0;

    readfd = open("file_name1.txt",O_RDONLY); //open the file in read only mode
    writefd = open("file_name2.txt",O_WRONLY); // open the file in write only mode

    filter(m, readfd, writefd);

}

int filter(int m, int readfd, int writefd)
{
    FILE *fp1,*fp2;

    fp1 = fdopen(readfd,"r");
    fp2 = fdopen(writefd,"w");
}
sk_58
  • 43
  • 5