tls: check size on "MAC-only, no crypt" code path too
[oweals/busybox.git] / editors / ed.c
index 8ed4cd42bcb81432e400a8bd42965e64220038df..c028b78cb8157599f3788e3a55e995b20401c395 100644 (file)
@@ -7,7 +7,23 @@
  * The "ed" built-in command (much simplified)
  */
 
+//config:config ED
+//config:      bool "ed"
+//config:      default y
+//config:      help
+//config:        The original 1970's Unix text editor, from the days of teletypes.
+//config:        Small, simple, evil. Part of SUSv3. If you're not already using
+//config:        this, you don't need it.
+
+//kbuild:lib-$(CONFIG_ED) += ed.o
+
+//applet:IF_ED(APPLET(ed, BB_DIR_BIN, BB_SUID_DROP))
+
+//usage:#define ed_trivial_usage ""
+//usage:#define ed_full_usage ""
+
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 typedef struct LINE {
        struct LINE *next;
@@ -20,8 +36,8 @@ typedef struct LINE {
 #define searchString bb_common_bufsiz1
 
 enum {
-       USERSIZE = sizeof(searchString) > 1024 ? 1024
-                : sizeof(searchString) - 1, /* max line length typed in by user */
+       USERSIZE = COMMON_BUFSIZE > 1024 ? 1024
+                : COMMON_BUFSIZE - 1, /* max line length typed in by user */
        INITBUF_SIZE = 1024, /* initial buffer size */
 };
 
@@ -51,6 +67,7 @@ struct globals {
 #define lines              (G.lines             )
 #define marks              (G.marks             )
 #define INIT_G() do { \
+       setup_common_bufsiz(); \
        SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
 } while (0)
 
@@ -129,7 +146,7 @@ static void doCommands(void)
                 * 0  on ctrl-C,
                 * >0 length of input string, including terminating '\n'
                 */
-               len = read_line_input(": ", buf, sizeof(buf), NULL);
+               len = read_line_input(NULL, ": ", buf, sizeof(buf), /*timeout*/ -1);
                if (len <= 0)
                        return;
                endbuf = &buf[len - 1];
@@ -191,7 +208,7 @@ static void doCommands(void)
                                if (fileName)
                                        printf("\"%s\"\n", fileName);
                                else
-                                       printf("No file name\n");
+                                       puts("No file name");
                                break;
                        }
                        free(fileName);
@@ -227,7 +244,7 @@ static void doCommands(void)
                        }
                        if (!dirty)
                                return;
-                       len = read_line_input("Really quit? ", buf, 16, NULL);
+                       len = read_line_input(NULL, "Really quit? ", buf, 16, /*timeout*/ -1);
                        /* read error/EOF - no way to continue */
                        if (len < 0)
                                return;
@@ -451,14 +468,10 @@ static void subCommand(const char *cmd, int num1, int num2)
 
                /*
                 * The new string is larger, so allocate a new line
-                * structure and use that.  Link it in in place of
+                * structure and use that.  Link it in place of
                 * the old line structure.
                 */
-               nlp = malloc(sizeof(LINE) + lp->len + deltaLen);
-               if (nlp == NULL) {
-                       bb_error_msg("cannot get memory for line");
-                       return;
-               }
+               nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen);
 
                nlp->len = lp->len + deltaLen;
 
@@ -545,7 +558,7 @@ static void addLines(int num)
                 * 0  on ctrl-C,
                 * >0 length of input string, including terminating '\n'
                 */
-               len = read_line_input("", buf, sizeof(buf), NULL);
+               len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1);
                if (len <= 0) {
                        /* Previously, ctrl-C was exiting to shell.
                         * Now we exit to ed prompt. Is in important? */
@@ -675,7 +688,7 @@ static int readLines(const char *file, int num)
 
        fd = open(file, 0);
        if (fd < 0) {
-               perror(file);
+               bb_simple_perror_msg(file);
                return FALSE;
        }
 
@@ -712,12 +725,7 @@ static int readLines(const char *file, int num)
 
                if (bufUsed >= bufSize) {
                        len = (bufSize * 3) / 2;
-                       cp = realloc(bufBase, len);
-                       if (cp == NULL) {
-                               bb_error_msg("no memory for buffer");
-                               close(fd);
-                               return FALSE;
-                       }
+                       cp = xrealloc(bufBase, len);
                        bufBase = cp;
                        bufPtr = bufBase + bufUsed;
                        bufSize = len;
@@ -726,11 +734,10 @@ static int readLines(const char *file, int num)
                cc = safe_read(fd, bufPtr, bufSize - bufUsed);
                bufUsed += cc;
                bufPtr = bufBase;
-
        } while (cc > 0);
 
        if (cc < 0) {
-               perror(file);
+               bb_simple_perror_msg(file);
                close(fd);
                return FALSE;
        }
@@ -770,7 +777,7 @@ static int writeLines(const char *file, int num1, int num2)
 
        fd = creat(file, 0666);
        if (fd < 0) {
-               perror(file);
+               bb_simple_perror_msg(file);
                return FALSE;
        }
 
@@ -785,7 +792,7 @@ static int writeLines(const char *file, int num1, int num2)
 
        while (num1++ <= num2) {
                if (full_write(fd, lp->data, lp->len) != lp->len) {
-                       perror(file);
+                       bb_simple_perror_msg(file);
                        close(fd);
                        return FALSE;
                }
@@ -795,7 +802,7 @@ static int writeLines(const char *file, int num1, int num2)
        }
 
        if (close(fd) < 0) {
-               perror(file);
+               bb_simple_perror_msg(file);
                return FALSE;
        }
 
@@ -872,11 +879,7 @@ static int insertLine(int num, const char *data, int len)
                return FALSE;
        }
 
-       newLp = malloc(sizeof(LINE) + len - 1);
-       if (newLp == NULL) {
-               bb_error_msg("failed to allocate memory for line");
-               return FALSE;
-       }
+       newLp = xmalloc(sizeof(LINE) + len - 1);
 
        memcpy(newLp->data, data, len);
        newLp->len = len;
@@ -983,7 +986,7 @@ static NOINLINE int searchLines(const char *str, int num1, int num2)
                lp = lp->next;
        }
 
-       bb_error_msg("cannot find string \"%s\"", str);
+       bb_error_msg("can't find string \"%s\"", str);
        return 0;
 }