当前位置: 首页 > news >正文

linux cat命令改变功能显示当前文件行号

linux的cat命令使用-n显示多个文件行号时,行号是累加的,不是到了新文件就重新计数。这样满足不了我的需求。如果到了新文件能够重新计数,就能使用-nf(在上一篇-f显示文件名功能的基础上)加| grep xxx,既能直接显示xxx所在文件名,又能直接显示xxx所在文件的行号。

对比效果

cat1是上一次编译的增加了-f功能但是没有重置行号的可执行文件。

bibo@sunny:~/work/coreutils/coreutils$ ../../cat/cat1 ./src/*.c -nf | grep full_write
./src/cat.c:  1777	      if (full_write (STDOUT_FILENO, buf, n_read) != n_read)
./src/cat.c:  1792	      if (full_write (STDOUT_FILENO, outbuf, n_write) != n_write)
./src/cat.c:  1856	                  if (full_write (STDOUT_FILENO, wp, outsize) != outsize)
./src/cat.c:  2408	      if (full_write (STDOUT_FILENO, "\r", 1) != 1)
./src/copy.c:  6643	                  if (full_write (dest_fd, pbuf, psize) != psize)
./src/copy.c:  6739	      if ((full_write (fd, zeros, n)) != n)
./src/factor.c: 27708	  if (full_write (STDOUT_FILENO, lbuf.buf, size) != size)
./src/split.c: 66931	  if (full_write (output_desc, bp, bytes) == bytes)
./src/split.c: 67258	              if (full_write (STDOUT_FILENO, bp, to_write) != to_write)
./src/split.c: 67348	      if (full_write (STDOUT_FILENO, buf, n_read) != n_read
./src/split.c: 67515	                  if (full_write (STDOUT_FILENO, bp, to_write) != to_write)
./src/split.c: 67533	                  if (full_write (files[i_file].ofd, bp, to_write) != to_write
./src/yes.c: 85966	  while (full_write (STDOUT_FILENO, buf, bufused) == bufused)
bibo@sunny:~/work/coreutils/coreutils$ ./src/cat ./src/*.c -nf | grep full_write
./src/cat.c:   195	      if (full_write (STDOUT_FILENO, buf, n_read) != n_read)
./src/cat.c:   210	      if (full_write (STDOUT_FILENO, outbuf, n_write) != n_write)
./src/cat.c:   274	                  if (full_write (STDOUT_FILENO, wp, outsize) != outsize)
./src/cat.c:   826	      if (full_write (STDOUT_FILENO, "\r", 1) != 1)
./src/copy.c:   427	                  if (full_write (dest_fd, pbuf, psize) != psize)
./src/copy.c:   523	      if ((full_write (fd, zeros, n)) != n)
./src/factor.c:  2365	  if (full_write (STDOUT_FILENO, lbuf.buf, size) != size)
./src/split.c:   632	  if (full_write (output_desc, bp, bytes) == bytes)
./src/split.c:   959	              if (full_write (STDOUT_FILENO, bp, to_write) != to_write)
./src/split.c:  1049	      if (full_write (STDOUT_FILENO, buf, n_read) != n_read
./src/split.c:  1216	                  if (full_write (STDOUT_FILENO, bp, to_write) != to_write)
./src/split.c:  1234	                  if (full_write (files[i_file].ofd, bp, to_write) != to_write
./src/yes.c:   125	  while (full_write (STDOUT_FILENO, buf, bufused) == bufused)

做出改动的源文件

./src/cat.c

使用sed将源文件中的windows的回车符'\r'去掉,如果有回车符'\r'会提交失败,这是在windows和linux来回传输文件并编辑文件导致的。

sed -i 's/\r//' ./src/cat.c

使用git add将./src/cat.c添加到暂存区

git add ./src/cat.c

使用git commit提交这次代码更新,-m表示备注信息,备注信息必须要遵守原项目规定的格式,否则会提交失败。

git commit -m "cat: reset row number when open a new file"

使用git log查看提交日志,可以看到之前别人的提交备注信息都是简短的关键词加冒号,再加简单的说明。

bibo@sunny:~/work/coreutils/coreutils$ git log
commit 64efc26d39266b72af029e8acf537f1f28a92c0a (HEAD -> master)
Author: BIBO <120705103@qq.com>
Date:   Fri Dec 29 11:22:31 2023 +0800cat: reset row number when open a new filecommit 98d463ef5d44b9dfe6757942251164b56795b06f (origin/master, origin/HEAD)
Author: Christian Göttsche <cgzones@googlemail.com>
Date:   Tue Dec 19 15:55:28 2023 +0100copy,install: avoid unnecessary security context translationsDo not perform SELinux context translation for operations not involvinguser input or output.  Context translation converts MCS/MLS labels intohuman readable form, which is useful for user facing applications likels(1) or the --context=CTX argument of cp(1).* src/copy.c (set_process_security_ctx): Use raw selinux variants.* src/install.c (need_copy): Likewise.(setdefaultfilecon): Likewise.* src/selinux.c (computecon): Likewise.(defaultcon): Likewise.* tests/cp/no-ctx.sh: Add raw variants to preload lib.* NEWS: Mention the improvement.commit 343b8d7ba705c4e2a537bdc06ac6aa043e5f9e31
Author: Pádraig Brady <P@draigBrady.com>
Date:   Tue Dec 19 17:18:46 2023 +0000build: update gnulib to latest* gnulib: Primarily to get raw selinux wrappers

使用git diff加提交id查看提交前后的差异,新的提交id放在后面看起来会更舒服,显示的差异是增量。

bibo@sunny:~/work/coreutils/coreutils$ git diff 98d463ef5d44b9dfe6757942251164b56795b06f 64efc26d39266b72af029e8acf537f1f28a92c0a
diff --git a/src/cat.c b/src/cat.c
index e019faa8a..178c61c02 100644
--- a/src/cat.c
+++ b/src/cat.c
@@ -110,6 +110,7 @@ Concatenate FILE(s) to standard output.\n\-t                       equivalent to -vT\n\-T, --show-tabs          display TAB characters as ^I\n\-u                       (ignored)\n\
+  -f, --show-filename      display filename at begin of each line\n\-v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB\n\"), stdout);fputs (HELP_OPTION_DESCRIPTION, stdout);
@@ -126,6 +127,22 @@ Examples:\n\exit (status);}+/* Reset line number */
+static void
+reset_line_num (void)
+{
+  line_num_print = line_buf + LINE_COUNTER_BUF_LEN - 8;
+  line_num_start = line_buf + LINE_COUNTER_BUF_LEN - 3;
+  line_num_end = line_buf + LINE_COUNTER_BUF_LEN - 3;
+  for (int i = 0; i < 17; i++)
+    {
+      line_buf[i] = ' ';
+    }
+  line_buf[17] = '0';
+  line_buf[18] = '\t';
+  line_buf[19] = '\0';
+}
+/* Compute the next line number.  */static void
@@ -211,7 +228,7 @@ write_pending (char *outbuf, char **bpout)static boolcat (char *inbuf, idx_t insize, char *outbuf, idx_t outsize,bool show_nonprinting, bool show_tabs, bool number, bool number_nonblank,
-     bool show_ends, bool squeeze_blank)
+     bool show_ends, bool squeeze_blank, bool show_filename){/* Last character read from the input buffer.  */unsigned char ch;
@@ -395,6 +412,13 @@ cat (char *inbuf, idx_t insize, char *outbuf, idx_t outsize,pending_cr = false;}+      /* Are we at the beginning of a line, and filename are requested? */
+      if (newlines >= 0 && show_filename)
+        {
+          bpout = stpcpy (bpout, infile);
+          *bpout++ = ':';
+        }
+/* Are we at the beginning of a line, and line numbers are requested?  */if (newlines >= 0 && number)
@@ -547,6 +571,7 @@ main (int argc, char **argv)bool show_ends = false;bool show_nonprinting = false;bool show_tabs = false;
+  bool show_filename = false;int file_open_mode = O_RDONLY;static struct option const long_options[] =
@@ -558,6 +583,7 @@ main (int argc, char **argv){"show-ends", no_argument, nullptr, 'E'},{"show-tabs", no_argument, nullptr, 'T'},{"show-all", no_argument, nullptr, 'A'},
+         {"show-filename", no_argument, nullptr, 'f'},{GETOPT_HELP_OPTION_DECL},{GETOPT_VERSION_OPTION_DECL},{nullptr, 0, nullptr, 0}
@@ -578,7 +604,7 @@ main (int argc, char **argv)/* Parse command line options.  */int c;
-  while ((c = getopt_long (argc, argv, "benstuvAET", long_options, nullptr))
+  while ((c = getopt_long (argc, argv, "befnstuvAET", long_options, nullptr))!= -1){switch (c)
@@ -628,6 +654,10 @@ main (int argc, char **argv)show_tabs = true;break;+        case 'f':
+          show_filename = true;
+          break;
+case_GETOPT_HELP_CHAR;case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
@@ -667,6 +697,8 @@ main (int argc, char **argv)do{
+      reset_line_num();
+if (argind < argc)infile = argv[argind];@@ -722,7 +754,7 @@ main (int argc, char **argv)works, 'simple_cat' otherwise.  */if (! (number || show_ends || show_nonprinting
-             || show_tabs || squeeze_blank))
+             || show_tabs || squeeze_blank || show_filename)){int copy_cat_status =out_isreg && S_ISREG (stat_buf.st_mode) ? copy_cat () : 0;
@@ -773,7 +805,7 @@ main (int argc, char **argv)ok &= cat (inbuf, insize, outbuf, outsize, show_nonprinting,show_tabs, number, number_nonblank, show_ends,
-                     squeeze_blank);
+                     squeeze_blank, show_filename);alignfree (outbuf);}

修改后的./src/cat.c文件

/* cat -- concatenate files and print on the standard output.Copyright (C) 1988-2023 Free Software Foundation, Inc.This program is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program.  If not, see <https://www.gnu.org/licenses/>.  *//* Differences from the Unix cat:* Always unbuffered, -u is ignored.* Usually much faster than other versions of cat, the differenceis especially apparent when using the -v option.By tege@sics.se, Torbjörn Granlund, advised by rms, Richard Stallman.  */#include <config.h>#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>#if HAVE_STROPTS_H
# include <stropts.h>
#endif
#include <sys/ioctl.h>#include "system.h"
#include "alignalloc.h"
#include "ioblksize.h"
#include "fadvise.h"
#include "full-write.h"
#include "safe-read.h"
#include "xbinary-io.h"/* The official name of this program (e.g., no 'g' prefix).  */
#define PROGRAM_NAME "cat"#define AUTHORS \proper_name_lite ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \proper_name ("Richard M. Stallman")/* Name of input file.  May be "-".  */
static char const *infile;/* Descriptor on which input file is open.  */
static int input_desc;/* Buffer for line numbers.An 11 digit counter may overflow within an hour on a P2/466,an 18 digit counter needs about 1000y */
#define LINE_COUNTER_BUF_LEN 20
static char line_buf[LINE_COUNTER_BUF_LEN] ={' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0','\t', '\0'};/* Position in 'line_buf' where printing starts.  This will not changeunless the number of lines is larger than 999999.  */
static char *line_num_print = line_buf + LINE_COUNTER_BUF_LEN - 8;/* Position of the first digit in 'line_buf'.  */
static char *line_num_start = line_buf + LINE_COUNTER_BUF_LEN - 3;/* Position of the last digit in 'line_buf'.  */
static char *line_num_end = line_buf + LINE_COUNTER_BUF_LEN - 3;/* Preserves the 'cat' function's local 'newlines' between invocations.  */
static int newlines2 = 0;/* Whether there is a pending CR to process.  */
static bool pending_cr = false;void
usage (int status)
{if (status != EXIT_SUCCESS)emit_try_help ();else{printf (_("\
Usage: %s [OPTION]... [FILE]...\n\
"),program_name);fputs (_("\
Concatenate FILE(s) to standard output.\n\
"), stdout);emit_stdin_note ();fputs (_("\
\n\-A, --show-all           equivalent to -vET\n\-b, --number-nonblank    number nonempty output lines, overrides -n\n\-e                       equivalent to -vE\n\-E, --show-ends          display $ at end of each line\n\-n, --number             number all output lines\n\-s, --squeeze-blank      suppress repeated empty output lines\n\
"), stdout);fputs (_("\-t                       equivalent to -vT\n\-T, --show-tabs          display TAB characters as ^I\n\-u                       (ignored)\n\-f, --show-filename      display filename at begin of each line\n\-v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB\n\
"), stdout);fputs (HELP_OPTION_DESCRIPTION, stdout);fputs (VERSION_OPTION_DESCRIPTION, stdout);printf (_("\
\n\
Examples:\n\%s f - g  Output f's contents, then standard input, then g's contents.\n\%s        Copy standard input to standard output.\n\
"),program_name, program_name);emit_ancillary_info (PROGRAM_NAME);}exit (status);
}/* Reset line number */
static void
reset_line_num (void)
{line_num_print = line_buf + LINE_COUNTER_BUF_LEN - 8;line_num_start = line_buf + LINE_COUNTER_BUF_LEN - 3;line_num_end = line_buf + LINE_COUNTER_BUF_LEN - 3;for (int i = 0; i < 17; i++){line_buf[i] = ' ';}line_buf[17] = '0';line_buf[18] = '\t';line_buf[19] = '\0';
}/* Compute the next line number.  */static void
next_line_num (void)
{char *endp = line_num_end;do{if ((*endp)++ < '9')return;*endp-- = '0';}while (endp >= line_num_start);if (line_num_start > line_buf)*--line_num_start = '1';else*line_buf = '>';if (line_num_start < line_num_print)line_num_print--;
}/* Plain cat.  Copy the file behind 'input_desc' to STDOUT_FILENO.BUF (of size BUFSIZE) is the I/O buffer, used by reads and writes.Return true if successful.  */static bool
simple_cat (char *buf, idx_t bufsize)
{/* Loop until the end of the file.  */while (true){/* Read a block of input.  */size_t n_read = safe_read (input_desc, buf, bufsize);if (n_read == SAFE_READ_ERROR){error (0, errno, "%s", quotef (infile));return false;}/* End of this file?  */if (n_read == 0)return true;/* Write this block out.  */if (full_write (STDOUT_FILENO, buf, n_read) != n_read)write_error ();}
}/* Write any pending output to STDOUT_FILENO.Pending is defined to be the *BPOUT - OUTBUF bytes starting at OUTBUF.Then set *BPOUT to OUTPUT if it's not already that value.  */static inline void
write_pending (char *outbuf, char **bpout)
{idx_t n_write = *bpout - outbuf;if (0 < n_write){if (full_write (STDOUT_FILENO, outbuf, n_write) != n_write)write_error ();*bpout = outbuf;}
}/* Copy the file behind 'input_desc' to STDOUT_FILENO.Use INBUF and read INSIZE with each call,and OUTBUF and write OUTSIZE with each call.(The buffers are a bit larger than the I/O sizes.)The remaining boolean args say what 'cat' options to use.Return true if successful.Called if any option more than -u was specified.A newline character is always put at the end of the buffer, to makean explicit test for buffer end unnecessary.  */static bool
cat (char *inbuf, idx_t insize, char *outbuf, idx_t outsize,bool show_nonprinting, bool show_tabs, bool number, bool number_nonblank,bool show_ends, bool squeeze_blank, bool show_filename)
{/* Last character read from the input buffer.  */unsigned char ch;/* Determines how many consecutive newlines there have been in theinput.  0 newlines makes NEWLINES -1, 1 newline makes NEWLINES 1,etc.  Initially 0 to indicate that we are at the beginning of anew line.  The "state" of the procedure is determined byNEWLINES.  */int newlines = newlines2;#ifdef FIONREAD/* If nonzero, use the FIONREAD ioctl, as an optimization.(On Ultrix, it is not supported on NFS file systems.)  */bool use_fionread = true;
#endif/* The inbuf pointers are initialized so that BPIN > EOB, and thereby inputis read immediately.  *//* Pointer to the first non-valid byte in the input buffer, i.e., thecurrent end of the buffer.  */char *eob = inbuf;/* Pointer to the next character in the input buffer.  */char *bpin = eob + 1;/* Pointer to the position where the next character shall be written.  */char *bpout = outbuf;while (true){do{/* Write if there are at least OUTSIZE bytes in OUTBUF.  */if (outbuf + outsize <= bpout){char *wp = outbuf;idx_t remaining_bytes;do{if (full_write (STDOUT_FILENO, wp, outsize) != outsize)write_error ();wp += outsize;remaining_bytes = bpout - wp;}while (outsize <= remaining_bytes);/* Move the remaining bytes to the beginning of thebuffer.  */memmove (outbuf, wp, remaining_bytes);bpout = outbuf + remaining_bytes;}/* Is INBUF empty?  */if (bpin > eob){bool input_pending = false;
#ifdef FIONREADint n_to_read = 0;/* Is there any input to read immediately?If not, we are about to wait,so write all buffered output before waiting.  */if (use_fionread&& ioctl (input_desc, FIONREAD, &n_to_read) < 0){/* Ultrix returns EOPNOTSUPP on NFS;HP-UX returns ENOTTY on pipes.SunOS returns EINVAL andMore/BSD returns ENODEV on special fileslike /dev/null.Irix-5 returns ENOSYS on pipes.  */if (errno == EOPNOTSUPP || errno == ENOTTY|| errno == EINVAL || errno == ENODEV|| errno == ENOSYS)use_fionread = false;else{error (0, errno, _("cannot do ioctl on %s"),quoteaf (infile));newlines2 = newlines;return false;}}if (n_to_read != 0)input_pending = true;
#endifif (!input_pending)write_pending (outbuf, &bpout);/* Read more input into INBUF.  */size_t n_read = safe_read (input_desc, inbuf, insize);if (n_read == SAFE_READ_ERROR){error (0, errno, "%s", quotef (infile));write_pending (outbuf, &bpout);newlines2 = newlines;return false;}if (n_read == 0){write_pending (outbuf, &bpout);newlines2 = newlines;return true;}/* Update the pointers and insert a sentinel at the bufferend.  */bpin = inbuf;eob = bpin + n_read;*eob = '\n';}else{/* It was a real (not a sentinel) newline.  *//* Was the last line empty?(i.e., have two or more consecutive newlines been read?)  */if (++newlines > 0){if (newlines >= 2){/* Limit this to 2 here.  Otherwise, with lots ofconsecutive newlines, the counter could wraparound at INT_MAX.  */newlines = 2;/* Are multiple adjacent empty lines to be substitutedby single ditto (-s), and this was the second emptyline?  */if (squeeze_blank){ch = *bpin++;continue;}}/* Are line numbers to be written at empty lines (-n)?  */if (number && !number_nonblank){next_line_num ();bpout = stpcpy (bpout, line_num_print);}}/* Output a currency symbol if requested (-e).  */if (show_ends){if (pending_cr){*bpout++ = '^';*bpout++ = 'M';pending_cr = false;}*bpout++ = '$';}/* Output the newline.  */*bpout++ = '\n';}ch = *bpin++;}while (ch == '\n');/* Here CH cannot contain a newline character.  */if (pending_cr){*bpout++ = '\r';pending_cr = false;}/* Are we at the beginning of a line, and filename are requested? */if (newlines >= 0 && show_filename){bpout = stpcpy (bpout, infile);*bpout++ = ':';}/* Are we at the beginning of a line, and line numbers are requested?  */if (newlines >= 0 && number){next_line_num ();bpout = stpcpy (bpout, line_num_print);}/* The loops below continue until a newline character is found,which means that the buffer is empty or that a proper newlinehas been found.  *//* If quoting, i.e., at least one of -v, -e, or -t specified,scan for chars that need conversion.  */if (show_nonprinting){while (true){if (ch >= 32){if (ch < 127)*bpout++ = ch;else if (ch == 127){*bpout++ = '^';*bpout++ = '?';}else{*bpout++ = 'M';*bpout++ = '-';if (ch >= 128 + 32){if (ch < 128 + 127)*bpout++ = ch - 128;else{*bpout++ = '^';*bpout++ = '?';}}else{*bpout++ = '^';*bpout++ = ch - 128 + 64;}}}else if (ch == '\t' && !show_tabs)*bpout++ = '\t';else if (ch == '\n'){newlines = -1;break;}else{*bpout++ = '^';*bpout++ = ch + 64;}ch = *bpin++;}}else{/* Not quoting, neither of -v, -e, or -t specified.  */while (true){if (ch == '\t' && show_tabs){*bpout++ = '^';*bpout++ = ch + 64;}else if (ch != '\n'){if (ch == '\r' && *bpin == '\n' && show_ends){if (bpin == eob)pending_cr = true;else{*bpout++ = '^';*bpout++ = 'M';}}else*bpout++ = ch;}else{newlines = -1;break;}ch = *bpin++;}}}
}/* Copy data from input to output using copy_file_range if possible.Return 1 if successful, 0 if ordinary read+write should be tried,-1 if a serious problem has been diagnosed.  */static int
copy_cat (void)
{/* Copy at most COPY_MAX bytes at a time; this is min(SSIZE_MAX, SIZE_MAX) truncated to a value that issurely aligned well.  */ssize_t copy_max = MIN (SSIZE_MAX, SIZE_MAX) >> 30 << 30;/* copy_file_range does not support some cases, and itincorrectly returns 0 when reading from the proc filesystem on the Linux kernel through at least 5.6.19 (2020),so fall back on read+write if the copy_file_range isunsupported or the input file seems empty.  */for (bool some_copied = false; ; some_copied = true)switch (copy_file_range (input_desc, nullptr, STDOUT_FILENO, nullptr,copy_max, 0)){case 0:return some_copied;case -1:if (errno == ENOSYS || is_ENOTSUP (errno) || errno == EINVAL|| errno == EBADF || errno == EXDEV || errno == ETXTBSY|| errno == EPERM)return 0;error (0, errno, "%s", quotef (infile));return -1;}
}int
main (int argc, char **argv)
{/* Nonzero if we have ever read standard input.  */bool have_read_stdin = false;struct stat stat_buf;/* Variables that are set according to the specified options.  */bool number = false;bool number_nonblank = false;bool squeeze_blank = false;bool show_ends = false;bool show_nonprinting = false;bool show_tabs = false;bool show_filename = false;int file_open_mode = O_RDONLY;static struct option const long_options[] ={{"number-nonblank", no_argument, nullptr, 'b'},{"number", no_argument, nullptr, 'n'},{"squeeze-blank", no_argument, nullptr, 's'},{"show-nonprinting", no_argument, nullptr, 'v'},{"show-ends", no_argument, nullptr, 'E'},{"show-tabs", no_argument, nullptr, 'T'},{"show-all", no_argument, nullptr, 'A'},{"show-filename", no_argument, nullptr, 'f'},{GETOPT_HELP_OPTION_DECL},{GETOPT_VERSION_OPTION_DECL},{nullptr, 0, nullptr, 0}};initialize_main (&argc, &argv);set_program_name (argv[0]);setlocale (LC_ALL, "");bindtextdomain (PACKAGE, LOCALEDIR);textdomain (PACKAGE);/* Arrange to close stdout if we exit via thecase_GETOPT_HELP_CHAR or case_GETOPT_VERSION_CHAR code.Normally STDOUT_FILENO is used rather than stdout, soclose_stdout does nothing.  */atexit (close_stdout);/* Parse command line options.  */int c;while ((c = getopt_long (argc, argv, "befnstuvAET", long_options, nullptr))!= -1){switch (c){case 'b':number = true;number_nonblank = true;break;case 'e':show_ends = true;show_nonprinting = true;break;case 'n':number = true;break;case 's':squeeze_blank = true;break;case 't':show_tabs = true;show_nonprinting = true;break;case 'u':/* We provide the -u feature unconditionally.  */break;case 'v':show_nonprinting = true;break;case 'A':show_nonprinting = true;show_ends = true;show_tabs = true;break;case 'E':show_ends = true;break;case 'T':show_tabs = true;break;case 'f':show_filename = true;break;case_GETOPT_HELP_CHAR;case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);default:usage (EXIT_FAILURE);}}/* Get device, i-node number, and optimal blocksize of output.  */if (fstat (STDOUT_FILENO, &stat_buf) < 0)error (EXIT_FAILURE, errno, _("standard output"));/* Optimal size of i/o operations of output.  */idx_t outsize = io_blksize (&stat_buf);/* Device and I-node number of the output.  */dev_t out_dev = stat_buf.st_dev;ino_t out_ino = stat_buf.st_ino;/* True if the output is a regular file.  */bool out_isreg = S_ISREG (stat_buf.st_mode) != 0;if (! (number || show_ends || squeeze_blank)){file_open_mode |= O_BINARY;xset_binary_mode (STDOUT_FILENO, O_BINARY);}/* Main loop.  */infile = "-";int argind = optind;bool ok = true;idx_t page_size = getpagesize ();do{reset_line_num();if (argind < argc)infile = argv[argind];bool reading_stdin = STREQ (infile, "-");if (reading_stdin){have_read_stdin = true;input_desc = STDIN_FILENO;if (file_open_mode & O_BINARY)xset_binary_mode (STDIN_FILENO, O_BINARY);}else{input_desc = open (infile, file_open_mode);if (input_desc < 0){error (0, errno, "%s", quotef (infile));ok = false;continue;}}if (fstat (input_desc, &stat_buf) < 0){error (0, errno, "%s", quotef (infile));ok = false;goto contin;}/* Optimal size of i/o operations of input.  */idx_t insize = io_blksize (&stat_buf);fdadvise (input_desc, 0, 0, FADVISE_SEQUENTIAL);/* Don't copy a nonempty regular file to itself, as that wouldmerely exhaust the output device.  It's better to catch thiserror earlier rather than later.  */if (out_isreg&& stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino&& lseek (input_desc, 0, SEEK_CUR) < stat_buf.st_size){error (0, 0, _("%s: input file is output file"), quotef (infile));ok = false;goto contin;}/* Pointer to the input buffer.  */char *inbuf;/* Select which version of 'cat' to use.  If any format-orientedoptions were given use 'cat'; if not, use 'copy_cat' if itworks, 'simple_cat' otherwise.  */if (! (number || show_ends || show_nonprinting|| show_tabs || squeeze_blank || show_filename)){int copy_cat_status =out_isreg && S_ISREG (stat_buf.st_mode) ? copy_cat () : 0;if (copy_cat_status != 0){inbuf = nullptr;ok &= 0 < copy_cat_status;}else{insize = MAX (insize, outsize);inbuf = xalignalloc (page_size, insize);ok &= simple_cat (inbuf, insize);}}else{/* Allocate, with an extra byte for a newline sentinel.  */inbuf = xalignalloc (page_size, insize + 1);/* Why are(OUTSIZE - 1 + INSIZE * 4 + LINE_COUNTER_BUF_LEN)bytes allocated for the output buffer?A test whether output needs to be written is done when the inputbuffer empties or when a newline appears in the input.  Afteroutput is written, at most (OUTSIZE - 1) bytes will remain in thebuffer.  Now INSIZE bytes of input is read.  Each input charactermay grow by a factor of 4 (by the prepending of M-^).  If allcharacters do, and no newlines appear in this block of input, wewill have at most (OUTSIZE - 1 + INSIZE * 4) bytes in the buffer.If the last character in the preceding block of input was anewline, a line number may be written (according to the givenoptions) as the first thing in the output buffer. (Done after thenew input is read, but before processing of the input begins.)A line number requires seldom more than LINE_COUNTER_BUF_LENpositions.Align the output buffer to a page size boundary, for efficiencyon some paging implementations.  */idx_t bufsize;if (ckd_mul (&bufsize, insize, 4)|| ckd_add (&bufsize, bufsize, outsize)|| ckd_add (&bufsize, bufsize, LINE_COUNTER_BUF_LEN - 1))xalloc_die ();char *outbuf = xalignalloc (page_size, bufsize);ok &= cat (inbuf, insize, outbuf, outsize, show_nonprinting,show_tabs, number, number_nonblank, show_ends,squeeze_blank, show_filename);alignfree (outbuf);}alignfree (inbuf);contin:if (!reading_stdin && close (input_desc) < 0){error (0, errno, "%s", quotef (infile));ok = false;}}while (++argind < argc);if (pending_cr){if (full_write (STDOUT_FILENO, "\r", 1) != 1)write_error ();}if (have_read_stdin && close (STDIN_FILENO) < 0)error (EXIT_FAILURE, errno, _("closing standard input"));return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}

