ls fix
This commit is contained in:
parent
22d75498df
commit
bc6e7d8a60
|
@ -37,19 +37,20 @@ struct d_node {
|
|||
};
|
||||
|
||||
/* Work with dir */
|
||||
struct d_node *stat_file(char *filename) {
|
||||
struct d_node *stat_file(char *filename, int lfile) {
|
||||
/* lfile its flag. 1 if passed file from list_one() */
|
||||
struct d_node *file = malloc(sizeof(struct d_node));
|
||||
if (file == NULL)
|
||||
return NULL;
|
||||
|
||||
if (mu_get_lstat("ls", filename, &file->stats)) {
|
||||
if (mu_get_stats("ls", !L_flag, filename, &file->stats)) {
|
||||
free(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
file->full_name = filename;
|
||||
file->name = strrchr(filename, '/');
|
||||
if (file->name == NULL)
|
||||
if (file->name == NULL || lfile)
|
||||
file->name = filename;
|
||||
|
||||
else
|
||||
|
@ -77,7 +78,7 @@ struct d_node **list(const char *path, size_t *nfiles) {
|
|||
if (full_path == NULL)
|
||||
continue;
|
||||
|
||||
cur = stat_file(full_path);
|
||||
cur = stat_file(full_path, 0);
|
||||
if (cur == NULL) {
|
||||
free(full_path);
|
||||
continue;
|
||||
|
@ -109,12 +110,16 @@ struct d_node **list(const char *path, size_t *nfiles) {
|
|||
return dir;
|
||||
}
|
||||
|
||||
struct d_node **list_file(const char *path) {
|
||||
struct d_node **list_one(const char *path) {
|
||||
struct d_node **dir = malloc(sizeof(struct d_node *));
|
||||
|
||||
char *new_path = strdup(path);
|
||||
if (new_path == NULL) {
|
||||
free(dir);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dir[0] = stat_file(new_path);
|
||||
dir[0] = stat_file(new_path, 1);
|
||||
if (dir[0] == NULL) {
|
||||
free(dir);
|
||||
return NULL;
|
||||
|
@ -256,18 +261,18 @@ int ls(const char *dir_name, int label, struct winsize w) {
|
|||
return 1;
|
||||
|
||||
struct d_node **dir = NULL;
|
||||
if (S_ISDIR(sb.st_mode))
|
||||
if (S_ISDIR(sb.st_mode) && !d_flag)
|
||||
dir = list(dir_name, &files);
|
||||
|
||||
else {
|
||||
dir = list_file(dir_name);
|
||||
dir = list_one(dir_name);
|
||||
files++;
|
||||
}
|
||||
|
||||
if (dir == NULL)
|
||||
return 1;
|
||||
|
||||
if (label || R_flag)
|
||||
if ((label || R_flag) && !d_flag)
|
||||
printf("\n%s:\n", dir_name);
|
||||
|
||||
/* pipe print */
|
||||
|
@ -293,7 +298,7 @@ int ls(const char *dir_name, int label, struct winsize w) {
|
|||
|
||||
int main(int argc, char **argv) {
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "1alFcRL")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "1alFcRdL")) != -1) {
|
||||
switch (opt) {
|
||||
case '1':
|
||||
O_flag = 1;
|
||||
|
@ -316,10 +321,12 @@ int main(int argc, char **argv) {
|
|||
break;
|
||||
|
||||
case 'R':
|
||||
d_flag = 0;
|
||||
R_flag = 1;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
R_flag = 0;
|
||||
d_flag = 1;
|
||||
break;
|
||||
|
||||
|
|
Loading…
Reference in New Issue