File: ccc.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 (93 lines) | stat: -rw-r--r-- 1,657 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "chinese.h"

CMSG msg;
char path[256];

#ifndef CHSYS
#error "CHSYS not defined"
#endif

char **getopts(int *argc,char *argv[])
{
  while(argv[1][0] == '-')
    {
      if (strncmp(argv[1]+1,"h",1)==0)
	{
	  printf("Usage : ccc [-wx <num>] [-wy <num>]\n");
	  exit(0);
	}
      if (strncmp(argv[1]+1,"wx",2)==0)
	{
	  msg.width_of_screen = atoi(argv[1]+3);

	}
      else if (strncmp(argv[1]+1,"wy",2)==0)
	{
	  msg.lines_of_screen = atoi(argv[1]+3);
	}
      else if (argv[1][1] == 'k')
	{
	  if (msg.uid)
	    {
	      printf("Only a root user can kill daemon\n");
	      exit(0);
	    }
	  msg.type = CHTTY_KILL;
	}
      *argc--;
      argv++;
    }
  return argv;
}
load_config()
{
  FILE *in;
  in = fopen(CHSYS"ccc.conf","r");
  if (in == NULL)
    return;
  fscanf(in,"%d %d",&msg.width_of_screen,&msg.lines_of_screen);
  fclose(in);
}

main(int argc,char **argv)
{
  int link,i;
  char *ttys = ttyname(0);
  msg.type = CHTTY_OPENTTY;
  msg.lines_of_screen = 29;
  msg.width_of_screen = 80;
  getwd(path);
  msg.tty = ttys[8]-'1';
  msg.length_of_home = strlen(path);
  msg.uid = geteuid();
  msg.pid = getpid();
  msg.pgrp = getpgrp();
  load_config();
  argv = getopts(&argc,argv);

  alarm(20);
  close(0);
  close(1);
  close(2);
  link = open("/dev/cin",O_WRONLY);

  if (link < 0)
    {
      perror("Can't open /dev/cin to link to server\n");
      exit(1);
    }
  write(link,&msg,sizeof(CMSG));
  write(link,path,msg.length_of_home);
  if (msg.type == CHTTY_KILL)
    exit(0);
  /* wait chinese server to wake up me */
  alarm(0);
  pause();

}