2
.gitignore
vendored
2
.gitignore
vendored
@@ -50,3 +50,5 @@ modules.order
|
||||
Module.symvers
|
||||
Mkfile.old
|
||||
dkms.conf
|
||||
/tests/a
|
||||
/*.org
|
||||
|
||||
3
main.h
3
main.h
@@ -1,6 +1,9 @@
|
||||
#ifndef MAIN_H_
|
||||
#define MAIN_H_
|
||||
int _printf(const char *format, ...);
|
||||
int _contains(const char *str, char c);
|
||||
int _strlen(const char *str);
|
||||
char *_strcpy(char *dest, char *src);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
53
printf.c
Normal file
53
printf.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "main.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
/**
|
||||
* _printf - printf but worse
|
||||
* @format: format string
|
||||
* @...: ???
|
||||
* Return: characters written
|
||||
**/
|
||||
int _printf(const char *format, ...)
|
||||
{
|
||||
int buff_idx = 0;
|
||||
int fmt_idx = 0;
|
||||
unsigned int identifiers = _contains(format, '%');
|
||||
unsigned int BUFF_SIZE = _strlen(format) - (identifiers * 2);
|
||||
char *buffer, *next;
|
||||
va_list args;
|
||||
|
||||
va_start(args, format);
|
||||
buffer = malloc(BUFF_SIZE);
|
||||
|
||||
if (!format) /* No string. No laundry */
|
||||
return (0);
|
||||
while (*(format + fmt_idx))
|
||||
{
|
||||
if ((*(format + fmt_idx) == '%') && (*(format + fmt_idx + 1)))
|
||||
{
|
||||
switch (*(format + 1)) /*this needs to shrink*/
|
||||
{
|
||||
case 's':
|
||||
next = va_arg(args, char*); /*Store string temporarily*/
|
||||
buffer = _strcpy(buffer, next);
|
||||
BUFF_SIZE = _strlen(buffer);
|
||||
buff_idx += _strlen(next);
|
||||
fmt_idx += 2;
|
||||
break;
|
||||
case 'c': /* add 1 byte and i++ */
|
||||
break;
|
||||
case '%': /*add 1 byte*/
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*(buffer + buff_idx) = *(format + fmt_idx);
|
||||
buff_idx++;
|
||||
fmt_idx++;
|
||||
}
|
||||
}
|
||||
write(1, buffer, BUFF_SIZE + 1);
|
||||
return (_strlen(buffer));
|
||||
}
|
||||
27
strcpy.c
Normal file
27
strcpy.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "main.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* _strcpy - strcpy with an n
|
||||
* @dest: the buffer we copying to
|
||||
* @src: the idot we ctrl+c-ed
|
||||
* Return: dest
|
||||
*/
|
||||
char *_strcpy(char *dest, char *src)
|
||||
{
|
||||
int s_idx, i, strlen;
|
||||
char *new_me;
|
||||
|
||||
new_me = malloc(_strlen(dest) + _strlen(src) + 1);
|
||||
strlen = _strlen(src);
|
||||
|
||||
s_idx = -1;
|
||||
while (*(dest + ++s_idx))
|
||||
*(new_me + s_idx) = *(dest + s_idx);
|
||||
|
||||
for (i = 0; i < strlen; i++)
|
||||
*(new_me + s_idx++) = *(src + i);
|
||||
*(new_me + _strlen(new_me)) = '\0';
|
||||
|
||||
return (new_me);
|
||||
}
|
||||
37
strings.c
Normal file
37
strings.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "main.h"
|
||||
|
||||
/**
|
||||
* _strlen - Takes string and return its length
|
||||
*
|
||||
* @str: Address to the head of the string (Array of Characters) (Lost btw)
|
||||
*
|
||||
* Return: Length of String
|
||||
*/
|
||||
int _strlen(const char *str)
|
||||
{
|
||||
if (*str)
|
||||
return (1 + _strlen(++str));
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
|
||||
/**
|
||||
* _contains - gets number of unique identifiers
|
||||
*
|
||||
* @str: da string
|
||||
* @c: basically almost always '%'
|
||||
*
|
||||
* Return: number of unique cases of "%*" that aren't "%%"
|
||||
*/
|
||||
int _contains(const char *str, char c)
|
||||
{
|
||||
if (*str)
|
||||
{
|
||||
if (*str == c && *(str + 1) != c && *(str - 1) != c)
|
||||
return (1 + _contains(str + 1, c));
|
||||
else
|
||||
return (_contains(str + 1, c));
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
@@ -8,5 +8,6 @@ int main(void)
|
||||
{
|
||||
int len;
|
||||
|
||||
len = _printf("Hello world");
|
||||
len = _printf("Hello world\n");
|
||||
return (len);
|
||||
}
|
||||
|
||||
20
tests/str_print.c
Normal file
20
tests/str_print.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "../main.h"
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* main - Tests if '%s' works within our printf.
|
||||
*
|
||||
* Return: 0 On Failure, 1 Otherwise Success
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *test_0 = "Hej";
|
||||
char *test_1 = "pls";
|
||||
/*
|
||||
* Brain ded to think bout test cond
|
||||
*/
|
||||
_printf(" suh %s\n%s\n", test_0, test_1);
|
||||
return (0);
|
||||
|
||||
}
|
||||
17
tests/strcpy.c
Normal file
17
tests/strcpy.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "../main.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
/**
|
||||
* main - why does this not work! I know!
|
||||
* Return: 0...unless?
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
char *str, *str2;
|
||||
|
||||
str = "hello";
|
||||
str2 = " world!\n";
|
||||
str = _strcpy(str, str2);
|
||||
printf("%s", str);
|
||||
return (0);
|
||||
}
|
||||
19
tests/strlen.c
Normal file
19
tests/strlen.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "../main.h"
|
||||
|
||||
/**
|
||||
* main - test strlen
|
||||
* Return: 0 if yes, 1 if no
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
char *str;
|
||||
int len;
|
||||
|
||||
str = "hello";
|
||||
len =_strlen(str);
|
||||
if (len == 5)
|
||||
return (0);
|
||||
else
|
||||
return (1);
|
||||
|
||||
}
|
||||
3
tests/test.sh
Executable file
3
tests/test.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/bash
|
||||
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 $1 ../*.c -o a
|
||||
./a
|
||||
Reference in New Issue
Block a user