ZIP indir
0
main
1 dal
git@carbon4code.com:ahmet/libasm.git
Ahmet Kerem Final
4 commit
libasm main.c
main.c 1.8 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>

size_t	ft_strlen(const char *str);
char	*ft_strcpy(char *dst, const char *src);
int		ft_strcmp(const char *s1, const char *s2);
ssize_t	ft_write(int fd, const void *buf, size_t count);
ssize_t	ft_read(int fd, void *buf, size_t count);
char	*ft_strdup(const char *str);
void	*ft_calloc(size_t nmemb, size_t size);

static void	test_ok(void)
{
	char	dst[32] = {0};
	char	buf[32] = {0};
	char	*dup;
	int		fd[2];
	int		i;
	int		*numbers;

	printf("===== NORMAL =====\n");
	numbers = ft_calloc(10, sizeof(int));
	printf("calloc : ");
	i = 0;
	while (i < 10)
		printf("%d ", numbers[i++]);
	printf("\n");
	free(numbers);

	printf("strlen : %ld\n", ft_strlen("Ahmet"));
	printf("strcpy : %s\n", ft_strcpy(dst, "Ahmet"));
	printf("strcmp : %d\n", ft_strcmp("abc", "abd"));

	fflush(stdout);
	ft_write(1, "write  : ok\n", 12);

	if (pipe(fd) == -1)
		return ;
	if (write(fd[1], "read", 4) == 4)
		ft_read(fd[0], buf, 4);
	close(fd[0]);
	close(fd[1]);
	printf("read   : %s\n", buf);

	dup = ft_strdup("Ahmet");
	printf("strdup : %s\n", dup);
	free(dup);
}

static void	test_errno(void)
{
	char	buf[32];
	ssize_t	ret;
	void	*p;

	printf("\n===== HATA / ERRNO =====\n");

	errno = 0;
	ret = ft_write(-1, "x", 1);
	printf("ft_write(-1) : ret=%zd errno=%d (%s)\n", ret, errno, strerror(errno));

	errno = 0;
	ret = ft_read(-1, buf, 1);
	printf("ft_read(-1)  : ret=%zd errno=%d (%s)\n", ret, errno, strerror(errno));

	errno = 0;
	p = ft_calloc(SIZE_MAX, 2);
	printf("ft_calloc(SIZE_MAX, 2) : %p errno=%d (%s)\n",
		p, errno, strerror(errno));

	errno = 0;
	p = ft_calloc((size_t)1 << 40, (size_t)1 << 40);
	printf("ft_calloc(2^40, 2^40)  : %p errno=%d (%s)\n",
		p, errno, strerror(errno));
}

int	main(void)
{
	test_ok();
	test_errno();
	return (0);
}