Find the answer to your Linux question:
Results 1 to 5 of 5
Say there is a executable ./xyz < filename i need to know how i can restrict the i/p file's formats here... as in only allow .txt files or .c files ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    3

    some help regarding input file formats

    Say there is a executable

    ./xyz < filename

    i need to know how i can restrict the i/p file's formats here... as in only allow .txt files or .c files as inputs.

    regards

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Using IO redirection, you cannot do this. IO redirection is completely invisible to the invoked program. You could theoretically, I suppose, look at the /proc/self/fds/0 link to see where your stdin is coming from, but I can't imagine this being necessary.

    Why do you want to limit this? Under UNIX, a file isn't determined by its file extension, but rather its content. I could have a PDF file with an extension of .c, and it would still be a PDF file. What are you trying to achieve here? Maybe there's a better way.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    3
    basically what i am looking for is restricting file types for a compiler input
    like gcc takes only .c
    can u tell me neway i can do it ? i mean other than inserting a routine to chk extension
    i am kinda really a newbie at all this

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So the way gcc works is that it relies on the file extension, or by a user flag. gcc does not know what language the file is supposed to be based on the content.

    So for instance, when I run 'gcc file.xxx', gcc will use C as the default language, unless the 'xxx' is something it recognizes (for instance, 'cpp' for C++ or 'java' for Java, etc.). I could also say something like 'gcc -x c file.java', and because I told it to use C, it will use C.

    When I run 'g++ file.xxx', it is the same as above, only it uses C++ as the default language, and so on with gcj, g88, etc.

    In any event, you will notice that none of these uses IO redirection, but rather passes in the file as an argument. This way, gcc is able to look at the file extension.

    Now, assuming that you have appropriate checking, why not just try to compile anything that gets passed into you, and report errors when it's not a valid $LANG program. To ensure that a program is valid C, for instance, you would have to parse it anyway, so why not just do it anyway?
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    Sep 2007
    Posts
    3
    i get ur point !!!
    well i do have a mediocre compiler designed and wanted a unique extension for file of tat language :P

    Neways i'll try to do what u have mentioned
    Thanks a billion for u help buddy !!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...