Import version 1.8.3
[s390-tools.git] / tape390 / tape390_common.c
blobf4f1433e1c405e8385f999f90e2afcab5278feda
1 /*************************************************************************
3 * File...........: s390-tools/tape390/tape390_common.c
4 * Author(s)......: Frank Munzert <munzert@de.ibm.com>
5 * Michael Holzheu <holzheu@de.ibm.com>
7 * Copyright IBM Corp. 2006,2007
9 *************************************************************************/
11 #include <sys/stat.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include "tape390_common.h"
18 #define PROC_DEVICES_FILE "/proc/devices"
19 #define PROC_DEVICES_FILE_WIDTH 100
21 char *prog_name; /* Name of tool */
24 * Set name of tool
26 void set_prog_name(char *name)
28 prog_name = name;
32 * Check whether specified device node is tape device
34 int is_not_tape(char *device)
36 FILE* fh;
37 char line[PROC_DEVICES_FILE_WIDTH];
38 char last_line[PROC_DEVICES_FILE_WIDTH];
39 int found = 0;
40 struct stat stat_struct;
42 if (stat(device, &stat_struct)) {
43 ERRMSG("%s: Unable to get device status for "
44 "'%s'. \n", prog_name, device);
45 perror("");
46 return 1;
48 fh = fopen(PROC_DEVICES_FILE,"r");
49 if (!fh) {
50 ERRMSG("%s: WARNING: Cannot check for tape in file "
51 PROC_DEVICES_FILE ".\n", prog_name);
52 perror("");
53 return(0); /* check not possible, just continue */
55 while (!found && (fscanf(fh, "%s", line) != EOF)) {
56 if (strcmp(line, "tape") == 0)
57 found = 1;
58 else
59 strcpy(last_line, line);
61 fclose(fh);
62 if (found && (major(stat_struct.st_rdev) ==
63 (unsigned int) atoi(last_line)))
64 return (0);
65 else {
66 ERRMSG("%s: '%s' is not a tape device. \n", prog_name, device);
67 return 1;
72 * Open the tape device
74 int open_tape(char *device)
76 int fd;
77 fd = open(device,O_RDONLY);
78 if (fd < 0) {
79 ERRMSG("%s: Cannot open device %s.\n",
80 prog_name,device);
81 perror("");
82 exit(EXIT_MISUSE);
84 return fd;