Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions 0x17-doubly_linked_lists/0-print_dlistint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "lists.h"
#include <stdio.h>

/**
* print_dlistint - print all the elements of a dlistint_t list
* @h: pointer to the start of the linked list
*
* Return: number of nodes
*/
size_t print_dlistint(const dlistint_t *h)
{
size_t i;

for (i = 0; h != NULL; i++)
{
printf("%d\n", h->n);
h = h->next;
}
return (i);
}
16 changes: 16 additions & 0 deletions 0x17-doubly_linked_lists/1-dlistint_len.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "lists.h"

/**
* dlistint_len - count the number of nodes in the linked list
* @h: pointer to the beginning of a linked list
*
* Return: number of nodes
*/
size_t dlistint_len(const dlistint_t *h)
{
size_t i;

for (i = 0; h != NULL; i++)
h = h->next;
return (i);
}
1 change: 1 addition & 0 deletions 0x17-doubly_linked_lists/100-password
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
en C Pyfo neZ
1 change: 1 addition & 0 deletions 0x17-doubly_linked_lists/102-result
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
906609
45 changes: 45 additions & 0 deletions 0x17-doubly_linked_lists/103-keygen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/**
* main - generate a key depending on a username for crackme5
* @argc: number of arguments passed
* @argv: arguments passed to main
*
* Return: 0 on success, 1 on error
*/
int main(int argc, char *argv[])
{
unsigned int i, b;
size_t len, add;
char *l = "A-CHRDw87lNS0E9B2TibgpnMVys5XzvtOGJcYLU+4mjW6fxqZeF3Qa1rPhdKIouk";
char p[7] = " ";

if (argc != 2)
{
printf("Correct usage: ./keygen5 username\n");
return (1);
}
len = strlen(argv[1]);
p[0] = l[(len ^ 59) & 63];
for (i = 0, add = 0; i < len; i++)
add += argv[1][i];
p[1] = l[(add ^ 79) & 63];
for (i = 0, b = 1; i < len; i++)
b *= argv[1][i];
p[2] = l[(b ^ 85) & 63];
for (b = argv[1][0], i = 0; i < len; i++)
if ((char)b <= argv[1][i])
b = argv[1][i];
srand(b ^ 14);
p[3] = l[rand() & 63];
for (b = 0, i = 0; i < len; i++)
b += argv[1][i] * argv[1][i];
p[4] = l[(b ^ 239) & 63];
for (b = 0, i = 0; (char)i < argv[1][0]; i++)
b = rand();
p[5] = l[(b ^ 229) & 63];
printf("%s\n", p);
return (0);
}
27 changes: 27 additions & 0 deletions 0x17-doubly_linked_lists/2-add_dnodeint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "lists.h"
#include <stdlib.h>

/**
* add_dnodeint - add a new node at the beginning of the linked list
* @head: double pointer to the beginning of the linked list
* @n: value to add to the new node
*
* Return: pointer to the new node, or NULL on failure
*/
dlistint_t *add_dnodeint(dlistint_t **head, const int n)
{
dlistint_t *new;

if (head == NULL)
return (NULL);
new = malloc(sizeof(dlistint_t));
if (new == NULL)
return (NULL);
new->n = n;
new->prev = NULL;
new->next = *head;
*head = new;
if (new->next != NULL)
(new->next)->prev = new;
return (new);
}
34 changes: 34 additions & 0 deletions 0x17-doubly_linked_lists/3-add_dnodeint_end.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "lists.h"
#include <stdlib.h>

/**
* add_dnodeint_end - adds a node to the end of a linked list
* @head: double pointer to the beginning of the linked list
* @n: value to add to new node
*
* Return: pointer to the new node, or NULL on failure
*/
dlistint_t *add_dnodeint_end(dlistint_t **head, const int n)
{
dlistint_t *new, *tmp;

if (head == NULL)
return (NULL);
new = malloc(sizeof(dlistint_t));
if (new == NULL)
return (NULL);
new->n = n;
new->next = NULL;
if (*head == NULL)
{
new->prev = NULL;
*head = new;
return (new);
}
tmp = *head;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = new;
new->prev = tmp;
return (new);
}
20 changes: 20 additions & 0 deletions 0x17-doubly_linked_lists/4-free_dlistint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "lists.h"
#include <stdlib.h>

/**
* free_dlistint - free a dlistint_t list
* @head: pointer to the beginning of the linked list
*
* Return: void
*/
void free_dlistint(dlistint_t *head)
{
dlistint_t *next;

while (head != NULL)
{
next = head->next;
free(head);
head = next;
}
}
25 changes: 25 additions & 0 deletions 0x17-doubly_linked_lists/5-get_dnodeint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "lists.h"

/**
* get_dnodeint_at_index - finds a specific node of a linked list
* @head: pointer to the beginning of the list
* @index: index of the node to retrieve
*
* Return: pointer to the indexed node, or NULL on failure
*/
dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index)
{
unsigned int i;

if (head == NULL)
return (NULL);
if (index == 0)
return (head);
for (i = 0; i < index; i++)
{
if (head->next == NULL)
return (NULL);
head = head->next;
}
return (head);
}
19 changes: 19 additions & 0 deletions 0x17-doubly_linked_lists/6-sum_dlistint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "lists.h"

