Advertisement
parkdream1

ProFTPD

Mar 28th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.19 KB | None | 0 0
  1. proftpd/src/help.c
  2. /*
  3.  * ProFTPD - FTP server daemon
  4.  * Copyright (c) 2004-2009 The ProFTPD Project team
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
  19.  *
  20.  * As a special exemption, The ProFTPD Project team and other respective
  21.  * copyright holders give permission to link this program with OpenSSL, and
  22.  * distribute the resulting executable, without including the source code for
  23.  * OpenSSL in the source distribution.
  24.  */
  25.  
  26. /* HELP management code
  27.  * $Id: help.c,v 1.5 2009/06/30 23:31:18 castaglia Exp $
  28.  */
  29.  
  30. #include "conf.h"
  31.  
  32. struct help_rec {
  33.   const char *cmd;
  34.   const char *syntax;
  35.   int impl;
  36. };
  37.  
  38. static pool *help_pool = NULL;
  39. static array_header *help_list = NULL;
  40.  
  41. void pr_help_add(const char *cmd, const char *syntax, int impl) {
  42.   struct help_rec *help;
  43.  
  44.   if (!cmd || !syntax)
  45.     return;
  46.  
  47.   /* If no list has been allocated, create one. */
  48.   if (!help_pool) {
  49.     help_pool = make_sub_pool(permanent_pool);
  50.     pr_pool_tag(help_pool, "Help Pool");
  51.     help_list = make_array(help_pool, 0, sizeof(struct help_rec));
  52.   }
  53.  
  54.   /* Make sure that the command being added isn't already in the list.
  55.    * However, if it _is_ already in the list, but it's marked as not
  56.    * implemented, _and_ the given impl flag is TRUE, then handle it
  57.    * accordingly.
  58.    */
  59.   if (help_list->nelts > 0) {
  60.     register unsigned int i = 0;
  61.     struct help_rec *helps = help_list->elts;
  62.  
  63.     for (i = 0; i < help_list->nelts; i++)
  64.       if (strcmp(helps[i].cmd, cmd) == 0) {
  65.         if (helps[i].impl == FALSE &&
  66.             impl == TRUE) {
  67.           helps[i].impl = impl;
  68.         }
  69.  
  70.         return;
  71.       }
  72.   }
  73.  
  74.   help = push_array(help_list);
  75.   help->cmd = pstrdup(help_pool, cmd);
  76.   help->syntax = pstrdup(help_pool, syntax);
  77.   help->impl = impl;
  78. }
  79.  
  80. int pr_help_add_response(cmd_rec *cmd, const char *target) {
  81.   if (help_list) {
  82.     register unsigned int i;
  83.     struct help_rec *helps = help_list->elts;
  84.     char *outa[8], *outstr;
  85.     char buf[9] = {'\0'};
  86.     int col = 0;
  87.  
  88.     if (!target) {
  89.       pr_response_add(R_214,
  90.         _("The following commands are recognized (* =>'s unimplemented):"));
  91.  
  92.       memset(outa, '\0', sizeof(outa));
  93.  
  94.       for (i = 0; i < help_list->nelts; i++) {
  95.         outstr = "";
  96.  
  97.         if (helps[i].impl)
  98.           outa[col++] = (char *) helps[i].cmd;
  99.         else
  100.           outa[col++] = pstrcat(cmd->tmp_pool, helps[i].cmd, "*", NULL);
  101.  
  102.         /* 8 rows */
  103.         if ((i + 1) % 8 == 0 ||
  104.             helps[i+1].cmd == NULL) {
  105.           register unsigned int j;
  106.  
  107.           for (j = 0; j < 8; j++) {
  108.             if (outa[j]) {
  109.               snprintf(buf, sizeof(buf), "%-8s", outa[j]);
  110.               buf[sizeof(buf)-1] = '\0';
  111.               outstr = pstrcat(cmd->tmp_pool, outstr, buf, NULL);
  112.  
  113.             } else
  114.               break;
  115.           }
  116.  
  117.           if (*outstr)
  118.             pr_response_add(R_DUP, "%s", outstr);
  119.  
  120.           memset(outa, '\0', sizeof(outa));
  121.           col = 0;
  122.           outstr = "";
  123.         }
  124.       }
  125.  
  126.       pr_response_add(R_DUP, _("Direct comments to %s"),
  127.         cmd->server->ServerAdmin ? cmd->server->ServerAdmin : "ftp-admin");
  128.  
  129.     } else {
  130.  
  131.       /* List the syntax for the given target command. */
  132.       for (i = 0; i < help_list->nelts; i++) {
  133.         if (strcasecmp(helps[i].cmd, target) == 0) {
  134.           pr_response_add(R_214, "Syntax: %s %s", helps[i].cmd,
  135.             helps[i].syntax);
  136.           return 0;
  137.         }
  138.       }
  139.     }
  140.  
  141.     errno = ENOENT;
  142.     return -1;
  143.   }
  144.  
  145.   errno = ENOENT;
  146.   return -1;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement