# errno

***

**1. Checking for Error Codes in System Calls**

```c
#include <stdio.h>
#include <errno.h>

int main() {
  FILE *fp = fopen("myfile.txt", "r");
  if (fp == NULL) {
    perror("fopen");
    return errno;
  }
  fclose(fp);
  return 0;
}
```

**2. Retrieving Error Code from `errno`**

```c
#include <errno.h>

int main() {
  int errnum = errno;
  printf("Error code: %d\n", errnum);
  return 0;
}
```

**3. Setting Error Codes in Custom Functions**

```c
#include <errno.h>

void my_custom_function() {
  if (some_condition) {
    errno = EINVAL;
    return;
  }
  // ... do something else
}
```

**4. Using `errno` in `fork()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork");
    return errno;
  }
  return 0;
}
```

**5. Handling `ENOENT` Error in `open()`**

```c
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

int main() {
  int fd = open("myfile.txt", O_RDONLY);
  if (fd == -1 && errno == ENOENT) {
    printf("File not found\n");
    return errno;
  }
  close(fd);
  return 0;
}
```

**6. Detecting Out of Range Error in `malloc()`**

```c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main() {
  void *ptr = malloc(1024 * 1024 * 1024);  // 1GB of memory
  if (ptr == NULL && errno == ENOMEM) {
    printf("Out of memory\n");
    return errno;
  }
  free(ptr);
  return 0;
}
```

**7. Verifying File Permissions with `access()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  if (access("myfile.txt", R_OK) == -1 && errno == EACCES) {
    printf("File permissions denied\n");
    return errno;
  }
  return 0;
}
```

**8. Handling Socket Errors with `recv()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  char buffer[1024];
  if (recv(sockfd, buffer, 1024, 0) == -1 && errno == EWOULDBLOCK) {
    printf("Non-blocking socket: No data available\n");
    return errno;
  }
  close(sockfd);
  return 0;
}
```

**9. Distinguishing between Invalid File Descriptors and Other Errors in `read()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  int fd = open("myfile.txt", O_RDONLY);
  char buffer[1024];
  if (read(fd, buffer, 1024) == -1) {
    if (errno == EBADF) {
      printf("Invalid file descriptor\n");
    } else {
      printf("Other error occurred\n");
    }
    close(fd);
    return errno;
  }
  close(fd);
  return 0;
}
```

**10. Error Handling in `pthread_create()`**

```c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>

void *thread_function(void *arg) {
  return NULL;
}

int main() {
  pthread_t thread;
  int err = pthread_create(&thread, NULL, thread_function, NULL);
  if (err != 0) {
    perror("pthread_create");  // Print error message based on errno
    return err;
  }
  pthread_join(thread, NULL);
  return 0;
}
```

**11. Error Handling in `sem_init()`**

```c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <semaphore.h>

int main() {
  sem_t sem;
  int err = sem_init(&sem, 0, 1);
  if (err != 0) {
    perror("sem_init");  // Print error message based on errno
    return err;
  }
  sem_destroy(&sem);
  return 0;
}
```

**12. Error Handling in `snprintf()`**

```c
#include <stdio.h>
#include <errno.h>

int main() {
  int n;
  char buffer[10];
  n = snprintf(buffer, 10, "%d", 123456789);
  if (n >= 10) {
    errno = ERANGE;
    perror("snprintf");  // Print error message based on errno
    return errno;
  }
  printf("%s\n", buffer);
  return 0;
}
```

**13. Error Handling in `fwrite()`**

```c
#include <stdio.h>
#include <errno.h>

int main() {
  FILE *fp = fopen("myfile.txt", "w");
  char *data = "Hello, world!";
  size_t n = fwrite(data, sizeof(char), strlen(data), fp);
  if (n != strlen(data)) {
    perror("fwrite");  // Print error message based on errno
    return errno;
  }
  fclose(fp);
  return 0;
}
```

**14. Error Handling in `mmap()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>

int main() {
  void *addr = mmap(NULL, 1024, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  if (addr == MAP_FAILED) {
    perror("mmap");  // Print error message based on errno
    return errno;
  }
  munmap(addr, 1024);
  return 0;
}
```

