File: startim.c

package info (click to toggle)
imview 1.1.9c-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,068 kB
  • ctags: 3,686
  • sloc: cpp: 28,834; sh: 2,624; ansic: 1,818; makefile: 767; exp: 112; python: 88
file content (57 lines) | stat: -rw-r--r-- 1,331 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*------------------------------------------------------------------------
 *
 * This simple client program starts up an imview, where the portfile
 * is in fact a named pipe. This is trickier than with a file but it
 * works better if you want to start several imviews, as it forces
 * a measure of synchronization.
 *
 * Hugues Talbot	14 Mar 2001
 *      
 *-----------------------------------------------------------------------*/


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

#define PROG "startim"
#define PIPESTR "/tmp/toto"
#define IMVIEWCMD  "imview -server -fork -portfile " PIPESTR

#define FAIL do {perror(PROG); unlink(PIPESTR); exit(1);} while (0)

int main()
{
  char *execstring = IMVIEWCMD;
  char buffer[5];
  int   portnum, filenum, res;

  unlink(PIPESTR); /* might fail, don't care */

  res = mkfifo(PIPESTR,S_IRUSR|S_IWUSR);
  if (res != 0)
    FAIL;

  /* open the pipe for reading, notice the NO DELAY  */
  filenum = open(PIPESTR, O_RDONLY | O_NDELAY, 0);
  if (filenum < 0)
    FAIL;
    
  /* then exec imview */
  printf("Imview away...\n");
  system(execstring);

  /* then read the pipe */
  memset(buffer, '\0', 5);
  if (read(filenum, buffer, 4) <= 0)
    FAIL;


  printf("port number is %s\n", buffer);

  close(filenum);
  unlink(PIPESTR); 

  return 0;
}