]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - text-utils/pg.c
wipefs: add --lock and LOCK_BLOCK_DEVICE
[thirdparty/util-linux.git] / text-utils / pg.c
index b05ec3d70eb68532955490b54f7eed85b9cd3a04..5d01ebff34f1389ff0716e69b6034842fd02bfa1 100644 (file)
 #include <signal.h>
 #include <setjmp.h>
 
-#ifdef HAVE_NCURSES_H
-# include <ncurses.h>
+#if defined(HAVE_NCURSESW_NCURSES_H)
+# include <ncursesw/ncurses.h>
 #elif defined(HAVE_NCURSES_NCURSES_H)
 # include <ncurses/ncurses.h>
+#elif defined(HAVE_NCURSES_H)
+# include <ncurses.h>
 #endif
 
-#include <term.h>
+#if defined(HAVE_NCURSESW_TERM_H)
+# include <ncursesw/term.h>
+#elif defined(HAVE_NCURSES_TERM_H)
+# include <ncurses/term.h>
+#elif defined(HAVE_TERM_H)
+# include <term.h>
+#endif
 
 #include "nls.h"
 #include "xalloc.h"
@@ -215,8 +223,9 @@ static void __attribute__((__noreturn__)) quit(int status)
 }
 
 /* Usage message and similar routines. */
-static void __attribute__((__noreturn__)) usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        fputs(USAGE_HEADER, out);
        fprintf(out,
                _(" %s [options] [+line] [+/pattern/] [files]\n"),
@@ -238,23 +247,22 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
        fputs(_(" +/pattern/   start at the line containing pattern\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
-       fputs(USAGE_HELP, out);
-       fputs(USAGE_VERSION, out);
+       printf(USAGE_HELP_OPTIONS(16));
 
-       fprintf(out, USAGE_MAN_TAIL("pg(1)"));
-       quit(out == stderr ? 2 : 0);
+       printf(USAGE_MAN_TAIL("pg(1)"));
+       exit(0);
 }
 
 static void __attribute__((__noreturn__)) needarg(const char *s)
 {
        warnx(_("option requires an argument -- %s"), s);
-       usage(stderr);
+       errtryhelp(2);
 }
 
 static void __attribute__((__noreturn__)) invopt(const char *s)
 {
        warnx(_("illegal option -- %s"), s);
-       usage(stderr);
+       errtryhelp(2);
 }
 
 #ifdef HAVE_WIDECHAR
@@ -651,7 +659,7 @@ static void prompt(long long pageno)
                                        break;
                                case SEARCH_FIN:
                                        state = SEARCH;
-                                       /* FALLTHRU */
+                                       /* fallthrough */
                                case SEARCH:
                                        if (cmd.cmdline[cmd.cmdlen - 1] == '\\') {
                                                escape = 1;
@@ -730,7 +738,7 @@ static void prompt(long long pageno)
                                        continue;
                                }
                                state = COUNT;
-                               /* FALLTHRU */
+                               /* fallthrough */
                        case COUNT:
                                break;
                        case ADDON_FIN:
@@ -979,42 +987,43 @@ static void pgfile(FILE *f, const char *name)
                                        *b = '\0';
                                        dline = pagelen;
                                        break;
-                               } else {
-                                       if (nobuf)
-                                               fseeko(f, fpos, SEEK_SET);
-                                       canjump = 1;
-                                       p = fgets(b, READBUF, f);
-                                       if (nobuf)
-                                               if ((fpos = ftello(f)) == -1)
-                                                       warn("%s", name);
-                                       canjump = 0;
                                }
+
+                               if (nobuf)
+                                       fseeko(f, fpos, SEEK_SET);
+                               canjump = 1;
+                               p = fgets(b, READBUF, f);
+                               if (nobuf)
+                                       if ((fpos = ftello(f)) == -1)
+                                               warn("%s", name);
+                               canjump = 0;
+
                                if (p == NULL || *b == '\0') {
                                        if (ferror(f))
                                                warn("%s", name);
                                        eofline = fline;
                                        eof = 1;
                                        break;
-                               } else {
-                                       if (!nobuf)
-                                               fputs(b, fbuf);
-                                       fwrite_all(&pos, sizeof pos, 1, find);
-                                       if (!fflag) {
-                                               oldpos = pos;
-                                               p = b;
-                                               while (*(p = endline(ttycols,
-                                                                    p))
-                                                      != '\0') {
-                                                       pos = oldpos + (p - b);
-                                                       fwrite_all(&pos,
-                                                                  sizeof pos,
-                                                                  1, find);
-                                                       fline++;
-                                                       bline++;
-                                               }
+                               }
+
+                               if (!nobuf)
+                                       fputs(b, fbuf);
+                               fwrite_all(&pos, sizeof pos, 1, find);
+                               if (!fflag) {
+                                       oldpos = pos;
+                                       p = b;
+                                       while (*(p = endline(ttycols,
+                                                            p))
+                                              != '\0') {
+                                               pos = oldpos + (p - b);
+                                               fwrite_all(&pos,
+                                                          sizeof pos,
+                                                          1, find);
+                                               fline++;
+                                               bline++;
                                        }
-                                       fline++;
                                }
+                               fline++;
                        } while (line > bline++);
                } else {
                        /* eofline != 0 */
@@ -1049,7 +1058,9 @@ static void pgfile(FILE *f, const char *name)
                                skip(1);
                        }
                        continue;
-               } else if (eof) {
+               }
+
+               if (eof) {
                        /* We are not searching. */
                        line = bline;
                } else if (*b != '\0') {
@@ -1372,9 +1383,8 @@ static void pgfile(FILE *f, const char *name)
                                                my_sigset(SIGTERM, oldterm);
                                                execl(sh, sh, "-c",
                                                      cmd.cmdline + 1, NULL);
-                                               warn(_("failed to execute %s"), sh);
-                                               _exit(0177);
-                                               /* NOTREACHED */
+                                               errexec(sh);
+                                               break;
                                        }
                                        case -1:
                                                mesg(_("fork() failed, "
@@ -1537,7 +1547,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        if (tcgetattr(STDOUT_FILENO, &otio) == 0) {
                ontty = 1;
@@ -1559,11 +1569,11 @@ int main(int argc, char **argv)
                argc--;
 
                if (!strcmp(argv[arg], "--help")) {
-                   usage(stdout);
+                   usage();
                }
 
                if (!strcmp(argv[arg], "--version")) {
-                   printf(UTIL_LINUX_VERSION);
+                   print_version(EXIT_SUCCESS);
                    return EXIT_SUCCESS;
                }
 
@@ -1614,11 +1624,11 @@ int main(int argc, char **argv)
                        case 's':
                                sflag = 1;
                                break;
+
                        case 'h':
-                               usage(stdout);
+                               usage();
                        case 'V':
-                               printf(UTIL_LINUX_VERSION);
-                               return EXIT_SUCCESS;
+                               print_version(EXIT_SUCCESS);
                        default:
                                invopt(&argv[arg][i]);
                        }