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
116 changes: 116 additions & 0 deletions atoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
int length(char *s);
int exponent(int len);
int total(char *s);
int neg(char *s, int p);

/**
* _atoi - Converts numbers in a string to integers
* @s: string
* Return: String number converted to int
*/
int _atoi(char *s)
{
int i;
int len;
int number;
unsigned int body;
int expt;
int total_len;
int negatives;

len = length(s);
expt = exponent(len);
total_len = total(s);
body = 0;
for (i = total_len - len; s[i] != '\0'; i++)
{
number = (s[i] - 48) * expt;
body = number + body;
expt = expt / 10;
}
negatives = neg(s, total_len - len);
if (negatives % 2 != 0)
body = -body;
return (body);
}

/**
* length - Calculates the length of the number
* @s: string
* Return: The length of the number
*/
int length(char *s)
{
int i;
int len;

for (i = 0, len = 0; s[i] != '\0'; i++)
{
if (s[i] >= 48 && s[i] <= 57)
{
len++;
if (s[i + 1] == 32) /* stopping for spaces*/
break;
}
}
return (len);
}

/**
* exponent - Calculates the exponent to multiply the number by
* @len: length of a number
* Return: The exponent of a number
*/
int exponent(int len)
{
int expt;

for (expt = 1; len - 1 > 0; len--)
{
expt = expt * 10;
}
return (expt);
}

/**
* total - Calculates the total length of a string up to the numbers
* @s: string
* Return: Total len of a string
*/
int total(char *s)
{
int i;

for (i = 0; s[i] != '\0'; i++)
{
if (s[i] >= 48 && s[i] <= 57)
{
if (s[i + 1] < 48 || s[i + 1] > 57)
{
i++;
break;
}
}
}
return (i);
}

/**
* neg - Calculates the total number of negatives
* @s: string
* @p: position were the number starts in the string
* Return: Total count of negatives
*/
int neg(char *s, int p)
{
int i;
int counter;

for (counter = 0, i = 0; i < p; i++)
{
if (s[i] == 45)
counter++;
}
return (counter);
}

25 changes: 23 additions & 2 deletions builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,35 @@ void env_b(__attribute__((unused))char *line)
}

/**
* exit_b - Exits the shell. After freeing allocated resources.
* exit_b - Exits the shell with value 0. After freeing allocated resources.
* If exit value provided, exit with value. If exit value is not integer,
* exit with 2 and print error message.
* @line: A string representing the input from the user.
*/
void exit_b(char *line)
{
int token_count = 0, value;
char **argv;
const char *delim = "\n\t ";

argv = token_interface(line, delim, token_count);
if (argv[1] == NULL)
value = 0;
else if (_atoi(argv[1]))
value = _atoi(argv[1]);
else
{
value = 2;
print_str("logout", 0);
printf("exit: %s: numeric argument required\n", argv[1]);
double_free(argv);
free(line);
exit(value);
}
double_free(argv);
free(line);
print_str("logout", 0);
exit(1);
exit(value);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions olaf.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,10 @@ void cd_b(char *directory);
int _strcmp(char *s1, char *s2);
char *_strdup(char *src);
void print_str(char *str, int new_line);
int _atoi(char *s);
int length(char *s);
int exponent(int len);
int total(char *s);
int neg(char *s, int p);

#endif