prevent piping?

This is a discussion on prevent piping? within the C++ Programming forums, part of the General Programming Boards category; hi, how can i prevent piping from the command line? piping occurs both in windows and unix so its OS ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    84

    prevent piping?

    hi,

    how can i prevent piping from the command line? piping occurs both in windows and unix so its OS independent.

    so far i'm thinking this but this code doesn't work:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	string temp_string;
    	for(int i=0; i<argc; i++)
    	{
    		temp_string = argv[i];
    		if(temp_string == ">")
    		{
    			exit(99);
    		}
    		else
    		{
    			continue();
    		}
    	}
    return 0;
    }
    any suggestions?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295

    Wink

    The short answer is: You Can't.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    A correct short answer is that you can, but the means are operating system dependent and do not involve looking at the command line.

    A slightly longer answer is that piping is done by the command interpreter, so (under unix, depending on the shell), the result of

    PHP Code:
    prompt %   command arg1 arg2 file 
    the shell strips off everything from the > sign, sets things up so that standard output is redirected to "file" before executing "command arg1 arg2". Hence no information on the command line that can be used by the program to detect if its standard output is redirected.

    Under unix, a function named function fstat() is often a way to obtain information about an open file descriptor. The first argument is an integer that identifies the open file descriptor (IIRC, 0 corresponds to stdin, 1 corresponds to stdout, and 2 corresponds to stderr but you may wish to confirm that), and the second is a pointer to a structure that is populated with relevant information (eg is the descriptor for a pipe, an actual file, .....)

    I've never had reason to do such things under windows, but do recall seeing an fstat() function in help files with some compilers/libraries. As fstat() is NONSTANDARD, there is a good chance that it will work differently between unix and windows. In fact, I wouldn't necessarily bet it will work the same between different flavours of unix either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prevent user from typing keys - consoleapp
    By bandul in forum C++ Programming
    Replies: 8
    Last Post: 05-13-2006, 01:30 PM
  2. How to prevent a function from being overriden?
    By thomas_nibu in forum C++ Programming
    Replies: 18
    Last Post: 11-13-2005, 08:55 PM
  3. scanf, prevent input letters
    By renderstream in forum C Programming
    Replies: 3
    Last Post: 10-16-2005, 02:47 AM
  4. Piping error messages to a file?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-25-2002, 02:09 PM
  5. prevent exiting
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-12-2001, 01:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21