📄 nsh_fscmds.c
字号:
int fd = open(argv[1], O_RDONLY); if (fd < 0) { nsh_output(handle, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO); return; } /* And just dump it byte for byte into stdout */ for (;;) { int nbytesread = read(fd, buffer, 1024); /* Check for read errors */ if (nbytesread < 0) { /* EINTR is not an error */ if (errno != EINTR) { nsh_output(handle, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO); break; } } /* Check for data successfully read */ else if (nbytesread > 0) { int nbyteswritten = 0; while (nbyteswritten < nbytesread) { int n = write(1, buffer, nbytesread); if (n < 0) { /* EINTR is not an error */ if (errno != EINTR) { nsh_output(handle, g_fmtcmdfailed, argv[0], "write", NSH_ERRNO); break; } } else { nbyteswritten += n; } } } /* Otherwise, it is the end of file */ else { break; } } (void)close(fd);}#endif/**************************************************************************** * Name: cmd_cp ****************************************************************************/#if CONFIG_NFILE_DESCRIPTORS > 0void cmd_cp(FAR void *handle, int argc, char **argv){ struct stat buf; char *fullpath = NULL; const char *wrpath = argv[2]; int oflags = O_WRONLY|O_CREAT|O_TRUNC; int rdfd; int wrfd; int ret; /* Open the source file for reading */ rdfd = open(argv[1], O_RDONLY); if (rdfd < 0) { nsh_output(handle, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO); return; } /* Check if the destination is a directory */ ret = stat(wrpath, &buf); if (ret == 0) { /* Something exists here... is it a directory? */ if (S_ISDIR(buf.st_mode)) { /* Yes, it is a directory. Remove any trailing '/' characters from the path */ trim_dir(argv[2]); /* Construct the full path to the new file */ fullpath = getdirpath(argv[2], basename(argv[1]) ); if (!fullpath) { nsh_output(handle, g_fmtcmdoutofmemory, argv[0]); goto out_with_rdfd; } /* Open then fullpath for writing */ wrpath = fullpath; } else if (!S_ISREG(buf.st_mode)) { /* Maybe it is a driver? */ oflags = O_WRONLY; } } /* Now open the destination */ wrfd = open(wrpath, oflags, 0666); if (wrfd < 0) { nsh_output(handle, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO); goto out_with_fullpath; } /* Now copy the file */ for (;;) { int nbytesread; int nbyteswritten; do { nbytesread = read(rdfd, g_iobuffer, IOBUFFERSIZE); if (nbytesread == 0) { /* End of file */ goto out_with_wrfd; } else if (nbytesread < 0 && errno != EINTR) { /* Read error */ nsh_output(handle, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO); goto out_with_wrfd; } } while (nbytesread <= 0); do { nbyteswritten = write(wrfd, g_iobuffer, nbytesread); if (nbyteswritten >= 0) { nbytesread -= nbyteswritten; } else if (errno != EINTR) { /* Read error */ nsh_output(handle, g_fmtcmdfailed, argv[0], "write", NSH_ERRNO); goto out_with_wrfd; } } while (nbytesread > 0); }out_with_wrfd: close(wrfd);out_with_fullpath: if (fullpath) { free(fullpath); }out_with_rdfd: close(rdfd);}#endif/**************************************************************************** * Name: cmd_ls ****************************************************************************/#if CONFIG_NFILE_DESCRIPTORS > 0void cmd_ls(FAR void *handle, int argc, char **argv){ unsigned int lsflags = 0; int ret; /* Get the ls options */ int option; while ((option = getopt(argc, argv, "lRs")) != ERROR) { switch (option) { case 'l': lsflags |= (LSFLAGS_SIZE|LSFLAGS_LONG); break; case 'R': lsflags |= LSFLAGS_RECURSIVE; break; case 's': lsflags |= LSFLAGS_SIZE; break; case '?': default: nsh_output(handle, g_fmtarginvalid, argv[0]); return; } } /* There are one required arguments after the options */ if (optind + 1 < argc) { nsh_output(handle, g_fmttoomanyargs, argv[0]); return; } else if (optind + 1 > argc) { nsh_output(handle, g_fmtargrequired, argv[0]); return; } /* List the directory contents */ nsh_output(handle, "%s:\n", argv[optind]); ret = foreach_direntry(handle, "ls", argv[optind], ls_handler, (void*)lsflags); if (ret == OK && (lsflags & LSFLAGS_RECURSIVE) != 0) { /* Then recurse to list each directory within the directory */ ret = foreach_direntry(handle, "ls", argv[optind], ls_recursive, (void*)lsflags); }}#endif/**************************************************************************** * Name: cmd_mkdir ****************************************************************************/#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0void cmd_mkdir(FAR void *handle, int argc, char **argv){ int result = mkdir(argv[1], 0777); if ( result < 0) { nsh_output(handle, g_fmtcmdfailed, argv[0], "mkdir", NSH_ERRNO); }}#endif/**************************************************************************** * Name: cmd_mount ****************************************************************************/#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0#ifdef CONFIG_FS_FAT /* Need at least one filesytem in configuration */void cmd_mount(FAR void *handle, int argc, char **argv){ char *filesystem = 0; int result; /* Get the mount options */ int option; while ((option = getopt(argc, argv, ":t:")) != ERROR) { switch (option) { case 't': filesystem = optarg; break; case ':': nsh_output(handle, g_fmtargrequired, argv[0]); return; case '?': default: nsh_output(handle, g_fmtarginvalid, argv[0]); return; } } /* There are two required arguments after the options */ if (optind + 2 < argc) { nsh_output(handle, g_fmttoomanyargs, argv[0]); return; } else if (optind + 2 > argc) { nsh_output(handle, g_fmtargrequired, argv[0]); return; } /* Perform the mount */ result = mount(argv[optind], argv[optind+1], filesystem, 0, NULL); if ( result < 0) { nsh_output(handle, g_fmtcmdfailed, argv[0], "mount", NSH_ERRNO); }}#endif#endif/**************************************************************************** * Name: cmd_rm ****************************************************************************/#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0void cmd_rm(FAR void *handle, int argc, char **argv){ if (unlink(argv[1]) < 0) { nsh_output(handle, g_fmtcmdfailed, argv[0], "unlink", NSH_ERRNO); }}#endif/**************************************************************************** * Name: cmd_rm ****************************************************************************/#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0void cmd_rmdir(FAR void *handle, int argc, char **argv){ if (rmdir(argv[1]) < 0) { nsh_output(handle, g_fmtcmdfailed, argv[0], "rmdir", NSH_ERRNO); }}#endif/**************************************************************************** * Name: cmd_umount ****************************************************************************/#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0#ifdef CONFIG_FS_FAT /* Need at least one filesytem in configuration */void cmd_umount(FAR void *handle, int argc, char **argv){ /* Perform the umount */ int result = umount(argv[1]); if ( result < 0) { nsh_output(handle, g_fmtcmdfailed, argv[0], "umount", NSH_ERRNO); }}#endif#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -