/*
 *	scritto da zero per CloseBSD, Estate 2026
 */

// dim 'dir' stampa intero contenuto?

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

void dimensioni(int);

void dimensioni(int size) {
	if (size < 1023)
		printf("%3d\t", size);
	else if (size < 1024 * 1024 - 1)
		printf("%3dKB\t", size / 1024);
	else if (size < 1024 * 1024 * 1024 - 1)
		printf("%3dMB\t", size / (1024 * 1024));
	else printf("%3dGB\t", size / (1024 * 1024 * 1024));
}

int main(int argc, char *argv[])
{
	struct stat st;

	if (argc == 1) {
		printf("%s: nessun argomento\n", argv[0]);
		return 1;
	}
	
	for (int i = 1; i < argc; i++) {
		if (access(argv[i], F_OK) == 0) {
			stat(argv[i], &st);
			dimensioni(st.st_size);
			printf("%s\n", argv[i]);
		} else printf("%s: file non esistente\n", argv[i]);
	} 
	return 0;
}
