# ctermid

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           ctermid — generate a pathname for the controlling terminal
    


## SYNOPSIS



           #include <stdio.h>
    
           char *ctermid(char *_s_);
    


## DESCRIPTION



           The _ctermid_() function shall generate a string that, when used as
           a pathname, refers to the current controlling terminal for the
           current process. If _ctermid_() returns a pathname, access to the
           file is not guaranteed.
    
           The _ctermid_() function need not be thread-safe if called with a
           NULL parameter.
    


## RETURN VALUE



           If _s_ is a null pointer, the string shall be generated in an area
           that may be static, the address of which shall be returned. The
           application shall not modify the string returned. The returned
           pointer might be invalidated or the string content might be
           overwritten by a subsequent call to _ctermid_().  The returned
           pointer might also be invalidated if the calling thread is
           terminated.  If _s_ is not a null pointer, _s_ is assumed to point to
           a character array of at least L_ctermid bytes; the string is
           placed in this array and the value of _s_ shall be returned. The
           symbolic constant L_ctermid is defined in _< stdio.h>_, and shall
           have a value greater than 0.
    
           The _ctermid_() function shall return an empty string if the
           pathname that would refer to the controlling terminal cannot be
           determined, or if the function is unsuccessful.
    


## ERRORS



           No errors are defined.
    
           _The following sections are informative._
    


## EXAMPLES



       **Determining the Controlling Terminal for the Current Process**
           The following example returns a pointer to a string that
           identifies the controlling terminal for the current process. The
           pathname for the terminal is stored in the array pointed to by the
           _ptr_ argument, which has a size of L_ctermid bytes, as indicated by
           the _term_ argument.
    
               #include <stdio.h>
               ...
               char term[L_ctermid];
               char *ptr;
    
               ptr = ctermid(term);
    


## APPLICATION USAGE



           The difference between _ctermid_() and _ttyname_() is that _ttyname_()
           must be handed a file descriptor and return a path of the terminal
           associated with that file descriptor, while _ctermid_() returns a
           string (such as **"/dev/tty"**) that refers to the current controlling
           terminal if used as a pathname.
    


## RATIONALE



           L_ctermid must be defined appropriately for a given implementation
           and must be greater than zero so that array declarations using it
           are accepted by the compiler. The value includes the terminating
           null byte.
    
           Conforming applications that use multiple threads cannot call
           _ctermid_() with NULL as the parameter. If _s_ is not NULL, the
           _ctermid_() function generates a string that, when used as a
           pathname, refers to the current controlling terminal for the
           current process. If _s_ is NULL, the return value of _ctermid_() is
           undefined.
    
           There is no additional burden on the programmer—changing to use a
           hypothetical thread-safe version of _ctermid_() along with
           allocating a buffer is more of a burden than merely allocating a
           buffer. Application code should not assume that the returned
           string is short, as some implementations have more than two
           pathname components before reaching a logical device name.
    


## FUTURE DIRECTIONS



           None.
    


* * *