Skip to content

Commit

Permalink
Minor, unified style
Browse files Browse the repository at this point in the history
  • Loading branch information
tronkko committed Jul 9, 2022
1 parent 588c14e commit 9eb3b5e
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 196 deletions.
7 changes: 4 additions & 3 deletions examples/cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

static void output_file(const char *fn);


int main(int argc, char *argv[])
int
main(int argc, char *argv[])
{
/* Select default locale */
setlocale(LC_ALL, "");
Expand All @@ -47,7 +47,8 @@ int main(int argc, char *argv[])
/*
* Output file to screen
*/
static void output_file(const char *fn)
static void
output_file(const char *fn)
{
/* Open file */
FILE *fp = fopen(fn, "r");
Expand Down
250 changes: 127 additions & 123 deletions examples/dirls.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,155 +38,159 @@ static void list_size(const char* full_path);
static void print_error_msg(const char* dirname, char* msg_buffer);
static void get_full_path(const char* dirname, const char* filename, char* buffer);


int main(int argc, char* argv[])
int
main(int argc, char* argv[])
{
/* Select default locale */
setlocale(LC_ALL, "");

/* For each directory in command line. */
int i = 1;
while (i < argc) {
list_directory(argv[i]);
i++;
printf("\n");
}

/* List current working directory if no arguments on command line */
if (argc == 1)
list_directory(".");

return EXIT_SUCCESS;
/* Select default locale */
setlocale(LC_ALL, "");

/* For each directory in command line. */
int i = 1;
while (i < argc) {
list_directory(argv[i]);
i++;
printf("\n");
}

/* List current working directory if no arguments on command line */
if (argc == 1)
list_directory(".");

return EXIT_SUCCESS;
}

/*
* List files and file sizes in directory.
*/
static void list_directory(const char* dirname)
static void
list_directory(const char* dirname)
{
/* Open directory stream */
DIR* dir = opendir(dirname);
if (!dir) {
/* Could not open directory; print error message */
char msg_buff[ERR_MSG_LEN];

/* Function replaces strerror with strerror_s */
print_error_msg(dirname, msg_buff);

exit(EXIT_FAILURE);
}

struct dirent* ent;
/* Print all files, file sizes, and directories within the directory */
while ((ent = readdir(dir)) != NULL) {
char buffer[PATH_MAX + 2];

switch (ent->d_type) {
case DT_REG:
printf("%s ", ent->d_name);
/* Get full file path */
get_full_path(dirname, ent->d_name, buffer);
/* Print file size next to file name */
list_size(buffer);
break;

case DT_DIR:
printf("%s/\n", ent->d_name);
break;

case DT_LNK:
printf("%s@ ", ent->d_name);
/* Get full file path */
get_full_path(dirname, ent->d_name, buffer);
/* Print file size next to file name */
list_size(buffer);
break;

default:
printf("%s* ", ent->d_name);
/* Get full file path */
get_full_path(dirname, ent->d_name, buffer);
/* Print file size next to file name */
list_size(buffer);
break;
}
}

closedir(dir);
/* Open directory stream */
DIR* dir = opendir(dirname);
if (!dir) {
/* Could not open directory; print error message */
char msg_buff[ERR_MSG_LEN];

/* Function replaces strerror with strerror_s */
print_error_msg(dirname, msg_buff);

exit(EXIT_FAILURE);
}

/* Print files, file sizes, and directories within the directory */
struct dirent* ent;
while ((ent = readdir(dir)) != NULL) {
char buffer[PATH_MAX + 2];

switch (ent->d_type) {
case DT_REG:
printf("%s ", ent->d_name);
/* Get full file path */
get_full_path(dirname, ent->d_name, buffer);
/* Print file size next to file name */
list_size(buffer);
break;

case DT_DIR:
printf("%s/\n", ent->d_name);
break;

case DT_LNK:
printf("%s@ ", ent->d_name);
/* Get full file path */
get_full_path(dirname, ent->d_name, buffer);
/* Print file size next to file name */
list_size(buffer);
break;

default:
printf("%s* ", ent->d_name);
/* Get full file path */
get_full_path(dirname, ent->d_name, buffer);
/* Print file size next to file name */
list_size(buffer);
break;
}
}

closedir(dir);
}

/*
* Print file size next to file name.
*/
static void list_size(const char* full_path)
static void
list_size(const char* full_path)
{
struct stat stbuf;
struct stat stbuf;

if (stat(full_path, &stbuf) == -1)
printf("%s %s\n", "Can't access", full_path);
else
printf("%d\n", stbuf.st_size);
if (stat(full_path, &stbuf) == -1)
printf("%s %s\n", "Can't access", full_path);
else
printf("%d\n", stbuf.st_size);
}

/*
* Enforce error message size limit and ensure valid error number.
* Print error message.
*/
static void print_error_msg(const char* dirname, char* msg_buff)
static void
print_error_msg(const char* dirname, char* msg_buff)
{
int error_num;
error_num = strerror_s(msg_buff, ERR_MSG_LEN, errno);

switch (error_num) {
case 0:
printf("Cannot open %s (%s)\n", dirname, msg_buff);
break;

case EINVAL:
printf("strerror_s() failed: invalid error code, %d\n",
error_num);
break;

case ERANGE:
printf("strerror_s() failed: buffer too small: %d\n",
ERR_MSG_LEN);
break;
}
int error_num;
error_num = strerror_s(msg_buff, ERR_MSG_LEN, errno);

switch (error_num) {
case 0:
printf("Cannot open %s (%s)\n", dirname, msg_buff);
break;

case EINVAL:
printf("strerror_s() failed: invalid error code, %d\n",
error_num);
break;

case ERANGE:
printf("strerror_s() failed: buffer too small: %d\n",
ERR_MSG_LEN);
break;
}
}

/*
* Combine directory and file name.
* Write full path to memory.
*/
static void get_full_path(const char* dirname, const char* filename, char* buffer)
static void
get_full_path(const char* dirname, const char* filename, char* buffer)
{
char* dest = buffer;
char* end = &buffer[PATH_MAX];

/* Copy directory name to buffer */
const char* src = dirname;
while (dest < end && *src != '\0') {
*dest++ = *src++;
}
*dest = '\0';

char* new_dest = dest;
char c;

/* Get final character of directory name */
if (buffer < new_dest)
c = new_dest[-1];
else
c = ':';

/* Append directory separator if not already there */
if (c != ':' && c != '/' && c != '\\')
*new_dest++ = '/';

/* Append file name */
src = filename;
while (new_dest < end && *src != '\0') {
*new_dest++ = *src++;
}
*new_dest = '\0';
char* dest = buffer;
char* end = &buffer[PATH_MAX];

/* Copy directory name to buffer */
const char* src = dirname;
while (dest < end && *src != '\0') {
*dest++ = *src++;
}
*dest = '\0';

char* new_dest = dest;
char c;

/* Get final character of directory name */
if (buffer < new_dest)
c = new_dest[-1];
else
c = ':';

/* Append directory separator if not already there */
if (c != ':' && c != '/' && c != '\\')
*new_dest++ = '/';

/* Append file name */
src = filename;
while (new_dest < end && *src != '\0') {
*new_dest++ = *src++;
}
*new_dest = '\0';
}
7 changes: 4 additions & 3 deletions examples/find.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

static int find_directory(const char *dirname);


int main(int argc, char *argv[])
int
main(int argc, char *argv[])
{
/* Select default locale */
setlocale(LC_ALL, "");
Expand All @@ -57,7 +57,8 @@ int main(int argc, char *argv[])
}

/* Find files and subdirectories recursively */
static int find_directory(const char *dirname)
static int
find_directory(const char *dirname)
{
char buffer[PATH_MAX + 2];
char *p = buffer;
Expand Down
20 changes: 12 additions & 8 deletions examples/locate.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@
/* File name and location of database file */
#define DB_LOCATION L"locate.db"


/* Forward-decl */
static int db_locate(const wchar_t *pattern);
static int db_match(const wchar_t *fn, const wchar_t *pattern);
static void db_open(void);
static void db_close(void);
static int db_read(wchar_t *buffer, size_t max);


/* Module local variables */
static FILE *db = NULL;

int main(int argc, char *argv[])
int
main(int argc, char *argv[])
{
#ifdef WIN32
/* Prepare for unicode output */
Expand Down Expand Up @@ -85,7 +84,8 @@ int main(int argc, char *argv[])
}

/* Match pattern against files in locate.db file */
static int db_locate(const wchar_t *pattern)
static int
db_locate(const wchar_t *pattern)
{
int count = 0;

Expand All @@ -112,7 +112,8 @@ static int db_locate(const wchar_t *pattern)
}

/* Match pattern against file name */
static int db_match(const wchar_t *fn, const wchar_t *pattern)
static int
db_match(const wchar_t *fn, const wchar_t *pattern)
{
int found = 0;

Expand Down Expand Up @@ -170,7 +171,8 @@ static int db_match(const wchar_t *fn, const wchar_t *pattern)
* Read line from locate.db. This function is same as fgetws() except
* that new-line at the end of line is not included.
*/
static int db_read(wchar_t *buffer, size_t max)
static int
db_read(wchar_t *buffer, size_t max)
{
int ok = 0;

Expand Down Expand Up @@ -233,7 +235,8 @@ static int db_read(wchar_t *buffer, size_t max)
}

/* Open database file locate.db */
static void db_open(void)
static void
db_open(void)
{
#ifdef WIN32
if (db)
Expand All @@ -249,7 +252,8 @@ static void db_open(void)
}

/* Close database file */
static void db_close(void)
static void
db_close(void)
{
if (!db)
return;
Expand Down
Loading

0 comments on commit 9eb3b5e

Please sign in to comment.