From owner-svn-src-head@freebsd.org Tue Mar 28 21:50:12 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id F2EF6D23FCE; Tue, 28 Mar 2017 21:50:12 +0000 (UTC) (envelope-from tsoome@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id CD9ACF06; Tue, 28 Mar 2017 21:50:12 +0000 (UTC) (envelope-from tsoome@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v2SLoBhc004096; Tue, 28 Mar 2017 21:50:11 GMT (envelope-from tsoome@FreeBSD.org) Received: (from tsoome@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v2SLoBCb004095; Tue, 28 Mar 2017 21:50:11 GMT (envelope-from tsoome@FreeBSD.org) Message-Id: <201703282150.v2SLoBCb004095@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: tsoome set sender to tsoome@FreeBSD.org using -f From: Toomas Soome Date: Tue, 28 Mar 2017 21:50:11 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r316112 - head/sys/boot/common X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 28 Mar 2017 21:50:13 -0000 Author: tsoome Date: Tue Mar 28 21:50:11 2017 New Revision: 316112 URL: https://svnweb.freebsd.org/changeset/base/316112 Log: loader: ls command should display file types properly With some file system the ls is unable to display file types. Reviewed by: allanjude Approved by: allanjude (mentor) Differential Revision: https://reviews.freebsd.org/D10066 Modified: head/sys/boot/common/ls.c Modified: head/sys/boot/common/ls.c ============================================================================== --- head/sys/boot/common/ls.c Tue Mar 28 21:47:12 2017 (r316111) +++ head/sys/boot/common/ls.c Tue Mar 28 21:50:11 2017 (r316112) @@ -60,26 +60,26 @@ command_ls(int argc, char *argv[]) { int fd; struct stat sb; - struct dirent *d; + struct dirent *d; char *buf, *path; char lbuf[128]; /* one line */ int result, ch; int verbose; - + result = CMD_OK; fd = -1; verbose = 0; optind = 1; optreset = 1; while ((ch = getopt(argc, argv, "l")) != -1) { - switch(ch) { + switch (ch) { case 'l': verbose = 1; break; case '?': default: /* getopt has already reported an error */ - return(CMD_OK); + return (CMD_OK); } } argv += (optind - 1); @@ -91,6 +91,18 @@ command_ls(int argc, char *argv[]) path = argv[1]; } + if (stat(path, &sb) == 0 && !S_ISDIR(sb.st_mode)) { + if (verbose) { + printf(" %c %8d %s\n", + typestr[sb.st_mode >> 12], + (int)sb.st_size, path); + } else { + printf(" %c %s\n", + typestr[sb.st_mode >> 12], path); + } + return (CMD_OK); + } + fd = ls_getdir(&path); if (fd == -1) { result = CMD_ERROR; @@ -102,19 +114,28 @@ command_ls(int argc, char *argv[]) while ((d = readdirfd(fd)) != NULL) { if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) { - if (verbose) { + if (d->d_type == 0 || verbose) { /* stat the file, if possible */ sb.st_size = 0; + sb.st_mode = 0; buf = malloc(strlen(path) + strlen(d->d_name) + 2); - sprintf(buf, "%s/%s", path, d->d_name); - /* ignore return, could be symlink, etc. */ - if (stat(buf, &sb)) - sb.st_size = 0; - free(buf); - sprintf(lbuf, " %c %8d %s\n", typestr[d->d_type], + if (buf != NULL) { + sprintf(buf, "%s/%s", path, d->d_name); + /* ignore return, could be symlink, etc. */ + if (stat(buf, &sb)) { + sb.st_size = 0; + sb.st_mode = 0; + } + free(buf); + } + } + if (verbose) { + snprintf(lbuf, sizeof(lbuf), " %c %8d %s\n", + typestr[d->d_type? d->d_type:sb.st_mode >> 12], (int)sb.st_size, d->d_name); } else { - sprintf(lbuf, " %c %s\n", typestr[d->d_type], d->d_name); + snprintf(lbuf, sizeof(lbuf), " %c %s\n", + typestr[d->d_type? d->d_type:sb.st_mode >> 12], d->d_name); } if (pager_output(lbuf)) goto out; @@ -124,9 +145,8 @@ command_ls(int argc, char *argv[]) pager_close(); if (fd != -1) close(fd); - if (path != NULL) - free(path); - return(result); + free(path); /* ls_getdir() did allocate path */ + return (result); } /* @@ -145,6 +165,11 @@ ls_getdir(char **pathp) /* one extra byte for a possible trailing slash required */ path = malloc(strlen(*pathp) + 2); + if (path == NULL) { + snprintf(command_errbuf, sizeof (command_errbuf), + "out of memory"); + goto out; + } strcpy(path, *pathp); /* Make sure the path is respectable to begin with */ @@ -153,7 +178,7 @@ ls_getdir(char **pathp) "bad path '%s'", path); goto out; } - + /* If there's no path on the device, assume '/' */ if (*cp == 0) strcat(path, "/"); @@ -176,12 +201,12 @@ ls_getdir(char **pathp) } *pathp = path; - return(fd); + return (fd); out: free(path); *pathp = NULL; if (fd != -1) close(fd); - return(-1); + return (-1); }