ReactOS  0.4.13-dev-464-g6b95727
cat.c File Reference
#include <stdio.h>
#include <strings.h>
Include dependency graph for cat.c:

Go to the source code of this file.

Macros

#define O_TEXT   0x4000
 
#define O_BINARY   0x8000
 
#define _setmode(fd, mode)
 
#define _stricmp   strcasecmp
 
#define ARRAYSIZE(a)   (sizeof(a) / sizeof((a)[0]))
 

Functions

void help (void)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ _setmode

#define _setmode (   fd,
  mode 
)

Definition at line 21 of file cat.c.

◆ _stricmp

#define _stricmp   strcasecmp

Definition at line 22 of file cat.c.

◆ ARRAYSIZE

#define ARRAYSIZE (   a)    (sizeof(a) / sizeof((a)[0]))

Definition at line 25 of file cat.c.

◆ O_BINARY

#define O_BINARY   0x8000

Definition at line 20 of file cat.c.

◆ O_TEXT

#define O_TEXT   0x4000

Definition at line 19 of file cat.c.

Function Documentation

◆ help()

void help ( void  )

Definition at line 27 of file cat.c.

28 {
30  "\n"
31  "ReactOS File Concatenation Tool\n"
32  "\n"
33  "Usage: cat [options] [file [...]]\n"
34  "options - Currently ignored\n");
35 }
FILE * stdout
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)

Referenced by main().

◆ main()

int main ( int argc  ,
char argv[] 
)

hosttype.c Copyright (C) 2002 by Brian Palmer brian.nosp@m.p@sg.nosp@m.inet..nosp@m.com

Definition at line 37 of file cat.c.

38 {
39  int i;
40  FILE* in;
41  unsigned char buff[512];
42  size_t cnt, readcnt;
43 
44  if (argc >= 2)
45  {
46  if (_stricmp(argv[1], "-h" ) == 0 ||
47  _stricmp(argv[1], "--help") == 0 ||
48  _stricmp(argv[1], "/?" ) == 0 ||
49  _stricmp(argv[1], "/help" ) == 0)
50  {
51  help();
52  return 0;
53  }
54  }
55 
56  /* Set STDOUT to binary */
58 
59  /* Special case where we run 'cat' without any argument: we use STDIN */
60  if (argc <= 1)
61  {
62  unsigned int ch;
63 
64  /* Set STDIN to binary */
66 
67 #if 0 // Version using feof()
68  ch = fgetc(stdin);
69  while (!feof(stdin))
70  {
71  putchar(ch);
72  ch = fgetc(stdin);
73  }
74 #else
75  while ((ch = fgetc(stdin)) != EOF)
76  {
77  putchar(ch);
78  }
79 #endif
80 
81  return 0;
82  }
83 
84  /* We have files: read them and output them to STDOUT */
85  for (i = 1; i < argc; i++)
86  {
87  /* Open the file in binary read mode */
88  in = fopen(argv[i], "rb");
89  if (in == NULL)
90  {
91  fprintf(stderr, "Failed to open file '%s'\n", argv[i]);
92  return -1;
93  }
94 
95  /* Dump the file to STDOUT */
96  cnt = 0; readcnt = 0;
97  while (readcnt == cnt)
98  {
99  /* Read data from the input file */
100  cnt = ARRAYSIZE(buff);
101  readcnt = fread(&buff, sizeof(buff[0]), cnt, in);
102  if (readcnt != cnt)
103  {
104  /*
105  * The real number of read bytes differs from the number of bytes
106  * we wanted to read, so either a reading error occurred, or EOF
107  * was reached while reading. Bail out if it is a reading error.
108  */
109  if (!feof(in))
110  {
111  fprintf(stderr, "Error while reading file '%s'\n", argv[i]);
112  fclose(in);
113  return -1;
114  }
115  }
116 
117  /* Nothing to be read anymore, so we can gracefully break */
118  if (readcnt == 0) break;
119 
120  /* Write data to STDOUT */
121  fwrite(&buff, sizeof(buff[0]), readcnt, stdout);
122  }
123 
124  /* Finally close the file */
125  fclose(in);
126  }
127 
128  return 0;
129 }
static int argc
Definition: ServiceArgs.c:12
#define _setmode(fd, mode)
Definition: cat.c:21
#define ARRAYSIZE(a)
Definition: cat.c:25
FILE * stdin
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
#define argv
Definition: mplay32.c:18
#define _stricmp
Definition: cat.c:22
FILE * stdout
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
int putchar(int c)
Definition: crtsupp.c:12
smooth NULL
Definition: ftsmooth.c:416
_Check_return_ _CRTIMP int __cdecl feof(_In_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
void help(void)
Definition: cat.c:27
GLuint in
Definition: glext.h:9616
#define O_BINARY
Definition: cat.c:20
#define EOF
Definition: stdio.h:24
FILE * stderr
_Check_return_ _CRTIMP int __cdecl _fileno(_In_ FILE *_File)
static unsigned char buff[32768]
Definition: fatten.c:17
_Check_return_opt_ _CRTIMP int __cdecl fgetc(_Inout_ FILE *_File)