As CesarB noted, Unix disables the bits set in the umask process, so you must temporarily disable umask to gain full access.
mode_t oldmask = umask(0); fd = open(...); oldmask = umask(oldmask); assert(oldmask == 0);
(OK, you do not need to make this statement, it does not work.)
As Pistos noted, creating files in / tmp is a fraught process. If you think the file does not exist yet, add O_EXCL to prevent symbolic links from appearing in unexpected places.
One last point - why are you making an executable file? I think you should strive only for resolution 666, not 777 or 766. You certainly should not run a program that others can change at any time (therefore, the owner should not have permission to execute a file that others can write) , and the group members would probably also not appreciate the generosity. Others may perhaps get what they deserve if they execute the file, but still not very nice.
source share