home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1621 / misc.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  1KB  |  77 lines

  1. /* Copyright 1990, Daniel J. Bernstein. All rights reserved. */
  2.  
  3. #include "config.h"
  4. #include <strings.h>
  5. #include <stdio.h>
  6. #include <pwd.h>
  7. extern char *getenv(); /* grrrr */
  8. extern char *ttyname(); /* grrrr */
  9. #include "pty.h"
  10. #include "misc.h"
  11.  
  12. int sessdir()
  13. {
  14.  char suid[10];
  15.  char *home;
  16.  
  17.  if (flagxsetuid)
  18.   {
  19.    if (chdir(PTYDIR) == -1)
  20.      return -1;
  21.   }
  22.  else
  23.   {
  24.    if ((home = getenv("HOME")) == 0)
  25.      return -1;
  26.    else if (chdir(home) == -1)
  27.      return -1;
  28.    else if ((chdir(".pty") == -1)
  29.           &&((mkdir(".pty",0700) == -1)
  30.            ||(chdir(".pty") == -1)))
  31.           return -1;
  32.   }
  33.  
  34.  (void) sprintf(suid,"%d",uid);
  35.  if ((chdir(suid) == -1)
  36.    &&((mkdir(suid,0700) == -1)
  37.     ||(chdir(suid) == -1)))
  38.    return -1;
  39.  return 0;
  40. }
  41.  
  42. char *real_ttyname(fd)
  43. int fd; /* first guess; should be /dev/tty */
  44. {
  45.  char *ttyn;
  46.  
  47.  if ((ttyn = ttyname(fd))
  48.    &&(strcmp(ttyn,"/dev/tty")))
  49.   {
  50.    /* This would actually happen if opening /dev/tty converted into */
  51.    /* opening the actual terminal device---which would be nice. */
  52.    return ttyn;
  53.   }
  54.  /* So much for nice tries. */
  55.  for (fd = getdtablesize();fd >= 0;fd--)
  56.   {
  57.    if ((ttyn = ttyname(fd))
  58.      &&(strcmp(ttyn,"/dev/tty")))
  59.      return ttyn;
  60.   }
  61.  return (char *) 0;
  62. }
  63.  
  64. static struct passwd *pw;
  65. static char nopwun[12];
  66.  
  67. void setusername()
  68. {
  69.  if (pw = getpwuid(uid))
  70.    username = pw->pw_name;
  71.  else
  72.   {
  73.    (void) sprintf(nopwun,"%d",uid);
  74.    username = nopwun;
  75.   }
  76. }
  77.