Forum: PC-Programmierung Linux: Feststellen ob stdout umgeleitet wird oder nicht?


von Daniel (root) (Gast)


Lesenswert?

Hallo,

wie kann ein Prozess feststellen, ob die Ausgaben die es in stdout
schreibt, auf Bildschirm kommen oder in eine Datei umgeleitet werden?

Grüsse, Daniel

von Stefan B. (stefan) Benutzerseite


Lesenswert?

"isatty() is a function that returns TRUE if the handle is associated 
with a TTY (e.g. keyboard, screen, printer, serial port), and FALSE in 
the other circumstances (e.g. the handle is associated with a regular 
file, or is a part of a pipeline). For example, if the standard output 
is redirected to a file, or piped to another program, then 
isatty(STDOUT) returns FALSE."

http://www.cs.toronto.edu/~ramona/cosmin//TA/prog/sysconf/
http://linux.die.net/man/3/isatty

von Daniel (root) (Gast)


Lesenswert?

Danke.

Falls jemand nachvollziehen möchte.
1
#include <stdio.h>
2
#include <unistd.h>
3
4
int main(void) {
5
    FILE * ferror = fdopen(2, "w");
6
    if(isatty(1)) {
7
  fprintf(ferror, "ja\n");
8
  printf("ja\n");
9
    }
10
    else {
11
  fprintf(ferror,"nein\n");
12
  printf("nein\n");
13
    }
14
    return 0;
15
}
1
matrix:/pool/linux/output# gcc -o main main.c -Wall
2
matrix:/pool/linux/output# ./main
3
ja
4
ja
5
matrix:/pool/linux/output# ./main > x
6
nein
7
matrix:/pool/linux/output# cat x
8
nein
9
matrix:/pool/linux/output#

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.