相关文章:

linux cat命令改变功能显示当前文件行号

linux的cat命令使用-n显示多个文件行号时&#xff0c;行号是累加的&#xff0c;不是到了新文件就重新计数。这样满足不了我的需求。如果到了新文件能够重新计数&#xff0c;就能使用-nf&#xff08;在上一篇-f显示文件名功能的基础上&#xff09;加| grep xxx&#xff0c;既能直…...

Django-REST-Framework 如何快速生成Swagger, ReDoc格式的 REST API 文档

1、API 接口文档的几种规范格式 前后端分离项目中&#xff0c;使用规范、便捷的API接口文档工具&#xff0c;可以有效提高团队工作效率。 标准化的API文档的益处&#xff1a; 允许开发人员以交互式的方式查看、测试API接口&#xff0c;以方便使用将所有可暴露的API接口进行分…...

SpringBoot当中的Singleton和Prototype详解

在Spring Boot中&#xff0c;Singleton和Prototype是两种Bean的作用域。这两种作用域决定了Spring容器如何创建和管理Bean的实例。 Singleton&#xff08;单例&#xff09;&#xff1a; 当一个Bean被配置为Singleton作用域时&#xff0c;Spring容器在启动时只会创建该Bean的一个…...

LeetCode第1题 - 两数之和

