From c32acd2e5cc151a23c79c1532ad6494c482d14ad Mon Sep 17 00:00:00 2001 From: Mario Sergio Fujikawa Ferreira Date: Mon, 26 May 2003 17:06:05 +0000 Subject: [PATCH] Add a trailing '\n' character if none is found in the information obtained from a package. Patch show_file() [1] and show_index() [2] functions. PR: 52097 Reviewed by: bento, kris, portmgr, re, Michael Nottebrock , Martin Horcicka Approved by: re (scottl) Obtained from: NetBSD [1], OpenBSD [2] MFC after: 1 week --- usr.sbin/pkg_install/info/show.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/usr.sbin/pkg_install/info/show.c b/usr.sbin/pkg_install/info/show.c index 205f865328f..10f846abb91 100644 --- a/usr.sbin/pkg_install/info/show.c +++ b/usr.sbin/pkg_install/info/show.c @@ -39,12 +39,16 @@ show_file(const char *title, const char *fname) if (!Quiet) printf("%s%s", InfoPrefix, title); fp = fopen(fname, "r"); - if (!fp) + if (fp == (FILE *) NULL) printf("ERROR: show_file: Can't open '%s' for reading!\n", fname); else { + int append_nl = 0; while ((n = fread(line, 1, 1024, fp)) != 0) fwrite(line, 1, n, stdout); fclose(fp); + append_nl = (line[n - 1] != '\n'); /* Do we have a trailing \n ? */ + if (append_nl) + printf("\n"); } printf("\n"); /* just in case */ } @@ -55,20 +59,25 @@ show_index(const char *title, const char *fname) FILE *fp; char line[MAXINDEXSIZE+2]; + strlcpy(line, "???\n", sizeof(line)); + if (!Quiet) printf("%s%s", InfoPrefix, title); fp = fopen(fname, "r"); - if (!fp) { + if (fp == (FILE *) NULL) { warnx("show_file: can't open '%s' for reading", fname); - return; + } else { + if(fgets(line, MAXINDEXSIZE + 1, fp)) { + size_t line_length = strlen(line); + + if (line[line_length - 1] != '\n') { /* Do we have a trailing \n ? */ + line[line_length] = '\n'; /* Add a trailing \n */ + line[line_length + 1] = '\0'; /* Terminate string */ + } + } + fclose(fp); } - if(fgets(line, MAXINDEXSIZE+1, fp)) { - if(line[MAXINDEXSIZE-1] != '\n') - line[MAXINDEXSIZE] = '\n'; - line[MAXINDEXSIZE+1] = 0; - fputs(line, stdout); - } - fclose(fp); + fputs(line, stdout); } /* Show a packing list item type. If showall is TRUE, show all */