diff options
author | "Vladimir N. Oleynik" <dzo@simtreas.ru> | 2005-09-05 15:06:57 +0000 |
---|---|---|
committer | "Vladimir N. Oleynik" <dzo@simtreas.ru> | 2005-09-05 15:06:57 +0000 |
commit | a8c23aa7959716bf33b41a890791eb7c3c4d3e74 (patch) | |
tree | b0003827ec2a5557889a51add98f710ba53c7ec8 | |
parent | 27421a1878847389391c1a43420baaccf57057a7 (diff) | |
download | busybox-w32-a8c23aa7959716bf33b41a890791eb7c3c4d3e74.tar.gz busybox-w32-a8c23aa7959716bf33b41a890791eb7c3c4d3e74.tar.bz2 busybox-w32-a8c23aa7959716bf33b41a890791eb7c3c4d3e74.zip |
- usage fresh bb_getopt_ulflags for ls -- very size reduce.
- small indent corrections
- remove unecessary malloc and free
-rw-r--r-- | coreutils/ls.c | 141 |
1 files changed, 64 insertions, 77 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c index 75d7b1f33..ac2557a55 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c | |||
@@ -83,15 +83,15 @@ enum { | |||
83 | 83 | ||
84 | /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */ | 84 | /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */ |
85 | /* what file information will be listed */ | 85 | /* what file information will be listed */ |
86 | #define LIST_INO (1U<<0) | 86 | #define LIST_INO (1U<<0) |
87 | #define LIST_BLOCKS (1U<<1) | 87 | #define LIST_BLOCKS (1U<<1) |
88 | #define LIST_MODEBITS (1U<<2) | 88 | #define LIST_MODEBITS (1U<<2) |
89 | #define LIST_NLINKS (1U<<3) | 89 | #define LIST_NLINKS (1U<<3) |
90 | #define LIST_ID_NAME (1U<<4) | 90 | #define LIST_ID_NAME (1U<<4) |
91 | #define LIST_ID_NUMERIC (1U<<5) | 91 | #define LIST_ID_NUMERIC (1U<<5) |
92 | #define LIST_CONTEXT (1U<<6) | 92 | #define LIST_CONTEXT (1U<<6) |
93 | #define LIST_SIZE (1U<<7) | 93 | #define LIST_SIZE (1U<<7) |
94 | #define LIST_DEV (1U<<8) | 94 | #define LIST_DEV (1U<<8) |
95 | #define LIST_DATE_TIME (1U<<9) | 95 | #define LIST_DATE_TIME (1U<<9) |
96 | #define LIST_FULLTIME (1U<<10) | 96 | #define LIST_FULLTIME (1U<<10) |
97 | #define LIST_FILENAME (1U<<11) | 97 | #define LIST_FILENAME (1U<<11) |
@@ -103,22 +103,22 @@ enum { | |||
103 | 103 | ||
104 | /* what files will be displayed */ | 104 | /* what files will be displayed */ |
105 | /* TODO -- We may be able to make DISP_NORMAL 0 to save a bit slot. */ | 105 | /* TODO -- We may be able to make DISP_NORMAL 0 to save a bit slot. */ |
106 | #define DISP_NORMAL (1U<<14) /* show normal filenames */ | 106 | #define DISP_NORMAL (1U<<14) /* show normal filenames */ |
107 | #define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */ | 107 | #define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */ |
108 | #define DISP_HIDDEN (1U<<16) /* show filenames starting with . */ | 108 | #define DISP_HIDDEN (1U<<16) /* show filenames starting with . */ |
109 | #define DISP_DOT (1U<<17) /* show . and .. */ | 109 | #define DISP_DOT (1U<<17) /* show . and .. */ |
110 | #define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */ | 110 | #define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */ |
111 | #define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */ | 111 | #define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */ |
112 | #define DISP_ROWS (1U<<20) /* print across rows */ | 112 | #define DISP_ROWS (1U<<20) /* print across rows */ |
113 | 113 | ||
114 | #define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_NORMAL - 1)) | 114 | #define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_NORMAL - 1)) |
115 | 115 | ||
116 | #ifdef CONFIG_FEATURE_LS_SORTFILES | 116 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
117 | /* how will the files be sorted */ | 117 | /* how will the files be sorted */ |
118 | #define SORT_ORDER_FORWARD 0 /* sort in reverse order */ | 118 | #define SORT_ORDER_FORWARD 0 /* sort in reverse order */ |
119 | #define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */ | 119 | #define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */ |
120 | 120 | ||
121 | #define SORT_NAME 0 /* sort by file name */ | 121 | #define SORT_NAME 0 /* sort by file name */ |
122 | #define SORT_SIZE (1U<<28) /* sort by file size */ | 122 | #define SORT_SIZE (1U<<28) /* sort by file size */ |
123 | #define SORT_ATIME (2U<<28) /* sort by last access time */ | 123 | #define SORT_ATIME (2U<<28) /* sort by last access time */ |
124 | #define SORT_CTIME (3U<<28) /* sort by last change time */ | 124 | #define SORT_CTIME (3U<<28) /* sort by last change time */ |
@@ -170,25 +170,25 @@ static int show_color = 0; | |||
170 | 170 | ||
171 | /* long option entry used only for --color, which has no short option | 171 | /* long option entry used only for --color, which has no short option |
172 | * equivalent. */ | 172 | * equivalent. */ |
173 | static int got_color_opt; | ||
174 | static struct option ls_color_opt[] = | 173 | static struct option ls_color_opt[] = |
175 | { | 174 | { |
176 | {"color", optional_argument, &got_color_opt, 1}, | 175 | {"color", optional_argument, NULL, 1}, |
177 | {NULL, 0, NULL, 0} | 176 | {NULL, 0, NULL, 0} |
178 | }; | 177 | }; |
179 | 178 | ||
180 | #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ | 179 | #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ |
181 | "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) | 180 | "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) |
182 | #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ | 181 | #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ |
183 | "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)]) | 182 | "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)]) |
184 | #endif | 183 | #endif |
185 | 184 | ||
186 | /* | 185 | /* |
187 | * a directory entry and its stat info are stored here | 186 | * a directory entry and its stat info are stored here |
188 | */ | 187 | */ |
189 | struct dnode { /* the basic node */ | 188 | struct dnode { /* the basic node */ |
190 | char *name; /* the dir entry name */ | 189 | char *name; /* the dir entry name */ |
191 | char *fullname; /* the dir entry name */ | 190 | char *fullname; /* the dir entry name */ |
191 | int allocated; | ||
192 | struct stat dstat; /* the file stat info */ | 192 | struct stat dstat; /* the file stat info */ |
193 | #ifdef CONFIG_SELINUX | 193 | #ifdef CONFIG_SELINUX |
194 | security_context_t sid; | 194 | security_context_t sid; |
@@ -235,8 +235,7 @@ static struct dnode *my_stat(char *fullname, char *name) | |||
235 | } | 235 | } |
236 | #endif | 236 | #endif |
237 | rc = stat(fullname, &dstat); | 237 | rc = stat(fullname, &dstat); |
238 | if(rc) | 238 | if(rc) { |
239 | { | ||
240 | bb_perror_msg("%s", fullname); | 239 | bb_perror_msg("%s", fullname); |
241 | status = EXIT_FAILURE; | 240 | status = EXIT_FAILURE; |
242 | return 0; | 241 | return 0; |
@@ -251,8 +250,7 @@ static struct dnode *my_stat(char *fullname, char *name) | |||
251 | } | 250 | } |
252 | #endif | 251 | #endif |
253 | rc = lstat(fullname, &dstat); | 252 | rc = lstat(fullname, &dstat); |
254 | if(rc) | 253 | if(rc) { |
255 | { | ||
256 | bb_perror_msg("%s", fullname); | 254 | bb_perror_msg("%s", fullname); |
257 | status = EXIT_FAILURE; | 255 | status = EXIT_FAILURE; |
258 | return 0; | 256 | return 0; |
@@ -320,11 +318,10 @@ static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs) | |||
320 | dirs = 0; | 318 | dirs = 0; |
321 | for (i = 0; i < nfiles; i++) { | 319 | for (i = 0; i < nfiles; i++) { |
322 | if (S_ISDIR(dn[i]->dstat.st_mode) | 320 | if (S_ISDIR(dn[i]->dstat.st_mode) |
323 | && (notsubdirs | 321 | && (notsubdirs || |
324 | || ((dn[i]->name[0] != '.') | 322 | ((dn[i]->name[0] != '.') || (dn[i]->name[1] |
325 | || (dn[i]->name[1] | ||
326 | && ((dn[i]->name[1] != '.') | 323 | && ((dn[i]->name[1] != '.') |
327 | || dn[i]->name[2]))))) | 324 | || dn[i]->name[2]))))) |
328 | dirs++; | 325 | dirs++; |
329 | } | 326 | } |
330 | return (dirs); | 327 | return (dirs); |
@@ -352,8 +349,7 @@ static struct dnode **dnalloc(int num) | |||
352 | if (num < 1) | 349 | if (num < 1) |
353 | return (NULL); | 350 | return (NULL); |
354 | 351 | ||
355 | p = (struct dnode **) xcalloc((size_t) num, | 352 | p = (struct dnode **) xcalloc((size_t) num, (size_t) (sizeof(struct dnode *))); |
356 | (size_t) (sizeof(struct dnode *))); | ||
357 | return (p); | 353 | return (p); |
358 | } | 354 | } |
359 | 355 | ||
@@ -367,7 +363,8 @@ static void dfree(struct dnode **dnp) | |||
367 | 363 | ||
368 | cur = dnp[0]; | 364 | cur = dnp[0]; |
369 | while (cur != NULL) { | 365 | while (cur != NULL) { |
370 | free(cur->fullname); /* free the filename */ | 366 | if(cur->allocated) |
367 | free(cur->fullname); /* free the filename */ | ||
371 | next = cur->next; | 368 | next = cur->next; |
372 | free(cur); /* free the dnode */ | 369 | free(cur); /* free the dnode */ |
373 | cur = next; | 370 | cur = next; |
@@ -622,6 +619,7 @@ static struct dnode **list_dir(const char *path) | |||
622 | cur = my_stat(fullname, strrchr(fullname, '/') + 1); | 619 | cur = my_stat(fullname, strrchr(fullname, '/') + 1); |
623 | if (!cur) | 620 | if (!cur) |
624 | continue; | 621 | continue; |
622 | cur->allocated = 1; | ||
625 | cur->next = dn; | 623 | cur->next = dn; |
626 | dn = cur; | 624 | dn = cur; |
627 | nfiles++; | 625 | nfiles++; |
@@ -756,7 +754,7 @@ static int list_single(struct dnode *dn) | |||
756 | safe_strncpy(context, dn->sid, len); | 754 | safe_strncpy(context, dn->sid, len); |
757 | freecon(dn->sid); | 755 | freecon(dn->sid); |
758 | }else { | 756 | }else { |
759 | safe_strncpy(context, "unknown",8); | 757 | safe_strncpy(context, "unknown", 8); |
760 | } | 758 | } |
761 | printf("%-32s ", context); | 759 | printf("%-32s ", context); |
762 | column += MAX(33, len); | 760 | column += MAX(33, len); |
@@ -952,6 +950,9 @@ static const unsigned opt_flags[] = { | |||
952 | #ifdef CONFIG_SELINUX | 950 | #ifdef CONFIG_SELINUX |
953 | LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */ | 951 | LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */ |
954 | #endif | 952 | #endif |
953 | #ifdef CONFIG_FEATURE_AUTOWIDTH | ||
954 | 0, 0, /* T, w - ignored */ | ||
955 | #endif | ||
955 | (1U<<31) | 956 | (1U<<31) |
956 | }; | 957 | }; |
957 | 958 | ||
@@ -977,6 +978,9 @@ extern int ls_main(int argc, char **argv) | |||
977 | char *tabstops_str = NULL; | 978 | char *tabstops_str = NULL; |
978 | char *terminal_width_str = NULL; | 979 | char *terminal_width_str = NULL; |
979 | #endif | 980 | #endif |
981 | #ifdef CONFIG_FEATURE_LS_COLOR | ||
982 | char *color_opt; | ||
983 | #endif | ||
980 | 984 | ||
981 | all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO | 985 | all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO |
982 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS | 986 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS |
@@ -1000,7 +1004,11 @@ extern int ls_main(int argc, char **argv) | |||
1000 | 1004 | ||
1001 | /* process options */ | 1005 | /* process options */ |
1002 | #ifdef CONFIG_FEATURE_AUTOWIDTH | 1006 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
1003 | opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str); | 1007 | opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str |
1008 | #ifdef CONFIG_FEATURE_LS_COLOR | ||
1009 | , &color_opt | ||
1010 | #endif | ||
1011 | ); | ||
1004 | if (tabstops_str) { | 1012 | if (tabstops_str) { |
1005 | tabstops = atoi(tabstops_str); | 1013 | tabstops = atoi(tabstops_str); |
1006 | } | 1014 | } |
@@ -1008,11 +1016,16 @@ extern int ls_main(int argc, char **argv) | |||
1008 | terminal_width = atoi(terminal_width_str); | 1016 | terminal_width = atoi(terminal_width_str); |
1009 | } | 1017 | } |
1010 | #else | 1018 | #else |
1011 | opt = bb_getopt_ulflags(argc, argv, ls_options); | 1019 | opt = bb_getopt_ulflags(argc, argv, ls_options |
1020 | #ifdef CONFIG_FEATURE_LS_COLOR | ||
1021 | , &color_opt | ||
1022 | #endif | ||
1023 | ); | ||
1012 | #endif | 1024 | #endif |
1013 | for (i = 0; opt_flags[i] != (1U<<31); i++) { | 1025 | for (i = 0; opt_flags[i] != (1U<<31); i++) { |
1014 | if (opt & (1 << i)) { | 1026 | if (opt & (1 << i)) { |
1015 | unsigned int flags = opt_flags[i]; | 1027 | unsigned int flags = opt_flags[i]; |
1028 | |||
1016 | if (flags & LIST_MASK_TRIGGER) { | 1029 | if (flags & LIST_MASK_TRIGGER) { |
1017 | all_fmt &= ~LIST_MASK; | 1030 | all_fmt &= ~LIST_MASK; |
1018 | } | 1031 | } |
@@ -1045,51 +1058,28 @@ extern int ls_main(int argc, char **argv) | |||
1045 | } | 1058 | } |
1046 | 1059 | ||
1047 | #ifdef CONFIG_FEATURE_LS_COLOR | 1060 | #ifdef CONFIG_FEATURE_LS_COLOR |
1048 | if (got_color_opt) { | 1061 | { |
1049 | /* there is no way for bb_getopt_ulflags() to | 1062 | /* find color bit value - last position for short getopt */ |
1050 | * return us the argument string for long options | 1063 | |
1051 | * which don't have a short option equivalent. | ||
1052 | * all we can find out is that the option was | ||
1053 | * present, and we have to rescan to find the | ||
1054 | * argument string. | ||
1055 | */ | ||
1056 | got_color_opt=0; | ||
1057 | optind = 1; | ||
1058 | while ((i = getopt_long (argc, argv, ls_options, | ||
1059 | ls_color_opt, NULL)) >= 0) { | ||
1060 | if (i != 0) continue; | ||
1061 | if (got_color_opt) { | ||
1062 | if (!optarg || strcmp("always", optarg) == 0) | ||
1063 | show_color = 1; | ||
1064 | else if (strcmp("never", optarg) == 0) | ||
1065 | show_color = 0; | ||
1066 | else if (strcmp("auto", optarg) == 0 && | ||
1067 | isatty(STDOUT_FILENO)) | ||
1068 | show_color = 1; | ||
1069 | |||
1070 | /* don't break; want to a) pick up repeated | ||
1071 | * --color options, and b) leave optind | ||
1072 | * set correctly when we're done. | ||
1073 | */ | ||
1074 | got_color_opt = 0; | ||
1075 | } | ||
1076 | } | ||
1077 | #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT | 1064 | #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT |
1078 | } else { | ||
1079 | /* if no option set by user, then this config option | ||
1080 | * forces "auto", which is what busybox 1.00 and previous | ||
1081 | * did. however, provide one more "out" for users that | ||
1082 | * don't want color: if LS_COLOR is set, and is null or | ||
1083 | * "none" -- then default coloring to "off". | ||
1084 | */ | ||
1085 | char *p; | 1065 | char *p; |
1066 | |||
1086 | if ((p = getenv ("LS_COLORS")) != NULL && | 1067 | if ((p = getenv ("LS_COLORS")) != NULL && |
1087 | (*p == '\0' || (strcmp(p, "none") == 0))) { | 1068 | (*p == '\0' || (strcmp(p, "none") == 0))) { |
1088 | show_color = 0; | 1069 | ; |
1089 | } else if (isatty(STDOUT_FILENO)) { | 1070 | } else if (isatty(STDOUT_FILENO)) { |
1090 | show_color = 1; | 1071 | show_color = 1; |
1091 | } | 1072 | } |
1092 | #endif | 1073 | #endif |
1074 | |||
1075 | if((opt & (1 << i))) { /* next flag after short options */ | ||
1076 | if (color_opt == NULL || strcmp("always", color_opt) == 0) | ||
1077 | show_color = 1; | ||
1078 | else if (color_opt != NULL && strcmp("never", color_opt) == 0) | ||
1079 | show_color = 0; | ||
1080 | else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO)) | ||
1081 | show_color = 1; | ||
1082 | } | ||
1093 | } | 1083 | } |
1094 | #endif | 1084 | #endif |
1095 | 1085 | ||
@@ -1129,14 +1119,12 @@ extern int ls_main(int argc, char **argv) | |||
1129 | */ | 1119 | */ |
1130 | ac = argc - optind; /* how many cmd line args are left */ | 1120 | ac = argc - optind; /* how many cmd line args are left */ |
1131 | if (ac < 1) { | 1121 | if (ac < 1) { |
1132 | av = (char **) xcalloc((size_t) 1, (size_t) (sizeof(char *))); | 1122 | static const char * const dotdir[] = { "." }; |
1133 | av[0] = bb_xstrdup("."); | 1123 | |
1124 | av = (char **) dotdir; | ||
1134 | ac = 1; | 1125 | ac = 1; |
1135 | } else { | 1126 | } else { |
1136 | av = (char **) xcalloc((size_t) ac, (size_t) (sizeof(char *))); | 1127 | av = argv + optind; |
1137 | for (oi = 0; oi < ac; oi++) { | ||
1138 | av[oi] = argv[optind++]; /* copy pointer to real cmd line arg */ | ||
1139 | } | ||
1140 | } | 1128 | } |
1141 | 1129 | ||
1142 | /* now, everything is in the av array */ | 1130 | /* now, everything is in the av array */ |
@@ -1146,11 +1134,10 @@ extern int ls_main(int argc, char **argv) | |||
1146 | /* stuff the command line file names into an dnode array */ | 1134 | /* stuff the command line file names into an dnode array */ |
1147 | dn = NULL; | 1135 | dn = NULL; |
1148 | for (oi = 0; oi < ac; oi++) { | 1136 | for (oi = 0; oi < ac; oi++) { |
1149 | char *fullname = bb_xstrdup(av[oi]); | 1137 | cur = my_stat(av[oi], av[oi]); |
1150 | |||
1151 | cur = my_stat(fullname, fullname); | ||
1152 | if (!cur) | 1138 | if (!cur) |
1153 | continue; | 1139 | continue; |
1140 | cur->allocated = 0; | ||
1154 | cur->next = dn; | 1141 | cur->next = dn; |
1155 | dn = cur; | 1142 | dn = cur; |
1156 | nfiles++; | 1143 | nfiles++; |