cut, mount: small improvements

This commit is contained in:
Denis Vlasenko 2006-10-12 22:42:33 +00:00
parent 9c267b851e
commit 372686bde7
4 changed files with 29 additions and 30 deletions

View File

@ -13,13 +13,11 @@
/* option vars */ /* option vars */
static const char optstring[] = "b:c:f:d:sn"; static const char optstring[] = "b:c:f:d:sn";
#define CUT_OPT_BYTE_FLGS (1<<0) #define CUT_OPT_BYTE_FLGS (1<<0)
#define CUT_OPT_CHAR_FLGS (1<<1) #define CUT_OPT_CHAR_FLGS (1<<1)
#define CUT_OPT_FIELDS_FLGS (1<<2) #define CUT_OPT_FIELDS_FLGS (1<<2)
#define CUT_OPT_DELIM_FLGS (1<<3) #define CUT_OPT_DELIM_FLGS (1<<3)
#define CUT_OPT_SUPPRESS_FLGS (1<<4) #define CUT_OPT_SUPPRESS_FLGS (1<<4)
static unsigned opt;
static char delim = '\t'; /* delimiter, default is tab */ static char delim = '\t'; /* delimiter, default is tab */
@ -61,7 +59,7 @@ static void cut_file(FILE * file)
int spos; int spos;
/* cut based on chars/bytes XXX: only works when sizeof(char) == byte */ /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
if ((opt & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS))) { if (option_mask32 & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS)) {
/* print the chars specified in each cut list */ /* print the chars specified in each cut list */
for (; cl_pos < nlists; cl_pos++) { for (; cl_pos < nlists; cl_pos++) {
spos = cut_lists[cl_pos].startpos; spos = cut_lists[cl_pos].startpos;
@ -115,7 +113,7 @@ static void cut_file(FILE * file)
/* does this line contain any delimiters? */ /* does this line contain any delimiters? */
if (strchr(line, delim) == NULL) { if (strchr(line, delim) == NULL) {
if (!(opt & CUT_OPT_SUPPRESS_FLGS)) if (!(option_mask32 & CUT_OPT_SUPPRESS_FLGS))
puts(line); puts(line);
goto next_line; goto next_line;
} }
@ -125,7 +123,6 @@ static void cut_file(FILE * file)
for (; cl_pos < nlists && line; cl_pos++) { for (; cl_pos < nlists && line; cl_pos++) {
spos = cut_lists[cl_pos].startpos; spos = cut_lists[cl_pos].startpos;
do { do {
/* find the field we're looking for */ /* find the field we're looking for */
while (line && ndelim < spos) { while (line && ndelim < spos) {
field = strsep(&line, delimiter); field = strsep(&line, delimiter);
@ -156,7 +153,7 @@ static void cut_file(FILE * file)
/* if we printed anything at all, we need to finish it with a /* if we printed anything at all, we need to finish it with a
* newline cuz we were handed a chomped line */ * newline cuz we were handed a chomped line */
putchar('\n'); putchar('\n');
next_line: next_line:
linenum++; linenum++;
free(printed); free(printed);
free(orig_line); free(orig_line);
@ -170,14 +167,13 @@ int cut_main(int argc, char **argv)
char *sopt, *ltok; char *sopt, *ltok;
opt_complementary = "b--bcf:c--bcf:f--bcf"; opt_complementary = "b--bcf:c--bcf:f--bcf";
opt = getopt32(argc, argv, optstring, &sopt, &sopt, &sopt, &ltok); getopt32(argc, argv, optstring, &sopt, &sopt, &sopt, &ltok);
if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS))) if (!(option_mask32 & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS)))
bb_error_msg_and_die bb_error_msg_and_die("expected a list of bytes, characters, or fields");
("expected a list of bytes, characters, or fields"); if (option_mask32 & BB_GETOPT_ERROR)
if (opt & BB_GETOPT_ERROR)
bb_error_msg_and_die("only one type of list may be specified"); bb_error_msg_and_die("only one type of list may be specified");
if ((opt & (CUT_OPT_DELIM_FLGS))) { if (option_mask32 & CUT_OPT_DELIM_FLGS) {
if (strlen(ltok) > 1) { if (strlen(ltok) > 1) {
bb_error_msg_and_die("the delimiter must be a single character"); bb_error_msg_and_die("the delimiter must be a single character");
} }
@ -185,8 +181,8 @@ int cut_main(int argc, char **argv)
} }
/* non-field (char or byte) cutting has some special handling */ /* non-field (char or byte) cutting has some special handling */
if (!(opt & CUT_OPT_FIELDS_FLGS)) { if (!(option_mask32 & CUT_OPT_FIELDS_FLGS)) {
if (opt & CUT_OPT_SUPPRESS_FLGS) { if (option_mask32 & CUT_OPT_SUPPRESS_FLGS) {
bb_error_msg_and_die bb_error_msg_and_die
("suppressing non-delimited lines makes sense%s", ("suppressing non-delimited lines makes sense%s",
_op_on_field); _op_on_field);
@ -251,10 +247,9 @@ int cut_main(int argc, char **argv)
bb_error_msg_and_die("invalid byte or field list"); bb_error_msg_and_die("invalid byte or field list");
/* add the new list */ /* add the new list */
cut_lists = cut_lists = xrealloc(cut_lists, sizeof(struct cut_list) * (++nlists));
xrealloc(cut_lists, sizeof(struct cut_list) * (++nlists)); cut_lists[nlists-1].startpos = s;
cut_lists[nlists - 1].startpos = s; cut_lists[nlists-1].endpos = e;
cut_lists[nlists - 1].endpos = e;
} }
/* make sure we got some cut positions out of all that */ /* make sure we got some cut positions out of all that */

View File

@ -124,9 +124,9 @@ static struct sort_key *add_key(void)
} }
#define GET_LINE(fp) (global_flags&FLAG_z) ? bb_get_chunk_from_file(fp,NULL) \ #define GET_LINE(fp) (global_flags&FLAG_z) ? bb_get_chunk_from_file(fp,NULL) \
: bb_get_chomped_line_from_file(fp) : bb_get_chomped_line_from_file(fp)
#else #else
#define GET_LINE(fp) bb_get_chomped_line_from_file(fp) #define GET_LINE(fp) bb_get_chomped_line_from_file(fp)
#endif #endif
/* Iterate through keys list and perform comparisons */ /* Iterate through keys list and perform comparisons */

View File

@ -41,6 +41,7 @@ char *bb_get_chunk_from_file(FILE * file, int *end)
free(linebuf); free(linebuf);
return NULL; return NULL;
} }
linebuf = xrealloc(linebuf, idx+1);
linebuf[idx] = 0; linebuf[idx] = 0;
} }
return linebuf; return linebuf;

View File

@ -172,8 +172,12 @@ static int parse_mount_options(char *options, char **unrecognized)
static llist_t *get_block_backed_filesystems(void) static llist_t *get_block_backed_filesystems(void)
{ {
char *fs, *buf, static const char *const filesystems[] = {
*filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0}; "/etc/filesystems",
"/proc/filesystems",
0
};
char *fs, *buf;
llist_t *list = 0; llist_t *list = 0;
int i; int i;
FILE *f; FILE *f;
@ -182,16 +186,15 @@ static llist_t *get_block_backed_filesystems(void)
f = fopen(filesystems[i], "r"); f = fopen(filesystems[i], "r");
if (!f) continue; if (!f) continue;
for (fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f)); while ((buf = bb_get_chomped_line_from_file(f)) != 0) {
free(buf)) if (!strncmp(buf, "nodev", 5) && isspace(buf[5]))
{ continue;
if (!strncmp(buf,"nodev",5) && isspace(buf[5])) continue; fs = buf;
while (isspace(*fs)) fs++; while (isspace(*fs)) fs++;
if (*fs=='#' || *fs=='*') continue; if (*fs=='#' || *fs=='*' || !*fs) continue;
if (!*fs) continue;
llist_add_to_end(&list,xstrdup(fs)); llist_add_to_end(&list, xstrdup(fs));
free(buf);
} }
if (ENABLE_FEATURE_CLEAN_UP) fclose(f); if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
} }