Пример #1
0
static void update_column_widths(int * widths, struct tfile * file) {
	char tmp[256];
	int n;

	/* Links */
	TRACE("links");
	n = sprintf(tmp, "%d", file->statbuf.st_nlink);
	if (n > widths[0]) widths[0] = n;

	/* User */
	TRACE("user");
	n = print_username(tmp, file->statbuf.st_uid);
	if (n > widths[1]) widths[1] = n;

	/* Group */
	TRACE("group");
	n = print_username(tmp, file->statbuf.st_gid);
	if (n > widths[2]) widths[2] = n;

	/* File size */
	TRACE("file size");
	if (human_readable) {
		n = print_human_readable_size(tmp, file->statbuf.st_size);
	} else {
		n = sprintf(tmp, "%d", (int)file->statbuf.st_size);
	}
	if (n > widths[3]) widths[3] = n;
}
Пример #2
0
Файл: ls.c Проект: campaul/osdev
void print_entry_long(const char * filename, const char * srcpath) {
	/* Figure out full relpath */
	char * relpath = malloc(strlen(srcpath) + strlen(filename) + 2);
	sprintf(relpath, "%s/%s", srcpath, filename);

	/* Classify file */
	struct stat statbuf;
	stat(relpath, &statbuf);
	free(relpath);

	const char * ansi_color_str;
	if (S_ISDIR(statbuf.st_mode)) {
		// Directory
		ansi_color_str = DIR_COLOR;
	} else if (statbuf.st_mode & 0111) {
		// Executable
		ansi_color_str = EXE_COLOR;
	} else {
		// Something else
		ansi_color_str = REG_COLOR;
	}

	/* file permissions */
	if (S_ISLNK(statbuf.st_mode)) {
		printf("l");
	} else {
		printf( (S_ISDIR(statbuf.st_mode))  ? "d" : "-");
	}
	printf( (statbuf.st_mode & S_IRUSR) ? "r" : "-");
	printf( (statbuf.st_mode & S_IWUSR) ? "w" : "-");
	printf( (statbuf.st_mode & S_IXUSR) ? "x" : "-");
	printf( (statbuf.st_mode & S_IRGRP) ? "r" : "-");
	printf( (statbuf.st_mode & S_IWGRP) ? "w" : "-");
	printf( (statbuf.st_mode & S_IXGRP) ? "x" : "-");
	printf( (statbuf.st_mode & S_IROTH) ? "r" : "-");
	printf( (statbuf.st_mode & S_IWOTH) ? "w" : "-");
	printf( (statbuf.st_mode & S_IXOTH) ? "x" : "-");

	printf( " - "); /* number of links, not supported */

	print_username(statbuf.st_uid);
	printf("\t");
	print_username(statbuf.st_gid);
	printf("\t");

	printf(" %8d ", statbuf.st_size);

	char time_buf[80];
	struct tm * timeinfo;
	timeinfo = localtime(&statbuf.st_mtime);
	strftime(time_buf, 80, "%b %d  %Y", timeinfo);
	printf("%s ", time_buf);

	/* Print the file name */
	printf("\033[%sm%s\033[0m\n", ansi_color_str, filename);

}
Пример #3
0
static void print_entry_long(int * widths, struct tfile * file) {
	const char * ansi_color_str = color_str(&file->statbuf);

	/* file permissions */
	if (S_ISLNK(file->statbuf.st_mode))       { printf("l"); }
	else if (S_ISCHR(file->statbuf.st_mode))  { printf("c"); }
	else if (S_ISBLK(file->statbuf.st_mode))  { printf("b"); }
	else if (S_ISDIR(file->statbuf.st_mode))  { printf("d"); }
	else { printf("-"); }
	printf( (file->statbuf.st_mode & S_IRUSR) ? "r" : "-");
	printf( (file->statbuf.st_mode & S_IWUSR) ? "w" : "-");
	if (file->statbuf.st_mode & S_ISUID) {
		printf("s");
	} else {
		printf( (file->statbuf.st_mode & S_IXUSR) ? "x" : "-");
	}
	printf( (file->statbuf.st_mode & S_IRGRP) ? "r" : "-");
	printf( (file->statbuf.st_mode & S_IWGRP) ? "w" : "-");
	printf( (file->statbuf.st_mode & S_IXGRP) ? "x" : "-");
	printf( (file->statbuf.st_mode & S_IROTH) ? "r" : "-");
	printf( (file->statbuf.st_mode & S_IWOTH) ? "w" : "-");
	printf( (file->statbuf.st_mode & S_IXOTH) ? "x" : "-");

	printf( " %*d ", widths[0], file->statbuf.st_nlink); /* number of links, not supported */

	char tmp[100];
	print_username(tmp, file->statbuf.st_uid);
	printf("%-*s ", widths[1], tmp);
	print_username(tmp, file->statbuf.st_gid);
	printf("%-*s ", widths[2], tmp);

	if (human_readable) {
		print_human_readable_size(tmp, file->statbuf.st_size);
		printf("%*s ", widths[3], tmp);
	} else {
		printf("%*d ", widths[3], (int)file->statbuf.st_size);
	}

	char time_buf[80];
	struct tm * timeinfo = localtime((time_t*)&file->statbuf.st_mtime);
	if (timeinfo->tm_year == this_year) {
		strftime(time_buf, 80, "%b %d %H:%M", timeinfo);
	} else {
		strftime(time_buf, 80, "%b %d  %Y", timeinfo);
	}
	printf("%s ", time_buf);

	/* Print the file name */
	if (stdout_is_tty) {
		printf("\033[%sm%s\033[0m", ansi_color_str, file->name);
		if (S_ISLNK(file->statbuf.st_mode)) {
			const char * s = color_str(&file->statbufl);
			printf(" -> \033[%sm%s\033[0m", s, file->link);
		}
	} else {
		printf("%s", file->name);
		if (S_ISLNK(file->statbuf.st_mode)) {
			printf(" -> %s", file->link);
		}
	}

	printf("\n");
}
Пример #4
0
static void print_entry_long(int * widths, struct tfile * file) {
    const char * ansi_color_str;
    if (S_ISDIR(file->statbuf.st_mode)) {
        // Directory
        ansi_color_str = DIR_COLOR;
    } else if (file->statbuf.st_mode & S_ISUID) {
        ansi_color_str = SETUID_COLOR;
    } else if (file->statbuf.st_mode & 0111) {
        // Executable
        ansi_color_str = EXE_COLOR;
    } else if (S_ISBLK(file->statbuf.st_mode) || S_ISCHR(file->statbuf.st_mode)) {
        /* Device file */
        ansi_color_str = DEVICE_COLOR;
    } else {
        // Something else
        ansi_color_str = REG_COLOR;
    }

    /* file permissions */
    if (S_ISLNK(file->statbuf.st_mode))       {
        printf("l");
    }
    else if (S_ISCHR(file->statbuf.st_mode))  {
        printf("c");
    }
    else if (S_ISBLK(file->statbuf.st_mode))  {
        printf("b");
    }
    else if (S_ISDIR(file->statbuf.st_mode))  {
        printf("d");
    }
    else {
        printf("-");
    }
    printf( (file->statbuf.st_mode & S_IRUSR) ? "r" : "-");
    printf( (file->statbuf.st_mode & S_IWUSR) ? "w" : "-");
    if (file->statbuf.st_mode & S_ISUID) {
        printf("s");
    } else {
        printf( (file->statbuf.st_mode & S_IXUSR) ? "x" : "-");
    }
    printf( (file->statbuf.st_mode & S_IRGRP) ? "r" : "-");
    printf( (file->statbuf.st_mode & S_IWGRP) ? "w" : "-");
    printf( (file->statbuf.st_mode & S_IXGRP) ? "x" : "-");
    printf( (file->statbuf.st_mode & S_IROTH) ? "r" : "-");
    printf( (file->statbuf.st_mode & S_IWOTH) ? "w" : "-");
    printf( (file->statbuf.st_mode & S_IXOTH) ? "x" : "-");

    printf( " %*d ", widths[0], file->statbuf.st_nlink); /* number of links, not supported */

    char tmp[100];
    print_username(tmp, file->statbuf.st_uid);
    printf("%-*s ", widths[1], tmp);
    print_username(tmp, file->statbuf.st_gid);
    printf("%-*s ", widths[2], tmp);

    if (human_readable) {
        print_human_readable_size(tmp, file->statbuf.st_size);
        printf("%*s ", widths[3], tmp);
    } else {
        printf("%*d ", widths[3], file->statbuf.st_size);
    }

    char time_buf[80];
    struct tm * timeinfo = localtime(&file->statbuf.st_mtime);
    if (timeinfo->tm_year == this_year) {
        strftime(time_buf, 80, "%b %d %H:%M", timeinfo);
    } else {
        strftime(time_buf, 80, "%b %d  %Y", timeinfo);
    }
    printf("%s ", time_buf);

    /* Print the file name */
    if (stdout_is_tty) {
        printf("\033[%sm%s\033[0m", ansi_color_str, file->name);
    } else {
        printf("%s", file->name);
    }

    printf("\n");
}