The functions you listed can be grouped based on their primary purpose, and many can be used to build interactive and advanced applications. Here's an organized breakdown:
-
Reads a line of input with editing capabilities (like in bash).
-
Adds functionality like history and auto-completion.
-
Example:
#include <stdio.h> #include <readline/readline.h> #include <readline/history.h> int main() { char *input = readline("Enter something: "); if (input) { printf("You typed: %s\n", input); free(input); } return 0; }
-
Related functions:
add_history
: Adds a command to the history.rl_clear_history
: Clears the history.rl_replace_line
: Replaces the current input line.rl_on_new_line
: Prepares for a new line.rl_redisplay
: Redisplays the current line.
open
,read
,write
,close
: Low-level file I/O operations.access
: Checks file accessibility.unlink
: Deletes a file.stat
,lstat
,fstat
: Retrieves file metadata.
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int fd = open("test.txt", O_RDONLY);
if (fd == -1) {
perror("Error opening file");
return 1;
}
char buffer[256];
ssize_t bytesRead = read(fd, buffer, sizeof(buffer) - 1);
if (bytesRead > 0) {
buffer[bytesRead] = '\0';
printf("File content: %s\n", buffer);
}
close(fd);
return 0;
}
-
opendir
,readdir
,closedir
: Manage directory streams. -
Example:
#include <dirent.h> #include <stdio.h> int main() { DIR *dir = opendir("."); if (!dir) { perror("opendir failed"); return 1; } struct dirent *entry; while ((entry = readdir(dir)) != NULL) { printf("Found: %s\n", entry->d_name); } closedir(dir); return 0; }
fork
,execve
,wait
,waitpid
,wait3
,wait4
: Create and synchronize processes.
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// Child process
execlp("ls", "ls", NULL);
perror("execlp failed");
} else if (pid > 0) {
// Parent process
wait(NULL);
printf("Child process finished.\n");
} else {
perror("fork failed");
}
return 0;
}
-
signal
,sigaction
: Handle signals likeSIGINT
orSIGTERM
. -
Example:
#include <signal.h> #include <stdio.h> #include <unistd.h> void handle_sigint(int sig) { printf("\nCaught signal %d (SIGINT). Exiting gracefully.\n", sig); _exit(0); } int main() { signal(SIGINT, handle_sigint); while (1) { printf("Running... Press Ctrl+C to stop.\n"); sleep(1); } return 0; }
-
isatty
,ttyname
,tcsetattr
,tcgetattr
,ioctl
: Interact with terminal devices. -
Example (checking if input is from a TTY):
#include <unistd.h> #include <stdio.h> int main() { if (isatty(STDIN_FILENO)) { printf("Input is from a terminal.\n"); } else { printf("Input is not from a terminal.\n"); } return 0; }
tgetent
,tgetflag
,tgetnum
,tgetstr
,tputs
: Manage terminal capabilities for advanced control.
malloc
,free
: Allocate and free dynamic memory.
-
getenv
: Retrieve environment variable values. -
Example:
#include <stdlib.h> #include <stdio.h> int main() { const char *path = getenv("PATH"); if (path) { printf("PATH: %s\n", path); } else { printf("PATH is not set.\n"); } return 0; }
Let me know which functions you'd like detailed further or need examples for specific use cases!