Sahih al-Bukhari, Book 97, Hadith 80 (In-Book)

Narrated `Abdullah bin Mas`ud: Allah's Messenger the true and truly inspired, narrated to us, "The creation of everyone of you starts with the process of collecting the material for his body within forty days and forty nights in the womb of his mother. Then he becomes a clot of thick blood for a similar period (40 days) and then he becomes like a piece of flesh for a similar period. Then an angel is sent to him (by Allah) and the angel is allowed (ordered) to write four things; his livelihood, his (date of) death, his deeds, and whether he will be a wretched one or a blessed one (in the Hereafter) and then the soul is breathed into him. So one of you may do (good) deeds characteristic of the people of Paradise so much that there is nothing except a cubit between him and Paradise but then what has been written for him decides his behavior and he starts doing (evil) deeds characteristic of the people of Hell (Fire) and (ultimately) enters Hell (Fire); and one of you may do (evil) deeds characteristic of the people of Hell (Fire) so much so that there is nothing except a cubit between him and Hell (Fire), then what has been written for him decides his behavior and he starts doing (good) deeds characteristic of the people of Paradise and ultimately) enters Paradise." (See Hadith No. 430, Vol. 4)

حَدَّثَنَا آدَمُ، حَدَّثَنَا شُعْبَةُ، حَدَّثَنَا الأَعْمَشُ، سَمِعْتُ زَيْدَ بْنَ وَهْبٍ، سَمِعْتُ عَبْدَ اللَّهِ بْنَ مَسْعُودٍ ـ رضى الله عنه ـ حَدَّثَنَا رَسُولُ اللَّهِ صلى الله عليه وسلم وَهْوَ الصَّادِقُ الْمَصْدُوقُ ‏ "‏ إِنَّ خَلْقَ أَحَدِكُمْ يُجْمَعُ فِي بَطْنِ أُمِّهِ أَرْبَعِينَ يَوْمًا وَأَرْبَعِينَ لَيْلَةً، ثُمَّ يَكُونُ عَلَقَةً مِثْلَهُ، ثُمَّ يَكُونُ مُضْغَةً مِثْلَهُ، ثُمَّ يُبْعَثُ إِلَيْهِ الْمَلَكُ فَيُؤْذَنُ بِأَرْبَعِ كَلِمَاتٍ، فَيَكْتُبُ رِزْقَهُ وَأَجَلَهُ وَعَمَلَهُ وَشَقِيٌّ أَمْ سَعِيدٌ ثُمَّ يَنْفُخُ فِيهِ الرُّوحَ، فَإِنَّ أَحَدَكُمْ لَيَعْمَلُ بِعَمَلِ أَهْلِ الْجَنَّةِ، حَتَّى لاَ يَكُونُ بَيْنَهَا وَبَيْنَهُ إِلاَّ ذِرَاعٌ، فَيَسْبِقُ عَلَيْهِ الْكِتَابُ، فَيَعْمَلُ بِعَمَلِ أَهْلِ النَّارِ فَيَدْخُلُ النَّارَ، وَإِنَّ أَحَدَكُمْ لَيَعْمَلُ بِعَمَلِ أَهْلِ النَّارِ، حَتَّى مَا يَكُونُ بَيْنَهَا وَبَيْنَهُ إِلاَّ ذِرَاعٌ فَيَسْبِقُ عَلَيْهِ الْكِتَابُ، فَيَعْمَلُ عَمَلَ أَهْلِ الْجَنَّةِ فَيَدْخُلُهَا ‏" ‏‏.‏

sunnah.com