Exemplo n.º 1
0
static void display_tfiles(struct tfile ** ents_array, int numents) {
    if (long_mode) {
        int widths[4] = {0,0,0,0};
        for (int i = 0; i < numents; i++) {
            update_column_widths(widths, ents_array[i]);
        }
        for (int i = 0; i < numents; i++) {
            print_entry_long(widths, ents_array[i]);
        }
    } else {
        /* Determine the gridding dimensions */
        int ent_max_len = 0;
        for (int i = 0; i < numents; i++) {
            ent_max_len = MAX(ent_max_len, strlen(ents_array[i]->name));
        }

        int col_ext = ent_max_len + MIN_COL_SPACING;
        int cols = ((term_width - ent_max_len) / col_ext) + 1;

        /* Print the entries */

        for (int i = 0; i < numents;) {

            /* Columns */
            print_entry(ents_array[i++], ent_max_len);

            for (int j = 0; (i < numents) && (j < (cols-1)); j++) {
                printf("  ");
                print_entry(ents_array[i++], ent_max_len);
            }

            printf("\n");
        }
    }
}
Exemplo n.º 2
0
Arquivo: ls.c Projeto: campaul/osdev
int main (int argc, char * argv[]) {

	/* Parse arguments */
	char * p = ".";
	int explicit_path_set = 0;
	int show_hidden = 0;
	int long_mode   = 0;

	if (argc > 1) {
		int index, c;
		while ((c = getopt(argc, argv, "al?")) != -1) {
			switch (c) {
				case 'a':
					show_hidden = 1;
					break;
				case 'l':
					long_mode = 1;
					break;
				case '?':
					show_usage(argc, argv);
					return 0;
			}
		}

		if (optind < argc) {
			p = argv[optind];
		}
	}

	/* Open the directory */
	DIR * dirp = opendir(p);
	if (dirp == NULL) {
		printf("no such directory\n");
		return -1;
	}

	/* Read the entries in the directory */
	list_t * ents_list = list_create();

	struct dirent * ent = readdir(dirp);
	while (ent != NULL) {
		if (show_hidden || (ent->d_name[0] != '.')) {
			struct dirent * entcpy = malloc(sizeof(struct dirent));
			memcpy(entcpy, ent, sizeof(struct dirent));
			list_insert(ents_list, (void *)entcpy);
		}

		ent = readdir(dirp);
	}
	closedir(dirp);

	/* Now, copy those entries into an array (for sorting) */
	struct dirent ** ents_array = malloc(sizeof(struct dirent *) * ents_list->length);
	int index = 0;
	node_t * node;
	foreach(node, ents_list) {
		ents_array[index++] = (struct dirent *)node->value;
	}
	list_free(ents_list);
	int numents = index;

	qsort(ents_array, numents, sizeof(struct dirent *), entcmp);

	if (long_mode) {
		printf("printing listing\n");
		for (int i = 0; i < numents; i++) {
			print_entry_long(ents_array[i]->d_name, p);
		}
	} else {
		/* Determine the gridding dimensions */
		int ent_max_len = 0;
		for (int i = 0; i < numents; i++) {
			ent_max_len = MAX(ent_max_len, strlen(ents_array[i]->d_name));
		}

		int term_width = DEFAULT_TERM_WIDTH;
		int term_height = DEFAULT_TERM_HEIGHT;

		/* This is a hack to determine the terminal with in a toaru terminal */
		printf("\033[1003z");
		fflush(stdout);
		scanf("%d,%d", &term_width, &term_height);
		term_width -= 1; /* And this just helps clean up our math */

		int col_ext = ent_max_len + MIN_COL_SPACING;
		int cols = ((term_width - ent_max_len) / col_ext) + 1;

		/* Print the entries */

		for (int i = 0; i < numents;) {

			/* Columns */
			print_entry(ents_array[i++]->d_name, p, ent_max_len);

			for (int j = 0; (i < numents) && (j < (cols-1)); j++) {
				printf("  ");
				print_entry(ents_array[i++]->d_name, p, ent_max_len);
			}

			printf("\n");
		}
	}

	free(ents_array);

	return 0;
}