Skip to content

Latest commit

 

History

History
222 lines (164 loc) · 4.33 KB

functions.md

File metadata and controls

222 lines (164 loc) · 4.33 KB

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:


Alternatives for Input Handling

1. readline

  • 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.

File and Directory Operations

2. File Operations

  • open, read, write, close: Low-level file I/O operations.
  • access: Checks file accessibility.
  • unlink: Deletes a file.
  • stat, lstat, fstat: Retrieves file metadata.

Example:

#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;
}

3. Directory Operations

  • 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;
    }

Process Management

4. Creating and Managing Processes

  • fork, execve, wait, waitpid, wait3, wait4: Create and synchronize processes.

Example:

#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 Handling

5. Handling Signals

  • signal, sigaction: Handle signals like SIGINT or SIGTERM.

  • 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;
    }

Terminal and TTY Management

6. Terminal Control

  • 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;
    }

7. Termcap Functions

  • tgetent, tgetflag, tgetnum, tgetstr, tputs: Manage terminal capabilities for advanced control.

Memory Management

8. Allocating and Freeing Memory

  • malloc, free: Allocate and free dynamic memory.

Environment Variables

9. Environment Management

  • 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!