Skip to content

Commit d575ce7

Browse files
committed
Add flag to print temperature in Farenheit
1 parent a7162e3 commit d575ce7

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/main.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ void fill_output()
207207

208208
static const struct option longopts[] =
209209
{
210-
{ "help", no_argument, NULL, 'h' }
210+
{ "farenheit", no_argument, NULL, 'f' }
211+
, { "help", no_argument, NULL, 'h' }
211212
, { "nocolor", no_argument, NULL, 'n' }
212213
, { "reverse", no_argument, NULL, 'r' }
213214
, { "sort", required_argument, NULL, 's' }
@@ -246,6 +247,7 @@ static void print_help(const char* argv0)
246247
printf("Usage: %s [OPTION]...\n\n", progname);
247248
printf(
248249
"Mandatory arguments to long options are also mandatory for short options.\n"
250+
" -f --farenheit Use Farenheit temperatur unit\n"
249251
" -h --help Display this help message.\n"
250252
" -n --nocolor Do not display color codes.\n"
251253
" -r --reverse Reverse sort order.\n"
@@ -265,10 +267,13 @@ int main(int argc, char* argv[])
265267
int sort_dir = 1;
266268
int opt;
267269

268-
while (-1 != (opt = getopt_long(argc, argv, "hnrs:", longopts, NULL)))
270+
while (-1 != (opt = getopt_long(argc, argv, "fhnrs:", longopts, NULL)))
269271
{
270272
switch (opt)
271273
{
274+
case 'f':
275+
output_fmt_temp_unit = TEMP_UNIT_FARENHEIT;
276+
break;
272277
case 'r':
273278
sort_dir = -1;
274279
break;

src/output_fmt.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,18 @@ temp_conv_C(
236236
return temp_K - 273.0;
237237
}
238238

239+
double
240+
temp_conv_F(
241+
double temp_K
242+
)
243+
{
244+
return (((temp_K - 273.0) * 9.0) / 5.0) + 32.0;
245+
}
246+
239247
static const struct temp_param temp_params[_TEMP_UNIT_MAX] =
240248
{
241249
[TEMP_UNIT_CELCIUS] = { "C", temp_conv_C, 40.0, 49.0 }
250+
, [TEMP_UNIT_FARENHEIT] = { "F", temp_conv_F, 104.0, 120.2 }
242251
};
243252

244253
enum TEMP_UNIT output_fmt_temp_unit = TEMP_UNIT_CELCIUS;

src/output_fmt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum TEMP_UNIT
2424
{
2525
_TEMP_UNIT_FIRST = 0
2626
, TEMP_UNIT_CELCIUS = _TEMP_UNIT_FIRST
27+
, TEMP_UNIT_FARENHEIT
2728
, _TEMP_UNIT_MAX
2829
};
2930

0 commit comments

Comments
 (0)