PKUOS - Pintos
Pintos source browser for PKU Operating System course
mcat.c
Go to the documentation of this file.
1/** mcat.c
2
3 Prints files specified on command line to the console, using
4 mmap. */
5
6#include <stdio.h>
7#include <syscall.h>
8
9int
10main (int argc, char *argv[])
11{
12 int i;
13
14 for (i = 1; i < argc; i++)
15 {
16 int fd;
17 mapid_t map;
18 void *data = (void *) 0x10000000;
19 int size;
20
21 /* Open input file. */
22 fd = open (argv[i]);
23 if (fd < 0)
24 {
25 printf ("%s: open failed\n", argv[i]);
26 return EXIT_FAILURE;
27 }
28 size = filesize (fd);
29
30 /* Map files. */
31 map = mmap (fd, data);
32 if (map == MAP_FAILED)
33 {
34 printf ("%s: mmap failed\n", argv[i]);
35 return EXIT_FAILURE;
36 }
37
38 /* Write file to console. */
39 write (STDOUT_FILENO, data, size);
40
41 /* Unmap files (optional). */
42 munmap (map);
43 }
44 return EXIT_SUCCESS;
45}
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
mapid_t mmap(int fd, void *addr)
Project 3 and optionally project 4.
Definition: syscall.c:145
int filesize(int fd)
Definition: syscall.c:109
int open(const char *file)
Definition: syscall.c:103
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
void munmap(mapid_t mapid)
Definition: syscall.c:151
#define EXIT_SUCCESS
Typical return values from main() and arguments to exit().
Definition: syscall.h:19
#define EXIT_FAILURE
Unsuccessful execution.
Definition: syscall.h:20
#define MAP_FAILED
Definition: syscall.h:13
int mapid_t
Map region identifier.
Definition: syscall.h:12
int main(int argc, char *argv[])
mcat.c
Definition: mcat.c:10
#define STDOUT_FILENO
Definition: stdio.h:16