source: rtems/cpukit/libmisc/shell/main_cmdchmod.c @ 3fe5d01

4.115
Last change on this file since 3fe5d01 was 3fe5d01, checked in by Sebastian Huber <sebastian.huber@…>, on 11/18/14 at 13:05:15

shell: Add CMDLS, CMDCHOWN, CMDCHMOD commands

  • Property mode set to 100644
File size: 1.8 KB
Line 
1/*
2 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#if HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include <errno.h>
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
23
24#include <rtems/shellconfig.h>
25#include <rtems/stringto.h>
26
27#include "internal.h"
28
29static int usage(void)
30{
31  puts(rtems_shell_CMDCHMOD_Command.usage);
32
33  return -1;
34}
35
36static void error(const char *s, int eno)
37{
38  printf("%s: %s\n", s, strerror(eno));
39}
40
41static int rtems_shell_main_cmdchmod(int argc, char **argv)
42{
43  if (argc >= 2) {
44    unsigned long mode;
45    rtems_status_code sc;
46    uid_t task_uid;
47    int i;
48
49    sc = rtems_string_to_unsigned_long(argv[1], &mode, NULL, 8);
50    if (sc != RTEMS_SUCCESSFUL) {
51      return usage();
52    }
53
54    task_uid = getuid();
55
56    for (i = 2; i < argc; ++i) {
57      const char *cmd = argv[i];
58      rtems_shell_cmd_t *shell_cmd = rtems_shell_lookup_cmd(cmd);
59
60      if (shell_cmd != NULL) {
61        if (task_uid == 0 || task_uid == shell_cmd->uid) {
62          shell_cmd->mode = mode
63            & (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
64        } else if (rtems_shell_can_see_cmd(shell_cmd)) {
65          error(cmd, EACCES);
66        } else {
67          error(cmd, ENOENT);
68        }
69      } else {
70        error(cmd, ENOENT);
71      }
72    }
73  } else {
74    return usage();
75  }
76
77  return 0;
78}
79
80rtems_shell_cmd_t rtems_shell_CMDCHMOD_Command = {
81  .name = "cmdchmod",
82  .usage = "cmdchmod OCTAL-MODE COMMAND...",
83  .topic = "misc",
84  .command = rtems_shell_main_cmdchmod
85};
Note: See TracBrowser for help on using the repository browser.