Narzędzia użytkownika

Narzędzia witryny


Pasek boczny


O mnie


Dydaktyka:

Feedback


so:prog_files:solutions

Zadanie 1

hello_world_v1.c
 
#include <unistd.h>
int main() {
 
  write(1, "Hello world!\n", 13);
  return 0;
}
hello_world_v2.c
#include <string.h>
#include <unistd.h>
int main() {
  const char *text = "Hello world!\n";
  write(STDOUT_FILENO, text, strlen(text));
  return 0;
}

Zadanie 2

greeter_v1.c
#include <string.h>
#include <unistd.h>
int main() {
  const char *text = "Podaj swoje imię: ";
  write(STDOUT_FILENO, text, strlen(text));
 
  char buffer[256];
  int nameLength;
  nameLength = read(0, buffer, 256);
  text = "Witaj ";
  write(STDOUT_FILENO, text, strlen(text));
  write(STDOUT_FILENO, buffer, nameLength);
  return 0;
}
greeter_v2.c
#include <string.h>
#include <unistd.h>
int main() {
  const char *text = "Podaj swoje imię: ";
  write(STDOUT_FILENO, text, strlen(text));
  #define BUFFSIZE 256
  char buffer[BUFFSIZE];
  int nameLength;
  nameLength = read(STDIN_FILENO, buffer, BUFFSIZE);
  text = "Witaj ";
  write(STDOUT_FILENO, text, strlen(text));
  write(STDOUT_FILENO, buffer, nameLength);
  return 0;
}
greeter_v3.c
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main() {
  const char *text = "Podaj swoje imię: ";
  write(STDOUT_FILENO, text, strlen(text));
  int buffsize = 256;
  char *buffer = malloc(buffsize);
  int nameLength;
  nameLength = read(STDIN_FILENO, buffer, buffsize);
  text = "Witaj ";
  write(STDOUT_FILENO, text, strlen(text));
  write(STDOUT_FILENO, buffer, nameLength);
  free(buffer);
  return 0;
}

Zadanie 3

stdlib_buffering.c
#include <stdio.h>
#include <unistd.h>
int main() {
  printf("no");
  write(1, "to", 2);
  printf("wać\n");
 
  printf("no");
  write(1, "to", 2);
  printf("wać\n");
  return 0;
}

Por. man stdbuf i funkcja setbuf/setvbuf.

Zadanie 4

Program zakłada że nie wystąpią błędy (obsługę błędów pokazuje kolejne zadanie).

data_txt_displayer.c
#include <fcntl.h>
#include <unistd.h>
int main() {
 
  int fd = open("data.txt", O_RDONLY);
  while (1) {
    char buf[256];
    int cnt = read(fd, buf, 256);
    if (cnt <= 0)
      break;
    write(1, buf, cnt);
  }
  close(fd);
 
  return 0;
}
cat.c
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv) {
  for (int i = 1; i < argc; ++i) {
    int fd = open(argv[i], O_RDONLY);
    while (1) {
      char buf[256];
      int cnt = read(fd, buf, 256);
      if (cnt <= 0)
        break;
      write(1, buf, cnt);
    }
    close(fd);
  }
  return 0;
}

Zadanie 5

open_failure_reason_v1.c
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
 
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "No file name given!\n");
    return 1;
  }
  int fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    switch (errno) {
    case ENOENT: fprintf(stderr, "\e[44m"); break;
    case EACCES: fprintf(stderr, "\e[41m"); break;
    case EISDIR: fprintf(stderr, "\e[42m"); break;
    case ELOOP:  fprintf(stderr, "\e[46m"); break;
    case ENXIO:  fprintf(stderr, "\e[45m"); break;
    }
    perror("Failed to open file");
    fprintf(stderr, "\e[0m");
 
    return 1;
  }
  printf("File successfully opened.");
  close(fd);
  return 0;
}
open_failure_reason_v2.c
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "No file name given!\n");
    return 1;
  }
  int fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    int color =   errno == ENOENT ? 44
                : errno == EACCES ? 41
                : errno == EISDIR ? 42
                : errno == ELOOP  ? 46
                : errno == ENXIO  ? 45
                                  : 0;
 
    fprintf(stderr,
      "Failed to open file: \e[%dm%s\e[0m.\n",
      color, strerror(errno));
    return 1;
  }
  printf("File successfully opened.");
  close(fd);
  return 0;
}

Zadanie 6

cmp.c
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc != 3) { fprintf(stderr, "Wrong argument count!\n"); return 2; }
 
  int a = open(argv[1], O_RDONLY);
  if (a == -1) { perror("Opening the first file failed"); return 2; }
 
  int b = open(argv[2], O_RDONLY);
  if (b == -1) { perror("Opening the second file failed"); close(a); return 2; }
 
  char bufA[256], bufB[256];
  int cntA, cntB, rv;
  while (1) {
    cntA = read(a, bufA, 256);
    cntB = read(b, bufB, 256);
    if (cntA != cntB) {
      printf("Files differ\n");
      rv = 1; break;
    }
    if (memcmp(bufA, bufB, cntA)) {
      printf("Files differ\n");
      rv = 1; break;
    }
    if (cntA <= 0) {
      printf("Files are identical\n");
      rv = 0; break;
    }
  }
  close(a);
  close(b);
  return rv;
}

Zadanie 7

upper_lower.c
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int src = open(argv[1], O_RDONLY);
  if (src == -1) {
    perror("Opening the source file failed");
    return 1;
  }
 
  char *filename_buffer = malloc(strlen(argv[1]) + 3);
 
  sprintf(filename_buffer, "%s_l", argv[1]);
  int lower = open(filename_buffer, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (lower == -1)
    perror("Creating the lowercase variant failed");
 
  sprintf(filename_buffer, "%s_u", argv[1]);
  int upper = open(filename_buffer, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (upper == -1)
    perror("Creating the uppercase variant failed");
 
  free(filename_buffer);
 
  if (lower == -1 && upper == -1) {
    close(src);
    return 1;
  }
 
  char buf[256];
  while (1) {
    int cnt = read(src, buf, 256);
    if (cnt <= 0)
      break;
 
    for (int i = 0; i < cnt; ++i)
      buf[i] = tolower(buf[i]);
    write(lower, buf, cnt);
 
    for (int i = 0; i < cnt; ++i)
      buf[i] = toupper(buf[i]);
    write(upper, buf, cnt);
  }
  close(src);
  close(lower);
  close(upper);
  return 0;
}

Zadanie 8

last_bytes.c
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("Opening the file failed");
    return 1;
  }
 
  int filesize = lseek(fd, 0, SEEK_END);
 
  char buf[256];
  printf("The file has %d bytes.\nThe last %d bytes are:\n",
         filesize, filesize < 16 ? filesize : 16);
  fflush(stdout);
 
  if (filesize < 16)
    lseek(fd, 0, SEEK_SET);
  else
    lseek(fd, -16, SEEK_CUR);
 
  int cnt = read(fd, buf, 16);
  close(fd);
 
  write(1, buf, cnt);
 
  return 0;
}

Zadanie 9

uppercaser_v1.c
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    perror("Opening the file failed");
    return 1;
  }
 
 
 
 
 
 
 
 
  while (1) {
    char buf[256];
    int cnt = read(fd, buf, 256);
    if (cnt <= 0)
      break;
 
    for (int i = 0; i < cnt; ++i)
      buf[i] = toupper(buf[i]);
 
    lseek(fd, -cnt, SEEK_CUR);
    write(fd, buf, cnt);
  }
 
  close(fd);
 
  return 0;
}
uppercaser_v2.c
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int reader = open(argv[1], O_RDONLY);
  if (reader == -1) {
    perror("Opening the file for reading failed");
    return 1;
  }
 
  int writer = open(argv[1], O_WRONLY);
  if (writer == -1) {
    perror("Opening the file for writing failed");
    close(reader);
    return 1;
  }
 
  while (1) {
    char buf[256];
    int cnt = read(reader, buf, 256);
    if (cnt <= 0)
      break;
 
    for (int i = 0; i < cnt; ++i)
      buf[i] = toupper(buf[i]);
 
 
    write(writer, buf, cnt);
  }
 
  close(reader);
  close(writer);
  return 0;
}

Zadanie 10

