File: Handle.c

package info (click to toggle)
mysecureshell 2.00%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 888 kB
  • sloc: ansic: 7,421; sh: 700; perl: 264; makefile: 116
file content (125 lines) | stat: -rw-r--r-- 2,872 bytes parent folder | download | duplicates (4)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 MySecureShell permit to add restriction to modified sftp-server
 when using MySecureShell as shell.
 Copyright (C) 2007-2014 MySecureShell Team

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation (version 2)

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "../config.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "Handle.h"
#include "../Core/security.h"

/*@null@*/ static tHandle *gHandle = NULL;

void HandleInit()
{
	int i;

	gHandle = calloc(HANDLE_NUMBER, sizeof(*gHandle));
	if (gHandle != NULL)
		for (i = 0; i < HANDLE_NUMBER; i++)
		{
			gHandle[i].id = i;
			gHandle[i].fd = -1;
		}
}

void HandleCloseAll()
{
	if (gHandle != NULL)
	{
		int i;

		for (i = 0; i < HANDLE_NUMBER; i++)
			if (gHandle[i].state != HANDLE_UNUSED)
				HandleClose(i);
		free(gHandle);
		gHandle = NULL;
	}
}

tHandle *HandleNew(int state, char *path, int fd, DIR *dir, int fileIsText, int flags)
{
	int i;

	if (gHandle != NULL)
		for (i = 0; i < HANDLE_NUMBER; i++)
			if (gHandle[i].state == HANDLE_UNUSED)
			{
				gHandle[i].state = state;
				gHandle[i].dir = dir;
				gHandle[i].fd = fd;
				gHandle[i].path = path;
				gHandle[i].fileIsText = fileIsText;
				gHandle[i].flags = flags;
				return (&gHandle[i]);
			}
	return (NULL);
}

tHandle *HandleGet(int pos)
{
	if (gHandle != NULL && pos >= 0 && pos < HANDLE_NUMBER)
		return (&gHandle[pos]);
	return (NULL);
}

tHandle *HandleGetFile(int pos)
{
	if (gHandle != NULL && pos >= 0 && pos < HANDLE_NUMBER && gHandle[pos].state == HANDLE_FILE)
		return (&gHandle[pos]);
	return (NULL);
}

tHandle *HandleGetDir(int pos)
{
	if (gHandle != NULL && pos >= 0 && pos < HANDLE_NUMBER && gHandle[pos].state == HANDLE_DIR)
		return (&gHandle[pos]);
	return (NULL);
}

tHandle *HandleGetLastOpen(int state)
{
	tHandle *lastHdl = NULL;
	int i;

	if (gHandle != NULL)
		for (i = 0; i < HANDLE_NUMBER; i++)
			if (gHandle[i].state == state)
				lastHdl = &gHandle[i];
	return (lastHdl);
}

void HandleClose(int pos)
{
	if (gHandle != NULL && pos >= 0 && pos < HANDLE_NUMBER)
	{
		if (gHandle[pos].state == HANDLE_DIR)
			(void) closedir(gHandle[pos].dir);
		else
			xclose(gHandle[pos].fd);
		free(gHandle[pos].path);

		gHandle[pos].dir = NULL;
		gHandle[pos].fd = -1;
		gHandle[pos].path = NULL;
		gHandle[pos].state = HANDLE_UNUSED;
	}
}