From c1bd3b5824027f23b41bbac77c5e28f856c5dc18 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 21 Sep 2015 11:24:49 -0700 Subject: Eliminate global variables associated with builtin IO This change eliminates global variables like stdout_buffer. Instead we wrap up the IO information into a new struct io_streams_t, and thread that through every builtin. This makes the intent clearer, gives us a place to hang new IO data, and eliminates the ugly global state management like builtin_push_io. --- src/builtin_ulimit.cpp | 54 ++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) (limited to 'src/builtin_ulimit.cpp') diff --git a/src/builtin_ulimit.cpp b/src/builtin_ulimit.cpp index 64b126b7..3b2dadd6 100644 --- a/src/builtin_ulimit.cpp +++ b/src/builtin_ulimit.cpp @@ -139,21 +139,21 @@ static rlim_t get(int resource, int hard) /** Print the value of the specified resource limit */ -static void print(int resource, int hard) +static void print(int resource, int hard, io_streams_t &streams) { rlim_t l = get(resource, hard); if (l == RLIM_INFINITY) - stdout_buffer.append(L"unlimited\n"); + streams.out.append(L"unlimited\n"); else - append_format(stdout_buffer, L"%d\n", l / get_multiplier(resource)); + streams.out.append_format( L"%d\n", l / get_multiplier(resource)); } /** Print values of all resource limits */ -static void print_all(int hard) +static void print_all(int hard, io_streams_t &streams) { int i; int w=0; @@ -172,7 +172,7 @@ static void print_all(int hard) const wchar_t *unit = ((resource_arr[i].resource==RLIMIT_CPU)?L"(seconds, ":(get_multiplier(resource_arr[i].resource)==1?L"(":L"(kB, ")); - append_format(stdout_buffer, + streams.out.append_format( L"%-*ls %10ls-%lc) ", w, resource_arr[i].desc, @@ -181,11 +181,11 @@ static void print_all(int hard) if (l == RLIM_INFINITY) { - stdout_buffer.append(L"unlimited\n"); + streams.out.append(L"unlimited\n"); } else { - append_format(stdout_buffer, L"%d\n", l/get_multiplier(resource_arr[i].resource)); + streams.out.append_format( L"%d\n", l/get_multiplier(resource_arr[i].resource)); } } @@ -213,7 +213,7 @@ static const wchar_t *get_desc(int what) does _not_ multiply the limit value by the multiplier constant used by the commandline ulimit. */ -static int set(int resource, int hard, int soft, rlim_t value) +static int set(int resource, int hard, int soft, rlim_t value, io_streams_t &streams) { struct rlimit ls; getrlimit(resource, &ls); @@ -240,9 +240,9 @@ static int set(int resource, int hard, int soft, rlim_t value) if (setrlimit(resource, &ls)) { if (errno == EPERM) - append_format(stderr_buffer, L"ulimit: Permission denied when changing resource of type '%ls'\n", get_desc(resource)); + streams.err.append_format(L"ulimit: Permission denied when changing resource of type '%ls'\n", get_desc(resource)); else - builtin_wperror(L"ulimit"); + builtin_wperror(L"ulimit", streams); return 1; } return 0; @@ -252,7 +252,7 @@ static int set(int resource, int hard, int soft, rlim_t value) The ulimit builtin, used for setting resource limits. Defined in builtin_ulimit.c. */ -static int builtin_ulimit(parser_t &parser, wchar_t ** argv) +static int builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv) { wgetopter_t w; int hard=0; @@ -348,11 +348,10 @@ static int builtin_ulimit(parser_t &parser, wchar_t ** argv) case 0: if (long_options[opt_index].flag != 0) break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, + streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0], long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; @@ -416,11 +415,11 @@ static int builtin_ulimit(parser_t &parser, wchar_t ** argv) #endif case L'h': - builtin_print_help(parser, argv[0], stdout_buffer); + builtin_print_help(parser, streams, argv[0], streams.out); return 0; case L'?': - builtin_unknown_option(parser, argv[0], argv[w.woptind-1]); + builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]); return 1; } } @@ -429,13 +428,13 @@ static int builtin_ulimit(parser_t &parser, wchar_t ** argv) { if (argc - w.woptind == 0) { - print_all(hard); + print_all(hard, streams); } else { - stderr_buffer.append(argv[0]); - stderr_buffer.append(L": Too many arguments\n"); - builtin_print_help(parser, argv[0], stderr_buffer); + streams.err.append(argv[0]); + streams.err.append(L": Too many arguments\n"); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } @@ -449,7 +448,7 @@ static int builtin_ulimit(parser_t &parser, wchar_t ** argv) /* Show current limit value */ - print(what, hard); + print(what, hard, streams); break; } @@ -487,24 +486,23 @@ static int builtin_ulimit(parser_t &parser, wchar_t ** argv) new_limit = wcstol(argv[w.woptind], &end, 10); if (errno || *end) { - append_format(stderr_buffer, - L"%ls: Invalid limit '%ls'\n", + streams.err.append_format(L"%ls: Invalid limit '%ls'\n", argv[0], argv[w.woptind]); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } new_limit *= get_multiplier(what); } - return set(what, hard, soft, new_limit); + return set(what, hard, soft, new_limit, streams); } default: { - stderr_buffer.append(argv[0]); - stderr_buffer.append(L": Too many arguments\n"); - builtin_print_help(parser, argv[0], stderr_buffer); + streams.err.append(argv[0]); + streams.err.append(L": Too many arguments\n"); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } -- cgit v1.2.3