read_pos_write_pos.c
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
  setvbuf(stdout, NULL, _IONBF, 0);
 
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    perror("Opening the file failed");
    return 1;
  }
 
  int cnt, pos;
  char buf[16];
 
  pos = lseek(fd, 0, SEEK_CUR);
  printf("Pos upon opening: %d\n", pos);
 
  cnt = read(fd, buf, 16);
  printf("First 16 bytes: ");
  write(1, buf, cnt);
 
  pos = lseek(fd, 0, SEEK_CUR);
  printf("\nPos before 16-byte write: %d\n",
         pos);
 
  cnt = write(fd, "Kilroy was here ", 16);
 
  pos = lseek(fd, 0, SEEK_CUR);
  printf("Pos after  16-byte write: %d\n",
         pos);
 
  close(fd);
 
  return 0;
}
read_pos_append_pos.c
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
  setvbuf(stdout, NULL, _IONBF, 0);
 
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int fd = open(argv[1], O_RDWR | O_APPEND);
  if (fd == -1) {
    perror("Opening the file failed");
    return 1;
  }
 
  int cnt, pos;
  char buf[16];
 
  pos = lseek(fd, 0, SEEK_CUR);
  printf("Pos upon opening: %d\n", pos);
 
  cnt = read(fd, buf, 16);
  printf("First 16 bytes: ");
  write(1, buf, cnt);
 
  pos = lseek(fd, 0, SEEK_CUR);
  printf("\nPos before 16-byte write: %d\n",
         pos);
 
  cnt = write(fd, "Kilroy was here ", 16);
 
  pos = lseek(fd, 0, SEEK_CUR);
  printf("Pos after  16-byte write: %d\n",
         pos);
 
  close(fd);
 
  return 0;
}

Zadanie 11

trim_to_one_line.c
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    perror("Opening the file failed");
    return 1;
  }
 
  int newSize = 0;
 
  while (1) {
    char buf[256];
    int cnt = read(fd, buf, 256);
    if (cnt <= 0) {
      close(fd);
      return 0;
    }
 
    char *nlPos = memchr(buf, '\n', cnt);
    if (!nlPos) {
      newSize += cnt;
      continue;
    }
    newSize += nlPos - buf + 1;
    break;
  };
 
  ftruncate(fd, newSize);
  close(fd);
 
  return 0;
}

Zadanie 12

petabytes.c
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
const char *head = "    |\\/\\\n   / oo |\n\\\\_\\ T  /"
"\n \\---|  \\\n    /   |\n    |   |\n    |   |\n\n\n\n";
const char *tail = "\n\n\n\n\n\n    |   |\n    | A \\"
"\n    || ||\\\\\n    || || \\\\\n    ww  ww  \\\n";
 
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int fd = open(argv[1], O_RDWR | O_CREAT | O_EXCL, 0644);
  if (fd == -1) {
    perror("Creating the file failed");
    return 1;
  }
 
  ftruncate(fd, 4.5036e15);
 
  write(fd, head, strlen(head));
  lseek(fd, 0, SEEK_END);
  write(fd, tail, strlen(tail));
 
  close(fd);
 
  return 0;
}

Zadanie 13

duplicating_v1.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int fd = open(argv[1],
                O_WRONLY | O_TRUNC | O_CREAT,
                0644);
  if (fd == -1) {
    perror("Opening the file failed");
    return 1;
  }
 
  close(1);
  dup(fd);
  close(fd);
 
  system("date '+%F %T'");
  write(STDOUT_FILENO, "Hello", 5);
  printf(" world!\n");
 
  return 0;
}
duplicating_v2.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int fd = open(argv[1],
                O_WRONLY | O_TRUNC | O_CREAT,
                0644);
  if (fd == -1) {
    perror("Opening the file failed");
    return 1;
  }
 
 
  dup2(fd, 1);
  close(fd);
 
  system("date '+%F %T'");
  write(STDOUT_FILENO, "Hello", 5);
  printf(" world!\n");
 
  return 0;
}

Zadanie 14

unlink.c
#include <fcntl.h>
#include <stdio.h>
 
 
 
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("Opening the file failed");
    return 1;
  }
 
  unlink(argv[1]);
 
 
 
 
 
 
 
 
 
 
 
  write(1, "Reading and printing the file:\n",
        31);
  while (1) {
    char buf[256];
    int cnt = read(fd, buf, 256);
    if (cnt <= 0)
      break;
    write(1, buf, cnt);
  }
  close(fd);
 
  return 0;
}
unlink_plus_stat.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Wrong argument count!\n");
    return 1;
  }
 
  int fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("Opening the file failed");
    return 1;
  }
 
  unlink(argv[1]);
 
  char *buf = malloc(strlen(argv[1]) + 17);
  sprintf(buf, "Executing stat %s\n", argv[1]);
  write(1, buf, strlen(buf));
  system(buf + 10);
 
  struct stat sb;
  fstat(fd, &sb);
  sprintf(buf, "Link count: %zu\n", sb.st_nlink);
  write(1, buf, strlen(buf));
 
  write(1, "Reading and printing the file:\n",
        31);
  while (1) {
    char buf[256];
    int cnt = read(fd, buf, 256);
    if (cnt <= 0)
      break;
    write(1, buf, cnt);
  }
  close(fd);
 
  return 0;
}
so/prog_files/solutions.txt · ostatnio zmienione: 2025/06/03 01:30 przez jkonczak