Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,63 @@
/*
* Program seconds2string, C89 version.
*
* Read input from argv[1] or stdin, write output to stdout.
*/
#define _CRT_SECURE_NO_WARNINGS /* unlocks printf in Microsoft Visual Studio */
#include <stdio.h>
#include <stdlib.h>
/*
* Converting the number of seconds in a human-readable string.
* It is worth noting that direct output to stdout would be even simpler.
*/
char* seconds2string(unsigned long seconds)
{
int i;
const unsigned long s = 1;
const unsigned long m = 60 * s;
const unsigned long h = 60 * m;
const unsigned long d = 24 * h;
const unsigned long w = 7 * d;
const unsigned long coeff[5] = { w, d, h, m, s };
const char units[5][4] = { "wk", "d", "hr", "min", "sec" };
static char buffer[256];
char* ptr = buffer;
for ( i = 0; i < 5; i++ )
{
unsigned long value;
value = seconds / coeff[i];
seconds = seconds % coeff[i];
if ( value )
{
if ( ptr != buffer )
ptr += sprintf(ptr, ", ");
ptr += sprintf(ptr,"%lu %s",value,units[i]);
}
}
return buffer;
}
/*
* Main function for seconds2string program.
*/
int main(int argc, char argv[])
{
unsigned long seconds;
if ( (argc < 2) && scanf( "%lu", &seconds )
|| (argc >= 2) && sscanf( argv[1], "%lu", & seconds ) )
{
printf( "%s\n", seconds2string(seconds) );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}

View file

@ -0,0 +1,140 @@
#include <inttypes.h> /* requires c99 */
#include <stdbool.h> /* requires c99 */
#include <stdio.h>
#include <stdlib.h>
#define N_EL 5
uintmax_t sec_to_week(uintmax_t);
uintmax_t sec_to_day(uintmax_t);
uintmax_t sec_to_hour(uintmax_t);
uintmax_t sec_to_min(uintmax_t);
uintmax_t week_to_sec(uintmax_t);
uintmax_t day_to_sec(uintmax_t);
uintmax_t hour_to_sec(uintmax_t);
uintmax_t min_to_sec(uintmax_t);
char *format_sec(uintmax_t);
/* the primary function */
int main(int argc, char *argv[])
{
uintmax_t input;
char *a;
if(argc<2) {
printf("usage: %s #seconds\n", argv[0]);
return 1;
}
input = strtoumax(argv[1],(void *)0, 10 /*base 10*/);
if(input<1) {
printf("Bad input: %s\n", argv[1]);
printf("usage: %s #seconds\n", argv[0]);
return 1;
}
printf("Number entered: %" PRIuMAX "\n", input);
a = format_sec(input);
printf(a);
free(a);
return 0;
}
/* note: must free memory
* after using this function */
char *format_sec(uintmax_t input)
{
int i;
bool first;
uintmax_t weeks, days, hours, mins;
/*seconds kept in input*/
char *retval;
FILE *stream;
size_t size;
uintmax_t *traverse[N_EL]={&weeks,&days,
&hours,&mins,&input};
char *labels[N_EL]={"wk","d","hr","min","sec"};
weeks = sec_to_week(input);
input = input - week_to_sec(weeks);
days = sec_to_day(input);
input = input - day_to_sec(days);
hours = sec_to_hour(input);
input = input - hour_to_sec(hours);
mins = sec_to_min(input);
input = input - min_to_sec(mins);
/* input now has the remaining seconds */
/* open stream */
stream = open_memstream(&retval,&size);
if(stream == 0) {
fprintf(stderr,"Unable to allocate memory");
return 0;
}
/* populate stream */
first = true;
for(i=0;i<N_EL;i++) {
if ( *(traverse[i]) != 0 ) {
if(!first) {
fprintf(stream,", %" PRIuMAX " %s",
*(traverse[i]), labels[i]);
} else {
fprintf(stream,"%" PRIuMAX " %s",
*(traverse[i]), labels[i]);
}
fflush(stream);
first=false;
}
}
fprintf(stream,"\n");
fclose(stream);
return retval;
}
uintmax_t sec_to_week(uintmax_t seconds)
{
return sec_to_day(seconds)/7;
}
uintmax_t sec_to_day(uintmax_t seconds)
{
return sec_to_hour(seconds)/24;
}
uintmax_t sec_to_hour(uintmax_t seconds)
{
return sec_to_min(seconds)/60;
}
uintmax_t sec_to_min(uintmax_t seconds)
{
return seconds/60;
}
uintmax_t week_to_sec(uintmax_t weeks)
{
return day_to_sec(weeks*7);
}
uintmax_t day_to_sec(uintmax_t days)
{
return hour_to_sec(days*24);
}
uintmax_t hour_to_sec(uintmax_t hours)
{
return min_to_sec(hours*60);
}
uintmax_t min_to_sec(uintmax_t minutes)
{
return minutes*60;
}