题目 给定一个整数数组 nums 和一个目标值 target&#xff0c;请你在该数组中找出和为目标值的那 两个 整数&#xff0c;并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;你不能重复利用这个数组中同样的元素。 示例 给定 nums [2, 7, 11, 15], …...

(14)Linux 地址空间的理解

前言&#xff1a;本章核心主题为 "进程地址空间"。 一、Linux 进程地址空间 程序地址空间是内存吗&#xff1f;不是&#xff01;程序地址空间不是内存&#xff01; 其实&#xff0c;我们称之为程序地址空间都不准确&#xff0c;应该叫 进程地址空间&#xff0c;这…...

Java中的设计模式

设计模式是软件开发中常见问题的可重用解决方案。在Java中&#xff0c;设计模式有助于提高代码的可维护性、可读性和可扩展性。以下是一篇关于Java中设计模式的文章&#xff0c;以帮助您更好地理解这些模式。 一、设计模式简介 设计模式是经过验证的解决方案&#xff0c;用于…...

Hadoop(2):常见的MapReduce[在Ubuntu中运行!]

1 以词频统计为例子介绍 mapreduce怎么写出来的 弄清楚MapReduce的各个过程&#xff1a; 将文件输入后&#xff0c;返回的<k1,v1>代表的含义是&#xff1a;k1表示偏移量&#xff0c;即v1的第一个字母在文件中的索引&#xff08;从0开始数的&#xff09;&#xff1b;v1表…...

