2017-02-13 21:08:19 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/stat/stat_main.c
|
2017-02-13 21:08:19 +01:00
|
|
|
*
|
2020-04-11 20:13:56 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-02-13 21:08:19 +01:00
|
|
|
*
|
2020-04-11 20:13:56 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-02-13 21:08:19 +01:00
|
|
|
*
|
2020-04-11 20:13:56 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-02-13 21:08:19 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-06-15 08:02:49 +02:00
|
|
|
#include <malloc.h>
|
2017-02-13 21:57:36 +01:00
|
|
|
#include <string.h>
|
2017-02-13 21:08:19 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2017-02-13 21:08:19 +01:00
|
|
|
|
2017-02-13 21:57:36 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct mallinfo g_mmbefore;
|
|
|
|
static struct mallinfo g_mmprevious;
|
|
|
|
static struct mallinfo g_mmafter;
|
|
|
|
|
2017-02-13 21:08:19 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-13 22:24:58 +01:00
|
|
|
static void showusage(FAR struct mallinfo *mmbefore,
|
|
|
|
FAR struct mallinfo *mmafter, FAR const char *msg)
|
2017-02-13 21:57:36 +01:00
|
|
|
{
|
2017-02-13 23:46:23 +01:00
|
|
|
if (mmbefore->uordblks != mmafter->uordblks)
|
2017-02-13 22:24:58 +01:00
|
|
|
{
|
|
|
|
printf("\n%s:\n", msg);
|
|
|
|
printf("VARIABLE BEFORE AFTER\n");
|
|
|
|
printf("======== ======== ========\n");
|
|
|
|
printf("arena %8x %8x\n", mmbefore->arena, mmafter->arena);
|
|
|
|
printf("ordblks %8d %8d\n", mmbefore->ordblks, mmafter->ordblks);
|
|
|
|
printf("mxordblk %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk);
|
|
|
|
printf("uordblks %8x %8x\n", mmbefore->uordblks, mmafter->uordblks);
|
|
|
|
printf("fordblks %8x %8x\n", mmbefore->fordblks, mmafter->fordblks);
|
|
|
|
}
|
2017-02-13 21:57:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stepusage(void)
|
|
|
|
{
|
|
|
|
/* Get the current memory usage */
|
|
|
|
|
|
|
|
g_mmafter = mallinfo();
|
|
|
|
|
|
|
|
/* Show the change from the previous loop */
|
|
|
|
|
2017-02-13 22:24:58 +01:00
|
|
|
showusage(&g_mmprevious, &g_mmafter, "Step memory leak");
|
2017-02-13 21:57:36 +01:00
|
|
|
|
|
|
|
/* Set up for the next test */
|
|
|
|
|
|
|
|
g_mmprevious = g_mmafter;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void endusage(void)
|
|
|
|
{
|
|
|
|
g_mmafter = mallinfo();
|
2017-02-13 22:24:58 +01:00
|
|
|
showusage(&g_mmbefore, &g_mmafter, "End-of-test memory leak");
|
2017-02-13 21:57:36 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 21:08:19 +01:00
|
|
|
static void dump_stat(FAR struct stat *buf)
|
|
|
|
{
|
2017-02-13 23:19:55 +01:00
|
|
|
char details[] = "----------";
|
2017-02-13 21:08:19 +01:00
|
|
|
|
|
|
|
if (S_ISLNK(buf->st_mode))
|
|
|
|
{
|
2017-02-13 23:19:55 +01:00
|
|
|
details[0] = 'l'; /* Takes precedence over type of the target */
|
2017-02-13 21:08:19 +01:00
|
|
|
}
|
2018-09-22 21:24:24 +02:00
|
|
|
else if (S_ISBLK(buf->st_mode))
|
|
|
|
{
|
|
|
|
details[0] = 'b';
|
|
|
|
}
|
2017-02-13 21:08:19 +01:00
|
|
|
else if (S_ISCHR(buf->st_mode))
|
|
|
|
{
|
2017-02-13 23:19:55 +01:00
|
|
|
details[0] = 'c';
|
2017-02-13 21:08:19 +01:00
|
|
|
}
|
|
|
|
else if (S_ISDIR(buf->st_mode))
|
|
|
|
{
|
2017-02-13 23:19:55 +01:00
|
|
|
details[0] = 'd';
|
2017-02-13 21:08:19 +01:00
|
|
|
}
|
2018-09-22 21:24:24 +02:00
|
|
|
else if (S_ISMTD(buf->st_mode))
|
2017-02-13 21:08:19 +01:00
|
|
|
{
|
2018-09-22 21:24:24 +02:00
|
|
|
details[0] = 'f';
|
|
|
|
}
|
|
|
|
else if (S_ISSHM(buf->st_mode))
|
|
|
|
{
|
|
|
|
details[0] = 'h';
|
|
|
|
}
|
|
|
|
else if (S_ISMQ(buf->st_mode))
|
|
|
|
{
|
|
|
|
details[0] = 'm';
|
|
|
|
}
|
|
|
|
else if (S_ISSOCK(buf->st_mode))
|
|
|
|
{
|
|
|
|
details[0] = 'n';
|
|
|
|
}
|
|
|
|
else if (S_ISSEM(buf->st_mode))
|
|
|
|
{
|
|
|
|
details[0] = 's';
|
2017-02-13 21:08:19 +01:00
|
|
|
}
|
2017-02-13 23:19:55 +01:00
|
|
|
else if (!S_ISREG(buf->st_mode))
|
2017-02-13 21:08:19 +01:00
|
|
|
{
|
2017-02-13 23:19:55 +01:00
|
|
|
details[0] = '?';
|
2017-02-13 21:08:19 +01:00
|
|
|
}
|
2017-02-13 23:19:55 +01:00
|
|
|
|
|
|
|
if ((buf->st_mode & S_IRUSR) != 0)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
details[1] = 'r';
|
2017-02-13 23:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf->st_mode & S_IWUSR) != 0)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
details[2] = 'w';
|
2017-02-13 23:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf->st_mode & S_IXUSR) != 0)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
details[3] = 'x';
|
2017-02-13 23:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf->st_mode & S_IRGRP) != 0)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
details[4] = 'r';
|
2017-02-13 23:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf->st_mode & S_IWGRP) != 0)
|
2017-02-13 21:08:19 +01:00
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
details[5] = 'w';
|
2017-02-13 21:08:19 +01:00
|
|
|
}
|
2017-02-13 23:19:55 +01:00
|
|
|
|
|
|
|
if ((buf->st_mode & S_IXGRP) != 0)
|
2017-02-13 21:08:19 +01:00
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
details[6] = 'x';
|
2017-02-13 21:08:19 +01:00
|
|
|
}
|
2017-02-13 23:19:55 +01:00
|
|
|
|
|
|
|
if ((buf->st_mode & S_IROTH) != 0)
|
2017-02-13 21:08:19 +01:00
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
details[7] = 'r';
|
2017-02-13 21:08:19 +01:00
|
|
|
}
|
2017-02-13 23:19:55 +01:00
|
|
|
|
|
|
|
if ((buf->st_mode & S_IWOTH) != 0)
|
2017-02-13 21:08:19 +01:00
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
details[8] = 'w';
|
2017-02-13 21:08:19 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 23:57:41 +01:00
|
|
|
if ((buf->st_mode & S_IXOTH) != 0)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
details[9] = 'x';
|
2017-02-13 23:57:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 23:46:23 +01:00
|
|
|
printf("stat buffer:\n");
|
2017-02-13 23:19:55 +01:00
|
|
|
printf(" st_mode: %04x %s\n", buf->st_mode, details);
|
2017-02-13 21:08:19 +01:00
|
|
|
printf(" st_size: %llu\n", (unsigned long long)buf->st_size);
|
|
|
|
printf(" st_blksize: %lu\n", (unsigned long)buf->st_blksize);
|
|
|
|
printf(" st_blocks: %lu\n", (unsigned long)buf->st_blocks);
|
|
|
|
printf(" st_atime: %08lx\n", (unsigned long)buf->st_atime);
|
|
|
|
printf(" st_mtime: %08lx\n", (unsigned long)buf->st_mtime);
|
|
|
|
printf(" st_ctime: %08lx\n", (unsigned long)buf->st_ctime);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_statfs(FAR struct statfs *buf)
|
|
|
|
{
|
2017-02-13 23:46:23 +01:00
|
|
|
printf("statfs buffer:\n");
|
2017-02-13 21:08:19 +01:00
|
|
|
printf(" f_type: %lu\n", (unsigned long)buf->f_type);
|
|
|
|
printf(" f_namelen: %lu\n", (unsigned long)buf->f_namelen);
|
|
|
|
printf(" f_bsize: %lu\n", (unsigned long)buf->f_bsize);
|
|
|
|
printf(" f_blocks: %llu\n", (unsigned long long)buf->f_blocks);
|
|
|
|
printf(" f_bfree: %llu\n", (unsigned long long)buf->f_bfree);
|
|
|
|
printf(" f_bavail: %llu\n", (unsigned long long)buf->f_bavail);
|
|
|
|
printf(" f_files: %llu\n", (unsigned long long)buf->f_files);
|
|
|
|
printf(" f_ffree: %llu\n", (unsigned long long)buf->f_ffree);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* stat_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
FAR const char *path;
|
|
|
|
struct stat statbuf;
|
|
|
|
struct statfs statfsbuf;
|
|
|
|
bool isreg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Argument is expected... the path to the file to test */
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Invalid number of arguments: %d (expected 2)\n",
|
|
|
|
argc);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = argv[1];
|
|
|
|
printf("Testing path: \"%s\"\n", path);
|
|
|
|
|
2017-02-13 21:57:36 +01:00
|
|
|
/* Set up memory monitoring */
|
|
|
|
|
2020-04-11 20:13:56 +02:00
|
|
|
g_mmbefore = mallinfo();
|
2017-02-13 21:57:36 +01:00
|
|
|
g_mmprevious = g_mmbefore;
|
|
|
|
|
2017-02-13 21:08:19 +01:00
|
|
|
/* Try stat first */
|
|
|
|
|
2017-02-13 23:46:23 +01:00
|
|
|
printf("\nTest stat(%s)\n", path);
|
2017-02-13 21:08:19 +01:00
|
|
|
ret = stat(path, &statbuf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: stat(%s) failed: %d\n",
|
|
|
|
path, errcode);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_stat(&statbuf);
|
2017-02-13 21:57:36 +01:00
|
|
|
stepusage();
|
2017-02-13 21:08:19 +01:00
|
|
|
isreg = S_ISREG(statbuf.st_mode);
|
|
|
|
|
|
|
|
/* Try statfs */
|
|
|
|
|
2017-02-13 23:46:23 +01:00
|
|
|
printf("\nTest statfs(%s)\n", path);
|
2017-02-13 21:08:19 +01:00
|
|
|
ret = statfs(path, &statfsbuf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: statfs(%s) failed: %d\n",
|
|
|
|
path, errcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_statfs(&statfsbuf);
|
2017-02-13 21:57:36 +01:00
|
|
|
stepusage();
|
2017-02-13 21:08:19 +01:00
|
|
|
|
|
|
|
/* Try fstat (only if it is a regular file) */
|
|
|
|
|
|
|
|
if (isreg)
|
|
|
|
{
|
2017-02-13 23:46:23 +01:00
|
|
|
int fd;
|
|
|
|
|
2017-02-17 17:53:24 +01:00
|
|
|
printf("\nOpen(%s)\n", path);
|
2017-02-13 23:46:23 +01:00
|
|
|
fd = open(path, O_RDONLY);
|
2017-02-13 21:08:19 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: open(%s) failed: %d\n",
|
|
|
|
path, errcode);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-02-17 17:53:24 +01:00
|
|
|
/* Try fstat */
|
|
|
|
|
|
|
|
printf("\nTest fstat(%s)\n", path);
|
2017-02-13 21:08:19 +01:00
|
|
|
ret = fstat(fd, &statbuf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr,
|
2017-02-14 01:21:56 +01:00
|
|
|
"ERROR: fstat(%s) failed: %d\n",
|
2017-02-13 21:08:19 +01:00
|
|
|
path, errcode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dump_stat(&statbuf);
|
|
|
|
}
|
2018-08-13 15:47:26 +02:00
|
|
|
|
2017-02-17 17:53:24 +01:00
|
|
|
/* Try fstatfs */
|
|
|
|
|
|
|
|
printf("\nTest fstatfs(%s)\n", path);
|
|
|
|
ret = fstatfs(fd, &statfsbuf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: fstatfs(%s) failed: %d\n",
|
|
|
|
path, errcode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dump_statfs(&statfsbuf);
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:08:19 +01:00
|
|
|
close(fd);
|
2017-02-13 23:46:23 +01:00
|
|
|
stepusage();
|
2017-02-13 21:08:19 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 21:57:36 +01:00
|
|
|
endusage();
|
2017-02-13 21:08:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|