dd.c

来自「Linux下文件工具。」· C语言 代码 · 共 1,229 行 · 第 1/3 页

C
1,229
字号
/* dd -- convert a file while copying it.   Copyright (C) 85, 90, 91, 1995-2002 Free Software Foundation, Inc.   This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 2, 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 of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software Foundation,   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  *//* Written by Paul Rubin, David MacKenzie, and Stuart Kemp. */#include <config.h>#include <stdio.h>#define SWAB_ALIGN_OFFSET 2#if HAVE_INTTYPES_H# include <inttypes.h>#endif#include <sys/types.h>#include <signal.h>#include <getopt.h>#include "system.h"#include "closeout.h"#include "error.h"#include "full-write.h"#include "getpagesize.h"#include "human.h"#include "long-options.h"#include "quote.h"#include "safe-read.h"#include "xstrtol.h"/* The official name of this program (e.g., no `g' prefix).  */#define PROGRAM_NAME "dd"#define AUTHORS N_ ("Paul Rubin, David MacKenzie, and Stuart Kemp")#ifndef SIGINFO# define SIGINFO SIGUSR1#endif#ifndef S_TYPEISSHM# define S_TYPEISSHM(Stat_ptr) 0#endif#define ROUND_UP_OFFSET(X, M) ((M) - 1 - (((X) + (M) - 1) % (M)))#define PTR_ALIGN(Ptr, M) ((Ptr) \			   + ROUND_UP_OFFSET ((char *)(Ptr) - (char *)0, (M)))#define max(a, b) ((a) > (b) ? (a) : (b))#define output_char(c)				\  do						\    {						\      obuf[oc++] = (c);				\      if (oc >= output_blocksize)		\	write_output ();			\    }						\  while (0)/* Default input and output blocksize. */#define DEFAULT_BLOCKSIZE 512/* Conversions bit masks. */#define C_ASCII 01#define C_EBCDIC 02#define C_IBM 04#define C_BLOCK 010#define C_UNBLOCK 020#define C_LCASE 040#define C_UCASE 0100#define C_SWAB 0200#define C_NOERROR 0400#define C_NOTRUNC 01000#define C_SYNC 02000/* Use separate input and output buffers, and combine partial input blocks. */#define C_TWOBUFS 04000/* The name this program was run with. */char *program_name;/* The name of the input file, or NULL for the standard input. */static char const *input_file = NULL;/* The name of the output file, or NULL for the standard output. */static char const *output_file = NULL;/* The number of bytes in which atomic reads are done. */static size_t input_blocksize = 0;/* The number of bytes in which atomic writes are done. */static size_t output_blocksize = 0;/* Conversion buffer size, in bytes.  0 prevents conversions. */static size_t conversion_blocksize = 0;/* Skip this many records of `input_blocksize' bytes before input. */static uintmax_t skip_records = 0;/* Skip this many records of `output_blocksize' bytes before output. */static uintmax_t seek_records = 0;/* Copy only this many records.  The default is effectively infinity.  */static uintmax_t max_records = (uintmax_t) -1;/* Bit vector of conversions to apply. */static int conversions_mask = 0;/* If nonzero, filter characters through the translation table.  */static int translation_needed = 0;/* Number of partial blocks written. */static uintmax_t w_partial = 0;/* Number of full blocks written. */static uintmax_t w_full = 0;/* Number of partial blocks read. */static uintmax_t r_partial = 0;/* Number of full blocks read. */static uintmax_t r_full = 0;/* Records truncated by conv=block. */static uintmax_t r_truncate = 0;/* Output representation of newline and space characters.   They change if we're converting to EBCDIC.  */static char newline_character = '\n';static char space_character = ' ';/* Output buffer. */static char *obuf;/* Current index into `obuf'. */static size_t oc = 0;/* Index into current line, for `conv=block' and `conv=unblock'.  */static size_t col = 0;struct conversion{  char *convname;  int conversion;};static struct conversion conversions[] ={  {"ascii", C_ASCII | C_TWOBUFS},	/* EBCDIC to ASCII. */  {"ebcdic", C_EBCDIC | C_TWOBUFS},	/* ASCII to EBCDIC. */  {"ibm", C_IBM | C_TWOBUFS},	/* Slightly different ASCII to EBCDIC. */  {"block", C_BLOCK | C_TWOBUFS},	/* Variable to fixed length records. */  {"unblock", C_UNBLOCK | C_TWOBUFS},	/* Fixed to variable length records. */  {"lcase", C_LCASE | C_TWOBUFS},	/* Translate upper to lower case. */  {"ucase", C_UCASE | C_TWOBUFS},	/* Translate lower to upper case. */  {"swab", C_SWAB | C_TWOBUFS},	/* Swap bytes of input. */  {"noerror", C_NOERROR},	/* Ignore i/o errors. */  {"notrunc", C_NOTRUNC},	/* Do not truncate output file. */  {"sync", C_SYNC},		/* Pad input records to ibs with NULs. */  {NULL, 0}};/* Translation table formed by applying successive transformations. */static unsigned char trans_table[256];static char const ascii_to_ebcdic[] ={  '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',  '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',  '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',  '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',  '\100', '\117', '\177', '\173', '\133', '\154', '\120', '\175',  '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',  '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',  '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',  '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',  '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',  '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',  '\347', '\350', '\351', '\112', '\340', '\132', '\137', '\155',  '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',  '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',  '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',  '\247', '\250', '\251', '\300', '\152', '\320', '\241', '\007',  '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',  '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',  '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',  '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',  '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',  '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',  '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',  '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',  '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',  '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',  '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',  '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',  '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',  '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',  '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',  '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'};static char const ascii_to_ibm[] ={  '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',  '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',  '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',  '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',  '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',  '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',  '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',  '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',  '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',  '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',  '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',  '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',  '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',  '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',  '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',  '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',  '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',  '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',  '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',  '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',  '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',  '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',  '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',  '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',  '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',  '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',  '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',  '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',  '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',  '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',  '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',  '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'};static char const ebcdic_to_ascii[] ={  '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',  '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',  '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',  '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',  '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',  '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',  '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',  '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',  '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',  '\247', '\250', '\133', '\056', '\074', '\050', '\053', '\041',  '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',  '\260', '\261', '\135', '\044', '\052', '\051', '\073', '\136',  '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',  '\270', '\271', '\174', '\054', '\045', '\137', '\076', '\077',  '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',  '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',  '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',  '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',  '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',  '\161', '\162', '\313', '\314', '\315', '\316', '\317', '\320',  '\321', '\176', '\163', '\164', '\165', '\166', '\167', '\170',  '\171', '\172', '\322', '\323', '\324', '\325', '\326', '\327',  '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',  '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',  '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',  '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',  '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',  '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',  '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',  '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',  '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',  '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'};voidusage (int status){  if (status != 0)    fprintf (stderr, _("Try `%s --help' for more information.\n"),	     program_name);  else    {      printf (_("Usage: %s [OPTION]...\n"), program_name);      fputs (_("\Copy a file, converting and formatting according to the options.\n\\n\  bs=BYTES        force ibs=BYTES and obs=BYTES\n\  cbs=BYTES       convert BYTES bytes at a time\n\  conv=KEYWORDS   convert the file as per the comma separated keyword list\n\  count=BLOCKS    copy only BLOCKS input blocks\n\  ibs=BYTES       read BYTES bytes at a time\n\"), stdout);      fputs (_("\  if=FILE         read from FILE instead of stdin\n\  obs=BYTES       write BYTES bytes at a time\n\  of=FILE         write to FILE instead of stdout\n\  seek=BLOCKS     skip BLOCKS obs-sized blocks at start of output\n\  skip=BLOCKS     skip BLOCKS ibs-sized blocks at start of input\n\"), stdout);      fputs (HELP_OPTION_DESCRIPTION, stdout);      fputs (VERSION_OPTION_DESCRIPTION, stdout);      fputs (_("\\n\BLOCKS and BYTES may be followed by the following multiplicative suffixes:\n\xM M, c 1, w 2, b 512, kB 1000, K 1024, MB 1,000,000, M 1,048,576,\n\GB 1,000,000,000, G 1,073,741,824, and so on for T, P, E, Z, Y.\n\Each KEYWORD may be:\n\\n\"), stdout);      fputs (_("\  ascii     from EBCDIC to ASCII\n\  ebcdic    from ASCII to EBCDIC\n\  ibm       from ASCII to alternated EBCDIC\n\  block     pad newline-terminated records with spaces to cbs-size\n\  unblock   replace trailing spaces in cbs-size records with newline\n\  lcase     change upper case to lower case\n\"), stdout);      fputs (_("\  notrunc   do not truncate the output file\n\  ucase     change lower case to upper case\n\  swab      swap every pair of input bytes\n\  noerror   continue after read errors\n\  sync      pad every input block with NULs to ibs-size; when used\n\              with block or unblock, pad with spaces rather than NULs\n\"), stdout);      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);    }  exit (status);}static voidtranslate_charset (char const *new_trans){  int i;  for (i = 0; i < 256; i++)    trans_table[i] = new_trans[trans_table[i]];  translation_needed = 1;}/* Return the number of 1 bits in `i'. */static intbit_count (register int i){  register int set_bits;  for (set_bits = 0; i != 0; set_bits++)    i &= i - 1;  return set_bits;}static voidprint_stats (void){  char buf[2][LONGEST_HUMAN_READABLE + 1];  fprintf (stderr, _("%s+%s records in\n"),	   human_readable (r_full, buf[0], 1, 1),	   human_readable (r_partial, buf[1], 1, 1));  fprintf (stderr, _("%s+%s records out\n"),	   human_readable (w_full, buf[0], 1, 1),	   human_readable (w_partial, buf[1], 1, 1));  if (r_truncate > 0)    {      fprintf (stderr, "%s %s\n",	       human_readable (r_truncate, buf[0], 1, 1),	       (r_truncate == 1		? _("truncated record")		: _("truncated records")));    }}static voidcleanup (void){  print_stats ();  if (close (STDIN_FILENO) < 0)    error (1, errno, _("closing input file %s"), quote (input_file));  if (close (STDOUT_FILENO) < 0)    error (1, errno, _("closing output file %s"), quote (output_file));}static inline voidquit (int code){  cleanup ();  exit (code);}static RETSIGTYPEinterrupt_handler (int sig){#ifdef SA_NOCLDSTOP  struct sigaction sigact;  sigact.sa_handler = SIG_DFL;  sigemptyset (&sigact.sa_mask);  sigact.sa_flags = 0;  sigaction (sig, &sigact, NULL);#else  signal (sig, SIG_DFL);#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?