/*
 * (C) Copyright Fuji Xerox Co., Ltd. All rights are reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 2017/12/15	nobuo.iwata@fujixerox.co.jp 
 */

#ifndef __FXLSM_LIST_USER_H
#define __FXLSM_LIST_USER_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#define atomic_t int
#define ATOMIC_INIT(val) val
#define KERN_ERR "ERR "
#define KERN_WARNING "WRN "
#define KERN_INFO "INFO "
#define KERN_DEBUG "DBG "
#define likely(a) a
#define unlikely(a) a
#define printk(...) fprintf(stderr, ##__VA_ARGS__)
#define kmalloc(s, f) malloc(s)
#define krealloc(p, s, f) realloc(p, s)
#define kfree(p) free(p)
#define rwlock_t int
#define rwlock_init(l) do {} while (0)
#define read_lock(l) do {} while (0)
#define read_unlock(l) do {} while (0)
#define write_lock(l) do {} while (0)
#define write_unlock(l) do {} while (0)
#define simple_strtoul strtoul
#define __user
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
#define MAX_ERRNO 4095
#define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO)

static inline void atomic_set(atomic_t *p, int val)
{
	*p = val;
}

static inline int atomic_read(atomic_t *p)
{
	return *p;
}

static inline int test_bit(int nr, unsigned long *p)
{
	return ((*p & ((unsigned long)1 << nr)) != 0);
}

static inline void set_bit(int nr, unsigned long *p)
{
	*p |= ((unsigned long)1 << nr);
}

static inline int test_and_set_bit(int nr, unsigned long *p)
{
	int prev = test_bit(nr, p);

	set_bit(nr, p);
	return prev;
}

static inline void clear_bit(int nr, unsigned long *p)
{
	*p &= ~((unsigned long)1 << nr);
}

static inline int test_and_clear_bit(int nr, unsigned long *p)
{
	int prev = ((*p & ((unsigned long)1 << nr)) != 0);

	*p &= ~((unsigned long)1 << nr);
	return prev;
}

static inline void *ERR_PTR(long error)
{
	return (void *) error;
}

static inline long PTR_ERR(const void *ptr)
{
	return (long) ptr;
}

static inline long IS_ERR(const void *ptr)
{
	return IS_ERR_VALUE((unsigned long)ptr);
}

struct list_head {
	struct list_head *next, *prev;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }

static inline void INIT_LIST_HEAD(struct list_head *list)
{
	list->next = list->prev = list;
}

#define LIST_HEAD(name) \
	struct list_head name = LIST_HEAD_INIT(name)

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

#define container_of(ptr, type, member) ({		      \
	const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
	(type *)( (char *)__mptr - offsetof(type,member) );})

#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)

#define list_for_each_entry(pos, head, member)			  \
	for (pos = list_entry((head)->next, typeof(*pos), member);      \
	     &pos->member != (head);    \
	     pos = list_entry(pos->member.next, typeof(*pos), member))

#define list_for_each_entry_safe(pos, n, head, member)		  \
	for (pos = list_entry((head)->next, typeof(*pos), member),      \
		n = list_entry(pos->member.next, typeof(*pos), member); \
	     &pos->member != (head);				    \
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))

static inline void __list_add(struct list_head *_new,
			      struct list_head *prev,
			      struct list_head *next)
{
	next->prev = _new;
	_new->next = next;
	_new->prev = prev;
	prev->next = _new;
}

static inline void list_add_tail(struct list_head *_new, struct list_head *head)
{
	__list_add(_new, head->prev, head);
}

static inline void __list_del(struct list_head *prev, struct list_head *next)
{
	next->prev = prev;
	prev->next = next;
}

#define LIST_POISON1  ((void *) 0x00100100)
#define LIST_POISON2  ((void *) 0x00200200)

static inline void list_del(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	entry->next = (struct list_head*)LIST_POISON1;
	entry->prev = (struct list_head*)LIST_POISON2;
}

typedef void *mm_segment_t;

static inline mm_segment_t get_fs(void)
{
	return NULL;
}

static inline void set_fs(mm_segment_t fs)
{
}

static inline mm_segment_t get_ds(void)
{
	return NULL;
}

struct file {
	int fd;
	int f_pos;
};

static inline struct file *filp_open(const char *path, int flags, int mode)
{
	struct file *fp;

	fp = (struct file *)malloc(sizeof(struct file));
	if (!fp)
		goto err_out;
	fp->fd = open(path, flags);
	if (fp->fd < 0)
		goto err_free;
	return fp;
err_free:
	free(fp);
err_out:
	return (struct file *)-errno;
}

static inline int kread(struct file *fp, const char *buf, int len)
{
	return read(fp->fd, (void *)buf, len);
}

static inline int kwrite(struct file *fp, const char *buf, int len)
{
	ssize_t written = write(fp->fd, (void *)buf, len);
	return (written == len) ? 0 : -1;
}

static inline void filp_close(struct file *fp, int mode)
{
	close(fp->fd);
	free(fp);
}

static inline int fxlsm_get_comms(void)
{
	return 1;
}

static inline int fxlsm_get_lds(void)
{
	return 1;
}

struct fxlsm_list_paths;

void fxlsm_list_set_path(struct fxlsm_list_paths *paths,
		int id, const char *path);
void fxlsm_list_update_paths(struct fxlsm_list_paths *paths, int nullify);

#endif /* !__FXLSM_LIST_USER_H */
