Raymond Toy pushed to branch issue-364-add-mkstemp-mkdtemp at cmucl / cmucl

Commits:

1 changed file:

Changes:

  • src/code/unix.lisp
    ... ... @@ -2902,13 +2902,30 @@
    2902 2902
     	c-string))
    
    2903 2903
     
    
    2904 2904
     (defun unix-mkstemp (template)
    
    2905
    -  _N"Generates a unique temporary file name from TEMPLATE, creates and
    
    2906
    - opens the file and returns a file stream for the file.
    
    2905
    +  _N"Generates a unique temporary file name from TEMPLATE, and creates
    
    2906
    +  and opens the file.  On success, the corresponding file descriptor
    
    2907
    +  and name of the file is returned.
    
    2907 2908
     
    
    2908 2909
      The last six characters of the template must be \"XXXXXX\"."
    
    2909
    -  (syscall ("mkstemp" c-call:c-string)
    
    2910
    -	   result
    
    2911
    -	   (copy-seq template)))
    
    2910
    +  ;; Hope this buffer is large enough!
    
    2911
    +  (let ((octets (%name->file template)))
    
    2912
    +    (unless (< (length octets) 8192)
    
    2913
    +      (error "Internal buffer is too small for encoded file name of ~D octets"
    
    2914
    +	     (length octets)))
    
    2915
    +    (with-alien ((buf (array c-call:unsigned-char 8192)))
    
    2916
    +      ;; Convert the Lisp string and copy it to the alien buffer, being
    
    2917
    +      ;; sure to zero-terminate the buffer.
    
    2918
    +      (loop for k from 0
    
    2919
    +	    for c across octets
    
    2920
    +	    do
    
    2921
    +	       (setf (deref buf k) (char-code c))
    
    2922
    +	    finally (setf (deref buf k) 0))
    
    2923
    +
    
    2924
    +      (syscall ("mkstemp" (* c-call:unsigned-char))
    
    2925
    +	       (values result
    
    2926
    +		       ;; Convert the file name back to a Lisp string.
    
    2927
    +		       (%file->name (cast buf c-call:c-string)))
    
    2928
    +	       (cast buf (* c-call:unsigned-char))))))
    
    2912 2929
     
    
    2913 2930
     (defun unix-mkdtemp (template)
    
    2914 2931
       _N"Generate a uniquely named temporary directory from Template,