0
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
        int val;

        if(argc!=2)
        {
                printf("Usage: %s <descriptor> \n", argv[0]);
                exit(1);
        }

        val=fcntl(atoi(argv[1]), F_GETFL, 0);


        switch(val & O_ACCMODE)
        {
                case O_RDONLY:
                        printf("read only");
                        break;
                case O_WRONLY:
                        printf("write only");
                        break;
                case O_RDWR:
                        printf("read write");
                        break;
                default:
                        printf("unknown access mode");
        }

        if(val & O_APPEND)
                printf(", append");
        if(val & O_NONBLOCK)
                printf(", nonblocking");
        if(val & O_SYNC)
                printf(", synchronous writes");

        putchar('\n');
        exit(0);
 }

I have this code. Anyway, source is fully understandable.

But, I got confused using this program with command line argument with redirecting.

using this program and result are following

    $ ./a.out 0 < /dev/tty
    read only
    $ ./a.out 1 > temp.foo
    $ cat temp.foo
    write only
    $ ./a.out 2 2>>temp.foo
    write only, append
    $ ./a.out 5 5<>temp.foo
    read write

I know how to do redirecting, but using this technique with command line argument is so confusing. I don't know anything.

Well, in my knowledge, this program gets 1 argument which is file descriptor number.

So, when I use this program like

$ ./a.out 0
read write
$ ./a.out 1
read write
$ ./a.out 2
read write

This is ok. stdin, stdout, stderr file status is read write.

But when I use this command with redirecting. I don't know what mechanism is doing.

For example:

./a.out 0 < /dev/tty

Here, /dev/tty file is now same as standard input, so 0 means /dev/tty after all?

And, what's the difference between &2 ,&1 and 2, 1 when we use redirecting?

2

I know how to do redirecting, but using this technique with command line argument is so confusing. I don't know anything.

Please describe more detailed what exactly confuses you. As for me, the results are very expected: if a file object is opened for read-only (as '<' redirection does), F_GETFL will give O_RDONLY. The same for other types of redirections.

Without redirection, a child command gets the same standard descriptiors as the parent shall has. In a typical interactive case, it's the user terminal and all descriptors are in read-write. It's decided useless to make them read-only, write-only, etc. because it's more convenient to use the same descriptor for interactive terminal activity (as e.g. full-screen activity with curses), than to split it between 0 for terminal input, 1 for terminal output, etc.; but this isn't extended for user-specified redirections. Is that the thing you asked?

And, what's the difference between &2 ,&1 and 2, 1 when we use redirecting?

If you mean forms like 2>1 and 2>&1, they are principally different. 2>1 redirects 2 as writing to file 1. 2>&1 redirects 2 as copy of descriptor 1 (on C level, this is dup2(1,2)). Both n<&m and n>&m for numeric n, m are identical and don't add restriction to access mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.