Unity | 快速修复Animation missing错误

目录 一、背景 二、效果 三、解决办法 一、背景 最近在做2D 骨骼动画相关的Demo&#xff0c;我自己使用Unity引擎进行骨骼绑定并创建了anim后&#xff0c;一切正常&#xff0c;anim也能播放。但是昨天我修改Obj及子物体的名称&#xff08;由中文改为英文&#xff0c;如&…...

ssm基于web的志愿者管理系统的设计与实现+vue论文

摘 要 使用旧方法对志愿者管理系统的信息进行系统化管理已经不再让人们信赖了&#xff0c;把现在的网络信息技术运用在志愿者管理系统的管理上面可以解决许多信息管理上面的难题&#xff0c;比如处理数据时间很长&#xff0c;数据存在错误不能及时纠正等问题。这次开发的志愿者…...

C++运算符重载(插入and提取)

介绍 本文主要介绍 插入(>>) and 提取(<<)的运算符重载 1.插入(>>) 提取(<<)只能是友元函数 2.插入关键词istream 例子&#xff1a;istream& operator>>(istream& in, sumber&Left) 3.提取关键词ostream 例子&#xff1a;ostream&a…...

C#高级 08Json操作

1.概念 Json是存储和交换文本信息的语法。类似于XML。Json比XML更小、更快、更易解析。Json与XML一样是一种数据格式。Json是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。Json采取完全独立于语言的文本格式&#xff0c; 但是也使用了类似于C语言的习惯。这些特性使…...

