File: ptyopen.c

package info (click to toggle)
chdrv 1.0.13-0.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,204 kB
  • ctags: 1,069
  • sloc: ansic: 8,972; perl: 412; asm: 234; makefile: 195; sh: 36
file content (73 lines) | stat: -rw-r--r-- 1,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
   The below code is adapted from Steven's book.I have midified it slightly
*/

/* I can't compile on my machine (Potato, glibc 2.1, kernel 2.2.5-ac1)
 * without including stdio.h */
#include    <stdio.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<errno.h>
#include	<fcntl.h>
#include	<grp.h>


int ptym_open(char *pts_name)
{
  int		fdm;
  char	*ptr1, *ptr2;
  
  strcpy(pts_name, "/dev/ptyp0");
  /* array index: 0123456789 (for references in following code) */
  for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) 
    {
      pts_name[9] = *ptr2;
    
      /* try to open master */
      if ( (fdm = open(pts_name, O_RDWR)) < 0) 
	{
	  if (errno == ENOENT)	/* different from EIO */
	    return(-1);			/* out of pty devices */
	  else
	    continue;			/* try next pty device */
	}
    
      pts_name[5] = 't';	/* change "pty" to "tty" */
      return(fdm);		/* got it, return fd of master */
    }
  
  return(-1);		/* out of pty devices */
}


int ptys_open(int fdm, char *pts_name)
{

  struct group	*grptr;
  int	gid, fds;
  
  if ( (grptr = getgrnam("tty")) != NULL)
    gid = grptr->gr_gid;
  else
    gid = -1;		/* group tty is not in the group file */
  
  /* following two functions don't work unless we're root */
  chown(pts_name, getuid(), gid);
  chmod(pts_name, S_IRUSR | S_IWUSR | S_IWGRP);
  
  if ( (fds = open(pts_name, O_RDWR)) < 0) 
    {
      return(-1);
    }

  return(fds);
}