comparison main.c @ 548:28688eb812de

Add helper functions for checking if target path is writable, readable or directory and creating directory structures recursively.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 12 Nov 2012 14:18:09 +0200
parents 3d7ff16389cf
children dd7b58eca06d
comparison
equal deleted inserted replaced
547:1176c4b9ed93 548:28688eb812de
17 #define random rand 17 #define random rand
18 #else 18 #else
19 #include <sys/wait.h> 19 #include <sys/wait.h>
20 #include <sys/stat.h> 20 #include <sys/stat.h>
21 #include <sys/types.h> 21 #include <sys/types.h>
22 #include <unistd.h>
22 #endif 23 #endif
23 24
24 #ifdef __WIN32 25 #ifdef __WIN32
25 #define SET_CONFIG_FILE "nnchat.txt" 26 #define SET_CONFIG_FILE "nnchat.txt"
26 #define SET_LOG_DIR "NNChat Log Files" 27 #define SET_LOG_DIR "NNChat Log Files"
1576 th_free(win->logFilename); 1577 th_free(win->logFilename);
1577 win->logFilename = NULL; 1578 win->logFilename = NULL;
1578 } 1579 }
1579 1580
1580 1581
1582 BOOL nn_mkdir_rec(const char *cpath)
1583 {
1584 char save, *path = th_strdup(cpath);
1585 size_t start = 0, end;
1586 BOOL res = FALSE;
1587
1588 do
1589 {
1590 for (save = 0, end = start; path[end] != 0; end++)
1591 if (path[end] == SET_DIR_SEPARATOR)
1592 {
1593 save = path[end];
1594 path[end] = 0;
1595 break;
1596 }
1597
1598 if (path[start] != 0)
1599 {
1600 #ifdef __WIN32
1601 if (!CreateDirectory(path, NULL))
1602 goto error;
1603 #else
1604 if (mkdir(path, 711) < 0)
1605 goto error;
1606 #endif
1607 }
1608
1609 path[end] = save;
1610 start = end + 1;
1611 } while (save != 0);
1612
1613 res = TRUE;
1614
1615 error:
1616 th_free(path);
1617 return res;
1618 }
1619
1620
1621 BOOL nn_stat_path(const char *path, BOOL *isDirectory, BOOL *isWritable, BOOL *isReadable)
1622 {
1623 #ifdef __WIN32
1624 DWORD attr = GetFileAttributes(path);
1625
1626 *isDirectory = (attr & FILE_ATTRIBUTE_DIRECTORY);
1627 *isWritable = (attr & FILE_ATTRIBUTE_READONLY) == 0 && *isDirectory == FALSE;
1628 *isReadable = TRUE;
1629 #else
1630 uid_t id = geteuid();
1631 struct stat sb;
1632 if (stat(path, &sb) < 0)
1633 return FALSE;
1634
1635 *isDirectory = (S_ISDIR(sb.st_mode));
1636 *isWritable = (id == sb.st_uid && (sb.st_mode & S_IWUSR));
1637 *isReadable = (id == sb.st_uid && (sb.st_mode & S_IRUSR));
1638 #endif
1639 return TRUE;
1640 }
1641
1642
1581 int main(int argc, char *argv[]) 1643 int main(int argc, char *argv[])
1582 { 1644 {
1583 char *tmpStr; 1645 char *tmpStr;
1584 int index, updateCount = 0; 1646 int index, updateCount = 0;
1585 BOOL argsOK, colorSet = FALSE; 1647 BOOL argsOK, colorSet = FALSE;