封装uniapp签字板

新开发的业务涉及到签字功能&#xff0c;由于是动态的表单&#xff0c;无法确定它会出现在哪里&#xff0c;不得已封装模块。 其中涉及到一个难点就是this的指向性问题&#xff0c; 第二个是微信小程序写法&#xff0c; 我这个写法里用了u-view的写法&#xff0c;可以自己修改组…...

Mybatis行为配置之Ⅳ—日志

专栏精选 引入Mybatis Mybatis的快速入门 Mybatis的增删改查扩展功能说明 mapper映射的参数和结果 Mybatis复杂类型的结果映射 Mybatis基于注解的结果映射 Mybatis枚举类型处理和类型处理器 再谈动态SQL Mybatis配置入门 Mybatis行为配置之Ⅰ—缓存 Mybatis行为配置…...

Java设计模式-外观模式

目录 一、影院管理项目 二、外观模式 &#xff08;一&#xff09;基本介绍 &#xff08;二&#xff09;原理类图 &#xff08;三&#xff09;解决影院管理 &#xff08;四&#xff09;注意事项和细节 &#xff08;五&#xff09;外观模式在MyBatis框架应用的源码分析 一…...

js+css实现颜色选择器

<!DOCTYPE html> <html> <head><meta charset"UTF-8"><title>颜色选择器</title><style>.color-box {width: 50px;height: 50px;border: 1px solid #000;cursor: pointer;}</style> </head> <body><…...

Go语言中的包管理工具之Go Modules的使用

GoLang 中常用的包管理的方式 常用的有三种 Go PathGo VendorGo Modules 关于 Go Modules 1 ) 概述 Go的包管理&#xff0c;经过社区和官方的共同努力下&#xff0c;最终在百家争鸣后Go官方在 2018.8 推出了go 1.11版本中的Go Modules&#xff0c;并且很快成为一统江湖的包…...

