/*	Copyright 2015-2026 Rivoreo

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
	IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
	OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
	THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <stdint.h>
#include <unistd.h>
#if 0
#include "syncrw.h"
#else
#include <errno.h>
#endif
#include <fcntl.h>
#include <stdio.h>

#define UFS_MAXNAMLEN 255

#define UFS_DT_UNKNOWN   0
#define UFS_DT_FIFO      1
#define UFS_DT_CHR       2
#define UFS_DT_DIR       4
#define UFS_DT_BLK       6
#define UFS_DT_REG       8
#define UFS_DT_LNK      10
#define UFS_DT_SOCK     12
#define UFS_DT_WHT      14

struct ufs_direntry {
	uint32_t d_ino;
	uint16_t d_reclen;
	uint8_t d_type;
	uint8_t d_namlen;
	char d_name[UFS_MAXNAMLEN + 1];
};

static const char ufs_type_chars[] = {
	[UFS_DT_UNKNOWN] = '?',
	[UFS_DT_FIFO] = 'p',
	[UFS_DT_CHR] = 'c',
	[UFS_DT_DIR] = 'd',
	[UFS_DT_BLK] = 'b',
	[UFS_DT_REG] = '-',
	[UFS_DT_LNK] = 'l',
	[UFS_DT_SOCK] = 's',
	[UFS_DT_WHT] = 'w',
	[3] = '?',
	[5] = '?',
	[7] = '?',
	[9] = '?',
	[11] = '?',
	[13] = '?'
};

static int sync_read(int fd, void *buffer, size_t count) {
	char *p = buffer;
	do {
		int s = read(fd, p, count);
		if(s < 0) {
			if(errno == EINTR) continue;
			return -1;
		}
		if(!s) return p - (char *)buffer;
		count -= s;
		p += s;
	} while(count > 0);
	return p - (char *)buffer;
}

int main(int argc, char **argv) {
	if(argc != 2) {
		fprintf(stderr, "Usage: %s <path>\n", argv[0]);
		return -1;
	}
	int fd = open(argv[1], O_RDONLY);
	if(fd == -1) {
		perror(argv[1]);
		return 1;
	}
	off_t offset = 0;
	while(1) {
		struct ufs_direntry de;
		int s = sync_read(fd, &de, 8);
		if(s < 8) break;
		if(de.d_reclen < 8) {
			fputs("Directory is corrupted\n", stderr);
			return 1;
		}
		if(de.d_namlen > de.d_reclen - 8) de.d_namlen = de.d_reclen - 8;
		s = sync_read(fd, de.d_name, de.d_namlen);
		if(s < de.d_namlen) break;
		de.d_name[de.d_namlen] = 0;
		if(!offset) puts("    OFFSET      INDEX T NAME");
		printf("%10ld %10u %c %s\n", (long int)offset, de.d_ino,
			de.d_type < sizeof ufs_type_chars ? ufs_type_chars[de.d_type] : '?',
			de.d_name);
		offset = lseek(fd, de.d_reclen - 8 - de.d_namlen, SEEK_CUR);
		if(offset < 0) {
			perror("lseek");
			return 1;
		}
	}
	return 0;
}
