CS318 - Pintos
Pintos source browser for JHU CS318 course
cat.c
Go to the documentation of this file.
1 /** cat.c
2 
3  Prints files specified on command line to the console. */
4 
5 #include <stdio.h>
6 #include <syscall.h>
7 
8 int
9 main (int argc, char *argv[])
10 {
11  bool success = true;
12  int i;
13 
14  for (i = 1; i < argc; i++)
15  {
16  int fd = open (argv[i]);
17  if (fd < 0)
18  {
19  printf ("%s: open failed\n", argv[i]);
20  success = false;
21  continue;
22  }
23  for (;;)
24  {
25  char buffer[1024];
26  int bytes_read = read (fd, buffer, sizeof buffer);
27  if (bytes_read == 0)
28  break;
29  write (STDOUT_FILENO, buffer, bytes_read);
30  }
31  close (fd);
32  }
33  return success ? EXIT_SUCCESS : EXIT_FAILURE;
34 }
STDOUT_FILENO
#define STDOUT_FILENO
Definition: stdio.h:16
write
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
EXIT_FAILURE
#define EXIT_FAILURE
Unsuccessful execution.
Definition: syscall.h:20
buffer
static struct intq buffer
Stores keys from the keyboard and serial port.
Definition: input.c:7
printf
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
open
int open(const char *file)
Definition: syscall.c:103
EXIT_SUCCESS
#define EXIT_SUCCESS
Typical return values from main() and arguments to exit().
Definition: syscall.h:19
main
int main(int argc, char *argv[])
cat.c
Definition: cat.c:9
close
void close(int fd)
Definition: syscall.c:139
read
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115