LINUX.ORG.RU

История изменений

Исправление LeninGad, (текущая версия) :

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

int main(int argc, char *argv[])
{
  int fd;
  if (argc<2) {
    fprintf(stderr,"Usage: %s fifo command args...\n",argv[0]);
    return 2;
  }
  do {
    fd = open(argv[1],O_WRONLY|O_NONBLOCK);
  } while (fd<0 && errno == EINTR);
  if (fd<0) {
    if (errno == ENXIO) {
      unlink(argv[1]);
      return 1;
    } else {
      perror("Pipe open");
      return 3;
    }
  } else {
    fcntl(fd,F_SETFL,0);  /* disable O_NONBLOCK */
    dup2(fd,1);           /* replace stdout */
    close(fd);
    execvp(argv[2],argv+2);
  }
  return 0;
}

Назвать можно trypipe, запускать trypipe my/pipe echo muahaha.

Исходная версия LeninGad, :

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

int main(int argc, char *argv[])
{
  int fd;
  if (argc<2) {
    fprintf(stderr,"Usage: %s fifo command args...\n",argv[0]);
    return 2;
  }
  do {
    fd = open(argv[1],O_WRONLY|O_NONBLOCK);
  } while (fd<0 && errno == EINTR);
  if (fd<0) {
    if (errno == ENXIO) {
      unlink(argv[1]);
      return 1;
    } else {
      perror("Pipe open");
      return 3;
    }
  } else {
    fcntl(fd,F_SETFL,0);  /* disable O_NONBLOCK */
    dup2(fd,1);           /* replace stdout */
    close(fd);
    execvp(argv[2],argv+2);
  }
  return 0;
}