eject: close file description after usage

Also reindent the switch statement. CID 702040.
This commit is contained in:
Philippe Saint-Pierre
2015-07-02 16:12:34 -04:00
parent e2a8ef11f6
commit 39ceaa9c28
+15 -6
View File
@@ -151,17 +151,17 @@ static int do_eject(char operation, char *device)
return 1; return 1;
} }
switch (operation) { switch (operation) {
case 'h':
return usage("eject");
case 'e': case 'e':
if (ioctl(fd, B_EJECT_DEVICE, NULL, 0) < 0) { if (ioctl(fd, B_EJECT_DEVICE, NULL, 0) < 0) {
perror(device); perror(device);
close(fd);
return 1; return 1;
} }
break; break;
case 'l': case 'l':
if (ioctl(fd, B_LOAD_MEDIA, NULL, 0) < 0) { if (ioctl(fd, B_LOAD_MEDIA, NULL, 0) < 0) {
perror(device); perror(device);
close(fd);
return 1; return 1;
} }
break; break;
@@ -169,6 +169,7 @@ static int do_eject(char operation, char *device)
bval = true; bval = true;
if (ioctl(fd, B_SCSI_PREVENT_ALLOW, &bval, sizeof(bval)) < 0) { if (ioctl(fd, B_SCSI_PREVENT_ALLOW, &bval, sizeof(bval)) < 0) {
perror(device); perror(device);
close(fd);
return 1; return 1;
} }
break; break;
@@ -176,12 +177,15 @@ static int do_eject(char operation, char *device)
bval = false; bval = false;
if (ioctl(fd, B_SCSI_PREVENT_ALLOW, &bval, sizeof(bval)) < 0) { if (ioctl(fd, B_SCSI_PREVENT_ALLOW, &bval, sizeof(bval)) < 0) {
perror(device); perror(device);
close(fd);
return 1; return 1;
} }
break; break;
case 'q': case 'q':
if (ioctl(fd, B_GET_MEDIA_STATUS, &devstatus, sizeof(devstatus)) < 0) { if (ioctl(fd, B_GET_MEDIA_STATUS, &devstatus, sizeof(devstatus))
< 0) {
perror(device); perror(device);
close(fd);
return 1; return 1;
} }
switch (devstatus) { switch (devstatus) {
@@ -193,8 +197,10 @@ static int do_eject(char operation, char *device)
} }
break; break;
case 's': case 's':
if (ioctl(fd, B_GET_MEDIA_STATUS, &devstatus, sizeof(devstatus)) < 0) { if (ioctl(fd, B_GET_MEDIA_STATUS, &devstatus, sizeof(devstatus))
< 0) {
perror(device); perror(device);
close(fd);
return 1; return 1;
} }
switch (devstatus) { switch (devstatus) {
@@ -202,12 +208,14 @@ static int do_eject(char operation, char *device)
case B_DEV_NO_MEDIA: case B_DEV_NO_MEDIA:
if (ioctl(fd, B_EJECT_DEVICE, NULL, 0) < 0) { if (ioctl(fd, B_EJECT_DEVICE, NULL, 0) < 0) {
perror(device); perror(device);
close(fd);
return 1; return 1;
} }
break; break;
case B_DEV_DOOR_OPEN: case B_DEV_DOOR_OPEN:
if (ioctl(fd, B_LOAD_MEDIA, NULL, 0) < 0) { if (ioctl(fd, B_LOAD_MEDIA, NULL, 0) < 0) {
perror(device); perror(device);
close(fd);
return 1; return 1;
} }
break; break;
@@ -215,9 +223,10 @@ static int do_eject(char operation, char *device)
perror(device); perror(device);
} }
break; break;
case 'h':
default: default:
usage("eject"); close(fd);
return 1; return usage("eject");
} }
close(fd); close(fd);
return 0; return 0;