1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 15:53:26 +01:00

Add enum for file_type() and a little cleanup

This commit is contained in:
jfreegman 2018-10-08 22:28:36 -04:00
parent 258736995d
commit 8f0e6026f0
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
5 changed files with 30 additions and 18 deletions

View File

@ -107,10 +107,9 @@ int complete_line(ToxWindow *self, const void *list, size_t n_items, size_t size
return -1; return -1;
} }
const char *L = (char *) list; const char *L = (const char *) list;
const char *endchrs = " "; const char *endchrs = " ";
char ubuf[MAX_STR_SIZE]; char ubuf[MAX_STR_SIZE] = {0};
memset(ubuf, 0, sizeof(ubuf));
/* work with multibyte string copy of buf for simplicity */ /* work with multibyte string copy of buf for simplicity */
if (wcs_to_mbs_buf(ubuf, ctx->line, sizeof(ubuf)) == -1) { if (wcs_to_mbs_buf(ubuf, ctx->line, sizeof(ubuf)) == -1) {

View File

@ -129,10 +129,9 @@ static const char special_commands[SPECIAL_COMMANDS][MAX_CMDNAME_SIZE] = {
/* Returns true if input command is in the special_commands array. */ /* Returns true if input command is in the special_commands array. */
static bool is_special_command(const char *input) static bool is_special_command(const char *input)
{ {
int s = char_find(0, input, ' '); const int s = char_find(0, input, ' ');
int i;
for (i = 0; i < SPECIAL_COMMANDS; ++i) { for (int i = 0; i < SPECIAL_COMMANDS; ++i) {
if (strncmp(input, special_commands[i], s) == 0) { if (strncmp(input, special_commands[i], s) == 0) {
return true; return true;
} }

View File

@ -512,7 +512,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
} }
#ifdef PYTHON #ifdef PYTHON
else if (wcsncmp(ctx->line, L"/run \"", wcslen(L"/run ")) == 0) { else if (wcsncmp(ctx->line, L"/run ", wcslen(L"/run ")) == 0) {
diff = dir_match(self, m, ctx->line, L"/run"); diff = dir_match(self, m, ctx->line, L"/run");
} }

View File

@ -481,22 +481,25 @@ bool file_exists(const char *path)
return stat(path, &s) == 0; return stat(path, &s) == 0;
} }
/* Returns 0 if path points to a directory. /*
* Returns 1 if path points to a regular file. * Checks the file type path points to and returns a File_Type enum value.
* Returns -1 on any other result. *
* Returns FILE_TYPE_DIRECTORY if path points to a directory.
* Returns FILE_TYPE_REGULAR if path points to a regular file.
* Returns FILE_TYPE_OTHER on any other result, including an invalid path.
*/ */
int file_type(const char *path) File_Type file_type(const char *path)
{ {
struct stat s; struct stat s;
stat(path, &s); stat(path, &s);
switch (s.st_mode & S_IFMT) { switch (s.st_mode & S_IFMT) {
case S_IFDIR: case S_IFDIR:
return 0; return FILE_TYPE_DIRECTORY;
case S_IFREG: case S_IFREG:
return 1; return FILE_TYPE_REGULAR;
default: default:
return -1; return FILE_TYPE_OTHER;
} }
} }

View File

@ -39,6 +39,14 @@
#define net_to_host(x, y) hst_to_net(x, y) #define net_to_host(x, y) hst_to_net(x, y)
#endif #endif
typedef enum File_Type
{
FILE_TYPE_REGULAR,
FILE_TYPE_DIRECTORY,
FILE_TYPE_OTHER,
} File_Type;
void hst_to_net(uint8_t *num, uint16_t numbytes); void hst_to_net(uint8_t *num, uint16_t numbytes);
/* /*
@ -146,11 +154,14 @@ void bytes_convert_str(char *buf, int size, uint64_t bytes);
/* checks if a file exists. Returns true or false */ /* checks if a file exists. Returns true or false */
bool file_exists(const char *path); bool file_exists(const char *path);
/* Returns 0 if path points to a directory. /*
* Returns 1 if path points to a regular file. * Checks the file type path points to and returns a File_Type enum value.
* Returns -1 on any other result. *
* Returns FILE_TYPE_DIRECTORY if path points to a directory.
* Returns FILE_TYPE_REGULAR if path points to a regular file.
* Returns FILE_TYPE_OTHER on any other result, including an invalid path.
*/ */
int file_type(const char *path); File_Type file_type(const char *path);
/* returns file size. If file doesn't exist returns 0. */ /* returns file size. If file doesn't exist returns 0. */
off_t file_size(const char *path); off_t file_size(const char *path);