summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjell Wooding <kjell@cvs.openbsd.org>2006-05-27 21:20:12 +0000
committerKjell Wooding <kjell@cvs.openbsd.org>2006-05-27 21:20:12 +0000
commit616097a064970a90e3c4b6d34f17bd091e82d1b4 (patch)
tree5610bcd8c49aafc2710a179941017e14b50dd9c6
parentc3b6aa69a0cd7326f49197b54b4119165cdb022f (diff)
Move mg "line to c-string" functionality to a function.
-rw-r--r--usr.bin/mg/def.h3
-rw-r--r--usr.bin/mg/line.c24
2 files changed, 25 insertions, 2 deletions
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h
index 1be37a960e4..ffb982be5fc 100644
--- a/usr.bin/mg/def.h
+++ b/usr.bin/mg/def.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: def.h,v 1.87 2006/05/08 21:24:24 kjell Exp $ */
+/* $OpenBSD: def.h,v 1.88 2006/05/27 21:20:11 kjell Exp $ */
/* This file is in the public domain. */
@@ -350,6 +350,7 @@ int lnewline(void);
int ldelete(RSIZE, int);
int ldelnewline(void);
int lreplace(RSIZE, char *);
+char * linetostr(const struct line *);
/* yank.c X */
diff --git a/usr.bin/mg/line.c b/usr.bin/mg/line.c
index 3178bcc6b30..014a0581e7f 100644
--- a/usr.bin/mg/line.c
+++ b/usr.bin/mg/line.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: line.c,v 1.37 2005/12/20 06:17:36 kjell Exp $ */
+/* $OpenBSD: line.c,v 1.38 2006/05/27 21:20:11 kjell Exp $ */
/* This file is in the public domain. */
@@ -569,3 +569,25 @@ lreplace(RSIZE plen, char *st)
undo_add_boundary();
return (TRUE);
}
+
+/*
+ * Allocate and return the supplied line as a C string
+ */
+char *
+linetostr(const struct line *ln)
+{
+ size_t len;
+ char *line;
+
+ len = llength(ln);
+ if (len == SIZE_MAX) /* (len + 1) overflow */
+ return (NULL);
+
+ if ((line = malloc(len + 1)) == NULL)
+ return (NULL);
+
+ (void)memcpy(line, ltext(ln), len);
+ line[len] = '\0';
+
+ return (line);
+}