Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfixes and improved tests #107

Merged
merged 15 commits into from
Oct 3, 2024
Merged
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
2 changes: 1 addition & 1 deletion libc/inc/bits/stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "time.h"

/// @brief Data structure which contains information about a file.
typedef struct stat_t {
typedef struct stat {
/// ID of device containing file.
dev_t st_dev;
/// File serial number.
Expand Down
2 changes: 1 addition & 1 deletion libc/inc/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "stdbool.h"

/// @brief Structure that describes scheduling parameters.
typedef struct sched_param_t {
typedef struct sched_param {
/// Static execution priority.
int sched_priority;
/// Expected period of the task
Expand Down
7 changes: 4 additions & 3 deletions libc/inc/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include "sys/types.h"
#include "stddef.h"

/// @brief List of signals.
typedef enum {
Expand Down Expand Up @@ -161,13 +162,13 @@ typedef void (*sighandler_t)(int);
/// Signals are divided into two cathegories, identified by the two unsigned longs:
/// [ 1, 31] corresponds to normal signals;
/// [32, 64] corresponds to real-time signals.
typedef struct sigset_t {
typedef struct sigset {
/// Signals divided into two cathegories.
unsigned long sig[2];
} sigset_t;

/// @brief Holds the information on how to handle a specific signal.
typedef struct sigaction_t {
typedef struct sigaction {
/// This field specifies the type of action to be performed; its value can be a pointer
/// to the signal handler, SIG_DFL (that is, the value 0) to specify that the default
/// action is performed, or SIG_IGN (that is, the value 1) to specify that the signal is
Expand All @@ -186,7 +187,7 @@ typedef union sigval {
} sigval_t;

/// @brief Stores information about an occurrence of a specific signal.
typedef struct siginfo_t {
typedef struct siginfo {
/// The signal number.
int si_signo;
/// A code identifying who raised the signal (see signal_sender_code_t).
Expand Down
12 changes: 8 additions & 4 deletions libc/inc/sys/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#define STDOUT_FILENO 1 ///< Standard output.
#define STDERR_FILENO 2 ///< Standard error output.

#define stdin STDIN_FILENO ///< Standard input.
#define stdout STDOUT_FILENO ///< Standard output.
#define stderr STDERR_FILENO ///< Standard error output.

/// @brief Read data from a file descriptor.
/// @param fd The file descriptor.
/// @param buf The buffer.
Expand Down Expand Up @@ -80,7 +84,7 @@ extern pid_t getpid(void);
/// If pid != 0 return the SID corresponding to the process having identifier == pid
///@param pid process identifier from wich we want the SID
///@return On success return SID of the session
/// Otherwise return -1 with errno set on: EPERM or ESRCH
/// Otherwise return -1 with errno set on: EPERM or ESRCH
extern pid_t getsid(pid_t pid);

///@brief creates a new session if the calling process is not a
Expand All @@ -90,7 +94,7 @@ extern pid_t getsid(pid_t pid);
/// of a new process group in the session (i.e., its process group ID
/// is made the same as its process ID).
///@return On success return SID of the session just created
/// Otherwise return -1 with errno : EPERM
/// Otherwise return -1 with errno : EPERM
extern pid_t setsid(void);

///@brief returns the Process Group ID (PGID) of the process specified by pid.
Expand All @@ -117,7 +121,7 @@ extern gid_t getegid(void);
///@brief sets the group IDs of the calling process.
///@param gid the Group ID to set
///@return On success, zero is returned.
/// Otherwise returns -1 with errno set to :EINVAL or EPERM
/// Otherwise returns -1 with errno set to :EINVAL or EPERM
extern int setgid(gid_t gid);

///@brief sets the real and effective group IDs of the calling process.
Expand All @@ -138,7 +142,7 @@ extern uid_t geteuid(void);
///@brief Sets the User IDs of the calling process.
///@param uid the new User ID.
///@return On success, zero is returned.
/// Otherwise returns -1 with errno set to :EINVAL or EPERM
/// Otherwise returns -1 with errno set to :EINVAL or EPERM
extern int setuid(uid_t uid);

///@brief Sets the effective and real User IDs of the calling process.
Expand Down
22 changes: 11 additions & 11 deletions libc/inc/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
typedef unsigned int time_t;

/// Used to get information about the current time.
typedef struct tm_t {
typedef struct tm {
/// Seconds [0 to 59]
int tm_sec;
/// Minutes [0 to 59]
Expand All @@ -44,22 +44,22 @@ typedef struct tm_t {
} tm_t;

/// Rappresents time.
typedef struct _timeval {
typedef struct timeval {
time_t tv_sec; ///< Seconds.
time_t tv_usec; ///< Microseconds.
} timeval;
} timeval_t;

/// Rappresents a time interval.
typedef struct _itimerval {
timeval it_interval; ///< Next value.
timeval it_value; ///< Current value.
} itimerval;
typedef struct itimerval {
struct timeval it_interval; ///< Next value.
struct timeval it_value; ///< Current value.
} itimerval_t;

/// @brief Specify intervals of time with nanosecond precision.
typedef struct timespec {
time_t tv_sec; ///< Seconds.
long tv_nsec; ///< Nanoseconds.
} timespec;
} timespec_t;

/// @brief Returns the current time.
/// @param t Where the time should be stored.
Expand Down Expand Up @@ -97,7 +97,7 @@ size_t strftime(char *s, size_t max, const char *format, const tm_t *tm);
/// in *req has elapsed, or the delivery of a signal that triggers the
/// invocation of a handler in the calling thread or that terminates
/// the process.
int nanosleep(const timespec *req, timespec *rem);
int nanosleep(const struct timespec *req, struct timespec *rem);

/// @brief Causes the calling thread to sleep either until the number of
/// real-time seconds specified in seconds have elapsed or
Expand All @@ -112,7 +112,7 @@ unsigned int sleep(unsigned int seconds);
/// @param which which timer.
/// @param curr_value where we place the timer value.
/// @return 0 on success, -1 on failure and errno is set to indicate the error.
int getitimer(int which, itimerval *curr_value);
int getitimer(int which, struct itimerval *curr_value);

/// @brief The system provides each process with three interval timers, each
/// decrementing in a distinct time domain. When any timer expires, a signal is
Expand All @@ -121,4 +121,4 @@ int getitimer(int which, itimerval *curr_value);
/// @param new_value the new value for the timer.
/// @param old_value output variable where the function places the previous value.
/// @return 0 on success, -1 on failure and errno is set to indicate the error.
int setitimer(int which, const itimerval *new_value, itimerval *old_value);
int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);
54 changes: 38 additions & 16 deletions libc/src/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

void putchar(int character)
{
write(STDOUT_FILENO, &character, 1);
write(STDOUT_FILENO, &character, 1U);
}

void puts(const char *str)
Expand Down Expand Up @@ -188,35 +188,57 @@ long strtol(const char *str, char **endptr, int base)

int fgetc(int fd)
{
unsigned char c;
if (read(fd, &c, 1) <= 0) {
return EOF;
char c;
ssize_t bytes_read;

// Read a single character from the file descriptor.
bytes_read = read(fd, &c, 1);

// Check for errors or EOF.
if (bytes_read == -1) {
perror("Error reading from file descriptor");
return EOF; // Return EOF on error.
} else if (bytes_read == 0) {
return EOF; // Return EOF if no bytes were read (end of file).
}
return c;

// Return the character as an unsigned char.
return (unsigned char)c;
}

char *fgets(char *buf, int n, int fd)
{
int c;
char *p;
char *p = buf;
int count = n - 1; // Leave space for null terminator

// Read characters until reaching the limit or newline
while (count > 0) {
ssize_t bytes_read = read(fd, &c, 1); // Read one character

/* get max bytes or upto a newline */
for (p = buf, n--; n > 0; n--) {
// Get the character.
c = fgetc(fd);
if (c == EOF) {
if (bytes_read < 0) {
perror("Error reading from file descriptor");
return NULL; // Return NULL on error
} else if (bytes_read == 0) {
// End of file
break;
}
*p++ = (char)c;

*p++ = (char)c; // Store the character in the buffer

if (c == '\n') {
break;
break; // Stop if we reach a newline
}
count--;
}
*p = 0;

*p = '\0'; // Null-terminate the string

if (p == buf || c == EOF) {
return NULL;
return NULL; // Return NULL if no characters were read or EOF was reached
}
return (p);

return buf; // Return the buffer
}

void perror(const char *s)
Expand Down
2 changes: 1 addition & 1 deletion libc/src/sys/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ key_t ftok(char *path, int id)
{
// Create a struct containing the serial number and the device number of the
// file we use to generate the key.
struct stat_t st;
struct stat st;
if (stat(path, &st) < 0) {
pr_err("Cannot stat the file `%s`...\n", path);
errno = ENOENT;
Expand Down
76 changes: 38 additions & 38 deletions libc/src/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,49 +116,49 @@ size_t strftime(char *str, size_t maxsize, const char *format, const tm_t *timep
break;
#endif
case 'b': // Abbreviated month name
{
strncat(s, months[timeptr->tm_mon], 3);
break;
}
{
strncat(s, months[timeptr->tm_mon], 3);
break;
}
case 'B': // Full month name
{
unsigned int i = (unsigned int)timeptr->tm_mon;
strcat(s, months[i]);
break;
}
{
unsigned int i = (unsigned int)timeptr->tm_mon;
strcat(s, months[i]);
break;
}
case 'd': /* day of month as decimal number (01-31) */
{
value = timeptr->tm_mday;
(*s++) = (char)('0' + ((value / 10) % 10));
(*s++) = (char)('0' + (value % 10));
break;
}
{
value = timeptr->tm_mday;
(*s++) = (char)('0' + ((value / 10) % 10));
(*s++) = (char)('0' + (value % 10));
break;
}
case 'H': /* hour (24-hour clock) as decimal (00-23) */
{
value = timeptr->tm_hour;
(*s++) = (char)('0' + ((value / 10) % 10));
(*s++) = (char)('0' + (value % 10));
break;
}
{
value = timeptr->tm_hour;
(*s++) = (char)('0' + ((value / 10) % 10));
(*s++) = (char)('0' + (value % 10));
break;
}
#if 0
case 'I': /* hour (12-hour clock) as decimal (01-12) */
SPRINTF("%02d", ((timeptr->tm_hour + 11) % 12) + 1);
break;
#endif
case 'j': // Day of year as decimal number (001-366)
{
value = timeptr->tm_yday;
(*s++) = (char)('0' + ((value / 10) % 10));
(*s++) = (char)('0' + (value % 10));
break;
}
{
value = timeptr->tm_yday;
(*s++) = (char)('0' + ((value / 10) % 10));
(*s++) = (char)('0' + (value % 10));
break;
}
case 'm': // Month as decimal number (01-12)
{
value = timeptr->tm_mon;
(*s++) = (char)('0' + ((value / 10) % 10));
(*s++) = (char)('0' + (value % 10));
break;
}
{
value = timeptr->tm_mon;
(*s++) = (char)('0' + ((value / 10) % 10));
(*s++) = (char)('0' + (value % 10));
break;
}
#if 0
case 'M': /* month as decimal number (00-59) */
SPRINTF("%02d", timeptr->tm_min);
Expand Down Expand Up @@ -381,11 +381,11 @@ size_t strftime(char *str, size_t maxsize, const char *format, const tm_t *timep
}

/// @brief nanosleep function.
_syscall2(int, nanosleep, const timespec *, req, timespec *, rem)
_syscall2(int, nanosleep, const struct timespec *, req, struct timespec *, rem)

unsigned int sleep(unsigned int seconds)
unsigned int sleep(unsigned int seconds)
{
timespec req, rem;
struct timespec req, rem;
req.tv_sec = seconds;
// Call the nanosleep.
int __ret = nanosleep(&req, &rem);
Expand All @@ -397,6 +397,6 @@ _syscall2(int, nanosleep, const timespec *, req, timespec *, rem)
return 0;
}

_syscall2(int, getitimer, int, which, itimerval *, curr_value)
_syscall2(int, getitimer, int, which, struct itimerval *, curr_value)

_syscall3(int, setitimer, int, which, const itimerval *, new_value, itimerval *, old_value)
_syscall3(int, setitimer, int, which, const struct itimerval *, new_value, struct itimerval *, old_value)
14 changes: 1 addition & 13 deletions mentos/inc/hardware/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void remove_timer(struct timer_list *timer);
/// in *req has elapsed, or the delivery of a signal that triggers the
/// invocation of a handler in the calling thread or that terminates
/// the process.
int sys_nanosleep(const timespec *req, timespec *rem);
int sys_nanosleep(const struct timespec *req, struct timespec *rem);

/// @brief Send signal to calling thread after desired seconds.
/// @param seconds The number of seconds in the interval
Expand All @@ -163,18 +163,6 @@ int sys_nanosleep(const timespec *req, timespec *rem);
/// scheduled alarm.
unsigned sys_alarm(int seconds);

/// @brief Rappresents a time value.
struct timeval {
time_t tv_sec; ///< Seconds.
time_t tv_usec; ///< Microseconds.
};

/// @brief Rappresents a time interval.
struct itimerval {
struct timeval it_interval; ///< Next value.
struct timeval it_value; ///< Current value.
};

/// @brief Fills the structure pointed to by curr_value with the current setting for the timer specified by which.
/// @param which The time domain (i.e., ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF)
/// @param curr_value There the values must be stored.
Expand Down
Loading
Loading