patch for cal.c

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

patch for cal.c

Han Boetes
Hi,

In my learn my self to code I made the following patch. I rather
enjoyed creating it.

I made the following changes:

- sizeof foo -> sizeof(foo)
- Add return in void functions.
- Make code easier to understand.
- Clarify variable names.
- Underscore current date.

Questions: I got the feeling I can make the ``*p++ = '\e';''
series easier to implement. I just don't see how.


Index: cal.c
===================================================================
RCS file: /cvs/src/usr.bin/cal/cal.c,v
retrieving revision 1.18
diff -u -p -r1.18 cal.c
--- cal.c 8 Dec 2005 14:54:30 -0000 1.18
+++ cal.c 20 Mar 2006 09:26:19 -0000
@@ -117,28 +117,37 @@ const char *j_day_headings = " Su  Mo  T
 #define leap_years_since_year_1(yr) \
  ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
 
-int julian;
-
 void ascii_day(char *, int);
 void center(const char *, int, int);
 void day_array(int, int, int *);
 int day_in_week(int, int, int);
 int day_in_year(int, int, int);
-void j_yearly(int);
+void yearly(int);
 void monthly(int, int);
+void j_yearly(int);
+void j_monthly(int, int);
 void trim_trailing_spaces(char *);
-void usage(void);
-void yearly(int);
 int parsemonth(const char *);
+void usage(void);
+
+int     julian, mdate_today, jdate_today, month_today, year_today;
 
 int
 main(int argc, char *argv[])
 {
  struct tm *local_time;
  time_t now;
- int ch, month, year, yflag;
+ int ch, month, year, yflag = 0;
+
+ (void)time(&now);
+ local_time = localtime(&now);
+ month = local_time->tm_mon + 1;
+ month_today = month;
+ year = local_time->tm_year + TM_YEAR_BASE;
+ year_today = year;
+ mdate_today = local_time->tm_mday;
+ jdate_today = day_in_year(mdate_today, month, year);
 
- yflag = year = 0;
  while ((ch = getopt(argc, argv, "jy")) != -1)
  switch(ch) {
  case 'j':
@@ -154,7 +163,6 @@ main(int argc, char *argv[])
  argc -= optind;
  argv += optind;
 
- month = 0;
  switch(argc) {
  case 2:
  month = parsemonth(*argv++);
@@ -166,89 +174,178 @@ main(int argc, char *argv[])
  month = parsemonth(*argv);
  if (!month)
  errx(1, "illegal year value: use 1-9999");
- (void)time(&now);
- local_time = localtime(&now);
- year = local_time->tm_year + TM_YEAR_BASE;
  } else {
+ if (argc == 1)
+ yflag = 1;
  if ((year = atoi(*argv)) < 1 || year > 9999)
  errx(1, "illegal year value: use 1-9999");
  }
  break;
  case 0:
- (void)time(&now);
- local_time = localtime(&now);
- year = local_time->tm_year + TM_YEAR_BASE;
- if (!yflag)
- month = local_time->tm_mon + 1;
  break;
  default:
  usage();
  }
 
- if (month)
- monthly(month, year);
- else if (julian)
- j_yearly(year);
- else
- yearly(year);
+ if (yflag)
+ month = 0;
+
+ if (julian) {
+ if (month)
+ j_monthly(month, year);
+ else
+ j_yearly(year);
+ } else {
+ if (month)
+ monthly(month, year);
+ else
+ yearly(year);
+ }
  exit(0);
 }
 
-#define DAY_LEN 3 /* 3 spaces per day */
-#define J_DAY_LEN 4 /* 4 spaces per day */
+#define DAY_LEN 3 /* 3 chars per day */
+#define J_DAY_LEN 4 /* 4 chars per day */
 #define WEEK_LEN 20 /* 7 * 3 - one space at the end */
 #define J_WEEK_LEN 27 /* 7 * 4 - one space at the end */
 #define HEAD_SEP 2 /* spaces between day headings */
 #define J_HEAD_SEP 2
 
 void