【c/c++】指针例图基础详解

文章目录 指针变量内存指针详解例1例2练习&答案解析 指针变量内存 int main(){// 各类型变量占字节数printf("char: %d\n",sizeof(char)); // 1printf("short: %d\n",sizeof(short)); // 2printf("int: %d\n",sizeof(int)); // 4pri…...

TCP/IP的网络层(即IP层)之IP地址和网络掩码,在视频监控系统中的配置和应用

在给客户讲解我们的AS-V1000视频监控平台的时候&#xff0c;有的客户经常会配置错误IP地址的掩码和网关&#xff0c;导致出现一些网路问题。而在视频监控系统中&#xff0c;IP地址和子网掩码是用于标识网络中设备的重要标识符。IP地址被用来唯一地标识一个网络设备&#xff0c;…...

代码随想录刷题 | Day1

今日学习目标 一、基础 数组 array类 模板类vector 数组是存放在连续内存空间上的相同类型数据的集合。 数组可以方便的通过下标索引的方式获取到下标下对应的数据。 需要两点注意的是 数组下标都是从0开始的。 数组内存空间的地址是连续的 而且大家如果使用C的话&…...

查看IOS游戏FPS

摘要 本篇技术博客将介绍如何使用克魔助手工具来查看iOS游戏的帧率&#xff08;FPS&#xff09;。通过克魔助手&#xff0c;开发者可以轻松监测游戏性能&#xff0c;以提升用户体验和游戏质量。 引言 在iOS游戏开发过程中&#xff0c;了解游戏的帧率对于优化游戏性能至关重要…...

挑战Python100题(7)

100+ Python challenging programming exercises 7 Question 61 Print a unicode string "hello world". Hints: Use ustrings format to define unicode string. 打印一个unicode字符串“helloworld”。 提示:使用u“字符串”格式定义unicode字符串。 Solution…...

HarmonyOS自学-Day4(TodoList案例)

目录 文章声明⭐⭐⭐让我们开始今天的学习吧&#xff01;TodoList小案例 文章声明⭐⭐⭐ 该文章为我&#xff08;有编程语言基础&#xff0c;非编程小白&#xff09;的 HarmonyOS自学笔记&#xff0c;此类文章笔记我会默认大家都学过前端相关的知识知识来源为 HarmonyOS官方文…...

LTPI协议的理解——2、LTPI实现的底层架构

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 LTPI协议的理解——2、LTPI实现的底层架构 前言一、体系结构三、实现细节四、物理接口信号传输方法总结 前言 前面讲了LTPI的定义和大概结构&#xff0c;接下来继续理解LTPI…...

CentOS 8.2 安装 Mysql 5.7.26(单机)

Mysql二进制包: mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz 1、卸载旧环境 rpm -qa|grep mysql rpm -qa|grep mariadb rpm -e XXX.rpm --nodeps # 强制卸载rpm包 rm -rf /etc/my.cnf rm -rf /etc/mysql rm -rf /usr/local/mysql 2、安装依赖包 yum -y install libaio yum…...

Vue Tinymce富文本组件自定义带下拉框的操作按钮

想实现如下效果 首先在init方法中的props&#xff0c;toolbar属性增加一个自定义按钮 增加一个setup方法 代码 setup: function(editor) { editor.ui.registry.addSplitButton(myDateButton, {text: 日期时间,onAction: (_) > editor.insertContent(getJsMonthDay(getNowDat…...

YOLOv5算法进阶改进(10)— 更换主干网络之MobileViTv3 | 轻量化Backbone

前言:Hello大家好,我是小哥谈。MobileViTv3是一种改进的模型架构,用于图像分类任务。它是在MobileViTv1和MobileViTv2的基础上进行改进的,通过引入新的模块和优化网络结构来提高性能。本节课就给大家介绍一下如何在主干网络中引入MobileViTv3网络结构,希望大家学习之后能够…...

Java UDP

接收方 创建DatagramSocket实例并指定端口。创建DatagramPacket实例接收信息。调用DatagramSocket的receive()方法将接收信息并传递给DatagramPacket。通过DatagramPacket的getData()方法获取信息内容&#xff0c;getLength()方法获取长度。 package io.github.jast90.udp;im…...

Halcon阈值处理的几种分割方法threshold/auto_threshold/binary_threshold/dyn_threshold

Halcon阈值处理的几种分割方法 文章目录 Halcon阈值处理的几种分割方法1. 全局阈值2. 基于直方图的自动阈值分割方法3. 自动全局阈值分割方法4. 局部阈值分割方法5. var_threshold算子6 . char_threshold 算子7. dual_threshold算子 在场景中选择物体或特征是图像测量或识别的重…...

FB混合C语言编译

这是群友分享的方法&#xff0c;这里只是作为记录和分享。 有了这个功能&#xff0c;可以很方便的拷贝一下C或者C代码直接用到FB上。 既然是混合C语言编译&#xff0c;当然得有C的代码。比如随便去网上找两个排序&#xff1a;冒泡排序和选择排序&#xff0c;代码如下&#xf…...

【机器学习】深度学习概论(二)

五、受限玻尔兹曼机&#xff08;Restricted Boltzmann Machine&#xff0c;RBM&#xff09; 5.1 RBM介绍 示例代码&#xff1a; Python 编写了一个简单的 RBM 实现&#xff0c;并用一些假数据训练了它。然后&#xff0c;他展示了如何用 RBM 来解释用户的电影偏好&#xff0c;以…...

词法语法语义分析程序设计及实现,包含出错提示和错误恢复

词法说明 (1)关键字 main, int, char, if, else, for, while, void (2)运算符 - * / < < > > ! (3)界符 ; ( ) { } (4)标识符 ID letter(letter|digit)* (5)整型常数 NUM digit digit* (6)空格 ‘ ‘ ‘\n’ ‘\r’ ‘\t’ 空格用来分隔ID,NUM,运算符,界…...

Linux的capability深入分析

from:https://www.cnblogs.com/iamfy/archive/2012/09/20/2694977.html 一)概述: 1)从2.1版开始,Linux内核有了能力(capability)的概念,即它打破了UNIX/LINUX操作系统中超级用户/普通用户的概念,由普通用户也可以做只有超级用户可以完成的工作. 2)capability可以作用在进程上…...

【自然语言处理】类似GPT的模型

除了GPT (Generative Pre-trained Transformer) 之外&#xff0c;还有一些其他的好用的类似工具可以用来生成文本。以下是几个受欢迎的工具&#xff1a; BERT (Bidirectional Encoder Representations from Transformers): BERT 是一个预训练的深度双向 Transformer 模型&#…...

【Unity】【FBX】如何将FBX模型导入Unity

【背景】 网上能够找到不少不错的FBX模型资源&#xff0c;大大加速游戏开发时间。如何将这些FBX导入Unity呢&#xff1f; 【步骤】 打开Unity项目文件&#xff0c;进入场景。 点击Projects面板&#xff0c;右键选择Import New Assets 选中FBX文件后导入。Assets文件夹中就会…...

腾讯云标准型S5服务器4核8G配置优惠价格表

腾讯云4核8G服务器S5和轻量应用服务器优惠价格表&#xff0c;轻量应用服务器和CVM云服务器均有活动&#xff0c;云服务器CVM标准型S5实例4核8G配置价格15个月1437.3元&#xff0c;5年6490.44元&#xff0c;轻量应用服务器4核8G12M带宽一年446元、529元15个月&#xff0c;腾讯云…...

学习笔记:R语言基础

文章目录 一、R语言简介二、选择R的原因三、R基本数据对象&#xff08;一&#xff09;向量&#xff08;二&#xff09;矩阵&#xff08;三&#xff09;数组&#xff08;四&#xff09;因子&#xff08;五&#xff09;列表&#xff08;六&#xff09;数据框&#xff08;七&#…...

初识智慧城市

文章目录 智慧家居 智慧社区 智慧交通 智慧医疗 智慧教育 智慧旅游 智慧农业 智慧安防 智慧家居 利用智能语音、智能交互等技术,实现用户对家居系统各设备的远程操控和能控制如开关窗帘(窗户)、操控家用电器和照明系统、打扫卫生等操作。利用计算机视觉等技术,对被照看…...

Zookeeper之手写一个分布式锁

前言 我之前写了一篇快速上手ZK的文章&#xff1a;https://blog.csdn.net/qq_38974073/article/details/135293106 本篇最要是进一步加深学习ZK&#xff0c;算是一次简单的实践&#xff0c;巩固学习成果。 设计一个分布式锁 对锁的基本要求 可重入&#xff1a;允许同一个应…...

【音视频 ffmpeg 学习】 RTMP推流 mp4文件

1.RTMP(实时消息传输协议)是Adobe 公司开发的一个基于TCP的应用层协议。 2.RTMP协议中基本的数据单元称为消息&#xff08;Message&#xff09;。 3.当RTMP协议在互联网中传输数据的时候&#xff0c;消息会被拆分成更小的单元&#xff0c;称为消息块&#xff08;Chunk&#xff…...

跨进程通信 macOS XPC 创建实例

一&#xff1a;简介 XPC 是 macOS 里苹果官方比较推荐和安全的的进程间通信机制。 集成流程简单&#xff0c;但是比较绕。 主要需要集成 XPC Server 这个模块&#xff0c;这个模块最终会被 apple 的根进程 launchd 管理和以独立进程的方法唤起和关闭&#xff0c; 我们主app 进…...

Python圣诞树代码

Python圣诞树代码 # 小黄 2023/12/25import turtle as t # as就是取个别名&#xff0c;后续调用的t都是turtle from turtle import * import random as rn 100.0speed(20) # 定义速度 pensize(5) # 画笔宽度 screensize(800, 800, bgblack) # 定义背景颜色&#xff0c;可…...

flask之文件管理系统-项目 JRP上线啦!!! ---修订版,兼容Windows和Linux系统

上一章的版本https://blog.csdn.net/weixin_44517278/article/details/135275066&#xff0c;在Windows下debug完成无异常后&#xff0c;上传到我的树莓下开始正式服役 由于开发环境是Windows&#xff0c;使用环境是Linux&#xff0c;导致最后没能成功运行起来 这个版本是今天去…...

希尔排序:排序算法中的调优大师

希尔排序&#xff1a;排序算法中的调优大师 大家好&#xff0c;我是免费搭建查券返利机器人赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01;今天&#xff0c;让我们一同探讨一个经典而高效的排序算法——希尔排序。…...

LeetCode 1185. 一周中的第几天

一、题目 1、题目描述 给你一个日期&#xff0c;请你设计一个算法来判断它是对应一周中的哪一天。 输入为三个整数&#xff1a;day、month 和 year&#xff0c;分别表示日、月、年。 您返回的结果必须是这几个值中的一个 {"Sunday", "Monday", "Tues…...

大数据学习(30)-Spark Shuffle

&&大数据学习&& &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 承认自己的无知&#xff0c;乃是开启智慧的大门 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4dd;支持一下博主哦&#x1f91…...

Linux部署ELK

大家好&#xff0c;我是升仔 引言 在复杂的系统架构中&#xff0c;日志管理是一个关键的环节。ELK栈提供了一个高效的解决方案&#xff0c;能够帮助我们快速定位问题、分析数据&#xff0c;并实现实时监控。部署ELK栈是一项挑战&#xff0c;但收益巨大。 基础安装和配置 环境准…...

Python 实现 PDF 到 Word 文档的高效转换(DOC、DOCX)

PDF&#xff08;Portable Document Format&#xff09;已成为一种广泛使用的电子文档格式。PDF的主要优势是跨平台&#xff0c;可以在不同设备上呈现一致的外观。然而&#xff0c;当我们需要对文件内容进行编辑或修改&#xff0c;直接编辑PDF文件会非常困难&#xff0c;而且效果…...

【MYSQL】MYSQL 的学习教程(七)之 慢 SQL 优化思路

1. 慢 SQL 优化思路 慢查询日志记录慢 SQLexplain 分析 SQL 的执行计划profile 分析执行耗时Optimizer Trace 分析详情确定问题并采用相应的措施 1. 慢查询日志记录慢 SQL 如何定位慢SQL呢&#xff1f; 我们可以通过 慢查询日志 来查看慢 SQL。 ①&#xff1a;开启慢查询日志…...

unity学习笔记----游戏练习0

一、修复植物种植的问题 1.当手上存在植物时&#xff0c;再次点击卡片上的植物就会在手上添加新的植物&#xff0c;需要修改成只有手上没有植物时才能再次获取到植物。需要修改AddPlant方法。 public bool AddPlant(PlantType plantType) { //防止手上出现多个植…...

ai概念:强人工智能介绍、迁移学习

强人工智能&#xff08;Strong Artificial Intelligence&#xff0c;SAI&#xff09;是指一种具有与人类智能相媲美或超越人类智能水平的人工智能系统。与弱人工智能&#xff08;Weak Artificial Intelligence&#xff0c;WAI&#xff09;不同&#xff0c;强人工智能具有更高级…...