source: rtems/cpukit/libmisc/shell/main_cmdchmod.c @ 255fe43

Last change on this file since 255fe43 was 255fe43, checked in by Joel Sherrill <joel@…>, on 03/01/22 at 20:40:44

cpukit/: Scripted embedded brains header file clean up

Updates #4625.

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