+j_monthly(int month, int year)
+{
+ int col, row, len, days[MAXDAYS], day;
+ char *p, lineout[39];
+
+ day_array(month, year, days);
+ (void)snprintf(lineout, sizeof(lineout), "%s %d",
+    month_names[month - 1], year);
+ len = strlen(lineout);
+ (void)printf("%*s%s\n%s\n",
+    (J_WEEK_LEN - len) / 2, "", lineout, j_day_headings);
+ for (row = 0; row < 6; row++) {
+ for (col = 0, p = lineout; col < 7; col++) {
+ day = days[row * 7 + col];
+ if (day == jdate_today &&
+    month_today == month &&
+    year_today == year) {
+ /* Escape code for underscored. */
+ *p++ = '\e';
+ *p++ = '[';
+ *p++ = '0';
+ *p++ = '4'; /* use 7 for inverse */
+ *p++ = 'm';
+ ascii_day(p, day);
+ p += 3;
+ /* Escape code for unset, and a space. */
+ *p++ = '\e';
+ *p++ = '[';
+ *p++ = '0';
+ *p++ = 'm';
+ *p++ = ' ';
+ } else {
+ ascii_day(p, day);
+ p += J_DAY_LEN;
+ }
+ }
+ *p = '\0';
+ trim_trailing_spaces(lineout);
+ (void)printf("%s\n", lineout);
+ }
+ return;
+}
+
+void
 monthly(int month, int year)
 {
- int col, row, len, days[MAXDAYS];
- char *p, lineout[30];
+ int col, row, len, days[MAXDAYS], day;
+ char *p, lineout[39];
 
  day_array(month, year, days);
- (void)snprintf(lineout, sizeof lineout, "%s %d",
+ (void)snprintf(lineout, sizeof(lineout), "%s %d",
     month_names[month - 1], year);
  len = strlen(lineout);
  (void)printf("%*s%s\n%s\n",
-    ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
-    lineout, julian ? j_day_headings : day_headings);
+    (WEEK_LEN - len) / 2, "", lineout, day_headings);
  for (row = 0; row < 6; row++) {
- for (col = 0, p = lineout; col < 7; col++,
-    p += julian ? J_DAY_LEN : DAY_LEN)
- ascii_day(p, days[row * 7 + col]);
+ for (col = 0, p = lineout; col < 7; col++) {
+ day = days[row * 7 + col];
+ if (day == mdate_today &&
+    month_today == month &&
+    year_today == year) {
+ /* Escape code for underscored. */
+ *p++ = '\e';
+ *p++ = '[';
+ *p++ = '0';
+ *p++ = '4'; /* use 7 for inverse */
+ *p++ = 'm';
+ ascii_day(p, day);
+ p += 2;
+ /* Escape code for unset, and a space. */
+ *p++ = '\e';
+ *p++ = '[';
+ *p++ = '0';
+ *p++ = 'm';
+ *p++ = ' ';
+ } else {
+ ascii_day(p, day);
+ p += DAY_LEN;
+ }
+ }
  *p = '\0';
  trim_trailing_spaces(lineout);
  (void)printf("%s\n", lineout);
  }
+ return;
 }
 
 void
 j_yearly(int year)
 {
- int col, *dp, i, month, row, which_cal;
+ int col, *dp, i, firstmonth, month, row, which_cal;
  int days[12][MAXDAYS];
  char *p, lineout[80];
 
- (void)snprintf(lineout, sizeof lineout, "%d", year);
+ (void)snprintf(lineout, sizeof(lineout), "%d", year);
  center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
  (void)printf("\n\n");
  for (i = 0; i < 12; i++)
  day_array(i + 1, year, days[i]);
  (void)memset(lineout, ' ', sizeof(lineout) - 1);
  lineout[sizeof(lineout) - 1] = '\0';
- for (month = 0; month < 12; month += 2) {
- center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
- center(month_names[month + 1], J_WEEK_LEN, 0);
+ for (firstmonth = 0; firstmonth < 12; firstmonth += 2) {
+ center(month_names[firstmonth], J_WEEK_LEN, J_HEAD_SEP);
+ center(month_names[firstmonth + 1], J_WEEK_LEN, 0);
  (void)printf("\n%s%*s%s\n", j_day_headings, J_HEAD_SEP, "",
     j_day_headings);
  for (row = 0; row < 6; row++) {
+ p = lineout;
  for (which_cal = 0; which_cal < 2; which_cal++) {
- p = lineout + which_cal * (J_WEEK_LEN + 2);
- dp = &days[month + which_cal][row * 7];
- for (col = 0; col < 7; col++, p += J_DAY_LEN)
- ascii_day(p, *dp++);
+ month = firstmonth + which_cal + 1;
+ dp = &days[firstmonth + which_cal][row * 7];
+ for (col = 0; col < 7; col++)
+ if (*dp == jdate_today &&
+    month_today == month &&
+    year_today == year) {
+ /* Escape code for underscored. */
+ *p++ = '\e';
+ *p++ = '[';
+ *p++ = '0';
+ *p++ = '4'; /* use 7 for inverse */
+ *p++ = 'm';
+ ascii_day(p, *dp++);
+ p += 3;
+ /* Escape code for unset, and a space. */
+ *p++ = '\e';
+ *p++ = '[';
+ *p++ = '0';
+ *p++ = 'm';
+ *p++ = ' ';
+ } else {
+ ascii_day(p, *dp++);
+ p += J_DAY_LEN;
+ }
+ *p++ =' ';
  }
  *p = '\0';
  trim_trailing_spaces(lineout);
@@ -256,34 +353,57 @@ j_yearly(int year)
  }
  }
  (void)printf("\n");
+ return;
 }
 
 void
 yearly(int year)
 {
- int col, *dp, i, month, row, which_cal;
+ int col, *dp, i, firstmonth, month, row, which_cal;
  int days[12][MAXDAYS];
- char *p, lineout[80];
+ char *p, lineout[89];
 
- (void)snprintf(lineout, sizeof lineout, "%d", year);
+ (void)snprintf(lineout, sizeof(lineout), "%d", year);
  center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
  (void)printf("\n\n");
  for (i = 0; i < 12; i++)
  day_array(i + 1, year, days[i]);
  (void)memset(lineout, ' ', sizeof(lineout) - 1);
  lineout[sizeof(lineout) - 1] = '\0';
- for (month = 0; month < 12; month += 3) {
- center(month_names[month], WEEK_LEN, HEAD_SEP);
- center(month_names[month + 1], WEEK_LEN, HEAD_SEP);
- center(month_names[month + 2], WEEK_LEN, 0);
+ for (firstmonth = 0; firstmonth < 12; firstmonth += 3) {
+ center(month_names[firstmonth], WEEK_LEN, HEAD_SEP);
+ center(month_names[firstmonth + 1], WEEK_LEN, HEAD_SEP);
+ center(month_names[firstmonth + 2], WEEK_LEN, 0);
  (void)printf("\n%s%*s%s%*s%s\n", day_headings, HEAD_SEP,
     "", day_headings, HEAD_SEP, "", day_headings);
  for (row = 0; row < 6; row++) {
+ p = lineout;
  for (which_cal = 0; which_cal < 3; which_cal++) {
- p = lineout + which_cal * (WEEK_LEN + 2);
- dp = &days[month + which_cal][row * 7];
- for (col = 0; col < 7; col++, p += DAY_LEN)
- ascii_day(p, *dp++);
+ month = firstmonth + which_cal + 1;
+ dp = &days[firstmonth + which_cal][row * 7];
+ for (col = 0; col < 7; col++)
+ if (*dp == mdate_today &&
+    month_today == month &&
+    year_today == year) {
+ /* Escape code for underscored. */
+ *p++ = '\e';
+ *p++ = '[';
+ *p++ = '0';
+ *p++ = '4'; /* use 7 for inverse */
+ *p++ = 'm';
+ ascii_day(p, *dp++);
+ p += 2;
+ /* Escape code for unset, and a space. */
+ *p++ = '\e';
+ *p++ = '[';
+ *p++ = '0';
+ *p++ = 'm';
+ *p++ = ' ';
+ } else {
+ ascii_day(p, *dp++);
+ p += DAY_LEN;
+ }
+ *p++ =' ';
  }
  *p = '\0';
  trim_trailing_spaces(lineout);
@@ -291,6 +411,7 @@ yearly(int year)
  }
  }
  (void)printf("\n");
+ return;
 }
 
 /*
@@ -334,11 +455,11 @@ day_in_year(int day, int month, int year
 }
 
 /*
- * day_in_week
- * return the 0 based day number for any date from 1 Jan. 1 to
- * 31 Dec. 9999.  Assumes the Gregorian reformation eliminates
- * 3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all
- * missing days.
+ * day_in_week --
+ * return the 0 based day number for any date from 1 Jan. 1 to 31
+ * Dec. 9999.  Takes in account the Gregorian reformation which eliminates
+ * 3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all missing
+ * days.
  */
 int
 day_in_week(int day, int month, int year)
@@ -392,6 +513,7 @@ ascii_day(char *p, int day)
  *p++ = aday[day][1];
  }
  *p = ' ';
+ return;
 }
 
 void
