libc.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017/10/15 bernard the first version
  9. */
  10. #include <rtthread.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <fcntl.h>
  14. #include <sys/time.h>
  15. #include "libc.h"
  16. int _EXFUN(putenv,(char *__string));
  17. extern char **__environ;
  18. int libc_system_init(void)
  19. {
  20. #if defined(RT_USING_DFS) & defined(RT_USING_DFS_DEVFS)
  21. #if defined(RT_USING_CONSOLE)
  22. rt_device_t dev_console;
  23. dev_console = rt_console_get_device();
  24. if (dev_console)
  25. {
  26. #if defined(RT_USING_POSIX)
  27. libc_stdio_set_console(dev_console->parent.name, O_RDWR);
  28. #else
  29. libc_stdio_set_console(dev_console->parent.name, O_WRONLY);
  30. #endif
  31. }
  32. #endif
  33. /* set PATH and HOME */
  34. putenv("PATH=/bin");
  35. putenv("HOME=/home");
  36. #endif
  37. return 0;
  38. }
  39. INIT_COMPONENT_EXPORT(libc_system_init);
  40. int env_set(int argc, char** argv)
  41. {
  42. switch (argc)
  43. {
  44. case 1:
  45. {
  46. int index;
  47. /* show all of environment variables */
  48. for(index = 0; __environ[index]!=NULL; index++)
  49. {
  50. printf("%2d.%s\n", index, __environ[index]);
  51. }
  52. }
  53. break;
  54. case 2:
  55. {
  56. char *c = strchr(argv[1], '=');
  57. if (c)
  58. {
  59. /* use setenv to add/update environment variable */
  60. *c = '\0';
  61. setenv(argv[1], c + 1, 1);
  62. }
  63. else
  64. {
  65. const char *value = getenv(argv[1]);
  66. if (value)
  67. {
  68. printf("%s=%s\n", argv[1], value);
  69. }
  70. }
  71. }
  72. break;
  73. default:
  74. break;
  75. }
  76. return 0;
  77. }
  78. MSH_CMD_EXPORT_ALIAS(env_set, set, set or show environment variable);