Reformat code

This commit is contained in:
Fabio Scotto di Santolo
2025-07-04 10:39:07 +02:00
parent 2bea38d7fc
commit dd53e83af6
28 changed files with 632 additions and 524 deletions

View File

@@ -1,15 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
void out(void) {
printf("atexit() succeeded!\n");
void out(void)
{
printf("atexit() succeeded!\n");
}
int main(void) {
if (atexit(out)) {
fprintf(stderr, "atexit() failed\n");
return 1;
}
return 0;
int main(void)
{
if (atexit(out)) {
fprintf(stderr, "atexit() failed\n");
return 1;
}
return 0;
}

View File

@@ -1,43 +1,46 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define NR_OPEN 1048576
int main(void) {
// create new process
pid_t pid = fork();
if (pid == -1) {
return -1;
} else if (pid != 0) {
printf("Running daemon with pid %d\n", pid);
exit(EXIT_SUCCESS);
}
int main(void)
{
// create new process
pid_t pid = fork();
if (pid == -1) {
return -1;
} else if (pid != 0) {
printf("Running daemon with pid %d\n", pid);
exit(EXIT_SUCCESS);
}
// create a new session and process group
if (setsid() == -1) return -1;
// create a new session and process group
if (setsid() == -1)
return -1;
// set the working directory to the root directory
if (chdir("/") == -1) return -1;
// set the working directory to the root directory
if (chdir("/") == -1)
return -1;
// close all open files--NR_OPEN is overkill, but works
for (int i = 0; i < NR_OPEN; i++) {
close(i);
}
// close all open files--NR_OPEN is overkill, but works
for (int i = 0; i < NR_OPEN; i++) {
close(i);
}
// redirect fd's 0,1,2 to /dev/null
open("/dev/null", O_RDWR); // stdin
dup(0); // stdout
dup(0); // stderr
// redirect fd's 0,1,2 to /dev/null
open("/dev/null", O_RDWR); // stdin
dup(0); // stdout
dup(0); // stderr
// do its daemon thing
for (int i = 0; i < 10; i++) {
printf("Hello from daemon %d\n", getpid());
sleep(1000);
}
// do its daemon thing
for (int i = 0; i < 10; i++) {
printf("Hello from daemon %d\n", getpid());
sleep(1000);
}
return 0;
return 0;
}

View File

@@ -2,8 +2,9 @@
#define _XOPEN_SOURCE 500
#include <unistd.h>
int main(void) {
printf("My session id=%d\n", getsid(0));
printf("My process group id=%d\n", getpgid(0));
return 0;
int main(void)
{
printf("My session id=%d\n", getsid(0));
printf("My process group id=%d\n", getpgid(0));
return 0;
}

View File

@@ -1,63 +1,65 @@
#include <stdio.h>
#define _XOPEN_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
/* my_system - synchronously spawns and waits for the command
* "/bin/sh -c <cmd>".
*
*
* Returns -1 on error of any sort, or the exit code from the
* launched process. Does not block or ignore any signals.
*/
int my_system(const char *);
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "usage: %s <command>", argv[0]);
return EXIT_FAILURE;
}
const char *command = argv[1];
pid_t pid = fork();
if (pid == -1) {
perror("fork");
return EXIT_FAILURE;
} else if (pid > 0) {
int ret = my_system(command);
if (ret == -1) {
fprintf(stderr, "command: \"%s\" error", command);
return EXIT_FAILURE;
}
return ret;
}
int status;
wait(&status);
return status;
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "usage: %s <command>", argv[0]);
return EXIT_FAILURE;
}
const char *command = argv[1];
pid_t pid = fork();
if (pid == -1) {
perror("fork");
return EXIT_FAILURE;
} else if (pid > 0) {
int ret = my_system(command);
if (ret == -1) {
fprintf(stderr, "command: \"%s\" error", command);
return EXIT_FAILURE;
}
return ret;
}
int status;
wait(&status);
return status;
}
int my_system(const char *cmd) {
pid_t pid = fork();
if (pid == -1) {
return -1;
} else if (pid == 0) {
const char *argv[4];
argv[0] = "sh";
argv[1] = "-c";
argv[2] = cmd;
argv[3] = NULL;
int my_system(const char *cmd)
{
pid_t pid = fork();
if (pid == -1) {
return -1;
} else if (pid == 0) {
const char *argv[4];
argv[0] = "sh";
argv[1] = "-c";
argv[2] = cmd;
argv[3] = NULL;
execv("/bin/sh", argv);
exit(-1);
}
int status;
if (waitpid(pid, &status, 0) == -1) {
return -1;
} else if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
return -1;
execv("/bin/sh", argv);
exit(-1);
}
int status;
if (waitpid(pid, &status, 0) == -1) {
return -1;
} else if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
return -1;
}

View File

@@ -1,31 +1,34 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
if (!fork())
return 1;
int main(void)
{
if (!fork())
return 1;
int status;
pid_t pid = wait(&status);
if (pid == -1) {
perror("wait");
}
int status;
pid_t pid = wait(&status);
if (pid == -1) {
perror("wait");
}
printf("pid=%d\n", pid);
if (WIFEXITED(status))
printf("Normal termination with exit status=%d\n", WEXITSTATUS(status));
printf("pid=%d\n", pid);
if (WIFSIGNALED(status))
printf("Killed by signal=%d%s\n", WTERMSIG(status), WCOREDUMP(status) ? " (dumped core)" : "");
if (WIFEXITED(status))
printf("Normal termination with exit status=%d\n",
WEXITSTATUS(status));
if (WIFSTOPPED(status))
printf("Stopped by signal=%d\n", WSTOPSIG(status));
if (WIFSIGNALED(status))
printf("Killed by signal=%d%s\n", WTERMSIG(status),
WCOREDUMP(status) ? " (dumped core)" : "");
if (WIFCONTINUED(status))
printf("Continued\n");
if (WIFSTOPPED(status))
printf("Stopped by signal=%d\n", WSTOPSIG(status));
return 0;
if (WIFCONTINUED(status))
printf("Continued\n");
return 0;
}