/ philosophers / philosophers / philo / validate.c
validate.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   validate.c                                         :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/04/21 17:06:56 by gychoi            #+#    #+#             */
 9  /*   Updated: 2023/04/21 21:07:47 by gychoi           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "philo.h"
14  
15  void	lock(pthread_mutex_t *mutex, t_philo *philo)
16  {
17  	if (pthread_mutex_lock(mutex) != 0)
18  	{
19  		if (philo != 0)
20  			philo->error = TRUE;
21  	}
22  }
23  
24  void	unlock(pthread_mutex_t *mutex, t_philo *philo)
25  {
26  	if (pthread_mutex_unlock(mutex) != 0)
27  	{
28  		if (philo != 0)
29  			philo->error = TRUE;
30  	}
31  }
32  
33  int	atoi_only_unsigned(char *n)
34  {
35  	int			i;
36  	int			mark;
37  	long long	value;
38  
39  	i = 0;
40  	mark = 0;
41  	while (n[i] != '\0')
42  	{
43  		if (!(n[i] == '+' || ('0' <= n[i] && n[i] <= '9')) || mark > 1)
44  			return (-1);
45  		if (n[i] == '+')
46  			mark++;
47  		i++;
48  	}
49  	i = mark;
50  	mark = 0;
51  	value = 0;
52  	while (('0' <= n[i] && n[i] <= '9') && n[i] != '\0')
53  	{
54  		value = value * 10 + (n[i++] - '0');
55  		mark++;
56  	}
57  	if (!mark || n[i] != '\0' || value > 2147483647)
58  		return (-1);
59  	return ((unsigned int)value);
60  }
61  
62  int	valid_input(char **argv)
63  {
64  	int	i;
65  
66  	i = 1;
67  	while (argv[i] != 0)
68  	{
69  		if (atoi_only_unsigned(argv[i]) <= 0)
70  			return (FALSE);
71  		i++;
72  	}
73  	return (TRUE);
74  }