Both are dopamine reuptake inhibitors, both also moderately affect norepinephrine. Take for example Wikipedia's description of the pharmocodynamics of these drugs:
For modafinil:
The exact mechanism of action is unclear, although in vitro studies have shown it to inhibit the reuptake of dopamine. While co-administration of a dopamine antagonist decreases the stimulant effect of amphetamine, it does not negate the wakefulness-promoting actions of modafinil. Modafinil activates glutaminergic circuits while inhibiting GABA. Modafinil is thought to have less potential for abuse than other stimulants due to the absence of any significant euphoric or pleasurable effects.
The central stimulating effect of modafinil shows dose and time-related features. The effect tends to be enhanced by chlorination but reduced by methylation. Modafinil blocks the reuptake of norepinephrine by the noradrenergic terminals on sleep-promoting neurons from the ventrolateral preoptic nucleus (VLPO). Such a mechanism could be at least partially responsible for the wake-promoting effect of modafinil.
For cocaine:
One significiant effect of cocaine on the central nervous system is the blockage of the dopamine transporter protein (DAT), hence cocaine is called a dopamine reuptake inhibitor. Brain regions that are rich with dopaminergic neurons are the ventral tegmental area (VTA), the nucleus accumbens and the prefrontal cortex.
A monoamine transmitter that is released by a neuron for signal firing is normally recycled via the transporter to terminate the signal and to spare transmitter resources. The transporter binds the transmitter and pumps it out of the synaptic cleft back into the pre-synaptic neuron. There it is taken up into storage vesicles. Cocaine binds tightly at the DAT forming a complex that blocks the transporter's function, this also blocks the reuptake of the transmitter. Once released into the extracellular space (synaptic cleft) dopamine accumulates there, because the recycling mechanism is inhibited by the cocaine. This results in an enhanced and prolonged firing (boosted signal transduction).
Cocaine is also a less potent blocker of the norepinephrine transporter (NET) and serotonin transporter (SERT).
So the bottom line is that the cognitive enhancing effects come from the increased dopamine and norepinephrine (though modafinil does have other routes also and doesn't seem to have the euphoric effect). The only difference I can currently come up with is that modafinil is just many times milder cocaine but in principle the same infamous drug everyone claims to be as one of the main killers of brain cells. So anyone with more prfound insight into psychodrugs could explain why I shouldn't be afraid to take modafinil. PS. I could have also replaced cocaine with amphetamine as their mechanisms are similiar.