1/64
Everything from the PDF provided by Dr. Kirkpatrick 😋😋😋😋😋
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
accept
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
Accepts an incoming connection on a listening socket. Returns a new connected socket.
Ex: accept(server_fd, (struct sockaddr *)&cli, &len);
bind
int bind(int socket, const struct sockaddr *address, socklen_t address_len);
Assigns an IP/port to a socket.
Ex: bind(fd, (struct sockaddr *)&addr, sizeof(addr));
calloc
void* calloc(size_t num, size_t size);
Allocates zero-initialized memory.
Ex: int *a = calloc(10, sizeof(int));
close
int close(int fd);
-
Closes a file descriptor or socket.
connect
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
-
Initiates a connection to a remote socket.
-
Ex: connect(fd, (struct sockaddr *)&server, sizeof(server));
dup2
int dup2(int oldfd, int newfd);
-
Duplicates a file descriptor to a specific fd number. dup2(fd, STDOUT_FILENO);
execl
int execl(const char *path, const char *arg, ...);
-
Executes a program.
-
execl("/bin/ls", "ls", "-l", NULL);
execle
int execle(const char *path, const char *arg, ..., char * const envp[]);
-
Executes a program with custom environment.
-
execle("/bin/ls", "ls", NULL, envp);
execlp
int execlp(const char *file, const char *arg, ...);
-
Executes using PATH search.
-
execlp("ls", "ls", "-a", NULL);
execv
int execv(const char *path, char *const argv[]);
-
Executes program with argv.
-
execv("/bin/ls", (char *[]){"ls", "-l", NULL});
execve
int execve(const char *path, char *const argv[], char *const envp[]);
-
Executes program with environment.
-
execve("/bin/ls", argv, envp);
execvp
int execvp(const char *file, char *const argv[]);
-
Executes using PATH.
-
execvp("ls", argv);
fork
pid_t fork(void);
-
Creates a new process. pid == 0 is child, < 0 failed, else parent
-
pid_t pid = fork();
free
void free(void *ptr);
-
Frees allocated memory.
-
free(p);
fstat
int fstat(int fd, struct stat *buf);
-
Gets file info from file descriptor.
-
fstat(fd, &st);
getaddrinfo
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
-
Hostname/IP resolution.
-
getaddrinfo("localhost", "80", &hints, &res);
getenv
char *getenv(const char *name);
-
Gets environment variable.
-
char *p = getenv("PATH");
getpid
pidt getpid(void);
-
Returns current process ID.
-
pidt pid = getpid();
getppid
pidt getppid(void);
-
Returns parent process ID.
-
pidt p = getppid();
htonl
uint32t htonl(uint32t hostlong);
-
Host to network (32-bit).
-
uint32_t v = htonl(1234);
htons
uint16t htons(uint16t hostshort);
-
Host to network (16-bit).
-
uint16_t p = htons(80);
inet_addr
inaddrt inetaddr(const char *cp);
-
String IPv4 to binary.
-
ip.saddr = inet_addr("127.0.0.1");
inet_aton
int inetaton(const char *cp, struct inaddr *inp);
-
Parses IPv4.
-
inet_aton("1.2.3.4", &addr);
inet_ntoa
char *inetntoa(struct inaddr in);
-
Binary IPv4 to string.
-
printf("%s", inet_ntoa(addr));
inet_ntop
const char *inetntop(int af, const void *src, char *dst, socklent size);
-
Binary IP → string.
-
inetntop(AFINET, &addr, buf, sizeof(buf));
kill
int kill(pid_t pid, int sig);
-
Sends signal to process.
-
kill(pid, SIGTERM);
listen
int listen(int sockfd, int backlog);
-
Marks socket as passive.
-
listen(fd, 10);
malloc
void *malloc(size_t size);
-
Allocates memory.
-
int *p = malloc(sizeof(int));
memcpy
void *memcpy(void *dest, const void *src, size_t n);
-
Copy memory.
-
memcpy(buf2, buf1, 100);
memset
void *memset(void *s, int c, size_t n);
-
Fill memory with value.
-
memset(buf, 0, 256);
mkfifo
int mkfifo(const char *pathname, mode_t mode);
-
Creates named pipe.
-
mkfifo("pipe1", 0666);
mmap
void *mmap(void *addr, sizet length, int prot, int flags, int fd, offt offset);
-
Maps file to memory.
-
ptr = mmap(NULL, 4096, PROTREAD, MAPPRIVATE, fd, 0);
msync
int msync(void *addr, sizet length, int flags);
-
Syncs mmap to disk.
-
msync(ptr, 4096, MSSYNC);
ntohl
uint32t ntohl(uint32t netlong);
-
Network → host (32-bit).
-
v = ntohl(n);
hawk tuah
spit on that thang!!!

ntohs
uint16t ntohs(uint16t netshort);
-
Network → host (16-bit).
-
p = ntohs(n);
open
int open(const char *pathname, int flags, …);
-
Opens a file.
-
fd = open("file.txt", O_RDONLY);
pipe
int pipe(int pipefd[2]);
-
Creates a pipe.
posix_spawn
int posix_spawn(pidt *pid, const char *path, …);
-
Spawns a process.
-
posix_spawn(&pid, "/bin/ls", NULL, NULL, argv, envp);
pthread_create
int pthread_create(pthread_t *thread, const pthread_attrt *attr, void (start_routine)(void *), void *arg);
-
Creates thread.
-
pthread_create(&t, NULL, func, NULL);
pthread_exit
void pthread_exit(void *retval);
-
Ends calling thread.
-
pthread_exit(NULL);
pthread_join
int pthread_join(pthread_t thread, void **retval);
-
Waits for a thread.
-
pthread_join(t, NULL);
pthreadmutexlock
int pthread_mutex_lock(pthreadmutext *mutex);
-
Locks mutex.
-
pthreadmutexlock(&m);
pthreadmutexunlock
int pthread_mutex_unlock(pthread_mutext *mutex);
-
Unlocks mutex.
-
pthread_mutex_unlock(&m);
read
ssizet read(int fd, void *buf, sizet count);
-
Reads bytes from fd.
-
read(fd, buf, 100);
realloc
void *realloc(void *ptr, size_t size);
-
Resizes memory.
-
p = realloc(p, 200);
recv
ssize_t recv(int sockfd, void *buf, sizet len, int flags);
-
Receives data.
-
recv(fd, buf, 256, 0);
Dr. Kirkpatrick’s Belly

recvfrom
ssize_t recvfrom(int sockfd, void *buf, sizet len, int flags, struct sockaddr *src, socklen_t *srclen);
-
Receives UDP datagram.
-
recvfrom(fd, buf, 256, 0, (struct sockaddr *)&cli, &len);
sem_init
int seminit(semt *sem, int pshared, unsigned int value);
-
Initializes semaphore.
-
sem_init(&sem, 0, 1);
sem_post
int sempost(semt *sem);
-
Increments semaphore.
-
sem_post(&sem);
sem_wait
int semwait(semt *sem);
-
Decrements semaphore.
-
sem_wait(&sem);
send
ssizet send(int sockfd, const void *buf, sizet len, int flags);
-
Sends data.
-
send(fd, "hi", 2, 0);
sendto
ssize_t sendto(int sockfd, const void *buf, sizet len, int flags, const struct sockaddr *dest, socklen_t destlen);
-
Sends UDP datagram.
-
sendto(fd, msg, mlen, 0, (struct sockaddr *)&addr, sizeof(addr));
sigaction
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
-
Installs signal handler.
-
sigaction(SIGINT, &sa, NULL);
LOL!!! TROLL CARD!!!

snprintf
int snprintf(char *str, size_t size, const char *format, …);
-
Formatted output (safe).
-
snprintf(buf, 64, "x=%d", x);
stat
int stat(const char *path, struct stat *buf);
-
Gets file metadata.
-
stat("file.txt", &st);
strdup
char *strdup(const char *s);
-
Duplicate string.
-
char *p = strdup("hello");
strncat
char *strncat(char *dest, const char *src, size_t n);
-
Append n chars.
-
strncat(a, b, 5);
strncpy
char *strncpy(char *dest, const char *src, size_t n);
-
Copy up to n chars.
-
strncpy(buf, src, 10);
strtok
char *strtok(char *str, const char *delim);
-
Tokenize string.
-
char *tok = strtok(s, " ");
wait
pid_t wait(int *status);
-
Waits for child.
-
wait(NULL);
waitpid
pid_t waitpid(pidt pid, int *status, int options);
-
Waits for specific child.
-
waitpid(pid, NULL, 0);
write
ssize_t write(int fd, const void *buf, sizet count);
-
Writes bytes to fd.
-
write(fd, "hi", 2);