libc.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifdef RT_USING_MUSL
  41. #if !defined(RT_USING_MLIB)
  42. int *__errno_location(void)
  43. {
  44. return _rt_errno();
  45. }
  46. #endif
  47. int *___errno_location(void)
  48. {
  49. return _rt_errno();
  50. }
  51. #endif
  52. int env_set(int argc, char** argv)
  53. {
  54. switch (argc)
  55. {
  56. case 1:
  57. {
  58. int index;
  59. /* show all of environment variables */
  60. for(index = 0; __environ[index]!=NULL; index++)
  61. {
  62. printf("%2d.%s\n", index, __environ[index]);
  63. }
  64. }
  65. break;
  66. case 2:
  67. {
  68. char *c = strchr(argv[1], '=');
  69. if (c)
  70. {
  71. /* use setenv to add/update environment variable */
  72. *c = '\0';
  73. setenv(argv[1], c + 1, 1);
  74. }
  75. else
  76. {
  77. const char *value = getenv(argv[1]);
  78. if (value)
  79. {
  80. printf("%s=%s\n", argv[1], value);
  81. }
  82. }
  83. }
  84. break;
  85. default:
  86. break;
  87. }
  88. return 0;
  89. }
  90. MSH_CMD_EXPORT_ALIAS(env_set, set, set or show environment variable);