The Pedigree Project 0.1
syscall-test.c
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <errno.h>
21#include <fcntl.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
27#define PASS 1
28#define FAIL 0
29
30int test_open(void) {
31 printf("open():\n");
32 printf("\tOpen existing file - ");
33 int fd = open("/usr/bin/bash", O_RDONLY);
34 if (fd != -1) {
35 close(fd);
36 printf("PASS\n");
37 } else {
38 printf("FAIL - errno %d (%s)\n", errno, strerror(errno));
39 return FAIL;
40 }
41
42 printf("\tOpen nonexistant file - ");
43 int fd2 = open("/usr/bin/does-not-exist", O_RDONLY);
44 if (fd2 != -1) {
45 close(fd2);
46 printf("FAIL - unexpectedly opened missing file\n");
47 return FAIL;
48 } else if (errno != ENOENT) {
49 printf("FAIL - errno %d (%s)\n", errno, strerror(errno));
50 return FAIL;
51 }
52 printf("PASS\n");
53
54 printf("\tCreate file - ");
55 int fd3 = open("/file-doesnt-exist", O_RDWR | O_CREAT, 0666);
56 if (fd3 != -1) {
57 close(fd3);
58 printf("PASS\n");
59 } else {
60 printf("FAIL - errno %d (%s)\n", errno, strerror(errno));
61 return FAIL;
62 }
63
64 printf("\tRecycle descriptors - ");
65 int fd4 = open("/usr/bin/bash", O_RDWR);
66 close(fd4);
67 int fd5 = open("/usr/bin/bash", O_RDWR);
68 close(fd5);
69 if (fd4 == fd5) {
70 printf("PASS\n");
71 } else {
72 printf("FAIL - %d, %d\n", fd4, fd5);
73 return FAIL;
74 }
75
76 int hahaha = open("/usr/bin/bash", O_RDWR);
77 pid_t pid = fork();
78
79 if (pid == -1) {
80 close(hahaha);
81 printf("FAIL - fork failed\n");
82 return FAIL;
83 }
84
85 if (pid == 0) {
86 close(hahaha);
87 int rofl = open("/usr/bin/bash", O_RDWR);
88
89 printf("%d/%d\n", hahaha, rofl);
90
91 close(rofl);
92 exit(0);
93 }
94 close(hahaha);
95
96 printf("Complete\n");
97
98 while (1)
99 ;
100
101 return PASS;
102}
103
104int main(int argc, char** argv) {
105 printf("argc: %d, argv[0]: %s, &optind: %p\n", argc, argv[0], (void*)&optind);
106 while (getopt(argc, argv, "h?ABC:DEFHIKLNOQ:RST:UVWY:abcdefgijklmo:pr:s:tvwxz") != -1) {
107 printf("bleh\n");
108 }
109 printf("optind: %d\n", optind);
110 printf("Syscall test starting...\n");
111
112 if (test_open() == PASS)
113 return EXIT_SUCCESS;
114 else
115 return EXIT_FAILURE;
116}