delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/06/14/14:15:20

From: "Mark E." <snowball3 AT bigfoot DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Gnu compiler for Perl
Date: Thu, 14 Jun 2001 14:05:21 -0400
Organization: Snowball's Web
Message-ID: <7suhitsd27aqps39ahmnhkvjntsnl87bpr@4ax.com>
References: <20010614145506 DOT EFE4 DOT H DOT M DOT BRAND AT hccnet DOT nl> <Pine DOT SUN DOT 3 DOT 91 DOT 1010614160710 DOT 6496A AT is> <20010614160042 DOT EFE9 DOT H DOT M DOT BRAND AT hccnet DOT nl> <2110-Thu14Jun2001190959+0300-eliz AT is DOT elta DOT co DOT il>
X-Newsreader: Forte Agent 1.8/32.548
MIME-Version: 1.0
NNTP-Posting-Host: slip-12-64-138-181.mis.prserv.net
X-Trace: 14 Jun 2001 18:02:31 GMT, slip-12-64-138-181.mis.prserv.net
Lines: 92
X-Complaints-To: abuse AT prserv DOT net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Eli Zaretskii" <eliz AT is DOT elta DOT co DOT il> wrote:

>Mark, could you please see what does Bash do with "> /dev/null" that
>it doesn't do with ">> /dev/null"?

This program simulates (if I did it right) what Bash does with 'echo x > /dev/null'
and 'echo x >> /dev/null'. I put in calls to 'perror' to show at what point the
program fails (if anywhere). This works under win98. Try it under w2k and report the
results. If this works, try modifying the example to run a program instead of
printing a message.

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

/* Simulate the script:
   echo "abc" > /dev/null
   echo "abc" >> /dev/null
*/

int main()
{
  int fd1, fd2, ret;

  fd1 = dup(1);

  /* open /dev/null */
  fd2 = open("/dev/null", O_WRONLY | O_TRUNC | O_CREAT, S_IWUSR);

  if (fd2 < 0)
  {
    perror("#1");
    return 1;
  }

  if (dup2(fd2, 1) < 0)
  {
    perror("#2");
    return 1;
  }

  /* echo "abc" > /dev/null */
  printf("abc");
  putchar('\n');
  fflush (stdout);

  if (dup2(fd1, 1) < 0)
  {
    perror("#3");
    return 1;
  }

  close(fd2);

  /* echo "abc" >> /dev/null */
  fd2 = open("/dev/null", O_WRONLY | O_APPEND | O_CREAT, S_IWUSR);

  if (fd2 < 0)
  {
    perror("#4");
    return 1;
  }

  if (dup2(fd2, 1) < 0)
  {
    perror("#5");
    return 1;
  }

  printf("abc");
  putchar('\n');
  fflush (stdout);

  if (dup2(fd1, 1) < 0)
  {
    perror("#6");
    return 1;
  }

  /* close up shop */
  close (fd2);
  close (fd1);

  printf("Success!\n");
  return 0;
}

-- 
Mark E.: snowball3 AT bigfoot DOT com
http://snowball.frogspace.net/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019