@@ -406,6 +528,7 @@ trim_trailing_spaces(char *s)
  if (p > s)
  ++p;
  *p = '\0';
+ return;
 }
 
 void
@@ -416,6 +539,7 @@ center(const char *str, int len, int sep
  (void)printf("%*s%s%*s", len / 2, "", str, len / 2 + len % 2, "");
  if (separate)
  (void)printf("%*s", separate, "");
+ return;
 }
 
 void
@@ -439,7 +563,7 @@ parsemonth(const char *s)
  if ((cp = strptime(s, "%b", &tm)) != NULL && *cp == '\0')
  v = tm.tm_mon + 1;
  }
- if (v <= 0 || v > 12)
+ if (v < 1 || v > 12)
  errx(1, "invalid month: use 1-12 or a name");
  return (v);
 }



# Han

Reply | Threaded
Open this post in threaded view
|

Re: patch for cal.c

Otto Moerbeek
On Mon, 20 Mar 2006, Han Boetes wrote:

> Hi,
>
> In my learn my self to code I made the following patch. I rather
> enjoyed creating it.
>
> I made the following changes:
>
> - sizeof foo -> sizeof(foo)

Why? To me it seems a gratuitous change.

> - Add return in void functions.

Why? You're afraid the funtion won't return?

> - Make code easier to understand.
> - Clarify variable names.
> - Underscore current date.

