misc.c
author nathan
Sat, 29 Dec 2007 15:23:49 +0100
branchtrunk
changeset 2 6bcb44b9edb1
parent 0 d85c12073dea
child 4 79da91042fcc
permissions -rw-r--r--
release 0.6.3
nathan@0
     1
/* misc.c
nathan@0
     2
Copyright (c) 2000-2002 Craig Condit, Stefan Hülswitt.
nathan@0
     3
nathan@0
     4
Redistribution and use in source and binary forms, with or without
nathan@0
     5
modification, are permitted provided that the following conditions are met: 
nathan@0
     6
nathan@0
     7
1. Redistributions of source code must retain the above copyright notice,
nathan@0
     8
   this list of conditions and the following disclaimer. 
nathan@0
     9
2. Redistributions in binary form must reproduce the above copyright notice,
nathan@0
    10
   this list of conditions and the following disclaimer in the documentation
nathan@0
    11
   and/or other materials provided with the distribution. 
nathan@0
    12
nathan@0
    13
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
nathan@0
    14
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
nathan@0
    15
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nathan@0
    16
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
nathan@0
    17
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nathan@0
    18
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nathan@0
    19
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nathan@0
    20
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
nathan@0
    21
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
nathan@0
    22
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
nathan@0
    23
SUCH DAMAGE.
nathan@0
    24
*/
nathan@0
    25
nathan@0
    26
#include <stdlib.h>
nathan@0
    27
#include <stdio.h>
nathan@0
    28
#include <unistd.h>
nathan@0
    29
#include <string.h>
nathan@0
    30
#include <sys/wait.h>
nathan@0
    31
#include <errno.h>
nathan@0
    32
nathan@0
    33
extern char *prg_name;
nathan@0
    34
nathan@0
    35
/****************************************************************************/
nathan@0
    36
nathan@0
    37
void error(char *text)
nathan@0
    38
{
nathan@0
    39
  fprintf(stderr,"%s: %s: %s\n",prg_name,text,strerror(errno));
nathan@0
    40
  exit(1);
nathan@0
    41
}
nathan@0
    42
nathan@0
    43
/****************************************************************************/
nathan@0
    44
nathan@0
    45
void serror(char *text)
nathan@0
    46
{
nathan@0
    47
  fprintf(stderr,"%s: %s\n",prg_name,text);
nathan@0
    48
  exit(1);
nathan@0
    49
}
nathan@0
    50
nathan@0
    51
/****************************************************************************/
nathan@0
    52
nathan@0
    53
int start_diskchange(char *multicmd, char *cd_dev)
nathan@0
    54
{
nathan@0
    55
int pid, status=0;
nathan@0
    56
char buffer[12];
nathan@0
    57
nathan@0
    58
if(multicmd) { /* we have an external script */
nathan@0
    59
  if((pid=fork())<0) error("Fork failed in diskchange");
nathan@0
    60
  if(pid==0) { /* child */
nathan@0
    61
    char *argv[3];
nathan@0
    62
nathan@0
    63
    dup2(2,0); dup2(2,1); /* duplicate stderr as stdin/stdout */
nathan@0
    64
    argv[0] = multicmd;
nathan@0
    65
    argv[1] = cd_dev;
nathan@0
    66
    argv[2] = 0;
nathan@0
    67
    execvp(multicmd, argv);
nathan@0
    68
    error("Starting diskchange command failed");
nathan@0
    69
    }
nathan@0
    70
nathan@0
    71
  while(1) {
nathan@0
    72
    if(waitpid(pid,&status,0)==-1) { if (errno != EINTR) { status=-1; break; } }
nathan@0
    73
    else break;
nathan@0
    74
    }
nathan@0
    75
  }
nathan@0
    76
else { /* use internal diskchange */
nathan@0
    77
  fprintf(stderr,"\n\nPlease insert next disk and press RETURN\n\n\n");
nathan@0
    78
  do {
nathan@0
    79
    pid=read(2,buffer,sizeof(buffer));
nathan@0
    80
    } while(pid==sizeof(buffer) && buffer[sizeof(buffer)-1]!='\n');
nathan@0
    81
  }
nathan@0
    82
return status;
nathan@0
    83
}