and use them in the example programs.
Add comments to syscall.h.
int
main (int argc, char *argv[])
{
+ bool success = true;
int i;
for (i = 1; i < argc; i++)
if (fd < 0)
{
printf ("%s: open failed\n", argv[i]);
+ success = false;
continue;
}
for (;;)
}
close (fd);
}
- return 0;
+ return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
if (argc != 3)
{
printf ("usage: cmp A B\n");
- return 1;
+ return EXIT_FAILURE;
}
/* Open files. */
if (fd[0] < 0)
{
printf ("%s: open failed\n", argv[1]);
- return 1;
+ return EXIT_FAILURE;
}
fd[1] = open (argv[2]);
if (fd[1] < 0)
{
printf ("%s: open failed\n", argv[1]);
- return 1;
+ return EXIT_FAILURE;
}
/* Compare data. */
pos + i,
buffer[0][i], buffer[0][i], argv[1],
buffer[1][i], buffer[1][i], argv[2]);
- return 1;
+ return EXIT_FAILURE;
}
if (min_read < bytes_read[1])
printf ("%s and %s are identical\n", argv[1], argv[2]);
- return 0;
+ return EXIT_SUCCESS;
}
if (argc != 3)
{
printf ("usage: cp OLD NEW\n");
- return 1;
+ return EXIT_FAILURE;
}
/* Open input file. */
if (in_fd < 0)
{
printf ("%s: open failed\n", argv[1]);
- return 1;
+ return EXIT_FAILURE;
}
/* Create and open output file. */
if (!create (argv[2], filesize (in_fd)))
{
printf ("%s: create failed\n", argv[2]);
- return 1;
+ return EXIT_FAILURE;
}
out_fd = open (argv[2]);
if (out_fd < 0)
{
printf ("%s: open failed\n", argv[2]);
- return 1;
+ return EXIT_FAILURE;
}
/* Copy data. */
if (write (out_fd, buffer, bytes_read) != bytes_read)
{
printf ("%s: write failed\n", argv[2]);
- return 1;
+ return EXIT_FAILURE;
}
}
- return 0;
+ return EXIT_SUCCESS;
}
#include <stdio.h>
+#include <syscall.h>
int
main (int argc, char **argv)
printf ("%s ", argv[i]);
printf ("\n");
- return 0;
+ return EXIT_SUCCESS;
}
int
main (int argc, char *argv[])
{
+ bool success = true;
int i;
for (i = 1; i < argc; i++)
if (fd < 0)
{
printf ("%s: open failed\n", argv[i]);
+ success = false;
continue;
}
for (;;)
}
close (fd);
}
- return 0;
+ return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
if (handle < 0)
{
printf ("%s: open failed\n", argv[i]);
- return -1;
+ return EXIT_FAILURE;
}
}
else
if (file_flag)
close (handle);
- return 0;
+ return EXIT_SUCCESS;
}
void
close (handle);
- return 0;
+ return EXIT_SUCCESS;
}
#include <stdio.h>
#include <string.h>
-static void
+static bool
list_dir (const char *dir, bool verbose)
{
int dir_fd = open (dir);
if (dir_fd == -1)
{
printf ("%s: not found\n", dir);
- return;
+ return false;
}
if (isdir (dir_fd))
}
else
printf ("%s: not a directory\n", dir);
- close (dir_fd);
+ close (dir_fd);
+ return true;
}
int
main (int argc, char *argv[])
{
+ bool success = true;
bool verbose = false;
+
if (argc > 1 && !strcmp (argv[1], "-l"))
{
verbose = true;
}
if (argc <= 1)
- list_dir (".", verbose);
+ success = list_dir (".", verbose);
else
{
int i;
for (i = 1; i < argc; i++)
- list_dir (argv[i], verbose);
+ if (!list_dir (argv[i], verbose))
+ success = false;
}
- return 0;
+ return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
if (fd < 0)
{
printf ("%s: open failed\n", argv[i]);
- return 1;
+ return EXIT_FAILURE;
}
size = filesize (fd);
if (map == MAP_FAILED)
{
printf ("%s: mmap failed\n", argv[i]);
- return 1;
+ return EXIT_FAILURE;
}
/* Write file to console. */
/* Unmap files (optional). */
munmap (map);
}
- return 0;
+ return EXIT_SUCCESS;
}
if (argc != 3)
{
printf ("usage: cp OLD NEW\n");
- return 1;
+ return EXIT_FAILURE;
}
/* Open input file. */
if (in_fd < 0)
{
printf ("%s: open failed\n", argv[1]);
- return 1;
+ return EXIT_FAILURE;
}
size = filesize (in_fd);
if (!create (argv[2], size))
{
printf ("%s: create failed\n", argv[2]);
- return 1;
+ return EXIT_FAILURE;
}
out_fd = open (argv[2]);
if (out_fd < 0)
{
printf ("%s: open failed\n", argv[2]);
- return 1;
+ return EXIT_FAILURE;
}
/* Map files. */
if (in_map == MAP_FAILED)
{
printf ("%s: mmap failed\n", argv[1]);
- return 1;
+ return EXIT_FAILURE;
}
out_map = mmap (out_fd, out_data);
if (out_map == MAP_FAILED)
{
printf ("%s: mmap failed\n", argv[2]);
- return 1;
+ return EXIT_FAILURE;
}
/* Copy files. */
munmap (in_map);
munmap (out_map);
- return 0;
+ return EXIT_SUCCESS;
}
int
main (int argc, char *argv[])
{
- if (argc != 2)
- PANIC ("usage: %s DIRECTORY\n", argv[0]);
+ if (argc != 2)
+ {
+ printf ("usage: %s DIRECTORY\n", argv[0]);
+ return EXIT_FAILURE;
+ }
- if (!mkdir (argv[1]))
- PANIC ("%s: mkdir failed\n", argv[1]);
-
- return 0;
+ if (!mkdir (argv[1]))
+ {
+ printf ("%s: mkdir failed\n", argv[1]);
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
}
if (getcwd (cwd, sizeof cwd))
{
printf ("%s\n", cwd);
- return 0;
+ return EXIT_SUCCESS;
}
else
{
printf ("error\n");
- return 1;
+ return EXIT_FAILURE;
}
}
\f
int
main (int argc, char *argv[])
{
+ bool success = true;
int i;
for (i = 1; i < argc; i++)
- if (!remove (argv[i]))
- printf ("%s: remove failed\n", argv[i]);
- return 0;
+ if (!remove (argv[i]))
+ {
+ printf ("%s: remove failed\n", argv[i]);
+ success = false;
+ }
+ return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
}
printf ("Shell exiting.");
- return 0;
+ return EXIT_SUCCESS;
}
/* Reads a line of input from the user into LINE, which has room
#include <stdbool.h>
#include <debug.h>
+/* Process identifier. */
typedef int pid_t;
#define PID_ERROR ((pid_t) -1)
+/* Map region identifier. */
typedef int mapid_t;
#define MAP_FAILED ((mapid_t) -1)
+/* Maximum characters in a filename written by readdir(). */
#define READDIR_MAX_LEN 14
+/* Typical return values from main() and arguments to exit(). */
+#define EXIT_SUCCESS 0 /* Successful execution. */
+#define EXIT_FAILURE 1 /* Unsuccessful execution. */
+
+/* Projects 2 and later. */
void halt (void) NO_RETURN;
void exit (int status) NO_RETURN;
pid_t exec (const char *file);
void seek (int fd, unsigned position);
unsigned tell (int fd);
void close (int fd);
+
+/* Project 3 and optionally project 4. */
mapid_t mmap (int fd, void *addr);
void munmap (mapid_t);
+
+/* Project 4 only. */
bool chdir (const char *dir);
bool mkdir (const char *dir);
bool readdir (int fd, char name[READDIR_MAX_LEN + 1]);