I stopped reading the diff after this. You'll be cursed by anybody
using a terminal not like a vt100 or xterm. Or somebody wirting cal
output to a file.

        -Otto
       

>
> Questions: I got the feeling I can make the ``*p++ = '\e';''
> series easier to implement. I just don't see how.
>
>
> Index: cal.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/cal/cal.c,v
> retrieving revision 1.18
> diff -u -p -r1.18 cal.c
> --- cal.c 8 Dec 2005 14:54:30 -0000 1.18
> +++ cal.c 20 Mar 2006 09:26:19 -0000
> @@ -117,28 +117,37 @@ const char *j_day_headings = " Su  Mo  T
>  #define leap_years_since_year_1(yr) \
>   ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
>  
> -int julian;
> -
>  void ascii_day(char *, int);
>  void center(const char *, int, int);
>  void day_array(int, int, int *);
>  int day_in_week(int, int, int);
>  int day_in_year(int, int, int);
> -void j_yearly(int);
> +void yearly(int);
>  void monthly(int, int);
> +void j_yearly(int);
> +void j_monthly(int, int);
>  void trim_trailing_spaces(char *);
> -void usage(void);
> -void yearly(int);
>  int parsemonth(const char *);
> +void usage(void);
> +
> +int     julian, mdate_today, jdate_today, month_today, year_today;
>  
>  int
>  main(int argc, char *argv[])
>  {
>   struct tm *local_time;
>   time_t now;
> - int ch, month, year, yflag;
> + int ch, month, year, yflag = 0;
> +
> + (void)time(&now);
> + local_time = localtime(&now);
> + month = local_time->tm_mon + 1;
> + month_today = month;
> + year = local_time->tm_year + TM_YEAR_BASE;
> + year_today = year;
> + mdate_today = local_time->tm_mday;
> + jdate_today = day_in_year(mdate_today, month, year);
>  
> - yflag = year = 0;
>   while ((ch = getopt(argc, argv, "jy")) != -1)
>   switch(ch) {
>   case 'j':
> @@ -154,7 +163,6 @@ main(int argc, char *argv[])
>   argc -= optind;
>   argv += optind;
>  
> - month = 0;
>   switch(argc) {
>   case 2:
>   month = parsemonth(*argv++);
> @@ -166,89 +174,178 @@ main(int argc, char *argv[])
>   month = parsemonth(*argv);
>   if (!month)
>   errx(1, "illegal year value: use 1-9999");
> - (void)time(&now);
> - local_time = localtime(&now);
> - year = local_time->tm_year + TM_YEAR_BASE;
>   } else {
> + if (argc == 1)
> + yflag = 1;
>   if ((year = atoi(*argv)) < 1 || year > 9999)
>   errx(1, "illegal year value: use 1-9999");
>   }
>   break;
>   case 0:
> - (void)time(&now);
> - local_time = localtime(&now);
> - year = local_time->tm_year + TM_YEAR_BASE;
> - if (!yflag)
> - month = local_time->tm_mon + 1;
>   break;
>   default:
>   usage();
>   }
>  
> - if (month)
> - monthly(month, year);
> - else if (julian)
> - j_yearly(year);
> - else
> - yearly(year);
> + if (yflag)
> + month = 0;
> +
> + if (julian) {
> + if (month)
> + j_monthly(month, year);
> + else
> + j_yearly(year);
> + } else {
> + if (month)
> + monthly(month, year);
> + else
> + yearly(year);
> + }
>   exit(0);
>  }
>  
> -#define DAY_LEN 3 /* 3 spaces per day */
> -#define J_DAY_LEN 4 /* 4 spaces per day */
> +#define DAY_LEN 3 /* 3 chars per day */
> +#define J_DAY_LEN 4 /* 4 chars per day */
>  #define WEEK_LEN 20 /* 7 * 3 - one space at the end */
>  #define J_WEEK_LEN 27 /* 7 * 4 - one space at the end */
>  #define HEAD_SEP 2 /* spaces between day headings */
>  #define J_HEAD_SEP 2
>  
>  void
> +j_monthly(int month, int year)
> +{
> + int col, row, len, days[MAXDAYS], day;
> + char *p, lineout[39];
> +
> + day_array(month, year, days);
> + (void)snprintf(lineout, sizeof(lineout), "%s %d",
> +    month_names[month - 1], year);
> + len = strlen(lineout);
> + (void)printf("%*s%s\n%s\n",
> +    (J_WEEK_LEN - len) / 2, "", lineout, j_day_headings);
> + for (row = 0; row < 6; row++) {
> + for (col = 0, p = lineout; col < 7; col++) {
> + day = days[row * 7 + col];
> + if (day == jdate_today &&
> +    month_today == month &&
> +    year_today == year) {
> + /* Escape code for underscored. */
> + *p++ = '\e';
> + *p++ = '[';
> + *p++ = '0';
> + *p++ = '4'; /* use 7 for inverse */
> + *p++ = 'm';
> + ascii_day(p, day);
> + p += 3;
> + /* Escape code for unset, and a space. */
> + *p++ = '\e';
> + *p++ = '[';
> + *p++ = '0';
> + *p++ = 'm';
> + *p++ = ' ';
> + } else {
> + ascii_day(p, day);
> + p += J_DAY_LEN;
> + }
> + }
> + *p = '\0';
> + trim_trailing_spaces(lineout);
> + (void)printf("%s\n", lineout);
> + }
> + return;
> +}
> +
> +void
>  monthly(int month, int year)
>  {
> - int col, row, len, days[MAXDAYS];
> - char *p, lineout[30];
> + int col, row, len, days[MAXDAYS], day;
> + char *p, lineout[39];
>  
>   day_array(month, year, days);
> - (void)snprintf(lineout, sizeof lineout, "%s %d",
> + (void)snprintf(lineout, sizeof(lineout), "%s %d",
>      month_names[month - 1], year);
>   len = strlen(lineout);
>   (void)printf("%*s%s\n%s\n",
> -    ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
> -    lineout, julian ? j_day_headings : day_headings);
> +    (WEEK_LEN - len) / 2, "", lineout, day_headings);
>   for (row = 0; row < 6; row++) {
> - for (col = 0, p = lineout; col < 7; col++,
> -    p += julian ? J_DAY_LEN : DAY_LEN)
> - ascii_day(p, days[row * 7 + col]);
> + for (col = 0, p = lineout; col < 7; col++) {
> + day = days[row * 7 + col];
> + if (day == mdate_today &&
> +    month_today == month &&
> +    year_today == year) {
> + /* Escape code for underscored. */
> + *p++ = '\e';
> + *p++ = '[';
> + *p++ = '0';
> + *p++ = '4'; /* use 7 for inverse */
> + *p++ = 'm';
> + ascii_day(p, day);
> + p += 2;
> + /* Escape code for unset, and a space. */
> + *p++ = '\e';
> + *p++ = '[';
> + *p++ = '0';
> + *p++ = 'm';
> + *p++ = ' ';
> + } else {
> + ascii_day(p, day);
> + p += DAY_LEN;
> + }
> + }
>   *p = '\0';
>   trim_trailing_spaces(lineout);
>   (void)printf("%s\n", lineout);
>   }
> + return;
>  }
>  
>  void
>  j_yearly(int year)
>  {
> - int col, *dp, i, month, row, which_cal;
> + int col, *dp, i, firstmonth, month, row, which_cal;
>   int days[12][MAXDAYS];
>   char *p, lineout[80];
>  
> - (void)snprintf(lineout, sizeof lineout, "%d", year);
> + (void)snprintf(lineout, sizeof(lineout), "%d", year);
>   center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
>   (void)printf("\n\n");
>   for (i = 0; i < 12; i++)
>   day_array(i + 1, year, days[i]);
>   (void)memset(lineout, ' ', sizeof(lineout) - 1);
>   lineout[sizeof(lineout) - 1] = '\0';
> - for (month = 0; month < 12; month += 2) {
> - center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
> - center(month_names[month + 1], J_WEEK_LEN, 0);
> + for (firstmonth = 0; firstmonth < 12; firstmonth += 2) {
> + center(month_names[firstmonth], J_WEEK_LEN, J_HEAD_SEP);
> + center(month_names[firstmonth + 1], J_WEEK_LEN, 0);
>   (void)printf("\n%s%*s%s\n", j_day_headings, J_HEAD_SEP, "",
>      j_day_headings);
>   for (row = 0; row < 6; row++) {
> + p = lineout;
>   for (which_cal = 0; which_cal < 2; which_cal++) {
> - p = lineout + which_cal * (J_WEEK_LEN + 2);
> - dp = &days[month + which_cal][row * 7];
> - for (col = 0; col < 7; col++, p += J_DAY_LEN)
> - ascii_day(p, *dp++);
> + month = firstmonth + which_cal + 1;
> + dp = &days[firstmonth + which_cal][row * 7];
> + for (col = 0; col < 7; col++)
> + if (*dp == jdate_today &&
> +    month_today == month &&
> +    year_today == year) {
> + /* Escape code for underscored. */
> + *p++ = '\e';
> + *p++ = '[';
> + *p++ = '0';
> + *p++ = '4'; /* use 7 for inverse */
> + *p++ = 'm';
> + ascii_day(p, *dp++);
> + p += 3;
> + /* Escape code for unset, and a space. */
> + *p++ = '\e';
> + *p++ = '[';
> + *p++ = '0';
> + *p++ = 'm';
> + *p++ = ' ';
> + } else {
> + ascii_day(p, *dp++);
> + p += J_DAY_LEN;
> + }
> + *p++ =' ';
>   }
>   *p = '\0';
>   trim_trailing_spaces(lineout);
> @@ -256,34 +353,57 @@ j_yearly(int year)
>   }
>   }
>   (void)printf("\n");
> + return;
>  }
>  
>  void
>  yearly(int year)
>  {
> - int col, *dp, i, month, row, which_cal;
> + int col, *dp, i, firstmonth, month, row, which_cal;
>   int days[12][MAXDAYS];
> - char *p, lineout[80];
> + char *p, lineout[89];
>  
> - (void)snprintf(lineout, sizeof lineout, "%d", year);
> + (void)snprintf(lineout, sizeof(lineout), "%d", year);
>   center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
>   (void)printf("\n\n");
>   for (i = 0; i < 12; i++)
>   day_array(i + 1, year, days[i]);
>   (void)memset(lineout, ' ', sizeof(lineout) - 1);
>   lineout[sizeof(lineout) - 1] = '\0';
> - for (month = 0; month < 12; month += 3) {
> - center(month_names[month], WEEK_LEN, HEAD_SEP);
> - center(month_names[month + 1], WEEK_LEN, HEAD_SEP);
> - center(month_names[month + 2], WEEK_LEN, 0);
> + for (firstmonth = 0; firstmonth < 12; firstmonth += 3) {
> + center(month_names[firstmonth], WEEK_LEN, HEAD_SEP);
> + center(month_names[firstmonth + 1], WEEK_LEN, HEAD_SEP);
> + center(month_names[firstmonth + 2], WEEK_LEN, 0);
>   (void)printf("\n%s%*s%s%*s%s\n", day_headings, HEAD_SEP,
>      "", day_headings, HEAD_SEP, "", day_headings);
>   for (row = 0; row < 6; row++) {
> + p = lineout;
>   for (which_cal = 0; which_cal < 3; which_cal++) {
> - p = lineout + which_cal * (WEEK_LEN + 2);
> - dp = &days[month + which_cal][row * 7];
> - for (col = 0; col < 7; col++, p += DAY_LEN)
> - ascii_day(p, *dp++);
> + month = firstmonth + which_cal + 1;
> + dp = &days[firstmonth + which_cal][row * 7];
> + for (col = 0; col < 7; col++)
> + if (*dp == mdate_today &&
> +    month_today == month &&
> +    year_today == year) {
> + /* Escape code for underscored. */
> + *p++ = '\e';
> + *p++ = '[';
> + *p++ = '0';
> + *p++ = '4'; /* use 7 for inverse */
> + *p++ = 'm';
> + ascii_day(p, *dp++);
> + p += 2;
> + /* Escape code for unset, and a space. */
> + *p++ = '\e';
> + *p++ = '[';
> + *p++ = '0';
> + *p++ = 'm';
> + *p++ = ' ';
> + } else {
> + ascii_day(p, *dp++);
> + p += DAY_LEN;
> + }
> + *p++ =' ';
>   }
>   *p = '\0';
>   trim_trailing_spaces(lineout);
> @@ -291,6 +411,7 @@ yearly(int year)
>   }
>   }
>   (void)printf("\n");
> + return;
>  }
>  
>  /*
> @@ -334,11 +455,11 @@ day_in_year(int day, int month, int year
>  }
>  
>  /*
> - * day_in_week
> - * return the 0 based day number for any date from 1 Jan. 1 to
> - * 31 Dec. 9999.  Assumes the Gregorian reformation eliminates
> - * 3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all
> - * missing days.
> + * day_in_week --
> + * return the 0 based day number for any date from 1 Jan. 1 to 31
> + * Dec. 9999.  Takes in account the Gregorian reformation which eliminates
> + * 3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all missing
> + * days.
>   */
>  int
>  day_in_week(int day, int month, int year)
> @@ -392,6 +513,7 @@ ascii_day(char *p, int day)
>   *p++ = aday[day][1];
>   }
>   *p = ' ';
> + return;
>  }
>  
>  void
> @@ -406,6 +528,7 @@ trim_trailing_spaces(char *s)
>   if (p > s)
>   ++p;
>   *p = '\0';
> + return;
>  }
>  
>  void
> @@ -416,6 +539,7 @@ center(const char *str, int len, int sep
>   (void)printf("%*s%s%*s", len / 2, "", str, len / 2 + len % 2, "");
>   if (separate)
>   (void)printf("%*s", separate, "");
> + return;
>  }
>  
>  void
> @@ -439,7 +563,7 @@ parsemonth(const char *s)
>   if ((cp = strptime(s, "%b", &tm)) != NULL && *cp == '\0')
>   v = tm.tm_mon + 1;
>   }
> - if (v <= 0 || v > 12)
> + if (v < 1 || v > 12)
>   errx(1, "invalid month: use 1-12 or a name");
>   return (v);
>  }
>
>
>
> # Han

