OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Jan 22, 2020 12:34 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Newlib does not flush stdout at end of execution
PostPosted: Fri Jan 17, 2020 11:55 am 
Offline
Member
Member

Joined: Wed Aug 10, 2016 3:07 am
Posts: 29
Hello, I have a problem with newlib: I'm trying to compile a very simple program for my kernem:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
    struct dirent *dirent = NULL;
    char *directory_path = ".";
    DIR * directory = NULL;

    if(argc >= 2)
        directory_path = argv[1];

    directory = opendir(directory_path);
    if(!directory)
        {printf("Impossible d'ouvrir le rĂ©pertoire %s", directory_path); return -1;}

    while ((dirent = readdir(directory)) != NULL)
       printf("%s\n", dirent->d_name);

    closedir(directory);
    return 0;
}

But this code doesn't display anything on the screen unless I explicitly clear stdout. I debugged a bit my program with gdb and I realized that no destructor was called (there is no one) when exit is called, why ? Normally the buffers are emptied when the program ends, right?

My crt0.c code:
Code:
#include <fcntl.h>
#include <stdlib.h>
#include "syscalls.h"

extern char end;
extern char **environ;
extern void exit(int code);
extern int brk(void * addr);
extern void __libc_init_array();
extern void __libc_fini_array();
extern int main(int argc, char ** argv, char ** envp);

extern void _init(void);

void _start(int args)
{
    int *params = &args - 1;
    int argc = *params;
    char **argv = (char **) (params + 1);

    environ = argv + argc + 1;

    brk(&end);                           // Initialise le heap
    __libc_init_array();            // Appelle les constructeurs
    atexit(__libc_fini_array);
    exit(main(argc, argv, environ));
}


Thank you in advance!

_________________
Sorry for my English, I'm a French student.
My own kernel: HaoudOS


Top
 Profile  
 
 Post subject: Re: Newlib does not flush stdout at end of execution
PostPosted: Fri Jan 17, 2020 10:56 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 337
If I read that right, there is a function called __sinit() that should be called from the stdio code. It will set GLOBAL_REENT->__cleanup to _cleanup_r(). exit(), then, will call that function through the pointer. There are not many ways this can go wrong. Find out if __sinit() is called and what GLOBAL_REENT->__cleanup is when exit is called.


Top
 Profile  
 
 Post subject: Re: Newlib does not flush stdout at end of execution
PostPosted: Sat Jan 18, 2020 4:23 pm 
Offline
Member
Member

Joined: Wed Aug 10, 2016 3:07 am
Posts: 29
Thanks a lot, everything works fine, the answer was given in this topic (viewtopic.php?f=1&t=31261) but due to a bug in the implementation of the system calls it was not working.

And sorry for the late response but my IP has been blacklisted by osdev, no idea why :(

_________________
Sorry for my English, I'm a French student.
My own kernel: HaoudOS


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: reapersms and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group