Tobiahao

SO2_LAB1_02

Mar 8th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. /* Sprawdz dzialanie funkcji operujacych na ciagach znakow w module jadra */
  2.  
  3. #include <linux/module.h>
  4. #include <linux/string.h>
  5. #include <linux/slab.h>
  6.  
  7. static int __init program_init(void)
  8. {
  9.     char* buffer = "Przykladowy ciag znakow";
  10.     char* copy_buffer; 
  11.     const char* buffer_find = "znak";
  12.  
  13.     printk(KERN_ALERT "Zadanie 2 (5 punktow)\n");
  14.     printk(KERN_DEFAULT "%s\n", buffer);
  15.     printk(KERN_DEFAULT "Dlugosc ciagu znakow [strlen()]: %zu\n", strlen(buffer));
  16.     printk(KERN_DEFAULT "Rozmiar ciagu znakow [sizeof()]: %zu\n", sizeof(buffer));
  17.  
  18.     copy_buffer = (char *)kmalloc(sizeof(buffer), GFP_KERNEL);
  19.     if (!copy_buffer) {
  20.         printk(KERN_ERR "Blad przy alokacji drugiego ciagu znakow\n");
  21.         return -1;
  22.     }
  23.     strncpy(copy_buffer, buffer, strlen(buffer));
  24.     printk(KERN_DEFAULT "Skopiowany ciag znakow [strncpy()]: %s\n", copy_buffer);
  25.     kfree(copy_buffer);
  26.  
  27.     printk(KERN_DEFAULT "Znajdz ciag znakow \"znak\" [strnstr()]: %s\n", strnstr(buffer, buffer_find, strlen(buffer)));
  28.  
  29.     return 0;
  30. }
  31.  
  32. static void __exit program_exit(void)
  33. {
  34.     printk(KERN_ALERT "Koniec\n");
  35. }
  36.  
  37. module_init(program_init);
  38. module_exit(program_exit);
  39.  
  40. MODULE_AUTHOR("Tobiasz Nartowski <tobiasznartowski@gmail.com");
  41. MODULE_DESCRIPTION("Zadanie 2 (5 punktow)");
  42. MODULE_LICENSE("GPL");
Add Comment
Please, Sign In to add comment