Cleanups to touch.c

- use const where appropriate
	- use static where appropriate
	- use explicit checks checks for error conditions

Reviewed by:	sbruno
Approved by:	cperciva (mentor)
Obtained by:	DragonFlyBSD
This commit is contained in:
Eitan Adler 2013-04-23 13:03:14 +00:00
parent e99f8bc02e
commit 4145c5f17e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=249805

View File

@ -56,12 +56,12 @@ static const char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
#include <time.h>
#include <unistd.h>
void stime_arg1(char *, struct timeval *);
void stime_arg2(char *, int, struct timeval *);
void stime_darg(char *, struct timeval *);
void stime_file(char *, struct timeval *);
int timeoffset(char *);
void usage(char *);
static void stime_arg1(const char *, struct timeval *);
static void stime_arg2(const char *, int, struct timeval *);
static void stime_darg(const char *, struct timeval *);
static void stime_file(const char *, struct timeval *);
static int timeoffset(const char *);
static void usage(char *);
int
main(int argc, char *argv[])
@ -78,7 +78,7 @@ main(int argc, char *argv[])
Aflag = aflag = cflag = mflag = timeset = 0;
stat_f = stat;
utimes_f = utimes;
if (gettimeofday(&tv[0], NULL))
if (gettimeofday(&tv[0], NULL) == -1)
err(1, "gettimeofday");
while ((ch = getopt(argc, argv, "A:acd:fhmr:t:")) != -1)
@ -115,7 +115,6 @@ main(int argc, char *argv[])
timeset = 1;
stime_arg1(optarg, tv);
break;
case '?':
default:
usage(myname);
}
@ -235,8 +234,8 @@ main(int argc, char *argv[])
#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
void
stime_arg1(char *arg, struct timeval *tvp)
static void
stime_arg1(const char *arg, struct timeval *tvp)
{
time_t now;
struct tm *t;
@ -290,14 +289,17 @@ stime_arg1(char *arg, struct timeval *tvp)
t->tm_isdst = -1; /* Figure out DST. */
tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
if (tvp[0].tv_sec == -1)
terr: errx(1,
"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
goto terr;
tvp[0].tv_usec = tvp[1].tv_usec = 0;
return;
terr:
errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
}
void
stime_arg2(char *arg, int year, struct timeval *tvp)
static void
stime_arg2(const char *arg, int year, struct timeval *tvp)
{
time_t now;
struct tm *t;
@ -326,8 +328,8 @@ stime_arg2(char *arg, int year, struct timeval *tvp)
tvp[0].tv_usec = tvp[1].tv_usec = 0;
}
void
stime_darg(char *arg, struct timeval *tvp)
static void
stime_darg(const char *arg, struct timeval *tvp)
{
struct tm t = { .tm_sec = 0 };
const char *fmt, *colon;
@ -372,7 +374,7 @@ stime_darg(char *arg, struct timeval *tvp)
/* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
int
timeoffset(char *arg)
timeoffset(const char *arg)
{
int offset;
int isneg;
@ -400,8 +402,8 @@ timeoffset(char *arg)
return (offset);
}
void
stime_file(char *fname, struct timeval *tvp)
static void
stime_file(const char *fname, struct timeval *tvp)
{
struct stat sb;
@ -411,7 +413,7 @@ stime_file(char *fname, struct timeval *tvp)
TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtim);
}
void
static void
usage(char *myname)
{
fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "