#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <cstack.h>
#include <ctype.h>
#include <tlm.h>
#include "tlm_proto.h"
cstack_t *
cstack_new(void)
{
cstack_t *stk;
if ((stk = ndmp_malloc(sizeof (cstack_t))) == NULL)
return (NULL);
return (stk);
}
void
cstack_delete(cstack_t *stk)
{
cstack_t *tmp;
if (stk == NULL) {
NDMP_LOG(LOG_DEBUG, "cstack_delete: invalid stack");
return;
}
while ((tmp = stk->next) != NULL) {
stk->next = tmp->next;
NDMP_LOG(LOG_DEBUG, "cstack_delete(element): 0x%p", tmp);
free(tmp);
}
NDMP_LOG(LOG_DEBUG, "cstack_delete: 0x%p", stk);
free(stk);
}
int
cstack_push(cstack_t *stk, void *data, int len)
{
cstack_t *stk_node;
if (stk == NULL) {
NDMP_LOG(LOG_DEBUG, "cstack_push: invalid stack");
return (-1);
}
if ((stk_node = ndmp_malloc(sizeof (cstack_t))) == NULL)
return (-1);
stk_node->data = data;
stk_node->len = len;
stk_node->next = stk->next;
stk->next = stk_node;
NDMP_LOG(LOG_DEBUG, "cstack_push(0x%p): 0x%p", stk, stk_node);
return (0);
}
int
cstack_pop(cstack_t *stk, void **data, int *len)
{
cstack_t *stk_node;
if (stk == NULL) {
NDMP_LOG(LOG_DEBUG, "cstack_pop: invalid stack");
return (-1);
}
if ((stk_node = stk->next) == NULL) {
NDMP_LOG(LOG_DEBUG, "cstack_pop: underflow");
return (-1);
}
if (data)
*data = stk_node->data;
if (len)
*len = stk_node->len;
stk->next = stk_node->next;
NDMP_LOG(LOG_DEBUG, "cstack_pop(0x%p): 0x%p", stk, stk_node);
free(stk_node);
return (0);
}
int
cstack_top(cstack_t *stk, void **data, int *len)
{
if (stk == NULL) {
NDMP_LOG(LOG_DEBUG, "cstack_pop: invalid stack");
return (-1);
}
if (stk->next == NULL) {
NDMP_LOG(LOG_DEBUG, "cstack_pop: underflow");
return (-1);
}
if (data)
*data = stk->next->data;
if (len)
*len = stk->next->len;
return (0);
}
boolean_t
match(char *patn, char *str)
{
for (; ; ) {
switch (*patn) {
case 0:
return (*str == 0);
case '?':
if (*str != 0) {
str++;
patn++;
continue;
}
return (FALSE);
case '*':
patn++;
if (*patn == 0)
return (TRUE);
while (*str) {
if (match(patn, str))
return (TRUE);
str++;
}
return (FALSE);
default:
if (*str != *patn)
return (FALSE);
str++;
patn++;
continue;
}
}
}
int
match_ci(char *patn, char *str)
{
if (strcmp(patn, "<") == 0) {
if ((strcmp(str, ".") == 0) || (strcmp(str, "..") == 0))
return (TRUE);
if (strchr(str, '.') == 0)
return (TRUE);
return (FALSE);
}
for (; ; ) {
switch (*patn) {
case 0:
return (*str == 0);
case '?':
if (*str != 0) {
str++;
patn++;
continue;
}
return (FALSE);
case '*':
patn++;
if (*patn == 0)
return (TRUE);
while (*str) {
if (match_ci(patn, str))
return (TRUE);
str++;
}
return (FALSE);
default:
if (*str != *patn) {
int c1 = *str;
int c2 = *patn;
c1 = tolower(c1);
c2 = tolower(c2);
if (c1 != c2)
return (FALSE);
}
str++;
patn++;
continue;
}
}
}
static boolean_t
parse_match(char line, char *seps)
{
char *sep = seps;
while (*sep != 0) {
if (*sep == line)
return (TRUE);
sep++;
}
return (FALSE);
}
char *
parse(char **line, char *seps)
{
char *start = *line;
while (**line != 0) {
*line = *line + 1;
if (parse_match(**line, seps)) {
while (parse_match(**line, seps)) {
**line = 0;
*line = *line + 1;
}
break;
}
}
return (start);
}
int
oct_atoi(char *p)
{
int v = 0;
int c;
while (*p == ' ')
p++;
while ('0' <= (c = *p++) && c <= '7') {
v <<= 3;
v += c - '0';
}
return (v);
}
char *
strupr(char *s)
{
char c;
unsigned char *p = (unsigned char *)s;
while (*p) {
c = toupper(*p);
*p++ = c;
}
return (s);
}
char *
trim_whitespace(char *buf)
{
char *p = buf;
char *q = buf;
if (buf == 0)
return (0);
while (*p && isspace(*p))
++p;
while ((*q = *p++) != 0)
++q;
if (q != buf) {
while ((--q, isspace(*q)) != 0)
*q = '\0';
}
return (buf);
}
char *
trim_name(char *nm)
{
while (*nm) {
if (*nm == '/') {
nm++;
continue;
}
if (*nm == '.' && nm[1] == '/' && nm[2]) {
nm += 2;
continue;
}
break;
}
return (nm);
}
char *
get_volname(char *path)
{
char *cp, *save;
int sp;
if (!path)
return (NULL);
if (!(save = strdup(path)))
return (NULL);
sp = strspn(path, "/");
if (*(path + sp) == '\0') {
free(save);
return (NULL);
}
if ((cp = strchr(save + sp, '/')))
*cp = '\0';
return (save);
}
boolean_t
fs_volexist(char *path)
{
struct stat64 st;
char *p;
if ((p = get_volname(path)) == NULL)
return (FALSE);
if (stat64(p, &st) != 0) {
free(p);
return (FALSE);
}
free(p);
return (TRUE);
}
int
tlm_tarhdr_size(void)
{
return (sizeof (tlm_tar_hdr_t));
}
struct full_dir_info *
dup_dir_info(struct full_dir_info *old_dir_info)
{
struct full_dir_info *new_dir_info;
new_dir_info = ndmp_malloc(sizeof (struct full_dir_info));
if (new_dir_info) {
bcopy(old_dir_info, new_dir_info,
sizeof (struct full_dir_info));
}
return (new_dir_info);
}
struct full_dir_info *
tlm_new_dir_info(struct fs_fhandle *fhp, char *dir, char *nm)
{
struct full_dir_info *fdip;
if (!(fdip = ndmp_malloc(sizeof (struct full_dir_info))))
return (NULL);
(void) memcpy(&fdip->fd_dir_fh, fhp, sizeof (fs_fhandle_t));
if (!tlm_cat_path(fdip->fd_dir_name, dir, nm)) {
free(fdip);
NDMP_LOG(LOG_DEBUG, "TAPE BACKUP Find> path too long [%s][%s]",
dir, nm);
return (NULL);
}
return (fdip);
}
int
sysattr_rdonly(char *name)
{
return (name && strcmp(name, SYSATTR_RDONLY) == 0);
}
int
sysattr_rw(char *name)
{
return (name && strcmp(name, SYSATTR_RW) == 0);
}