Removed next_command.c.
[uftps.git] / control.c
blob051fdcc4c716276d167cc7e82eb06896e2046314
1 /*
2 * User FTP Server, Share folders over FTP without being root.
3 * Copyright (C) 2008 Isaac Jurado
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
8 * version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "uftps.h"
21 #include <errno.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
27 #include "command_parser.h"
30 static void read_command (void)
32 int l, i, b;
34 i = 0;
35 l = SS.input_len;
36 if (SS.input_offset > 0)
37 memmove(SS.input, SS.input + SS.input_offset, l);
39 do {
40 while (i < l && SS.input[i] != '\n')
41 i++;
42 if (SS.input[i] == '\n')
44 if (i > 0 && SS.input[i - 1] == '\r')
45 break;
46 else
47 continue;
50 b = read(SS.control_sk, SS.input + l, LINE_SIZE - l);
51 if (b <= 0)
53 if (b == -1)
54 fatal("Control channel input");
55 else if (l == LINE_SIZE)
57 errno = 0;
58 fatal("Input buffer overflow");
60 else
62 notice("Peer closed control connection");
63 exit(EXIT_SUCCESS);
67 l += b;
68 } while (1);
70 SS.input[i - 1] = '\0';
71 SS.input[i] = '\0';
72 i++;
73 SS.input_len = l - i;
74 SS.input_offset = (l - i > 0 ? i : 0);
76 debug("Request '%s'", SS.input);
81 * Return the next command (as an integer) available in the control channel.
83 enum command next_command (void)
85 const struct Cmd *cmd;
86 int i;
88 read_command();
90 i = 0;
91 while (SS.input[i] != ' ' && SS.input[i] != '\0')
93 SS.input[i] = toupper(SS.input[i] & 0x07F);
94 i++;
97 SS.arg = (SS.input[i] == ' ' ? SS.input + i + 1 : NULL);
98 SS.input[i] = '\0';
100 cmd = parse_command(SS.input, i);
101 if (cmd == NULL)
102 return FTP_NONE;
103 return cmd->value;
108 * Send a reply to the peer over the control connection.
110 void reply (const char *str, int len)
112 int b;
114 debug("Reply '%*s'", len - 2, str);
116 do {
117 b = write(SS.control_sk, str, len);
118 if (b == -1)
119 fatal("Control channel output");
121 str += b;
122 len -= b;
123 } while (len > 0);