/**
* sum_dlistint - sums all of the data of a dlistint_t linked list
* @head: pointer to the beginning of the linked list
*
* Return: sum of all data, or 0 if the list is empty
*/
int sum_dlistint(dlistint_t *head)
{
int sum = 0;

while (head != NULL)
{
sum += head->n;
head = head->next;
}
return (sum);
}
48 changes: 48 additions & 0 deletions 0x17-doubly_linked_lists/7-insert_dnodeint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "lists.h"
#include <stdlib.h>
#include <stdio.h>

/**
* insert_dnodeint_at_index - inserts a new node at a given position
* @h: double pointer to the beginning of the linked list
* @idx: index at which to insert the new node
* @n: data to enter into new node
*
* Return: pointer to the new node, or NULL on failure
*/
dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n)
{
dlistint_t *new, *next, *current;
unsigned int i;

if (h == NULL)
return (NULL);
if (idx != 0)
{
current = *h;
for (i = 0; i < idx - 1 && current != NULL; i++)
current = current->next;
if (current == NULL)
return (NULL);
}
new = malloc(sizeof(dlistint_t));
if (new == NULL)
return (NULL);
new->n = n;
if (idx == 0)
{
next = *h;
*h = new;
new->prev = NULL;
}
else
{
new->prev = current;
next = current->next;
current->next = new;
}
new->next = next;
if (new->next != NULL)
new->next->prev = new;
return (new);
}
40 changes: 40 additions & 0 deletions 0x17-doubly_linked_lists/8-delete_dnodeint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "lists.h"
#include <stdlib.h>

/**
* delete_dnodeint_at_index - deletes a node at a specific index
* @head: double pointer to the linked list
* @index: index at which to delete node
*
* Return: 1 on success, -1 on failure
*/
int delete_dnodeint_at_index(dlistint_t **head, unsigned int index)
{
dlistint_t *current;
unsigned int i;

if (head == NULL || *head == NULL)
return (-1);
current = *head;
if (index == 0)
{
*head = current->next;
if (current->next != NULL)
{
current->next->prev = NULL;
}
free(current);
return (1);
}
for (i = 0; i < index; i++)
{
if (current->next == NULL)
return (-1);
current = current->next;
}
current->prev->next = current->next;
if (current->next != NULL)
current->next->prev = current->prev;
free(current);
return (1);
}
1 change: 1 addition & 0 deletions 0x17-doubly_linked_lists/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x17-doubly_linked_lists
31 changes: 31 additions & 0 deletions 0x17-doubly_linked_lists/lists.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef LIST_H
#define LIST_H

#include <stddef.h>
/**
* struct dlistint_s - doubly linked list
* @n: integer
* @prev: points to the previous node
* @next: points to the next node
*
* Description: doubly linked list node structure
*
*/
typedef struct dlistint_s
{
int n;
struct dlistint_s *prev;
struct dlistint_s *next;
} dlistint_t;

size_t print_dlistint(const dlistint_t *h);
size_t dlistint_len(const dlistint_t *h);
dlistint_t *add_dnodeint(dlistint_t **head, const int n);
dlistint_t *add_dnodeint_end(dlistint_t **head, const int n);
void free_dlistint(dlistint_t *head);
dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index);
int sum_dlistint(dlistint_t *head);
dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n);
int delete_dnodeint_at_index(dlistint_t **head, unsigned int index);

#endif/*LIST_H*/
11 changes: 11 additions & 0 deletions 0x17-doubly_linked_lists/palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/python3
def ispalindrome(n):
string = str(n)
return string == string[::-1]

array = []
for i in range(999, 99, -1):
for j in range(i, 99, -1):
if ispalindrome(i * j):
array.append(i * j)
print(max(array))
3 changes: 3 additions & 0 deletions 0x18-dynamic_libraries/1-create_dynamic_lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
gcc *.c -c -fPIC
gcc *.o -shared -o liball.so
Empty file.
11 changes: 11 additions & 0 deletions 0x18-dynamic_libraries/100-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import random
import ctypes

cops = ctypes.CDLL('./100-operations.so')
a = random.randint(-111, 111)
b = random.randint(-111, 111)
print("{} + {} = {}".format(a, b, cops.add(a, b)))
print("{} - {} = {}".format(a, b, cops.sub(a, b)))
print("{} x {} = {}".format(a, b, cops.mul(a, b)))
print("{} / {} = {}".format(a, b, cops.div(a, b)))
print("{} % {} = {}".format(a, b, cops.mod(a, b)))
3 changes: 3 additions & 0 deletions 0x18-dynamic_libraries/101-make_me_win.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
wget -P /tmp https://github.com/eyoul/alx-low_level_programming/raw/main/0x18-dynamic_libraries/iwin.so
export LD_PRELOAD=/tmp/iwin.so
17 changes: 17 additions & 0 deletions 0x18-dynamic_libraries/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 0x18. C - Dynamic libraries

In this project, i learnt about what dynamic library is, how it works, how to create one and how it works. It also helped me understand the differences between static and shared libraries.

## Function Prototypes :floppy_disk:
The following files are scripts written for the projects written in C:


## Table of contents
Files | Description
----- | -----------
[libdynamic.so](./libdynamic.so) | C dynamic library containing the function definitions
[main.h](./main.h) | Header files containing the function prototypes
[1-create_dynamic_lib.sh](./1-create_dynamic_lib.sh) | Bash script that creates a dynamic library called liball.so from all the .c files that are in the current directory
[100-operations.so](./100-operations.so) | C dynamic library that contains C functions that can be called from Python
[random.so](./random.so) | C dynamic library to inject in a giga million program
[101-make_me_win.sh](./101-make_me_win.sh) | Bash script to inject the libmask.so library, using LD_PRELOAD, in the giga million program
Loading