Mail Archives: djgpp/2001/06/14/14:15:20
"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 -