[Gllug] Off Topic: C File Descriptors

salsaman at xs4all.nl salsaman at xs4all.nl
Fri Mar 7 01:41:47 UTC 2008


On Fri, March 7, 2008 00:15, Alain Williams wrote:
> On Tue, Mar 04, 2008 at 12:10:02PM +0000, Mick Farmer wrote:
>> Dear GLLUGers,
>>
>> Is there a way of determining whether standard output and
>> standard error are going to the same file/device?
>

something like this (untested, YMMV) should do it:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>


int main(void) {
 char buf1[512],buf2[512];
 int mypid=(int)getpid();
 ssize_t size;
 char *string;

 sprintf(string,"/proc/%d/fd/1",mypid);
 size=readlink(string,buf1,511);
 memset(buf1+size,0,1);
 free(string);

 sprintf(string,"/proc/%d/fd/2",mypid);
 size=readlink(string,buf2,511);
 memset(buf2+size,0,1);
 free(string);

 if (!strcmp(buf1,buf2)) {
   printf("The same !\n");
 } else {
   printf("Not the same !\n");
 }

 return 0;
}



Gabriel.
http://lives.sf.net


-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list