Skip to content

Commit c1c8d15

Browse files
committed
posix: continue breaking up main
1 parent dc11b3c commit c1c8d15

7 files changed

Lines changed: 123 additions & 248 deletions

File tree

‎posix/README.md‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
1. semaphore_unnamed.c TODO
1515
1. semaphore_named.c TODO. https://stackoverflow.com/questions/16400820/c-how-to-use-posix-semaphores-on-forked-processes
1616
1. [mmap](mmap.c)
17-
1. [shm_open](shm_open.c)
1817
1. [MAP_PRIVATE](map_private.c)
18+
1. Shared memory
19+
1. [shm_open](shm_open.c)
20+
1. [shmget](shmget.c)
1921
1. [syslog](syslog.c)
2022
1. [getrusage](getrusage.c)
2123
1. [pthread](pthread.md)
@@ -39,16 +41,21 @@
3941
1. Signal
4042
1. [Kill](kill.c)
4143
1. Interactive
42-
1. [signal_sleep](interactive/signal_sleep.c)
43-
1. [sigint](interactive/sigint.c)
44-
1. [signal_fork](interactive/signal_fork.c)
45-
1. setpgid
46-
1. [setpgid.md](interactive/setpgid.md)
47-
1. [setpgid.c](interactive/setpgid.c)
48-
1. [alarm](interactive/alarm.c)
44+
1. [alarm](interactive/alarm.c)
45+
1. [signal_sleep](interactive/signal_sleep.c)
46+
1. [sigint](interactive/sigint.c)
47+
1. [signal_fork](interactive/signal_fork.c)
48+
4949
1. Interactive
50+
1. [getlogin](interactive/getlogin.c)
51+
1. [poll](interactive/poll.c)
52+
1. [pthread_work](interactive/pthread_work.c)
5053
1. [sleep cout](interactive/sleep_count.c)
5154
1. [sleep_1](interactive/sleep_1.c)
52-
1. [pthread_work](interactive/pthread_work.c)
53-
1. [poll](interactive/poll.c)
55+
1. [uname](interactive/uname.c)
56+
1. Process information
57+
1. [getcwd](interactive/uname.c)
58+
1. setpgid
59+
1. [setpgid.md](interactive/setpgid.md)
60+
1. [setpgid.c](interactive/setpgid.c)
5461
1. [Biblography](bibliography.md)

‎posix/interactive/getcwd.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* A pwd implementation. */
2+
3+
#include "common.h"
4+
5+
int main(void) {
6+
/* TODO don't hardcode. */
7+
enum Constexpr { N = 1 << 10 };
8+
char buf[N];
9+
if (getcwd(buf, N) == NULL) {
10+
perror("getcwd");
11+
exit(EXIT_FAILURE);
12+
}
13+
printf("%s\n", buf);
14+
return EXIT_SUCCESS;
15+
}

‎posix/interactive/getlogin.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* # getlogin
2+
*
3+
* Get login name of controlling terminal
4+
*
5+
* This is different from `getuid` since it looks at the controlling terminal,
6+
* and not at processes specific information.
7+
*
8+
* Observe the difference with:
9+
*
10+
* sudo ./getlogin.out
11+
*
12+
* Which outputs the username of the non-sudo user.
13+
*/
14+
15+
#include "common.h"
16+
17+
int main(void) {
18+
char *login;
19+
login = getlogin();
20+
if (login == NULL) {
21+
perror("getlogin");
22+
} else {
23+
printf("%s\n", login);
24+
}
25+
return EXIT_SUCCESS;
26+
}

‎posix/interactive/uname.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* # uname
2+
*
3+
* Get information about the current computer using `uname`.
4+
*
5+
* Unsurprisingly, it is the same information given by the POSIX utility `uname`.
6+
* https://unix.stackexchange.com/questions/136959/where-does-uname-get-its-information-from/485962#485962
7+
*/
8+
#include "common.h"
9+
10+
int main(void) {
11+
struct utsname info;
12+
if (uname(&info) == -1) {
13+
perror("uname");
14+
exit(EXIT_FAILURE);
15+
}
16+
printf("sysname = %s\n", info.sysname );
17+
printf("nodename = %s\n", info.nodename);
18+
printf("release = %s\n", info.release );
19+
printf("version = %s\n", info.version );
20+
printf("machine = %s\n", info.machine );
21+
return EXIT_SUCCESS;
22+
}

0 commit comments

Comments
 (0)