Skip to content

Commit 989cf4d

Browse files
authored
Merge pull request #33 from cuajarsaki/refactor/#7-norm-errors
fixed some norm errors in libft and remove debugs output
2 parents b4917b7 + 127976d commit 989cf4d

11 files changed

+68
-130
lines changed

libft/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ OBJ = ft_isalnum.o ft_itoa.o ft_memset.o ft_split.o ft_strlcat.o ft_strnstr.o
1212
get_next_line.o get_next_line_utils.o ft_parse_width.o ft_print_padding.o \
1313
ft_print_char.o ft_print_string.o ft_print_pointer.o ft_print_int.o \
1414
ft_print_unsigned.o ft_print_hexa.o ft_print_percent.o ft_printf.o \
15-
ft_strtok.o ft_strcmp.o ft_strncpy.o ft_realloc.o ft_strcat.o ft_strncat.o \
15+
ft_strtok.o ft_strcmp.o ft_realloc.o ft_strncat.o \
1616
ft_strndup.o ft_strtol.o ft_snprintf.o
1717

1818
HEADERS = libft.h

libft/ft_realloc.c

+17-16
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33
/* ::: :::::::: */
44
/* ft_realloc.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: jidler <jidler@student.42tokyo.jp> +#+ +:+ +#+ */
6+
/* By: pchung <pchung@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/26 14:21:24 by jidler #+# #+# */
9-
/* Updated: 2025/01/26 14:23:17 by jidler ### ########.fr */
9+
/* Updated: 2025/03/09 04:10:48 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13-
#include <stdlib.h> // for malloc and free
13+
#include <stdlib.h>
1414

15-
char **ft_realloc(char **ptr, size_t old_size, size_t new_size)
15+
char **ft_realloc(char **ptr, size_t old_size, size_t new_size)
1616
{
17-
// Allocate memory for the new pointer array
18-
char **new_ptr = malloc(new_size * sizeof(char *));
19-
if (!new_ptr) return NULL; // Return NULL if memory allocation fails
17+
char **new_ptr;
18+
size_t i;
2019

21-
// Copy data from old pointer array to new one, up to the minimum of old_size and new_size
22-
for (size_t i = 0; i < old_size && i < new_size; i++) {
23-
new_ptr[i] = ptr[i];
24-
}
25-
26-
// Free the old pointer array
27-
free(ptr);
28-
29-
return new_ptr;
20+
i = 0;
21+
new_ptr = malloc(new_size * sizeof(char *));
22+
if (!new_ptr)
23+
return (NULL);
24+
while (i < old_size && i < new_size)
25+
{
26+
new_ptr[i] = ptr[i];
27+
i++;
28+
}
29+
free(ptr);
30+
return (new_ptr);
3031
}

libft/ft_strcat.c

-29
This file was deleted.

libft/ft_strchr.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@
33
/* ::: :::::::: */
44
/* ft_strchr.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: jidler <jidler@student.42tokyo.jp> +#+ +:+ +#+ */
6+
/* By: pchung <pchung@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/12/26 15:54:18 by jidler #+# #+# */
9-
/* Updated: 2025/01/26 13:49:56 by jidler ### ########.fr */
9+
/* Updated: 2025/03/09 04:15:00 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include <stddef.h>
1414

15-
char *ft_strchr(const char *s, int c)
15+
char *ft_strchr(const char *s, int c)
1616
{
1717
while (*s)
1818
{
1919
if (*s == (char)c)
20-
return (char *)s;
20+
return ((char *)s);
2121
s++;
2222
}
23-
return (c == '\0') ? (char *)s : NULL;
24-
}
23+
if (c == '\0')
24+
return ((char *)s);
25+
else
26+
return (NULL);
27+
}

libft/ft_strdup.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: pchung <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/02/12 16:12:22 by jidler #+# #+# */
9-
/* Updated: 2025/02/26 13:52:00 by pchung ### ########.fr */
9+
/* Updated: 2025/03/09 04:13:06 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -31,4 +31,4 @@ char *ft_strdup(const char *src)
3131
}
3232
copy[length] = '\0';
3333
return (copy);
34-
}
34+
}

libft/ft_strncat.c

+6-11
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,24 @@
33
/* ::: :::::::: */
44
/* ft_strncat.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: jidler <jidler@student.42tokyo.jp> +#+ +:+ +#+ */
6+
/* By: pchung <pchung@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/03/02 15:33:36 by jidler #+# #+# */
9-
/* Updated: 2025/03/02 15:33:42 by jidler ### ########.fr */
9+
/* Updated: 2025/03/09 04:11:28 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include <stdio.h>
1414

15-
char *ft_strncat(char *dest, const char *src, size_t n)
15+
char *ft_strncat(char *dest, const char *src, size_t n)
1616
{
17-
char *ptr = dest;
17+
char *ptr;
1818

19-
// Move ptr to the end of dest
19+
ptr = dest;
2020
while (*ptr)
2121
ptr++;
22-
23-
// Append at most n characters from src
2422
while (n-- && *src)
2523
*ptr++ = *src++;
26-
27-
// Null-terminate the result
2824
*ptr = '\0';
29-
30-
return dest;
25+
return (dest);
3126
}

libft/ft_strncpy.c

-33
This file was deleted.

libft/ft_strndup.c

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
#include <stdlib.h>
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_strndup.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: pchung <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2025/03/09 04:15:22 by pchung #+# #+# */
9+
/* Updated: 2025/03/09 04:17:31 by pchung ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
213
#include <stdio.h>
14+
#include <stdlib.h>
315

4-
char *ft_strndup(const char *src, size_t n)
16+
char *ft_strndup(const char *src, size_t n)
517
{
6-
size_t i;
7-
char *dup;
8-
9-
// Find the length of the actual string to copy (max n)
10-
for (i = 0; i < n && src[i]; i++)
11-
;
18+
size_t i;
19+
size_t j;
20+
char *dup;
1221

13-
// Allocate memory for the new string (+1 for null terminator)
22+
i = 0;
23+
while (i < n && src[i])
24+
i++;
1425
dup = (char *)malloc(i + 1);
1526
if (!dup)
16-
return NULL;
17-
18-
// Copy up to n characters
19-
for (size_t j = 0; j < i; j++)
27+
return (NULL);
28+
j = 0;
29+
while (j < i)
30+
{
2031
dup[j] = src[j];
21-
22-
// Null-terminate the new string
32+
j++;
33+
}
2334
dup[i] = '\0';
24-
25-
return dup;
26-
}
35+
return (dup);
36+
}

libft/libft.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* libft.h :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: jidler <jidler@student.42tokyo.jp> +#+ +:+ +#+ */
6+
/* By: pchung <pchung@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/04/12 16:50:27 by jidler #+# #+# */
9-
/* Updated: 2025/03/04 18:55:05 by jidler ### ########.fr */
9+
/* Updated: 2025/03/09 04:19:43 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,7 +20,6 @@
2020
int ft_snprintf(char *str, size_t size, const char *format, ...);
2121
char *ft_strndup(const char *src, size_t n);
2222
char *ft_strncat(char *dest, const char *src, size_t n);
23-
char *ft_strcat(char *dest, const char *src);
2423
int ft_atoi(const char *str);
2524
void ft_bzero(void *s, size_t n);
2625
void *ft_calloc(size_t nelem, size_t elsize);
@@ -57,7 +56,6 @@ int ft_tolower(int c);
5756
int ft_toupper(int c);
5857
int ft_strcmp(const char *s1, const char *s2);
5958

60-
char *ft_strncpy(char *dest, const char *src, size_t n);
6159
char **ft_realloc(char **ptr, size_t old_size, size_t new_size);
6260

6361
char *ft_strchr(const char *s, int c);

src/buildin_commands/shell_cd.c

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: pchung <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/02/09 15:41:50 by jidler #+# #+# */
9-
/* Updated: 2025/03/08 21:04:33 by pchung ### ########.fr */
9+
/* Updated: 2025/03/09 04:42:38 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -21,13 +21,7 @@ void print_cd_result(void)
2121
{
2222
char cwd[1024];
2323

24-
if (getcwd(cwd, sizeof(cwd)) != NULL)
25-
{
26-
write(1, "Changed directory to: ", 22);
27-
write(1, cwd, strlen(cwd));
28-
write(1, "\n", 1);
29-
}
30-
else
24+
if (getcwd(cwd, sizeof(cwd)) == NULL)
3125
print_cd_error("getcwd");
3226
}
3327

src/executor/executor_command_execution2.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: pchung <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/03/09 00:41:03 by pchung #+# #+# */
9-
/* Updated: 2025/03/09 03:00:50 by pchung ### ########.fr */
9+
/* Updated: 2025/03/09 04:30:51 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -75,7 +75,6 @@ int ft_execvp(const char *file, char *const argv[], char **envp,
7575
return (-1);
7676
if (has_slash(file))
7777
{
78-
printf("Executing directly: %s\n", file);
7978
return (execve(file, argv, envp));
8079
}
8180
if (path_env)

0 commit comments

Comments
 (0)