#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libintl.h>
#include <time.h>
#include <pwd.h>
#include <auth_attr.h>
#include <auth_list.h>
#include <secdb.h>
#include "transport.h"
#include "util.h"
#include "mmc.h"
#include "msgs.h"
#include "misc_scsi.h"
#include "main.h"
#include "trackio.h"
#include "bstream.h"
char strbuf[81];
int priv_change_needed = 0;
void *
my_zalloc(size_t size)
{
void *ret;
ret = malloc(size);
if (ret == NULL) {
if (errno == EAGAIN) {
(void) sleep(1);
ret = malloc(size);
}
if (ret == NULL) {
(void) err_msg("%s\n", gettext(strerror(errno)));
(void) err_msg(gettext(
"Memory allocation failure, Exiting...\n"));
exit(1);
}
}
(void) memset(ret, 0, size);
return (ret);
}
int
str_print(char *str, int pos)
{
if ((pos > 0) && (pos < 80)) {
(void) memset(strbuf, 8, pos);
strbuf[pos] = 0;
(void) printf(strbuf);
(void) memset(strbuf, ' ', pos);
strbuf[pos] = 0;
(void) printf(strbuf);
(void) memset(strbuf, 8, pos);
strbuf[pos] = 0;
(void) printf(strbuf);
}
(void) printf("%s", str);
(void) fflush(stdout);
return (strlen(str));
}
void
print_trackio_error(struct trackio_error *te)
{
char *msg, *msg1;
msg = gettext("System could not supply data at the required rate.\n");
msg1 = gettext("Try using a lower speed for write\n");
switch (te->err_type) {
case TRACKIO_ERR_SYSTEM:
err_msg(gettext("System error: %s\n"), strerror(te->te_errno));
return;
case TRACKIO_ERR_TRANSPORT:
err_msg(gettext("Transport mechanism error:\n"));
if (te->status == 2) {
if ((te->key == 3) && (te->asc == 0x0c) &&
(te->ascq == 9)) {
err_msg(msg);
err_msg(msg1);
return;
}
if (te->key == 3) {
err_msg(gettext("Bad media.\n"));
return;
}
if (debug) {
err_msg("Sense key %x, asc/asq %x/%x\n",
te->key, te->asc, te->ascq);
} else {
err_msg(gettext("I/O error\n"));
}
return;
}
if (te->te_errno != 0)
err_msg("%s\n", strerror(te->te_errno));
return;
case TRACKIO_ERR_USER_ABORT:
err_msg(gettext("User abort.\n"));
return;
default:
err_msg(gettext("Unknown error type.\n"));
if (debug) {
err_msg("Trackio err type %d\n", te->err_type);
}
}
}
char *
get_err_str(void)
{
if (str_errno != 0)
return (str_errno_to_string(str_errno));
return (strerror(errno));
}
int
get_audio_type(char *ext)
{
if ((strcasecmp(ext, "au") == 0) ||
(strcasecmp(ext, "sun") == 0))
return (AUDIO_TYPE_SUN);
if ((strcasecmp(ext, "wav") == 0) ||
(strcasecmp(ext, "riff") == 0))
return (AUDIO_TYPE_WAV);
if (strcasecmp(ext, "cda") == 0)
return (AUDIO_TYPE_CDA);
if (strcasecmp(ext, "aur") == 0)
return (AUDIO_TYPE_AUR);
return (-1);
}
int progress_pos;
static uint64_t last_total;
time_t tm;
void
init_progress(void)
{
progress_pos = 0;
last_total = 0;
tm = time(NULL);
}
int
progress(int64_t arg, int64_t completed)
{
char s[BUFSIZE];
uint64_t total = (uint64_t)arg;
if (completed == -1) {
progress_pos = str_print("(flushing ...)", progress_pos+2);
return (0);
}
if (total == 0) {
if (tm != time(NULL)) {
tm = time(NULL);
(void) snprintf(s, BUFSIZE,
gettext("%d bytes written"), completed);
progress_pos = str_print(s, progress_pos);
}
} else {
total = (((uint64_t)completed) * 100)/total;
if (total == last_total)
return (0);
last_total = total;
if (total > 100) {
if (debug)
(void) printf("\nWrote more than 100 %% !!\n");
return (0);
}
if (total == 100) {
(void) snprintf(s, BUFSIZE, gettext("done.\n"));
} else {
(void) snprintf(s, BUFSIZE, "%d %%", (uint_t)total);
}
progress_pos = str_print(s, progress_pos);
}
return (0);
}
void
raise_priv(void)
{
if (priv_change_needed && (cur_uid != 0)) {
if (seteuid(0) == 0)
cur_uid = 0;
}
}
void
lower_priv(void)
{
if (priv_change_needed && (cur_uid == 0)) {
if (seteuid(ruid) == 0)
cur_uid = ruid;
}
}
int
check_auth(uid_t uid)
{
struct passwd *pw;
pw = getpwuid(uid);
if (pw == NULL) {
return (0);
}
if (chkauthattr(CDRW_AUTH, pw->pw_name) != 1) {
return (0);
} else {
return (1);
}
}
void
ms_delay(uint_t ms)
{
hrtime_t start, req;
start = gethrtime();
req = start + ((hrtime_t)ms * 1000000);
while (gethrtime() < req)
yield();
}