Skip to content

Commit

Permalink
Refactor log file name command line option parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
3cky committed Dec 25, 2023
1 parent 2b32d09 commit 3bed375
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "conn.h"
#include "queue.h"
#include "sig.h"
#include "util.h"
#ifdef LOG
# include "log.h"
#endif
Expand All @@ -56,20 +57,6 @@ ttydata_t tty;
/* Connections queue descriptor */
queue_t queue;

static int
main_is_empty(char *s)
{
char *p = s + strlen(s);
if (strlen(s) == 0)
return 1;
while (p > s && isspace((unsigned char )(*--p)))
{ //no-op
}
if (p == s && isspace((unsigned char )(*p)))
return 1;
return 0;
}

#ifndef HAVE_DAEMON
#include <fcntl.h>
#include <unistd.h>
Expand Down Expand Up @@ -273,25 +260,28 @@ main(int argc, char *argv[])
}
break;
case 'L':
if (main_is_empty(optarg))
{ /* report about invalid log file */
printf("%s: -L: missing logfile value\n", exename, optarg);
char *logfilenamevalue = strdup(optarg);
char *logfilename = util_trim(logfilenamevalue);
if (!strlen(logfilename))
{ /* report about empty log file */
printf("%s: -L: log file name is empty, exiting...\n", exename);
exit(-1);
}
else if (*optarg != '/')
else if (*logfilename != '/')
{
if (*optarg == '-')
if (*logfilename == '-')
{
/* logging to file disabled */
*cfg.logname = '\0';
}
else
{ /* concatenate given log file name with default path */
strncpy(cfg.logname, LOGPATH, INTBUFSIZE);
strncat(cfg.logname, optarg, INTBUFSIZE - strlen(cfg.logname));
strncat(cfg.logname, logfilename, INTBUFSIZE - strlen(cfg.logname));
}
}
else strncpy(cfg.logname, optarg, INTBUFSIZE);
else strncpy(cfg.logname, logfilename, INTBUFSIZE);
free(logfilenamevalue);
break;
#endif
case 'p':
Expand Down

0 comments on commit 3bed375

Please sign in to comment.