Move string_to_long() to misc.c

This commit is contained in:
Martin Lund 2022-03-11 13:33:35 +01:00
parent d7b038d4ef
commit e9d5a23129
4 changed files with 19 additions and 17 deletions

View file

@ -25,6 +25,7 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include <errno.h>
#include "error.h" #include "error.h"
#include "print.h" #include "print.h"
#include "options.h" #include "options.h"
@ -91,3 +92,19 @@ void delay(long ms)
nanosleep(&ts, NULL); nanosleep(&ts, NULL);
} }
long string_to_long(char *string)
{
long result;
char *end_token;
errno = 0;
result = strtol(string, &end_token, 10);
if ((errno != 0) || (*end_token != 0))
{
printf("Error: Invalid digit\n");
exit(EXIT_FAILURE);
}
return result;
}

View file

@ -24,3 +24,4 @@
char * current_time(void); char * current_time(void);
void delay(long ms); void delay(long ms);
long string_to_long(char *string);

View file

@ -32,6 +32,7 @@
#include <limits.h> #include <limits.h>
#include "options.h" #include "options.h"
#include "error.h" #include "error.h"
#include "misc.h"
/* Default options */ /* Default options */
struct option_t option = struct option_t option =
@ -80,22 +81,6 @@ void print_help(char *argv[])
printf("\n"); printf("\n");
} }
long string_to_long(char *string)
{
long result;
char *end_token;
errno = 0;
result = strtol(string, &end_token, 10);
if ((errno != 0) || (*end_token != 0))
{
printf("Error: Invalid digit\n");
exit(EXIT_FAILURE);
}
return result;
}
void parse_options(int argc, char *argv[]) void parse_options(int argc, char *argv[])
{ {
int c; int c;

View file

@ -56,5 +56,4 @@ struct option_t
extern struct option_t option; extern struct option_t option;
long string_to_long(char *string);
void parse_options(int argc, char *argv[]); void parse_options(int argc, char *argv[]);