**15. Error Handling in `waitpid()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/wait.h>

int main() {
  pid_t pid = fork();
  if (pid == -1) {
    perror("fork");  // Print error message based on errno
    return errno;
  }
  if (pid > 0) {
    int status;
    if (waitpid(pid, &status, 0) == -1) {
      perror("waitpid");  // Print error message based on errno
      return errno;
    }
  }
  return 0;
}
```

**16. Error Handling in `execve()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  char *argv[] = {"/bin/ls", NULL};
  if (execve(argv[0], argv, NULL) == -1) {
    perror("execve");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**17. Error Handling in `opendir()`**

```c
#include <stdio.h>
#include <errno.h>
#include <dirent.h>

int main() {
  DIR *dir = opendir(".");
  if (dir == NULL) {
    perror("opendir");  // Print error message based on errno
    return errno;
  }
  closedir(dir);
  return 0;
}
```

**18. Error Handling in `pthread_mutex_lock()`**

```c
#include <stdio.h>
#include <errno.h>
#include <pthread.h>

pthread_mutex_t mutex;

void *thread_function(void *arg) {
  int err = pthread_mutex_lock(&mutex);
  if (err != 0) {
    perror("pthread_mutex_lock");  // Print error message based on errno
    return (void *)err;
  }
  return NULL;
}

int main() {
  pthread_t thread;
  int err = pthread_create(&thread, NULL, thread_function, NULL);
  if (err != 0) {
    perror("pthread_create");  // Print error message based on errno
    return err;
  }
  pthread_join(thread, NULL);
  return 0;
}
```

**19. Error Handling in `readlink()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  char buffer[1024];
  ssize_t n = readlink("myfile.txt", buffer, sizeof(buffer));
  if (n == -1) {
    perror("readlink");  // Print error message based on errno
    return errno;
  }
  buffer[n] = '\0';  // Null-terminate the string
  printf("%s\n", buffer);
  return 0;
}
```

**20. Error Handling in `chown()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  if (chown("myfile.txt", 1000, 1000) == -1) {
    perror("chown");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**21. Error Handling in `unlink()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  if (unlink("myfile.txt") == -1) {
    perror("unlink");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**22. Error Handling in `mkdir()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main() {
  if (mkdir("mydirectory", 0755) == -1) {
    perror("mkdir");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**23. Error Handling in `rmdir()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main() {
  if (rmdir("mydirectory") == -1) {
    perror("rmdir");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**24. Error Handling in `rename()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  if (rename("myfile.txt", "myfile2.txt") == -1) {
    perror("rename");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**25. Error Handling in `link()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  if (link("myfile.txt", "myfile2.txt") == -1) {
    perror("link");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**26. Error Handling in `symlink()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  if (symlink("myfile.txt", "myfile2.txt") == -1) {
    perror("symlink");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**27. Error Handling in `lseek()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  int fd = open("myfile.txt", O_RDONLY);
  if (fd == -1) {
    perror("open");  // Print error message based on errno
    return errno;
  }
  off_t offset = lseek(fd, 10, SEEK_SET);
  if (offset == -1) {
    perror("lseek");  // Print error message based on errno
    close(fd);
    return errno;
  }
  close(fd);
  return 0;
}
```

**28. Error Handling in `write()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  int fd = open("myfile.txt", O_WRONLY);
  if (fd == -1) {
    perror("open");  // Print error message based on errno
    return errno;
  }
  char *data = "Hello, world!";
  ssize_t n = write(fd, data, strlen(data));
  if (n == -1) {
    perror("write");  // Print error message based on errno
    close(fd);
    return errno;
  }
  close(fd);
  return 0;
}
```

**29. Error Handling in `close()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  int fd = open("myfile.txt", O_RDONLY);
  if (fd == -1) {
    perror("open");  // Print error message based on errno
    return errno;
  }
  if (close(fd) == -1) {
    perror("close");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**30. Error Handling in `dup()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  int fd = open("myfile.txt", O_RDONLY);
  if (fd == -1) {
    perror("open");  // Print error message based on errno
    return errno;
  }
  int new_fd = dup(fd);
  if (new_fd == -1) {
    perror("dup");  // Print error message based on errno
    close(fd);
    return errno;
  }
  close(fd);
  close(new_fd);
  return 0;
}
```

**31. Error Handling in `pipe()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  int fds[2];
  if (pipe(fds) == -1) {
    perror("pipe");  // Print error message based on errno
    return errno;
  }
  close(fds[0]);
  close(fds[1]);
  return 0;
}
```

**32. Error Handling in `ftruncate()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  int fd = open("myfile.txt", O_RDWR);
  if (fd == -1) {
    perror("open");  // Print error message based on errno
    return errno;
  }
  if (ftruncate(fd, 1024) == -1) {
    perror("ftruncate");  // Print error message based on errno
    close(fd);
    return errno;
  }
  close(fd);
  return 0;
}
```

**33. Error Handling in `fsync()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  int fd = open("myfile.txt", O_RDWR);
  if (fd == -1) {
    perror("open");  // Print error message based on errno
    return errno;
  }
  if (fsync(fd) == -1) {
    perror("fsync");  // Print error message based on errno
    close(fd);
    return errno;
  }
  close(fd);
  return 0;
}
```

**34. Error Handling in `flock()`**

```c
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

int main() {
  int fd = open("myfile.txt", O_RDWR);
  if (fd == -1) {
    perror("open");  // Print error message based on errno
    return errno;
  }
  if (flock(fd, LOCK_EX) == -1) {
    perror("flock");  // Print error message based on errno
    close(fd);
    return errno;
  }
  close(fd);
  return 0;
}
```

**35. Error Handling in `chroot()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  if (chroot("/") == -1) {
    perror("chroot");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**36. Error Handling in `mount()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/mount.h>

int main() {
  if (mount("/dev/sda1", "/mnt", "ext4", MS_RDONLY, NULL) == -1) {
    perror("mount");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**37. Error Handling in `umount()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/mount.h>

int main() {
  if (umount("/mnt") == -1) {
    perror("umount");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**38. Error Handling in `ioctl()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/ioctl.h>

int main() {
  int fd = open("/dev/tty", O_RDWR);
  if (fd == -1) {
    perror("open");  // Print error message based on errno
    return errno;
  }
  if (ioctl(fd, TIOCGWINSZ, NULL) == -1) {
    perror("ioctl");  // Print error message based on errno
    close(fd);
    return errno;
  }
  close(fd);
  return 0;
}
```

**39. Error Handling in `fstat()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main() {
  int fd = open("myfile.txt", O_RDONLY);
  if (fd == -1) {
    perror("open");  // Print error message based on errno
    return errno;
  }
  struct stat statbuf;
  if (fstat(fd, &statbuf) == -1) {
    perror("fstat");  // Print error message based on errno
    close(fd);
    return errno;
  }
  close(fd);
  return 0;
}
```

**40. Error Handling in `stat()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main() {
  struct stat statbuf;
  if (stat("myfile.txt", &statbuf) == -1) {
    perror("stat");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**41. Error Handling in `lstat()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main() {
  struct stat statbuf;
  if (lstat("myfile.txt", &statbuf) == -1) {
    perror("lstat");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**42. Error Handling in `fchmod()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main() {
  int fd = open("myfile.txt", O_WRONLY);
  if (fd == -1) {
    perror("open");  // Print error message based on errno
    return errno;
  }
  if (fchmod(fd, 0644) == -1) {
    perror("fchmod");  // Print error message based on errno
    close(fd);
    return errno;
  }
  close(fd);
  return 0;
}
```

**43. Error Handling in `chmod()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main() {
  if (chmod("myfile.txt", 0644) == -1) {
    perror("chmod");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**44. Error Handling in `chown()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main() {
  if (chown("myfile.txt", 1000, 1000) == -1) {
    perror("chown");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**45. Error Handling in `lchown()`**

```c
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main() {
  if (lchown("myfile.txt", 1000, 1000) == -1) {
    perror("lchown");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**46. Error Handling in `setuid()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  if (setuid(1000) == -1) {
    perror("setuid");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**47. Error Handling in `setgid()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  if (setgid(1000) == -1) {
    perror("setgid");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**48. Error Handling in `getuid()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  uid_t uid = getuid();
  if (uid == -1) {
    perror("getuid");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**49. Error Handling in `getgid()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  gid_t gid = getgid();
  if (gid == -1) {
    perror("getgid");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```

**50. Error Handling in `getpid()`**

```c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main() {
  pid_t pid = getpid();
  if (pid == -1) {
    perror("getpid");  // Print error message based on errno
    return errno;
  }
  return 0;
}
```
