Просмотр исходного кода

Merge branch 'sprintf-updates'

* sprintf-updates:
  * Remove all uses of s[n]printf, change most egg_snprintf to simple_snprintf
  * Add right padding / left justification
  * Add left space padding (right justification)
  * Remove a use of snprintf
  * Convert more to simple_snprintf
  * Convert some egg_snprintf() to simple_snprintf() now that it supports zero padding
  * Add zero padding to simple_snprintf
Bryan Drewery 17 лет назад
Родитель
Сommit
389402fe78

+ 3 - 3
src/botcmd.c

@@ -373,7 +373,7 @@ static void remote_tell_who(int idx, char *nick, int chan)
   for (i = 0; i < dcc_total; i++) {
     if (dcc[i].type && dcc[i].type->flags & DCT_REMOTEWHO) {
       if (dcc[i].u.chat->channel == chan) {
-	k = egg_snprintf(s, sizeof(s), "  %c%-15s %s", (geticon(i) == '-' ? ' ' : geticon(i)),
+	k = simple_snprintf(s, sizeof(s), "  %c%-15s %s", (geticon(i) == '-' ? ' ' : geticon(i)),
 		    dcc[i].nick, dcc[i].host);
 	if (now - dcc[i].timeval > 300) {
 	  unsigned long mydays, hrs, mins;
@@ -400,7 +400,7 @@ static void remote_tell_who(int idx, char *nick, int chan)
 	ok = 1;
 	botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s:", "Bots connected");
       }
-      egg_snprintf(s, sizeof(s), "  %s%c%-15s %s",
+      simple_snprintf(s, sizeof(s), "  %s%c%-15s %s",
 	      dcc[i].status & STAT_CALLED ? "<-" : "->",
 	      dcc[i].status & STAT_SHARE ? '+' : ' ',
 	      dcc[i].nick, dcc[i].u.bot->version);
@@ -415,7 +415,7 @@ static void remote_tell_who(int idx, char *nick, int chan)
 	  ok = 1;
 	  botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s:", "Other people on the bot");
 	}
-	l = egg_snprintf(s, sizeof(s), "  %c%-15s %s", (geticon(i) == '-' ? ' ' : geticon(i)), dcc[i].nick, dcc[i].host);
+	l = simple_snprintf(s, sizeof(s), "  %c%-15s %s", (geticon(i) == '-' ? ' ' : geticon(i)), dcc[i].nick, dcc[i].host);
 	if (now - dcc[i].timeval > 300) {
 	  k = (now - dcc[i].timeval) / 60;
 	  if (k < 60)

+ 4 - 4
src/chanprog.c

@@ -277,13 +277,13 @@ void tell_verbose_uptime(int idx)
   total = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
   hr = (int) (total / 60);
   min = (int) (total - (hr * 60));
-  snprintf(s2, sizeof(s2), "CPU %02d:%02d (load avg %3.1f%%)", (int) hr, (int) min, 100.0 * ((float) total / (float) (now - online_since)));
+  egg_snprintf(s2, sizeof(s2), "CPU %02d:%02d (load avg %3.1f%%)", (int) hr, (int) min, 100.0 * ((float) total / (float) (now - online_since)));
 #else
 # if HAVE_CLOCK
   cl = (clock() / CLOCKS_PER_SEC);
   hr = (int) (cl / 60);
   min = (int) (cl - (hr * 60));
-  snprintf(s2, sizeof(s2), "CPU %02d:%02d (load avg %3.1f%%)", (int) hr, (int) min,  100.0 * ((float) cl / (float) (now - online_since)));
+  egg_snprintf(s2, sizeof(s2), "CPU %02d:%02d (load avg %3.1f%%)", (int) hr, (int) min,  100.0 * ((float) cl / (float) (now - online_since)));
 # else
   simple_snprintf(s2, sizeof(s2), "CPU ???");
 # endif
@@ -430,7 +430,7 @@ void load_internal_users()
             userlist = adduser(userlist, hand, "none", "-", USER_OP, 1);
             u = get_user_by_handle(userlist, hand);
 
-            egg_snprintf(tmp, sizeof(tmp), "%li [internal]", (long)now);
+            simple_snprintf(tmp, sizeof(tmp), "%li [internal]", (long)now);
             set_user(&USERENTRY_ADDED, u, tmp);
 
             bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
@@ -487,7 +487,7 @@ void load_internal_users()
             userlist = adduser(userlist, hand, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_OP | USER_PARTY | USER_HUBA | USER_CHUBA, 0);
             u = get_user_by_handle(userlist, hand);
             set_user(&USERENTRY_PASS, u, pass);
-            egg_snprintf(tmp, sizeof(tmp), "%li [internal]", (long)now);
+            simple_snprintf(tmp, sizeof(tmp), "%li [internal]", (long)now);
             set_user(&USERENTRY_ADDED, u, tmp);
             while (hosts) {
               char x[1024] = "";

+ 5 - 5
src/cmds.c

@@ -182,7 +182,7 @@ static void tell_who(int idx, int chan)
 	else if (!dcc[i].u.chat->channel)
 	  strlcat(s, "(party) ", sizeof(s));
 	else
-	  sprintf(&s[strlen(s)], "(%5d) ", dcc[i].u.chat->channel);
+	  simple_snprintf(&s[strlen(s)], sizeof(s) - strlen(s), "(%5d) ", dcc[i].u.chat->channel);
       }
       strlcat(s, dcc[i].host, sizeof(s));
       if (atr & USER_MASTER) {
@@ -840,7 +840,7 @@ static void print_users(char *work, int idx, int *cnt, int *tt, int bot, int fla
          ((!notflags) || !(u->flags & notflags)) &&
           (!bot || (bot == 2 && bot_hublevel(u) < 999) || (bot == 1 && bot_hublevel(u) == 999))) {
       if (!*cnt)
-        egg_snprintf(work, worksiz, "%-11s: ", str);
+        simple_snprintf(work, worksiz, "%-11s: ", str);
       else 
         strlcat(work, ", ", worksiz);
 
@@ -3764,9 +3764,9 @@ static void rcmd_cursrv(char * fbot, char * fhand, char * fidx) {
     if (server_online) {
       daysdur(now, server_online, tmp, sizeof(tmp));
       if (floodless)
-        egg_snprintf(cursrv, sizeof(cursrv), "Currently: %-30s (connected %s) [floodless ;)]", cursrvname, tmp);
+        simple_snprintf(cursrv, sizeof(cursrv), "Currently: %-30s (connected %s) [floodless ;)]", cursrvname, tmp);
       else
-        egg_snprintf(cursrv, sizeof(cursrv), "Currently: %-30s (connected %s)", cursrvname, tmp);
+        simple_snprintf(cursrv, sizeof(cursrv), "Currently: %-30s (connected %s)", cursrvname, tmp);
 
       if (server_lag > 0) {
         simple_snprintf(tmp, sizeof(tmp), " Lag:%ds", server_lag);
@@ -3859,7 +3859,7 @@ static void rcmd_curnick(char * fbot, char * fhand, char * fidx) {
     char tmp[301] = "";
 
     if (server_online)
-      egg_snprintf(tmp, sizeof(tmp), "Currently: %-20s ", botname);
+      simple_snprintf(tmp, sizeof(tmp), "Currently: %-20s ", botname);
     if (jupenick[0] && strncmp(botname, jupenick, strlen(botname)))
       simple_snprintf(tmp, sizeof(tmp), "%sJupe: %s ", tmp, jupenick);
     else if (jupenick[0] && strcmp(botname, origbotname))

+ 1 - 7
src/compat/inet_ntop.c

@@ -21,12 +21,6 @@
 #include <stdio.h>
 #include <string.h>
 
-#ifdef SPRINTF_CHAR
-# define SPRINTF(x) strlen(sprintf/**/x)
-#else
-# define SPRINTF(x) ((size_t)sprintf x)
-#endif
-
 #define NS_INADDRSZ     4       /* IPv4 T_A */
 #define NS_IN6ADDRSZ    16      /* IPv6 T_AAAA */
 #define NS_INT16SZ      2       /* #/bytes of data in a u_int16_t */
@@ -82,7 +76,7 @@ egg_inet_ntop4(const u_char *src, char *dst, socklen_t size)
 	static const char fmt[] = "%u.%u.%u.%u";
 	char tmp[sizeof "255.255.255.255"];
 
-	if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
+	if (simple_snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]) > size) {
 		return (NULL);
 	}
 	return strcpy(dst, tmp);

+ 1 - 1
src/conf.c

@@ -1160,7 +1160,7 @@ void conf_add_userlist_bots()
         userlist = adduser(userlist, bot->nick, "none", "-", USER_OP, 1);
         u = get_user_by_handle(userlist, bot->nick);
 
-        egg_snprintf(tmp, sizeof(tmp), "%li %s", (long)now, conf.bot->nick);
+        simple_snprintf(tmp, sizeof(tmp), "%li %s", (long)now, conf.bot->nick);
         set_user(&USERENTRY_ADDED, u, tmp);
 
         bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));

+ 1 - 1
src/core_binds.c

@@ -284,6 +284,6 @@ void check_bind_chof(char *hand, int idx)
 void check_bind_time(struct tm *tm)
 {
 	char full[32] = "";
-	egg_snprintf(full, sizeof(full), "%02d %02d %02d %02d %04d", tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year + 1900);
+	simple_snprintf(full, sizeof(full), "%02d %02d %02d %02d %04d", tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year + 1900);
 	check_bind(BT_time, full, NULL, tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year + 1900);
 }

+ 1 - 1
src/crypt.c

@@ -342,7 +342,7 @@ int sha1cmp(const char *hash, const char *string) {
 
 void btoh(const unsigned char *md, size_t md_len, char *buf, const size_t buf_len)
 {
-#define doblock(n) snprintf(&(buf[(i + n) << 1]), buf_len - ((i + n) << 1), "%02x", md[i + n]);
+#define doblock(n) simple_snprintf(&(buf[(i + n) << 1]), buf_len - ((i + n) << 1), "%02x", md[i + n]);
   for (size_t i = 0; i < md_len; i+=4) {
     doblock(0);
     doblock(1);

+ 1 - 1
src/enclink.c

@@ -63,7 +63,7 @@ static void ghost_link_case(int idx, direction_t direction)
 
     /* initkey-gen */
     /* salt1 salt2 port mynick conf.bot->nick */
-    sprintf(tmp, STR("%s@%s@%4x@%s@%s"), salt1, salt2, port, strtoupper(nick1), strtoupper(nick2));
+    simple_snprintf(tmp, sizeof(tmp), STR("%s@%s@%4x@%s@%s"), salt1, salt2, port, strtoupper(nick1), strtoupper(nick2));
     free(nick1);
     free(nick2);
     strlcpy(keyp, SHA1(tmp), ENC_KEY_LEN + 1);

+ 2 - 2
src/misc.c

@@ -342,7 +342,7 @@ void daysdur(time_t mynow, time_t then, char *out, size_t outsiz)
   mynow -= then;
   int hrs = (int) (mynow / 3600);
   int mins = (int) ((mynow - (hrs * 3600)) / 60);
-  egg_snprintf(s, sizeof(s), "%02d:%02d", hrs, mins);
+  simple_snprintf(s, sizeof(s), "%02d:%02d", hrs, mins);
   strlcat(out, s, outsiz);
 }
 
@@ -492,7 +492,7 @@ char *str_escape(const char *str, const char divc, const char mask)
     }
 
     if (*s == divc || *s == mask) {
-      egg_snprintf(b, buflen, "%c%02x", mask, *s);
+      simple_snprintf(b, buflen, "%c%02x", mask, *s);
       b += 3;
       blen += 3;
     } else {

+ 2 - 2
src/mod/channels.mod/cmdschan.c

@@ -1105,7 +1105,7 @@ static void show_flag(int idx, char *work, int *cnt, const char *name, unsigned
       strlcat(chr_state, "-", sizeof(chr_state));
     }
     strlcat(chr_state, COLOR_END(idx), sizeof(chr_state));
-    egg_snprintf(tmp, sizeof tmp, "%s%-17s", chr_state, name);
+    simple_snprintf(tmp, sizeof tmp, "%s%-17s", chr_state, name);
     strlcat(work, tmp, worksiz);
   }
   if (*cnt >= FLAG_COLS)
@@ -1146,7 +1146,7 @@ static void show_int(int idx, char *work, int *cnt, const char *desc, int state,
       strlcat(tmp2, chr_state, sizeof(tmp2));
     }
     strlcat(tmp2, BOLD_END(idx), sizeof(tmp2));
-    egg_snprintf(tmp, sizeof tmp, "%-30s %-20s %s", desc, tmp2, tmp3[0] ? tmp3 : "");
+    simple_snprintf(tmp, sizeof tmp, "%-30s %-20s %s", desc, tmp2, tmp3[0] ? tmp3 : "");
     strlcat(work, tmp, worksiz);
   }
   if (*cnt >= INT_COLS)

+ 1 - 1
src/mod/channels.mod/userchan.c

@@ -543,7 +543,7 @@ static void tell_masks(const char type, int idx, bool show_inact, char *match, b
 	  if (ml->timer != 0) {
 	    min = (now - ml->timer) / 60;
 	    sec = (now - ml->timer) - (min * 60);
-	    egg_snprintf(s, sizeof(s), " (active %02d:%02d)", min, sec);
+	    simple_snprintf(s, sizeof(s), " (active %02d:%02d)", min, sec);
 	    strlcat(fill, s, sizeof(fill));
 	  }
 	  if ((!match[0]) || (wild_match(match, ml->mask)))

+ 7 - 6
src/mod/irc.mod/cmdsirc.c

@@ -1341,12 +1341,13 @@ static void cmd_find(int idx, char *par)
 
     for (findex = 0; findex < fcount; findex++) {
       if (found[findex]) {
-        sprintf(tmp, "%s!%s %s%s%s on %s", found[findex]->nick, found[findex]->userhost, 
+        simple_snprintf(tmp, sizeof(tmp), "%s!%s %s%s%s on %s", found[findex]->nick, found[findex]->userhost,
          found[findex]->user ? "(user:" : "", found[findex]->user ? found[findex]->user->handle : "", found[findex]->user ? ")" : "", 
                                            cfound[findex]->name);
         for (i = findex + 1; i < fcount; i++) {
           if (found[i] && (!strcmp(found[i]->nick, found[findex]->nick))) {
-            sprintf(tmp + strlen(tmp), ", %s", cfound[i]->name);
+            strlcat(tmp, ", ", sizeof(tmp));
+            strlcat(tmp, cfound[i]->name, sizeof(tmp));
             found[i] = NULL;
           }
         }
@@ -1594,11 +1595,11 @@ static void cmd_channel(int idx, char *par)
       } else {
 	/* Determine idle time */
 	if (now - (m->last) > 86400)
-	  egg_snprintf(s1, sizeof s1, "%2dd", (int) ((now - (m->last)) / 86400));
+	  simple_snprintf(s1, sizeof s1, "%2dd", (int) ((now - (m->last)) / 86400));
 	else if (now - (m->last) > 3600)
-	  egg_snprintf(s1, sizeof s1, "%2dh", (int) ((now - (m->last)) / 3600));
+	  simple_snprintf(s1, sizeof s1, "%2dh", (int) ((now - (m->last)) / 3600));
 	else if (now - (m->last) > 180)
-	  egg_snprintf(s1, sizeof s1, "%2dm", (int) ((now - (m->last)) / 60));
+	  simple_snprintf(s1, sizeof s1, "%2dm", (int) ((now - (m->last)) / 60));
 	else
 	  strlcpy(s1, "   ", sizeof s1);
 	simple_snprintf(format, sizeof format, "%%c%%c%%-%zus %%-%zus %%s %%c   %%d %%s  %%s %%s\n", 
@@ -1774,7 +1775,7 @@ static void cmd_adduser(int idx, char *par)
   if (!u) {
     userlist = adduser(userlist, hand, p1, "-", USER_DEFAULT, 0);
     u = get_user_by_handle(userlist, hand);
-    egg_snprintf(tmp, sizeof(tmp), "%li %s", (long) now, dcc[idx].nick);
+    simple_snprintf(tmp, sizeof(tmp), "%li %s", (long) now, dcc[idx].nick);
     set_user(&USERENTRY_ADDED, u, tmp);
     make_rand_str(s2, MAXPASSLEN);
     set_user(&USERENTRY_PASS, u, s2);

+ 1 - 1
src/mod/irc.mod/irc.c

@@ -362,7 +362,7 @@ void makecookie(char *out, size_t len, const char *chname, const memberlist* opp
 
   make_rand_str(randstring, 4);
   /* &ts[4] is now last 6 digits of time */
-  egg_snprintf(ts, sizeof(ts), "%010li", (long) (now + timesync));
+  simple_snprintf(ts, sizeof(ts), "%010li", (long) (now + timesync));
   
   const char* hash1 = cookie_hash(chname, opper, m1, ts, randstring);
   const char* hash2 = m2 ? cookie_hash(chname, opper, m2, ts, randstring) : NULL;

+ 1 - 1
src/set.c

@@ -957,7 +957,7 @@ int cmd_set_real(const char *botnick, int idx, char *par)
         else {
           char buf[51] = "";
 
-          egg_snprintf(buf, sizeof(buf), "(%-6s)  %-19s:  ", var_type_name(var->flags), var->name);
+          simple_snprintf(buf, sizeof(buf), "(%-6s)  %-19s:  ", var_type_name(var->flags), var->name);
 //        dprintf(idx, "   %-15s:   %s\n", var->name, data);
           dumplots(idx, buf, data ? (char *) data : (char *) "(not set)");
         }

+ 61 - 2
src/sprintf.c

@@ -80,13 +80,47 @@ size_t simple_vsnprintf(char *buf, size_t size, const char *format, va_list va)
   char *fp = (char *) format;
   size_t c = 0;
   unsigned long i = 0;
-  bool islong = 0, caps = false;
+  long width = 0;
+  bool islong = 0, caps = false, width_modifier = false, rpad = false;
+  char pad = ' ';
 
+re_eval:
   while (*fp && c < size - 1) {
-    if (*fp == '%') {
+    if (*fp == '%' || width_modifier) {
 re_eval_with_modifier:
       ++fp;
+      if (width_modifier) {
+        if (egg_isdigit(*fp)) {
+          width = 10 * width + (*fp - '0');
+          goto re_eval;
+        } else
+          width_modifier = false;
+      }
+
       switch (*fp) {
+      /* Left padding with zeroes */
+      case '0':
+        width_modifier = true;
+        pad = '0';
+        goto re_eval_with_modifier;
+      /* Right padding with spaces */
+      case '-':
+        rpad = true;
+        goto re_eval_with_modifier;
+      /* Left padding with spaces */
+      case '1':
+      case '2':
+      case '3':
+      case '4':
+      case '5':
+      case '6':
+      case '7':
+      case '8':
+      case '9':
+        width_modifier = true;
+        width = 10 * width + (*fp - '0');
+        pad = ' ';
+        goto re_eval_with_modifier;
       case 'z':
       case 'l':
         islong = 1;
@@ -140,6 +174,25 @@ re_eval_with_modifier:
         continue;
       }
       if (s) {
+        if (width > 0) {
+          width -= strlen(s);
+          if (width < 0) width = 0;
+          if (rpad) {
+            width = -width;
+            rpad = false;
+          }
+        }
+
+        /* Left padding / right justification */
+        if (width > 0) {
+          while (width > 0 && c < size - 1) {
+            buf[c++] = pad;
+            --width;
+          }
+          width = 0;
+        }
+
+        /* Advance the buffer with content */
         if (caps) {
           while (*s && c < size - 1)
             buf[c++] = toupper(*s++);
@@ -148,6 +201,12 @@ re_eval_with_modifier:
           while (*s && c < size - 1)
             buf[c++] = *s++;
         }
+
+        /* Right padding / left justification */
+        while (width < 0 && c < size - 1) {
+          buf[c++] = pad;
+          ++width;
+        }
       }
       fp++;
     } else

+ 1 - 1
src/userent.c

@@ -529,7 +529,7 @@ void update_mod(char *handle, char *nick, char *cmd, char *par)
 {
   char tmp[100] = "";
 
-  egg_snprintf(tmp, sizeof tmp, "%li, %s (%s %s)", (long) now, nick, cmd, (par && par[0]) ? par : "");
+  simple_snprintf(tmp, sizeof tmp, "%li, %s (%s %s)", (long) now, nick, cmd, (par && par[0]) ? par : "");
   set_user(&USERENTRY_MODIFIED, get_user_by_handle(userlist, handle), tmp);
 }
 

+ 1 - 1
src/users.c

@@ -965,7 +965,7 @@ void link_pref_val(struct userrec *u, char *val)
   if (!ba->hublevel || ba->hublevel == 999) {
     return;
   }
-  egg_snprintf(val, HANDLEN + 4, "%02d%s", ba->hublevel, u->handle);
+  simple_snprintf(val, HANDLEN + 4, "%02d%s", ba->hublevel, u->handle);
 }
 
 /*