msh_file.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-09-25 Bernard the first verion for FinSH
  9. * 2021-06-09 Meco Man implement tail command
  10. */
  11. #include <rtthread.h>
  12. #if defined(RT_USING_FINSH) && defined(DFS_USING_POSIX)
  13. #include <finsh.h>
  14. #include "msh.h"
  15. #include <dfs_file.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #ifdef RT_USING_DFS_V2
  19. #include <dfs_mnt.h>
  20. #endif
  21. static int msh_readline(int fd, char *line_buf, int size)
  22. {
  23. char ch;
  24. int index = 0;
  25. do
  26. {
  27. if (read(fd, &ch, 1) != 1)
  28. {
  29. /* nothing in this file */
  30. return 0;
  31. }
  32. }
  33. while (ch == '\n' || ch == '\r');
  34. /* set the first character */
  35. line_buf[index ++] = ch;
  36. while (index < size)
  37. {
  38. if (read(fd, &ch, 1) == 1)
  39. {
  40. if (ch == '\n' || ch == '\r')
  41. {
  42. line_buf[index] = '\0';
  43. break;
  44. }
  45. line_buf[index++] = ch;
  46. }
  47. else
  48. {
  49. line_buf[index] = '\0';
  50. break;
  51. }
  52. }
  53. return index;
  54. }
  55. int msh_exec_script(const char *cmd_line, int size)
  56. {
  57. int ret;
  58. int fd = -1;
  59. char *pg_name;
  60. int length, cmd_length = 0;
  61. if (size == 0) return -RT_ERROR;
  62. /* get the length of command0 */
  63. while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
  64. cmd_length ++;
  65. /* get name length */
  66. length = cmd_length + 32;
  67. /* allocate program name memory */
  68. pg_name = (char *) rt_malloc(length);
  69. if (pg_name == RT_NULL) return -RT_ENOMEM;
  70. /* copy command0 */
  71. rt_memcpy(pg_name, cmd_line, cmd_length);
  72. pg_name[cmd_length] = '\0';
  73. if (strstr(pg_name, ".sh") != RT_NULL || strstr(pg_name, ".SH") != RT_NULL)
  74. {
  75. /* try to open program */
  76. fd = open(pg_name, O_RDONLY, 0);
  77. /* search in /bin path */
  78. if (fd < 0)
  79. {
  80. rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
  81. fd = open(pg_name, O_RDONLY, 0);
  82. }
  83. }
  84. rt_free(pg_name);
  85. if (fd >= 0)
  86. {
  87. /* found script */
  88. char *line_buf;
  89. int length;
  90. line_buf = (char *) rt_malloc(RT_CONSOLEBUF_SIZE);
  91. if (line_buf == RT_NULL)
  92. {
  93. close(fd);
  94. return -RT_ENOMEM;
  95. }
  96. /* read line by line and then exec it */
  97. do
  98. {
  99. length = msh_readline(fd, line_buf, RT_CONSOLEBUF_SIZE);
  100. if (length > 0)
  101. {
  102. char ch = '\0';
  103. int index;
  104. for (index = 0; index < length; index ++)
  105. {
  106. ch = line_buf[index];
  107. if (ch == ' ' || ch == '\t') continue;
  108. else break;
  109. }
  110. if (ch != '#') /* not a comment */
  111. msh_exec(line_buf, length);
  112. }
  113. }
  114. while (length > 0);
  115. close(fd);
  116. rt_free(line_buf);
  117. ret = 0;
  118. }
  119. else
  120. {
  121. ret = -1;
  122. }
  123. return ret;
  124. }
  125. #ifdef DFS_USING_WORKDIR
  126. extern char working_directory[];
  127. #endif
  128. static int cmd_ls(int argc, char **argv)
  129. {
  130. extern void ls(const char *pathname);
  131. if (argc == 1)
  132. {
  133. #ifdef DFS_USING_WORKDIR
  134. ls(working_directory);
  135. #else
  136. ls("/");
  137. #endif
  138. }
  139. else
  140. {
  141. ls(argv[1]);
  142. }
  143. return 0;
  144. }
  145. MSH_CMD_EXPORT_ALIAS(cmd_ls, ls, List information about the FILEs.);
  146. #ifdef RT_USING_DFS_V2
  147. static int cmd_ln(int argc, char **argv)
  148. {
  149. if (argc < 3)
  150. {
  151. rt_kprintf("Usage: ln target link_name\n");
  152. rt_kprintf("Make symbolic link between files.\n");
  153. }
  154. else
  155. {
  156. for(int i = 0; i + 3 <= argc; i ++)
  157. {
  158. dfs_file_symlink(argv[1], argv[2 + i]);
  159. }
  160. }
  161. return 0;
  162. }
  163. MSH_CMD_EXPORT_ALIAS(cmd_ln, ln, Make symbolic link between files);
  164. static int cmd_link(int argc, char **argv)
  165. {
  166. if (argc < 3)
  167. {
  168. rt_kprintf("Usage: link target link_name\n");
  169. rt_kprintf("Make link between files.\n");
  170. }
  171. else
  172. {
  173. for(int i = 0; i + 3 <= argc; i ++)
  174. {
  175. dfs_file_link(argv[1], argv[2 + i]);
  176. }
  177. }
  178. return 0;
  179. }
  180. MSH_CMD_EXPORT_ALIAS(cmd_link, link, Make link between files);
  181. #endif
  182. static int cmd_cp(int argc, char **argv)
  183. {
  184. void copy(const char *src, const char *dst);
  185. if (argc != 3)
  186. {
  187. rt_kprintf("Usage: cp SOURCE DEST\n");
  188. rt_kprintf("Copy SOURCE to DEST.\n");
  189. }
  190. else
  191. {
  192. copy(argv[1], argv[2]);
  193. }
  194. return 0;
  195. }
  196. MSH_CMD_EXPORT_ALIAS(cmd_cp, cp, Copy SOURCE to DEST.);
  197. static int cmd_mv(int argc, char **argv)
  198. {
  199. if (argc != 3)
  200. {
  201. rt_kprintf("Usage: mv SOURCE DEST\n");
  202. rt_kprintf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n");
  203. }
  204. else
  205. {
  206. int fd;
  207. char *dest = RT_NULL;
  208. rt_kprintf("%s => %s\n", argv[1], argv[2]);
  209. fd = open(argv[2], O_DIRECTORY, 0);
  210. if (fd >= 0)
  211. {
  212. char *src;
  213. close(fd);
  214. /* it's a directory */
  215. dest = (char *)rt_malloc(DFS_PATH_MAX);
  216. if (dest == RT_NULL)
  217. {
  218. rt_kprintf("out of memory\n");
  219. return -RT_ENOMEM;
  220. }
  221. src = argv[1] + rt_strlen(argv[1]);
  222. while (src != argv[1])
  223. {
  224. if (*src == '/') break;
  225. src --;
  226. }
  227. rt_snprintf(dest, DFS_PATH_MAX - 1, "%s/%s", argv[2], src);
  228. }
  229. else
  230. {
  231. fd = open(argv[2], O_RDONLY, 0);
  232. if (fd >= 0)
  233. {
  234. close(fd);
  235. unlink(argv[2]);
  236. }
  237. dest = argv[2];
  238. }
  239. rename(argv[1], dest);
  240. if (dest != RT_NULL && dest != argv[2]) rt_free(dest);
  241. }
  242. return 0;
  243. }
  244. MSH_CMD_EXPORT_ALIAS(cmd_mv, mv, Rename SOURCE to DEST.);
  245. static int cmd_cat(int argc, char **argv)
  246. {
  247. int index;
  248. extern void cat(const char *filename);
  249. if (argc == 1)
  250. {
  251. rt_kprintf("Usage: cat [FILE]...\n");
  252. rt_kprintf("Concatenate FILE(s)\n");
  253. return 0;
  254. }
  255. for (index = 1; index < argc; index ++)
  256. {
  257. cat(argv[index]);
  258. }
  259. return 0;
  260. }
  261. MSH_CMD_EXPORT_ALIAS(cmd_cat, cat, Concatenate FILE(s));
  262. static void directory_delete_for_msh(const char *pathname, char f, char v)
  263. {
  264. DIR *dir = NULL;
  265. struct dirent *dirent = NULL;
  266. char *full_path;
  267. if (pathname == RT_NULL)
  268. return;
  269. full_path = (char *)rt_malloc(DFS_PATH_MAX);
  270. if (full_path == RT_NULL)
  271. return;
  272. dir = opendir(pathname);
  273. if (dir == RT_NULL)
  274. {
  275. if (f == 0)
  276. {
  277. rt_kprintf("cannot remove '%s'\n", pathname);
  278. }
  279. rt_free(full_path);
  280. return;
  281. }
  282. while (1)
  283. {
  284. dirent = readdir(dir);
  285. if (dirent == RT_NULL)
  286. break;
  287. if (rt_strcmp(".", dirent->d_name) != 0 &&
  288. rt_strcmp("..", dirent->d_name) != 0)
  289. {
  290. rt_sprintf(full_path, "%s/%s", pathname, dirent->d_name);
  291. if (dirent->d_type != DT_DIR)
  292. {
  293. if (unlink(full_path) != 0)
  294. {
  295. if (f == 0)
  296. rt_kprintf("cannot remove '%s'\n", full_path);
  297. }
  298. else if (v)
  299. {
  300. rt_kprintf("removed '%s'\n", full_path);
  301. }
  302. }
  303. else
  304. {
  305. directory_delete_for_msh(full_path, f, v);
  306. }
  307. }
  308. }
  309. closedir(dir);
  310. rt_free(full_path);
  311. if (rmdir(pathname) != 0)
  312. {
  313. if (f == 0)
  314. rt_kprintf("cannot remove '%s'\n", pathname);
  315. }
  316. else if (v)
  317. {
  318. rt_kprintf("removed directory '%s'\n", pathname);
  319. }
  320. }
  321. static int cmd_rm(int argc, char **argv)
  322. {
  323. int index, n;
  324. char f = 0, r = 0, v = 0;
  325. if (argc == 1)
  326. {
  327. rt_kprintf("Usage: rm option(s) FILE...\n");
  328. rt_kprintf("Remove (unlink) the FILE(s).\n");
  329. return 0;
  330. }
  331. if (argv[1][0] == '-')
  332. {
  333. for (n = 0; argv[1][n]; n++)
  334. {
  335. switch (argv[1][n])
  336. {
  337. case 'f':
  338. f = 1;
  339. break;
  340. case 'r':
  341. r = 1;
  342. break;
  343. case 'v':
  344. v = 1;
  345. break;
  346. case '-':
  347. break;
  348. default:
  349. rt_kprintf("Error: Bad option: %c\n", argv[1][n]);
  350. return 0;
  351. }
  352. }
  353. argc -= 1;
  354. argv = argv + 1;
  355. }
  356. for (index = 1; index < argc; index ++)
  357. {
  358. struct stat s;
  359. #ifdef RT_USING_DFS_V2
  360. if (dfs_file_lstat(argv[index], &s) == 0)
  361. #else
  362. if (stat(argv[index], &s) == 0)
  363. #endif
  364. {
  365. if (s.st_mode & S_IFDIR)
  366. {
  367. if (r == 0)
  368. rt_kprintf("cannot remove '%s': Is a directory\n", argv[index]);
  369. else
  370. directory_delete_for_msh(argv[index], f, v);
  371. }
  372. else if (s.st_mode & S_IFREG)
  373. {
  374. if (unlink(argv[index]) != 0)
  375. {
  376. if (f == 0)
  377. rt_kprintf("cannot remove '%s'\n", argv[index]);
  378. }
  379. else if (v)
  380. {
  381. rt_kprintf("removed '%s'\n", argv[index]);
  382. }
  383. }
  384. }
  385. else if (f == 0)
  386. {
  387. rt_kprintf("cannot remove '%s': No such file or directory\n", argv[index]);
  388. }
  389. }
  390. return 0;
  391. }
  392. MSH_CMD_EXPORT_ALIAS(cmd_rm, rm, Remove(unlink) the FILE(s).);
  393. #ifdef DFS_USING_WORKDIR
  394. static int cmd_cd(int argc, char **argv)
  395. {
  396. if (argc == 1)
  397. {
  398. rt_kprintf("%s\n", working_directory);
  399. }
  400. else if (argc == 2)
  401. {
  402. if (chdir(argv[1]) != 0)
  403. {
  404. rt_kprintf("No such directory: %s\n", argv[1]);
  405. }
  406. }
  407. return 0;
  408. }
  409. MSH_CMD_EXPORT_ALIAS(cmd_cd, cd, Change the shell working directory.);
  410. static int cmd_pwd(int argc, char **argv)
  411. {
  412. rt_kprintf("%s\n", working_directory);
  413. return 0;
  414. }
  415. MSH_CMD_EXPORT_ALIAS(cmd_pwd, pwd, Print the name of the current working directory.);
  416. #endif
  417. static int cmd_mkdir(int argc, char **argv)
  418. {
  419. if (argc == 1)
  420. {
  421. rt_kprintf("Usage: mkdir [OPTION] DIRECTORY\n");
  422. rt_kprintf("Create the DIRECTORY, if they do not already exist.\n");
  423. }
  424. else
  425. {
  426. mkdir(argv[1], 0);
  427. }
  428. return 0;
  429. }
  430. MSH_CMD_EXPORT_ALIAS(cmd_mkdir, mkdir, Create the DIRECTORY.);
  431. static int cmd_mkfs(int argc, char **argv)
  432. {
  433. int result = 0;
  434. char *type = "elm"; /* use the default file system type as 'fatfs' */
  435. if (argc == 2)
  436. {
  437. result = dfs_mkfs(type, argv[1]);
  438. }
  439. else if (argc == 4)
  440. {
  441. if (strcmp(argv[1], "-t") == 0)
  442. {
  443. type = argv[2];
  444. result = dfs_mkfs(type, argv[3]);
  445. }
  446. }
  447. else
  448. {
  449. rt_kprintf("Usage: mkfs [-t type] device\n");
  450. return 0;
  451. }
  452. if (result != RT_EOK)
  453. {
  454. rt_kprintf("mkfs failed, result=%d\n", result);
  455. }
  456. return 0;
  457. }
  458. MSH_CMD_EXPORT_ALIAS(cmd_mkfs, mkfs, format disk with file system);
  459. /*
  460. * If no argument is specified, display the mount history;
  461. * If there are 3 arguments, mount the filesystem.
  462. * The order of the arguments is:
  463. * argv[1]: device name
  464. * argv[2]: mountpoint path
  465. * argv[3]: filesystem type
  466. */
  467. static int cmd_mount(int argc, char **argv)
  468. {
  469. if (argc == 1)
  470. {
  471. #ifdef RT_USING_DFS_V2
  472. /* display the mount history */
  473. rt_kprintf("filesystem device mountpoint refcount\n");
  474. rt_kprintf("---------- ------ ---------- --------\n");
  475. dfs_mnt_list(RT_NULL);
  476. #else
  477. extern struct dfs_filesystem filesystem_table[];
  478. struct dfs_filesystem *iter;
  479. /* display the mount history */
  480. rt_kprintf("filesystem device mountpoint\n");
  481. rt_kprintf("---------- ------ ----------\n");
  482. for (iter = &filesystem_table[0];
  483. iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
  484. {
  485. if ((iter != NULL) && (iter->path != NULL))
  486. {
  487. rt_kprintf("%-10s %-6s %-s\n",
  488. iter->ops->name, iter->dev_id->parent.name, iter->path);
  489. }
  490. }
  491. #endif
  492. return 0;
  493. }
  494. else if (argc == 4)
  495. {
  496. char *device = argv[1];
  497. char *path = argv[2];
  498. char *fstype = argv[3];
  499. char *data = 0;
  500. /* mount a filesystem to the specified directory */
  501. rt_kprintf("mount device %s(%s) onto %s ... ", device, fstype, path);
  502. if (strcmp(fstype, "nfs") == 0)
  503. {
  504. data = argv[1];
  505. device = 0;
  506. }
  507. if (dfs_mount(device, path, fstype, 0, data) == 0)
  508. {
  509. rt_kprintf("succeed!\n");
  510. return 0;
  511. }
  512. else
  513. {
  514. rt_kprintf("failed!\n");
  515. return -1;
  516. }
  517. }
  518. else if (argc == 3)
  519. {
  520. char *path = argv[1];
  521. char *fstype = argv[2];
  522. /* mount a filesystem to the specified directory */
  523. rt_kprintf("mount (%s) onto %s ... ", fstype, path);
  524. if (dfs_mount(NULL, path, fstype, 0, 0) == 0)
  525. {
  526. rt_kprintf("succeed!\n");
  527. return 0;
  528. }
  529. else
  530. {
  531. rt_kprintf("failed!\n");
  532. return -1;
  533. }
  534. }
  535. else
  536. {
  537. rt_kprintf("Usage: mount <device> <mountpoint> <fstype>.\n");
  538. return -1;
  539. }
  540. }
  541. MSH_CMD_EXPORT_ALIAS(cmd_mount, mount, mount <device> <mountpoint> <fstype>);
  542. /* unmount the filesystem from the specified mountpoint */
  543. static int cmd_umount(int argc, char **argv)
  544. {
  545. char *path = argv[1];
  546. if (argc != 2)
  547. {
  548. rt_kprintf("Usage: unmount <mountpoint>.\n");
  549. return -1;
  550. }
  551. rt_kprintf("unmount %s ... ", path);
  552. if (dfs_unmount(path) < 0)
  553. {
  554. rt_kprintf("failed!\n");
  555. return -1;
  556. }
  557. else
  558. {
  559. rt_kprintf("succeed!\n");
  560. return 0;
  561. }
  562. }
  563. MSH_CMD_EXPORT_ALIAS(cmd_umount, umount, Unmount the mountpoint);
  564. static int cmd_df(int argc, char **argv)
  565. {
  566. #ifndef RT_USING_DFS_V2
  567. extern int df(const char *path);
  568. if (argc != 2)
  569. {
  570. df("/");
  571. }
  572. else
  573. {
  574. if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0))
  575. {
  576. rt_kprintf("df [path]\n");
  577. }
  578. else
  579. {
  580. df(argv[1]);
  581. }
  582. }
  583. #endif
  584. return 0;
  585. }
  586. MSH_CMD_EXPORT_ALIAS(cmd_df, df, disk free);
  587. static int cmd_echo(int argc, char **argv)
  588. {
  589. if (argc == 2)
  590. {
  591. rt_kprintf("%s\n", argv[1]);
  592. }
  593. else if (argc == 3)
  594. {
  595. int fd;
  596. fd = open(argv[2], O_RDWR | O_APPEND | O_CREAT, 0);
  597. if (fd >= 0)
  598. {
  599. write(fd, argv[1], strlen(argv[1]));
  600. close(fd);
  601. }
  602. else
  603. {
  604. rt_kprintf("open file:%s failed!\n", argv[2]);
  605. }
  606. }
  607. else
  608. {
  609. rt_kprintf("Usage: echo \"string\" [filename]\n");
  610. }
  611. return 0;
  612. }
  613. MSH_CMD_EXPORT_ALIAS(cmd_echo, echo, echo string to file);
  614. static int cmd_tail(int argc, char **argv)
  615. {
  616. int fd;
  617. char c = RT_NULL;
  618. char *file_name = RT_NULL;
  619. rt_uint32_t total_lines = 0;
  620. rt_uint32_t target_line = 0;
  621. rt_uint32_t current_line = 0;
  622. rt_uint32_t required_lines = 0;
  623. rt_uint32_t start_line = 0;
  624. if (argc < 2)
  625. {
  626. rt_kprintf("Usage: tail [-n numbers] <filename>\n");
  627. return -1;
  628. }
  629. else if (argc == 2)
  630. {
  631. required_lines = 10; /* default: 10 lines from tail */
  632. file_name = argv[1];
  633. }
  634. else if (rt_strcmp(argv[1], "-n") == 0)
  635. {
  636. if (argv[2][0] != '+')
  637. {
  638. required_lines = atoi(argv[2]);
  639. }
  640. else
  641. {
  642. start_line = atoi(&argv[2][1]); /* eg: +100, to get the 100 */
  643. }
  644. file_name = argv[3];
  645. }
  646. else
  647. {
  648. rt_kprintf("Usage: tail [-n numbers] <filename>\n");
  649. return -1;
  650. }
  651. fd = open(file_name, O_RDONLY);
  652. if (fd < 0)
  653. {
  654. rt_kprintf("File doesn't exist\n");
  655. return -1;
  656. }
  657. while ((read(fd, &c, sizeof(char))) > 0)
  658. {
  659. if(total_lines == 0)
  660. {
  661. total_lines++;
  662. }
  663. if (c == '\n')
  664. {
  665. total_lines++;
  666. }
  667. }
  668. rt_kprintf("\nTotal Number of lines:%d\n", total_lines);
  669. if (start_line != 0)
  670. {
  671. if (total_lines >= start_line)
  672. {
  673. required_lines = total_lines - start_line + 1;
  674. }
  675. else
  676. {
  677. rt_kprintf("\nError:Required lines are more than total number of lines\n");
  678. close(fd);
  679. return -1;
  680. }
  681. }
  682. if (required_lines > total_lines)
  683. {
  684. rt_kprintf("\nError:Required lines are more than total number of lines\n");
  685. close(fd);
  686. return -1;
  687. }
  688. rt_kprintf("Required Number of lines:%d\n", required_lines);
  689. target_line = total_lines - required_lines;
  690. lseek(fd, 0, SEEK_SET); /* back to head */
  691. while ((read(fd, &c, sizeof(char))) > 0)
  692. {
  693. if (current_line >= target_line)
  694. {
  695. rt_kprintf("%c", c);
  696. }
  697. if (c == '\n')
  698. {
  699. current_line++;
  700. }
  701. }
  702. rt_kprintf("\n");
  703. close(fd);
  704. return 0;
  705. }
  706. MSH_CMD_EXPORT_ALIAS(cmd_tail, tail, print the last N - lines data of the given file);
  707. #ifdef RT_USING_DFS_V2
  708. static void directory_setattr(const char *pathname, struct dfs_attr *attr, char f, char v)
  709. {
  710. DIR *dir = NULL;
  711. struct dirent *dirent = NULL;
  712. char *full_path;
  713. if (pathname == RT_NULL)
  714. return;
  715. full_path = (char *)rt_malloc(DFS_PATH_MAX);
  716. if (full_path == RT_NULL)
  717. return;
  718. dir = opendir(pathname);
  719. if (dir == RT_NULL)
  720. {
  721. if (f == 0)
  722. {
  723. rt_kprintf("cannot open '%s'\n", pathname);
  724. }
  725. rt_free(full_path);
  726. return;
  727. }
  728. while (1)
  729. {
  730. dirent = readdir(dir);
  731. if (dirent == RT_NULL)
  732. break;
  733. if (rt_strcmp(".", dirent->d_name) != 0 &&
  734. rt_strcmp("..", dirent->d_name) != 0)
  735. {
  736. rt_sprintf(full_path, "%s/%s", pathname, dirent->d_name);
  737. if (dirent->d_type == DT_REG)
  738. {
  739. if (dfs_file_setattr(full_path, attr) != 0)
  740. {
  741. if (f == 0)
  742. {
  743. rt_kprintf("'%s' setattr failed, no such file or directory\n", full_path);
  744. }
  745. }
  746. else if (v)
  747. {
  748. rt_kprintf("'%s' setattr 0x%X\n", full_path, attr->st_mode);
  749. }
  750. }
  751. else if (dirent->d_type == DT_DIR)
  752. {
  753. directory_setattr(full_path, attr, f, v);
  754. }
  755. }
  756. }
  757. closedir(dir);
  758. rt_free(full_path);
  759. if (dfs_file_setattr(pathname, attr) != 0)
  760. {
  761. if (f == 0)
  762. {
  763. rt_kprintf("'%s' setattr failed, no such file or directory\n", pathname);
  764. }
  765. }
  766. else if (v)
  767. {
  768. rt_kprintf("'%s' setattr 0x%X\n", pathname, attr->st_mode);
  769. }
  770. }
  771. static int cmd_chmod(int argc, char **argv)
  772. {
  773. if (argc < 3)
  774. {
  775. rt_kprintf("Usage: chmod [OPTION]... MODE[,MODE]... FILE...\n");
  776. rt_kprintf(" chmod [-f|v|r] [u|g|o|a][+/-/=][r|w|x] file...\n");
  777. rt_kprintf(" -f suppress most error messages\n");
  778. rt_kprintf(" -v output a diagnostic for every file processed\n");
  779. rt_kprintf(" -r change files and directories recursively\n");
  780. rt_kprintf("Change the mode of each FILE to MODE.\n");
  781. }
  782. else
  783. {
  784. int argv_c = 1;
  785. char f = 0, r = 0, v = 0;
  786. if (argv[argv_c][0] == '-')
  787. {
  788. for (int i = 1; argv[argv_c][i]; i++)
  789. {
  790. switch (argv[argv_c][i])
  791. {
  792. case 'f':
  793. f = 1;
  794. break;
  795. case 'r':
  796. r = 1;
  797. break;
  798. case 'v':
  799. v = 1;
  800. break;
  801. default:
  802. rt_kprintf("Error: Bad option: %c\n", argv[argv_c][i]);
  803. return 0;
  804. }
  805. }
  806. argv_c++;
  807. }
  808. if (argc - argv_c > 1)
  809. {
  810. int U = 1, G = 2, O = 4, ALL = 7;
  811. int off[5] = {0, 6, 3, 0, 0};
  812. int ADD = 1, SUB = 2, SET = 4;
  813. int R = 4, W = 2, X = 1;
  814. int user[3] = {0}, change[3] = {0}, mode[3] = {0};
  815. struct dfs_attr attr;
  816. char *cmd = argv[argv_c];
  817. int index = 0, num = 0;
  818. while (cmd[index] != '\0')
  819. {
  820. switch (cmd[index])
  821. {
  822. case 'u':
  823. user[num] |= U;
  824. break;
  825. case 'g':
  826. user[num] |= G;
  827. break;
  828. case 'o':
  829. user[num] |= O;
  830. break;
  831. case 'a':
  832. user[num] |= ALL;
  833. break;
  834. case ',':
  835. if (num < 2)
  836. num++;
  837. break;
  838. }
  839. index++;
  840. }
  841. index = 0;
  842. num = 0;
  843. while (cmd[index] != '\0')
  844. {
  845. switch (cmd[index])
  846. {
  847. case '+':
  848. change[num] = ADD;
  849. break;
  850. case '-':
  851. change[num] = SUB;
  852. break;
  853. case '=':
  854. change[num] = SET;
  855. break;
  856. case ',':
  857. if (num < 2)
  858. num++;
  859. break;
  860. }
  861. index++;
  862. }
  863. index = 0;
  864. num = 0;
  865. while (cmd[index] != '\0')
  866. {
  867. switch (cmd[index])
  868. {
  869. case 'r':
  870. mode[num] |= R;
  871. break;
  872. case 'w':
  873. mode[num] |= W;
  874. break;
  875. case 'x':
  876. mode[num] |= X;
  877. break;
  878. case ',':
  879. if (num < 2)
  880. num++;
  881. break;
  882. }
  883. index++;
  884. }
  885. attr.st_mode = 0;
  886. for (int i = 0; i <= num; i++)
  887. {
  888. if (change[i] == ADD)
  889. {
  890. if (user[i] & U)
  891. {
  892. attr.st_mode |= mode[i] << off[user[i] & U];
  893. }
  894. if (user[i] & G)
  895. {
  896. attr.st_mode |= mode[i] << off[user[i] & G];
  897. }
  898. if (user[i] & O)
  899. {
  900. attr.st_mode |= mode[i] << off[user[i] & O];
  901. }
  902. }
  903. else if (change[i] == SUB)
  904. {
  905. if (user[i] & U)
  906. {
  907. attr.st_mode &= ~(mode[i] << off[user[i] & U]);
  908. }
  909. if (user[i] & G)
  910. {
  911. attr.st_mode &= ~(mode[i] << off[user[i] & G]);
  912. }
  913. if (user[i] & O)
  914. {
  915. attr.st_mode &= ~(mode[i] << off[user[i] & O]);
  916. }
  917. }
  918. else if (change[i] == SET)
  919. {
  920. if (user[i] & U)
  921. {
  922. attr.st_mode &= ~(7 << off[user[i] & U]);
  923. attr.st_mode |= mode[i] << off[user[i] & U];
  924. }
  925. if (user[i] & G)
  926. {
  927. attr.st_mode &= ~(7 << off[user[i] & G]);
  928. attr.st_mode |= mode[i] << off[user[i] & G];
  929. }
  930. if (user[i] & O)
  931. {
  932. attr.st_mode &= ~(7 << off[user[i] & O]);
  933. attr.st_mode |= mode[i] << off[user[i] & O];
  934. }
  935. }
  936. }
  937. argv_c++;
  938. for (int i = argv_c; i < argc; i++)
  939. {
  940. if (r)
  941. {
  942. struct stat s;
  943. if (stat(argv[i], &s) == 0)
  944. {
  945. if (s.st_mode & S_IFDIR)
  946. {
  947. directory_setattr(argv[i], &attr, f, v);
  948. }
  949. else if (f == 0)
  950. {
  951. rt_kprintf("'%s' is not a directory\n", argv[i]);
  952. }
  953. }
  954. }
  955. else
  956. {
  957. if (dfs_file_setattr(argv[i], &attr) != 0)
  958. {
  959. if (f == 0)
  960. {
  961. rt_kprintf("'%s' setattr failed, no such file or directory\n", argv[i]);
  962. }
  963. }
  964. else if (v)
  965. {
  966. rt_kprintf("'%s' setattr 0x%X\n", argv[i], attr.st_mode);
  967. }
  968. }
  969. }
  970. }
  971. }
  972. return 0;
  973. }
  974. MSH_CMD_EXPORT_ALIAS(cmd_chmod, chmod, Change the file attr.);
  975. #endif
  976. #endif /* defined(RT_USING_FINSH) && defined(DFS_USING_POSIX) */