Reply | Threaded
Open this post in threaded view
|

Re: patch for cal.c

Ray Lai
On Mon, Mar 20, 2006 at 11:19:46AM +0100, Otto Moerbeek wrote:
> On Mon, 20 Mar 2006, Han Boetes wrote:
> > In my learn my self to code I made the following patch. I rather
> > enjoyed creating it.
> >
> > I made the following changes:
> >
> > - sizeof foo -> sizeof(foo)
>
> Why? To me it seems a gratuitous change.

It's a KNF thing, but this diff isn't confined to KNF.  Please
submit simple diffs that make one change at a time, whether it is
for KNF or otherwise.

-Ray-

Reply | Threaded
Open this post in threaded view
|

Re: patch for cal.c

Han Boetes
Ray Lai wrote:
> It's a KNF thing, but this diff isn't confined to KNF.  Please
> submit simple diffs that make one change at a time, whether it
> is for KNF or otherwise.

OK, here is the first patch:

Changelog:
KNF


Index: cal.c
===================================================================
RCS file: /cvs/src/usr.bin/cal/cal.c,v
retrieving revision 1.18
diff -u -p -w -r1.18 cal.c
--- cal.c 8 Dec 2005 14:54:30 -0000 1.18
+++ cal.c 24 Mar 2006 03:19:08 -0000
@@ -208,7 +208,7 @@ monthly(int month, int year)
  char *p, lineout[30];
 
  day_array(month, year, days);
- (void)snprintf(lineout, sizeof lineout, "%s %d",
+ (void)snprintf(lineout, sizeof(lineout), "%s %d",
     month_names[month - 1], year);
  len = strlen(lineout);
  (void)printf("%*s%s\n%s\n",
@@ -231,7 +231,7 @@ j_yearly(int year)
  int days[12][MAXDAYS];
  char *p, lineout[80];
 
- (void)snprintf(lineout, sizeof lineout, "%d", year);
+ (void)snprintf(lineout, sizeof(lineout), "%d", year);
  center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
  (void)printf("\n\n");
  for (i = 0; i < 12; i++)
@@ -265,7 +265,7 @@ yearly(int year)
  int days[12][MAXDAYS];
  char *p, lineout[80];
 
- (void)snprintf(lineout, sizeof lineout, "%d", year);
+ (void)snprintf(lineout, sizeof(lineout), "%d", year);
  center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
  (void)printf("\n\n");
  for (i = 0; i < 12; i++)



# Han