File: common.c

package info (click to toggle)
xtell 1.91.2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 164 kB
  • ctags: 65
  • sloc: ansic: 962; makefile: 81; sh: 39
file content (76 lines) | stat: -rw-r--r-- 1,717 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
/*
 * ================================================================
 * Please view:
 *
 * 	README for program information.
 * 	COPYING for distribution information.
 *
 * 	based on ident2 by Michael Bacarella (defile@nyct.net)
 *
 * ================================================================ 
 */

#include "xtelld.h"
#include <sys/stat.h>

/* ------------------------------------------------------------------
 * get_request	:	a fgets for file descriptors 
 * ------------------------------------------------------------------
 */
short int get_request(int d, char buffer[], unsigned short int len)
{
    unsigned short i;
    char ch;

    memset(buffer, 0, len);
    for (i = 0; i < len; i++) {
	if (read(d, &ch, 1) != 1)
	    return -1;
	else if (ch == '\n')
	    break;
	else if (ch == '\r') {
	    read(d, &ch, 1);	/* it better be \n */
	    break;
	} else
	    buffer[i] = ch;
    }
    buffer[i] = '\0';
    return i;
}

FILE *logFILE(const char *fromluser)
{
    FILE *file;
    char *path;
    struct passwd *passs;
    struct stat s;

    if ((passs = getpwnam(fromluser)) != NULL) {
	path = malloc(strlen(passs->pw_dir) + sizeof(LOGFILE) + 1);
	if (path == NULL)
	    return NULL;
	strcpy(path, passs->pw_dir);
	strcat(path, "/");
	strcat(path, LOGFILE);
        if (stat(path, &s)==0) {
        if (S_ISREG(s.st_mode)) {
	if ((file = fopen(path, "a")) != NULL) {
            free (path);
	    return file;
	}}}
	free(path);
    }
    return NULL;
}


/* ------------------------------------------------------------------
 * killsock:
 *	violently kills a socket
 * ------------------------------------------------------------------
 */
void killsock(int s)
{
    shutdown(s, 2);
    close(s);
}