Hi!

----

Attached (as "astksh_stpcpy.diff") is a small patch which adds
|stpcpy()| and |stpncpy()| to libast (if the system doesn't provide
them).

IMO it would be nice to put this _both_ into the ksh93u+ and ksh93v-
trees... risk should be very small (except there is a platform which
doesn't have |strnlen()|).

----

Bye,
Roland

P.S.: ast-open needs a purge of cases where |strcat()| is used
multiple times in sequence, cases of |sprintf(buf, "%s%s", ...)| etc.
etc. and have these abdominations all replaced by |stpcpy()| (because
it's faster&&more efficient, e.g. |strcat(buf, "aaa"); strcat(buf,
"bbb");| can be replaced with |char *s=buf; s=stpcpy(s, "aaa");
s=stpcpy(s, "bbb");| ... this will be faster because |strcat()| always
starts from the beginning of the buffer and then searches for the end
while |stpcpy()| allows an application to always continue immediately
where the old string ended).

-- 
  __ .  . __
 (o.\ \/ /.o) roland.ma...@nrubsig.org
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)
diff -r -u -N original/src/cmd/ksh93/edit/emacs.c build_stpcpy//src/cmd/ksh93/edit/emacs.c
--- src/cmd/ksh93/edit/emacs.c	2012-06-19 22:01:43.000000000 +0200
+++ src/cmd/ksh93/edit/emacs.c	2012-07-24 14:21:52.445351143 +0200
@@ -1174,25 +1174,26 @@
 		case cntl('H'):		/* ^X^H show history info */
 			{
 				char hbuf[MAXLINE];
+				char *hbn=hbuf; /* hbuf_next */
 
-				strcpy(hbuf, "Current command ");
-				strcat(hbuf, itos(hline));
+				hbn=stpcpy(hbn, "Current command ");
+				hbn=stpcpy(hbn, itos(hline));
 				if (hloff)
 				{
-					strcat(hbuf, " (line ");
-					strcat(hbuf, itos(hloff+1));
-					strcat(hbuf, ")");
+					hbn=stpcpy(hbn, " (line ");
+					hbn=stpcpy(hbn, itos(hloff+1));
+					hbn=stpcpy(hbn, ")");
 				}
 				if ((hline != location.hist_command) ||
 				    (hloff != location.hist_line))
 				{
-					strcat(hbuf, "; Previous command ");
-					strcat(hbuf, itos(location.hist_command));
+					hbn=stpcpy(hbn, "; Previous command ");
+					hbn=stpcpy(hbn, itos(location.hist_command));
 					if (location.hist_line)
 					{
-						strcat(hbuf, " (line ");
-						strcat(hbuf, itos(location.hist_line+1));
-						strcat(hbuf, ")");
+						hbn=stpcpy(hbn, " (line ");
+						hbn=stpcpy(hbn, itos(location.hist_line+1));
+						hbn=stpcpy(hbn, ")");
 					}
 				}
 				show_info(ep,hbuf);
@@ -1202,19 +1203,20 @@
 		case cntl('D'):		/* ^X^D show debugging info */
 			{
 				char debugbuf[MAXLINE];
+				char *dbbn = debugbuf; /* debugbuf_next */
 
-				strcpy(debugbuf, "count=");
-				strcat(debugbuf, itos(count));
-				strcat(debugbuf, " eol=");
-				strcat(debugbuf, itos(eol));
-				strcat(debugbuf, " cur=");
-				strcat(debugbuf, itos(cur));
-				strcat(debugbuf, " crallowed=");
-				strcat(debugbuf, itos(crallowed));
-				strcat(debugbuf, " plen=");
-				strcat(debugbuf, itos(plen));
-				strcat(debugbuf, " w_size=");
-				strcat(debugbuf, itos(w_size));
+				dbbn=stpcpy(dbbn, "count=");
+				dbbn=stpcpy(dbbn, itos(count));
+				dbbn=stpcpy(dbbn, " eol=");
+				dbbn=stpcpy(dbbn, itos(eol));
+				dbbn=stpcpy(dbbn, " cur=");
+				dbbn=stpcpy(dbbn, itos(cur));
+				dbbn=stpcpy(dbbn, " crallowed=");
+				dbbn=stpcpy(dbbn, itos(crallowed));
+				dbbn=stpcpy(dbbn, " plen=");
+				dbbn=stpcpy(dbbn, itos(plen));
+				dbbn=stpcpy(dbbn, " w_size=");
+				dbbn=stpcpy(dbbn, itos(w_size));
 
 				show_info(ep,debugbuf);
 				return;
diff -r -u -N original/src/lib/libast/features/map.c build_stpcpy//src/lib/libast/features/map.c
--- src/lib/libast/features/map.c	2010-06-25 18:59:08.000000000 +0200
+++ src/lib/libast/features/map.c	2012-07-24 14:44:35.352408535 +0200
@@ -296,6 +296,12 @@
 	printf("#define signal      	_ast_signal\n");
 	printf("#undef	sigunblock\n");
 	printf("#define sigunblock      _ast_sigunblock\n");
+	printf("#undef	stpcpy\n");
+	printf("#define stpcpy		_ast_stpcpy\n");
+	printf("extern char *		stpcpy(char *, const char *);\n");
+	printf("#undef	stpncpy\n");
+	printf("#define stpncpy		_ast_stpncpy\n");
+	printf("extern char *		stpncpy(char *, const char *, size_t);\n");
 	printf("#undef	stracmp\n");
 	printf("#define stracmp		_ast_stracmp\n");
 	printf("#undef	strcopy\n");
diff -r -u -N original/src/lib/libast/features/sys build_stpcpy//src/lib/libast/features/sys
--- src/lib/libast/features/sys	2012-06-15 04:11:26.000000000 +0200
+++ src/lib/libast/features/sys	2012-07-24 14:43:23.085129284 +0200
@@ -244,6 +244,8 @@
 extern	strchr		char*		(const char*, int)
 extern	strcmp		int		(const char*, const char*)
 extern	strcoll		int		(const char*, const char*)
+extern	stpcpy		char*		(char*, const char*)
+extern	stpncpy		char*		(char*, const char*, size_t)
 extern	strcpy		char*		(char*, const char*)
 extern	strcspn		size_t		(const char*, const char*)
 extern	strdup		char*		(const char*)
diff -r -u -N original/src/lib/libast/Makefile build_stpcpy//src/lib/libast/Makefile
--- src/lib/libast/Makefile	2012-05-31 22:42:56.000000000 +0200
+++ src/lib/libast/Makefile	2012-07-24 14:45:51.452256510 +0200
@@ -71,7 +71,7 @@
 	stracmp.c strnacmp.c \
 	ccmap.c ccmapid.c ccnative.c \
 	chresc.c chrtoi.c streval.c strexpr.c strmatch.c strcopy.c \
-	modelib.h modei.c modex.c strmode.c \
+	modelib.h modei.c modex.c stpcpy.c stpncpy.c strmode.c \
 	strlcat.c strlcpy.c strlook.c strncopy.c strsearch.c strpsearch.c \
 	stresc.c stropt.c strtape.c strpcmp.c strnpcmp.c strvcmp.c strnvcmp.c \
 	tok.c tokline.c tokscan.c \
diff -r -u -N original/src/lib/libast/Mamfile build_stpcpy//src/lib/libast/Mamfile
--- src/lib/libast/Mamfile	2012-06-30 01:55:25.000000000 +0200
+++ src/lib/libast/Mamfile	2012-07-24 14:46:23.601790202 +0200
@@ -731,6 +731,24 @@
 prev string/chrtoi.c
 exec - ${CC} ${mam_cc_FLAGS} ${CCFLAGS} -I. -Icomp -Iinclude -Istd -D_PACKAGE_ast -c string/chrtoi.c
 done chrtoi.o generated
+make stpcpy.o
+make string/stpcpy.c
+prev ast_map.h implicit
+prev include/ast.h implicit
+done string/stpcpy.c
+meta stpcpy.o %.c>%.o string/stpcpy.c stpcpy
+prev string/stpcpy.c
+exec - ${CC} ${mam_cc_FLAGS} ${CCFLAGS} -I. -Icomp -Iinclude -Istd -D_PACKAGE_ast -c string/stpcpy.c
+done stpcpy.o generated
+make stpncpy.o
+make string/stpncpy.c
+prev ast_map.h implicit
+prev include/ast.h implicit
+done string/stpncpy.c
+meta stpncpy.o %.c>%.o string/stpncpy.c stpncpy
+prev string/stpncpy.c
+exec - ${CC} ${mam_cc_FLAGS} ${CCFLAGS} -I. -Icomp -Iinclude -Istd -D_PACKAGE_ast -c string/stpncpy.c
+done stpncpy.o generated
 make streval.o
 make string/streval.c
 prev include/ast.h implicit
@@ -804,6 +822,7 @@
 prev string/strlcat.c
 exec - ${CC} ${mam_cc_FLAGS} ${CCFLAGS} -I. -Icomp -Iinclude -Istd -D_PACKAGE_ast -c string/strlcat.c
 done strlcat.o generated
+
 make strlcpy.o
 make string/strlcpy.c
 prev ast_map.h implicit
@@ -813,6 +832,7 @@
 prev string/strlcpy.c
 exec - ${CC} ${mam_cc_FLAGS} ${CCFLAGS} -I. -Icomp -Iinclude -Istd -D_PACKAGE_ast -c string/strlcpy.c
 done strlcpy.o generated
+
 make strlook.o
 make string/strlook.c
 prev include/ast.h implicit
@@ -6098,7 +6118,7 @@
 exec - ${CC} ${mam_cc_FLAGS} ${CCFLAGS} -I. -Icomp -Iinclude -Istd -D_PACKAGE_ast -c obsolete/spawn.c
 done spawn.o generated
 exec - ${AR} rc libast.a state.o transition.o opendir.o readdir.o rewinddir.o seekdir.o telldir.o getcwd.o fastfind.o hashalloc.o hashdump.o hashfree.o hashlast.o hashlook.o hashscan.o hashsize.o hashview.o hashwalk.o memhash.o memsum.o strhash.o strkey.o strsum.o stracmp.o strnacmp.o ccmap.o ccmapid.o ccnative.o chresc.o chrtoi.o
-exec - ${AR} rc libast.a streval.o strexpr.o strmatch.o strcopy.o modei.o modex.o strmode.o strlcat.o strlcpy.o strlook.o strncopy.o strsearch.o strpsearch.o stresc.o stropt.o strtape.o strpcmp.o strnpcmp.o strvcmp.o strnvcmp.o tok.o tokline.o tokscan.o pathaccess.o pathcat.o pathcanon.o pathcheck.o pathpath.o pathexists.o pathfind.o pathkey.o pathprobe.o pathrepl.o pathnative.o pathposix.o pathtemp.o pathtmp.o pathstat.o pathgetlink.o pathsetlink.o pathbin.o pathshell.o pathcd.o pathprog.o fs3d.o ftwalk.o ftwflags.o fts.o astintercept.o conformance.o getenv.o setenviron.o optget.o optjoin.o optesc.o optctx.o strsort.o struniq.o magic.o mime.o mimetype.o signal.o sigflag.o systrace.o error.o errorf.o errormsg.o errorx.o localeconv.o setlocale.o translate.o catopen.o iconv.o lc.o lctab.o mc.o base64.o recfmt.o recstr.o reclen.o fmtrec.o fmtbase.o fmtbuf.o fmtclock.o fmtdev.o fmtelapsed.o fmterror.o fmtesc.o fmtfmt.o fmtfs.o fmtident.o fmtint.o fmtip4.o fmtip6.o fmtls.o fmtmatch.o fmtmode.o fmtnum.o fmtperm.o fmtre.o fmttime.o
+exec - ${AR} rc libast.a streval.o strexpr.o strmatch.o strcopy.o modei.o modex.o stpcpy.o strmode.o strlcat.o strlcpy.o strlook.o strncopy.o strsearch.o strpsearch.o stresc.o stropt.o strtape.o strpcmp.o strnpcmp.o strvcmp.o strnvcmp.o tok.o tokline.o tokscan.o pathaccess.o pathcat.o pathcanon.o pathcheck.o pathpath.o pathexists.o pathfind.o pathkey.o pathprobe.o pathrepl.o pathnative.o pathposix.o pathtemp.o pathtmp.o pathstat.o pathgetlink.o pathsetlink.o pathbin.o pathshell.o pathcd.o pathprog.o fs3d.o ftwalk.o ftwflags.o fts.o astintercept.o conformance.o getenv.o setenviron.o optget.o optjoin.o optesc.o optctx.o strsort.o struniq.o magic.o mime.o mimetype.o signal.o sigflag.o systrace.o error.o errorf.o errormsg.o errorx.o localeconv.o setlocale.o translate.o catopen.o iconv.o lc.o lctab.o mc.o base64.o recfmt.o recstr.o reclen.o fmtrec.o fmtbase.o fmtbuf.o fmtclock.o fmtdev.o fmtelapsed.o fmterror.o fmtesc.o fmtfmt.o fmtfs.o fmtident.o fmtint.o fmtip4.o fmtip6.o fmtls.o fmtmatch.o fmtmode.o fmtnum.o fmtperm.o fmtre.o fmttime.o
 exec - ${AR} rc libast.a fmtuid.o fmtgid.o fmtsignal.o fmtscale.o fmttmx.o fmttv.o fmtversion.o strelapsed.o strperm.o struid.o strgid.o strtoip4.o strtoip6.o stack.o stk.o swapget.o swapmem.o swapop.o swapput.o sigdata.o sigcrit.o sigunblock.o procopen.o procclose.o procrun.o procfree.o tmdate.o tmequiv.o tmfix.o tmfmt.o tmform.o tmgoff.o tminit.o tmleap.o tmlex.o tmlocale.o tmmake.o tmpoff.o tmscan.o tmsleep.o tmtime.o tmtype.o tmweek.o tmword.o tmzone.o tmxdate.o tmxduration.o tmxfmt.o tmxgettime.o tmxleap.o tmxmake.o tmxscan.o tmxsettime.o tmxsleep.o tmxtime.o tmxtouch.o tvcmp.o tvgettime.o tvsettime.o tvsleep.o tvtouch.o cmdarg.o vecargs.o vecfile.o vecfree.o vecload.o vecstring.o univdata.o touch.o mnt.o debug.o memccpy.o memchr.o memcmp.o memcpy.o memdup.o memmove.o memset.o mkdir.o mkfifo.o mknod.o rmdir.o remove.o rename.o link.o unlink.o strdup.o strchr.o strrchr.o strstr.o strtod.o strtold.o strtol.o strtoll.o strtoul.o strtoull.o strton.o strtonll.o strntod.o strntold.o strnton.o
 exec - ${AR} rc libast.a strntonll.o strntol.o strntoll.o strntoul.o strntoull.o strcasecmp.o strncasecmp.o strerror.o mktemp.o tmpnam.o fsync.o execlp.o execve.o execvp.o execvpe.o spawnveg.o vfork.o killpg.o hsearch.o tsearch.o getlogin.o putenv.o setenv.o unsetenv.o lstat.o statvfs.o eaccess.o gross.o omitted.o readlink.o symlink.o getpgrp.o setpgid.o setsid.o waitpid.o creat64.o fcntl.o open.o atexit.o getdents.o getwd.o dup2.o errno.o getpreroot.o ispreroot.o realopen.o setpreroot.o getgroups.o mount.o system.o iblocks.o modedata.o tmdata.o memfatal.o sfkeyprintf.o sfdcdio.o sfdcdos.o sfdcfilter.o sfdcseekable.o sfdcslow.o sfdcsubstr.o sfdctee.o sfdcunion.o sfdcmore.o sfdcprefix.o wc.o wc2utf8.o basename.o closelog.o dirname.o fmtmsglib.o fnmatch.o ftw.o getdate.o getsubopt.o glob.o nftw.o openlog.o re_comp.o resolvepath.o realpath.o regcmp.o regexp.o setlogmask.o strftime.o strptime.o swab.o syslog.o tempnam.o wordexp.o mktime.o regalloc.o regclass.o regcoll.o regcomp.o regcache.o regdecomp.o regerror.o regexec.o regfatal.o reginit.o
 exec - ${AR} rc libast.a regnexec.o regsubcomp.o regsubexec.o regsub.o regrecord.o regrexec.o regstat.o dtclose.o dtdisc.o dthash.o dtlist.o dtmethod.o dtopen.o dtstrhash.o dttree.o dtview.o dtwalk.o dtnew.o dtcomp.o sfclose.o sfclrlock.o sfdisc.o sfdlen.o sfexcept.o sfgetl.o sfgetu.o sfcvt.o sfecvt.o sffcvt.o sfextern.o sffilbuf.o sfflsbuf.o sfprints.o sfgetd.o sfgetr.o sfllen.o sfmode.o sfmove.o sfnew.o sfpkrd.o sfnotify.o sfnputc.o sfopen.o sfpeek.o sfpoll.o sfpool.o sfpopen.o sfprintf.o sfputd.o sfputl.o sfputr.o sfputu.o sfrd.o sfread.o sfreserve.o sfscanf.o sfseek.o sfset.o sfsetbuf.o sfsetfd.o sfsize.o sfsk.o sfstack.o sfstrtod.o sfsync.o sfswap.o sftable.o sftell.o sftmp.o sfungetc.o sfvprintf.o sfvscanf.o sfwr.o sfwrite.o sfpurge.o sfraise.o sfwalk.o sfgetm.o sfmutex.o sfputm.o sfresize.o _sfclrerr.o _sfeof.o _sferror.o _sffileno.o _sfopen.o _sfstacked.o _sfvalue.o _sfgetc.o _sfgetl.o _sfgetl2.o _sfgetu.o _sfgetu2.o _sfdlen.o _sfllen.o _sfslen.o _sfulen.o _sfputc.o _sfputd.o _sfputl.o _sfputm.o
diff -r -u -N original/src/lib/libast/string/stpcpy.c build_stpcpy//src/lib/libast/string/stpcpy.c
--- src/lib/libast/string/stpcpy.c	1970-01-01 01:00:00.000000000 +0100
+++ src/lib/libast/string/stpcpy.c	2012-07-24 14:23:19.238597877 +0200
@@ -0,0 +1,59 @@
+/***********************************************************************
+*                                                                      *
+*               This software is part of the ast package               *
+*             Copyright (c) 2012 AT&T Intellectual Property            *
+*                      and is licensed under the                       *
+*                 Eclipse Public License, Version 1.0                  *
+*                    by AT&T Intellectual Property                     *
+*                                                                      *
+*                A copy of the License is available at                 *
+*          http://www.eclipse.org/org/documents/epl-v10.html           *
+*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
+*                                                                      *
+*              Information and Software Systems Research               *
+*                            AT&T Research                             *
+*                           Florham Park NJ                            *
+*                                                                      *
+*                                                                      *
+*                 Roland Mainz <roland.ma...@nrubsig.org>              *
+*                                                                      *
+***********************************************************************/
+#pragma prototyped
+/*
+ * stpcpy implementation
+ */
+
+#define stpcpy		______stpcpy
+
+#include <ast.h>
+
+#undef	stpcpy
+
+#undef	_def_map_ast
+#include <ast_map.h>
+
+#if _lib_stpcpy
+
+NoN(stpcpy)
+
+#else
+
+#if defined(__EXPORT__)
+#define extern	__EXPORT__
+#endif
+
+/*
+ * |stpcpy| - like |strcpy()| but returns the end of the buffer
+ *
+ * Copy string s2 to s1.  s1 must be large enough.
+ * return s1-1 (position of string terminator ('\0') in
+ * destination buffer).
+ */
+char *stpcpy(char *s1, const char *s2)
+{
+	while (*s1++ = *s2++)
+		;
+	return (s1-1);
+}
+
+#endif
diff -r -u -N original/src/lib/libast/string/stpncpy.c build_stpcpy//src/lib/libast/string/stpncpy.c
--- src/lib/libast/string/stpncpy.c	1970-01-01 01:00:00.000000000 +0100
+++ src/lib/libast/string/stpncpy.c	2012-07-24 14:48:10.437720253 +0200
@@ -0,0 +1,65 @@
+/***********************************************************************
+*                                                                      *
+*               This software is part of the ast package               *
+*             Copyright (c) 2012 AT&T Intellectual Property            *
+*                      and is licensed under the                       *
+*                 Eclipse Public License, Version 1.0                  *
+*                    by AT&T Intellectual Property                     *
+*                                                                      *
+*                A copy of the License is available at                 *
+*          http://www.eclipse.org/org/documents/epl-v10.html           *
+*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
+*                                                                      *
+*              Information and Software Systems Research               *
+*                            AT&T Research                             *
+*                           Florham Park NJ                            *
+*                                                                      *
+*                                                                      *
+*                 Roland Mainz <roland.ma...@nrubsig.org>              *
+*                                                                      *
+***********************************************************************/
+#pragma prototyped
+/*
+ * stpncpy implementation
+ */
+
+#define stpncpy		______stpncpy
+
+#include <ast.h>
+
+#undef	stpncpy
+
+#undef	_def_map_ast
+#include <ast_map.h>
+
+#if _lib_stpncpy
+
+NoN(stpncpy)
+
+#else
+
+#if defined(__EXPORT__)
+#define extern	__EXPORT__
+#endif
+
+
+/*
+ * |stpncpy| - like |strncpy()| but returns the end of the buffer
+ *
+ * Copy s2 to s1, truncating or '\0'-padding to always copy n
+ * bytes, return position of string terminator ('\0') in
+ * destination buffer or if s1 is not '\0'-terminated, s1+n.
+ *
+ * See http://pubs.opengroup.org/onlinepubs/9699919799/functions/strncpy.html
+ */
+char *
+stpncpy(char *s1, const char *s2, size_t n)
+{
+	register size_t strn_len = strnlen(s2, n);
+
+	strncpy(s1, s2, n);
+
+	return (strn_len < n)?(s1 + strn_len):(s1+n);
+}
+
+#endif
_______________________________________________
ast-developers mailing list
ast-developers@research.att.com
https://mailman.research.att.com/mailman/listinfo/ast-developers

Reply via email to