sbr/trim.c: New helper function trim(). Strips whitespace from a string.
authormarkus schnalke <meillo@marmaro.de>
Thu, 25 Sep 2014 10:20:11 +0000 (12:20 +0200)
committermarkus schnalke <meillo@marmaro.de>
Thu, 25 Sep 2014 10:20:11 +0000 (12:20 +0200)
There is already trimcpy(), which does a bit more work, but adding trim()
appears to be a useful investment.
The static function trim() in slocal needed to be renamed to not collide.

h/prototypes.h
sbr/Makefile.in
sbr/trim.c [new file with mode: 0644]
uip/slocal.c

index a22eac6..ed7f1ae 100644 (file)
@@ -103,6 +103,7 @@ int smatch(char *, struct swit *);
 char *snprintb(char *, size_t, unsigned, char *);
 int stringdex(char *, char *);
 char *toabsdir(char *);
+char *trim(unsigned char *);
 char *trimcpy(unsigned char *);
 int unputenv(char *);
 int uprf(char *, char *);
index 5da6f89..dfd6c38 100644 (file)
@@ -66,7 +66,7 @@ SRCS = addrsbr.c ambigsw.c brkstring.c  \
        seq_print.c seq_read.c seq_save.c seq_setcur.c  \
        seq_setprev.c seq_setunseen.c signals.c  \
        smatch.c snprintb.c strcasecmp.c  \
-       strindex.c trimcpy.c uprf.c vfgets.c fmt_def.c  \
+       strindex.c trim.c trimcpy.c uprf.c vfgets.c fmt_def.c  \
        mf.c utils.c m_mktemp.c
 
 OBJS =  $(SRCS:.c=.o)
diff --git a/sbr/trim.c b/sbr/trim.c
new file mode 100644 (file)
index 0000000..9f08d99
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+** trim.c -- strip leading and trailing whitespace ... inplace!
+**
+** This code is Copyright (c) 2002, by the authors of nmh.  See the
+** COPYRIGHT file in the root directory of the nmh distribution for
+** complete copyright information.
+*/
+
+#include <h/mh.h>
+
+
+char *
+trim(unsigned char *cp)
+{
+       unsigned char *sp;
+
+       /* skip over leading whitespace */
+       while (isspace(*cp)) {
+               cp++;
+       }
+
+       /* start at the end and zap trailing whitespace */
+       for (sp = cp + strlen(cp) - 1; sp >= cp; sp--) {
+               if (isspace(*sp)) {
+                       *sp = '\0';
+               } else {
+                       break;
+               }
+       }
+
+       return cp;
+}
index 107117f..837f038 100644 (file)
@@ -162,7 +162,7 @@ static int copy_message(int, char *, int);
 static void verbose_printf(char *fmt, ...);
 static void adorn(char *, char *, ...);
 static void debug_printf(char *fmt, ...);
-static char *trim(char *);
+static char *trimstr(char *);
 
 
 int
@@ -382,15 +382,15 @@ main(int argc, char **argv)
        }
 
        if (debug) {
-               debug_printf("addr=\"%s\"\n", trim(addr));
-               debug_printf("user=\"%s\"\n", trim(user));
-               debug_printf("info=\"%s\"\n", trim(info));
-               debug_printf("sender=\"%s\"\n", trim(sender));
+               debug_printf("addr=\"%s\"\n", trimstr(addr));
+               debug_printf("user=\"%s\"\n", trimstr(user));
+               debug_printf("info=\"%s\"\n", trimstr(info));
+               debug_printf("sender=\"%s\"\n", trimstr(sender));
                debug_printf("envelope=\"%s\"\n",
-                               envelope ? trim(envelope) : "");
-               debug_printf("mbox=\"%s\"\n", trim(mbox));
-               debug_printf("home=\"%s\"\n", trim(home));
-               debug_printf("ddate=\"%s\"\n", trim(ddate));
+                               envelope ? trimstr(envelope) : "");
+               debug_printf("mbox=\"%s\"\n", trimstr(mbox));
+               debug_printf("home=\"%s\"\n", trimstr(home));
+               debug_printf("ddate=\"%s\"\n", trimstr(ddate));
                debug_printf("now=%02d:%02d\n\n", now->tw_hour, now->tw_min);
        }
 
@@ -483,7 +483,7 @@ usr_delivery(int fd, char *delivery, int su)
                if (debug) {
                        for (i = 0; vec[i]; i++) {
                                debug_printf("vec[%d]: \"%s\"\n",
-                                               i, trim(vec[i]));
+                                               i, trimstr(vec[i]));
                        }
                }
 
@@ -824,14 +824,14 @@ parse(int fd)
                p->p_flags &= ~P_CHK;
                if (debug) {
                        debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n",
-                                       p - vars, p->p_name, trim(p->p_value));
+                                       p - vars, p->p_name, trimstr(p->p_value));
                }
        }
        if (debug) {
                for (p = hdrs; p->p_name; p++) {
                        debug_printf("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
                                        p - hdrs, p->p_name,
-                                       p->p_value ? trim(p->p_value) : "");
+                                       p->p_value ? trimstr(p->p_value) : "");
                }
        }
        return 0;
@@ -909,7 +909,7 @@ glob(int fd)
        if (debug) {
                for (p = vars; p->p_name; p++) {
                        debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n",
-                                       p - vars, p->p_name, trim(p->p_value));
+                                       p - vars, p->p_name, trimstr(p->p_value));
                }
        }
 }
@@ -1237,7 +1237,7 @@ fputs_error:
 ** Trim strings for pretty printing of debugging output
 */
 static char *
-trim(char *cp)
+trimstr(char *cp)
 {
        char buffer[BUFSIZ*4];
        unsigned char *bp, *sp;