*
*/
-//#include "i18n.h"
#include "linux_version.h"
#include "c.h"
#include "nls.h"
#include <fcntl.h>
#include <limits.h>
#include <err.h>
+#include <stdarg.h>
#include <getopt.h>
#include <errno.h>
int a_arg = 0;
int c_arg = 0;
int x_arg = 0;
-char *programName; /* used in error messages */
+
/*
* These are the basenames of devices which can have multiple
else if (!strcmp(optarg, "on"))
a_arg = 1;
else {
- fprintf(stderr, _("%s: invalid argument to --auto/-a option\n"), programName);
- exit(1);
+ errx(1, _("invalid argument to --auto/-a option"));
}
break;
case 'c':
else {
c_arg = atoi(optarg);
if (c_arg <= 0) {
- fprintf(stderr, _("%s: invalid argument to --changerslot/-c option\n"), programName);
- exit(1);
+ errx(1, _("invalid argument to --changerslot/-c option"));
}
}
break;
else {
x_arg = atoi(optarg);
if (x_arg <= 0) {
- fprintf(stderr, _("%s: invalid argument to --cdspeed/-x option\n"), programName);
- exit(1);
+ errx(1, _("%s: invalid argument to --cdspeed/-x option"));
}
}
break;
}
/* check for a single additional argument */
if ((argc - optind) > 1) {
- fprintf(stderr, _("%s: too many arguments\n"), programName);
- exit(1);
+ errx(1, _("%s: too many arguments"));
}
if ((argc - optind) == 1) { /* one argument */
*device = strdup(argv[optind]);
buf = (char *) malloc(strlen(name)+14); /* to allow for "/dev/cdroms/ + "0" + null */
if (buf==NULL) {
- fprintf(stderr, _("%s: could not allocate memory\n"), programName);
- exit(1);
+ errx(1, _("could not allocate memory"));
}
if ((name[0] == '.') || (name[0] == '/')) {
strcpy(buf, name);
status = ioctl(fd, CDROMEJECT_SW, onOff);
if (status != 0) {
- fprintf(stderr, _("%s: CD-ROM auto-eject command failed: %s\n"), programName, strerror(errno));
- exit(1);
+ err(1, _("CD-ROM auto-eject command failed"));
}
}
#ifdef CDROM_SELECT_DISC
status = ioctl(fd, CDROM_SELECT_DISC, slot);
if (status < 0) {
- fprintf(stderr, _("%s: CD-ROM select disc command failed: %s\n"), programName, strerror(errno));
- exit(1);
+ err(1, _("CD-ROM select disc command failed"));
}
#elif defined CDROMLOADFROMSLOT
status = ioctl(fd, CDROMLOADFROMSLOT, slot);
if (status != 0) {
- fprintf(stderr, _("%s: CD-ROM load from slot command failed: %s\n"), programName, strerror(errno));
- exit(1);
+ err(1, _("CD-ROM load from slot command failed"));
}
#else
- fprintf(stderr, _("%s: IDE/ATAPI CD-ROM changer not supported by this kernel\n"), programName);
+ warnx( _("IDE/ATAPI CD-ROM changer not supported by this kernel\n") );
#endif
}
#ifdef CDROMCLOSETRAY
status = ioctl(fd, CDROMCLOSETRAY);
if (status != 0) {
- fprintf(stderr, _("%s: CD-ROM tray close command failed: %s\n"), programName, strerror(errno));
- exit(1);
+ err(1, _("CD-ROM tray close command failed"));
}
#else
- fprintf(stderr, _("%s: CD-ROM tray close command not supported by this kernel\n"), programName);
+ warnx( _("CD-ROM tray close command not supported by this kernel\n"));
#endif
}
CloseTray(fd);
#else
- fprintf(stderr, _("%s: CD-ROM tray toggle command not supported by this kernel\n"), programName);
+ warnx( _("CD-ROM tray toggle command not supported by this kernel"));
#endif
}
#ifdef CDROM_SELECT_SPEED
status = ioctl(fd, CDROM_SELECT_SPEED, speed);
if (status != 0) {
- fprintf(stderr, _("%s: CD-ROM select speed command failed: %s\n"), programName, strerror(errno));
- exit(1);
+ err(1, _("CD-ROM select speed command failed"));
}
#else
- fprintf(stderr, _("%s: CD-ROM select speed command not supported by this kernel\n"), programName);
+ warnx( _("CD-ROM select speed command not supported by this kernel"));
#endif
}
execl("/bin/umount", "/bin/umount", fullName, "-n", NULL);
else
execl("/bin/umount", "/bin/umount", fullName, NULL);
- fprintf(stderr, _("%s: unable to exec /bin/umount of `%s': %s\n"),
- programName, fullName, strerror(errno));
- exit(1);
+ errx(1, _("unable to exec /bin/umount of `%s'"), fullName);
break;
case -1:
- fprintf(stderr, _("%s: unable to fork: %s\n"), programName, strerror(errno));
+ warn( _("unable to fork"));
break;
default: /* parent */
wait(&status);
if (WIFEXITED(status) == 0) {
- fprintf(stderr, _("%s: unmount of `%s' did not exit normally\n"), programName, fullName);
- exit(1);
+ errx(1, _("unmount of `%s' did not exit normally\n"), fullName);
}
if (WEXITSTATUS(status) != 0) {
- fprintf(stderr, _("%s: unmount of `%s' failed\n"), programName, fullName);
- exit(1);
+ errx(1, _("unmount of `%s' failed\n"), fullName);
}
break;
}
{
int fd = open(fullName, O_RDONLY|O_NONBLOCK);
if (fd == -1) {
- fprintf(stderr, _("%s: unable to open `%s'\n"), programName, fullName);
- exit(1);
+ errx(1 , _("unable to open `%s'\n"), fullName);
}
return fd;
}
fp = fopen((p_option ? "/proc/mounts" : "/etc/mtab"), "r");
if (fp == NULL)
{
- fprintf(stderr, _("unable to open %s: %s\n"), (p_option ? "/proc/mounts" : "/etc/mtab"), strerror(errno));
- exit(1);
+ err(1, _("unable to open %s"), (p_option ? "/proc/mounts" : "/etc/mtab"));
}
while (fgets(line, sizeof(line), fp) != 0) {
/* fprintf(stderr, _("%s: unable to open /etc/fstab: %s\n"), programName, strerror(errno));
exit(1);*/
if (v_option) {
- printf( _("%s: unable to open /etc/fstab: %s\n"), programName, strerror(errno));
+ warn( _("unable to open /etc/fstab") );
}
return -1;
}
int status;
if (regcomp(&preg, pattern, REG_EXTENDED)!=0) {
- perror(programName);
- exit(1);
+ err(0, _("regcomp"));
}
fp = fopen((p_option ? "/proc/mounts" : "/etc/mtab"), "r");
if (fp == NULL)
{
- fprintf(stderr, _("unable to open %s: %s\n"),(p_option ? "/proc/mounts" : "/etc/mtab"), strerror(errno));
- exit(1);
+ err(1, _("unable to open %s"),(p_option ? "/proc/mounts" : "/etc/mtab"));
}
while (fgets(line, sizeof(line), fp) != 0) {
status = regexec(&preg, s1, 0, 0, 0);
if (status == 0) {
if (v_option)
- printf(_("%s: unmounting `%s'\n"), programName, s1);
+ printf(_("%s: unmounting `%s'\n"), program_invocation_short_name, s1);
Unmount(s1);
regfree(&preg);
}
result[strlen(partitionDevice[i]) + 6] = 0;
strcat(result, "([0-9]?[0-9])?$");
if (v_option)
- printf(_("%s: `%s' is a multipartition device\n"), programName, name);
+ printf(_("%s: `%s' is a multipartition device\n"), program_invocation_short_name, name);
return result;
}
}
if (v_option)
- printf(_("%s: `%s' is not a multipartition device\n"), programName, name);
+ printf(_("%s: `%s' is not a multipartition device\n"), program_invocation_short_name, name);
return 0;
}
if (v_option)
{
if (x_arg == 0)
- printf(_("%s: setting CD-ROM speed to auto\n"), programName);
+ printf(_("%s: setting CD-ROM speed to auto\n"), program_invocation_short_name);
else
- printf(_("%s: setting CD-ROM speed to %dX\n"), programName, x_arg);
+ printf(_("%s: setting CD-ROM speed to %dX\n"), program_invocation_short_name, x_arg);
}
fd = OpenDevice(deviceName);
SelectSpeedCdrom(fd, x_arg);
/* main program */
int main(int argc, char **argv)
{
+
const char *defaultDevice = EJECT_DEFAULT_DEVICE; /* default if no name passed by user */
int worked = 0; /* set to 1 when successfully ejected */
char *device = 0; /* name passed from user */
bindtextdomain("eject",LOCALEDIR);
/* program name is global variable used by other procedures */
- programName = strdup(argv[0]);
+ char *programName = program_invocation_short_name;
/* parse the command line arguments */
parse_args(argc, argv, &device);
}
if (!worked) {
- fprintf(stderr, _("%s: unable to eject, last error: %s\n"), programName, strerror(errno));
- exit(1);
+ err(1, _("unable to eject, last error"), programName, strerror(errno));
}
/* cleanup */