gitignore for build artifacts and executables
This commit is contained in:
parent
1899658f3e
commit
36783854f4
|
@ -0,0 +1,4 @@
|
||||||
|
dc
|
||||||
|
dc2
|
||||||
|
dc.o
|
||||||
|
dc2.o
|
38
README.md
38
README.md
|
@ -1,19 +1,19 @@
|
||||||
# Dircount assignment
|
# Dircount assignment
|
||||||
## CS426
|
## CS426
|
||||||
|
|
||||||
1 - prints every full pathname
|
1 - prints every full pathname
|
||||||
|
|
||||||
1 - prints a [f] or a [d] next to the path depending on type
|
1 - prints a [f] or a [d] next to the path depending on type
|
||||||
|
|
||||||
1 - counts the total number of files
|
1 - counts the total number of files
|
||||||
|
|
||||||
1 - counts the total number of directories
|
1 - counts the total number of directories
|
||||||
|
|
||||||
1 - Never count the same file twice, even if there are links.
|
1 - Never count the same file twice, even if there are links.
|
||||||
|
|
||||||
1 - does not go into an infinite loop even if the diretcory structure has loops
|
1 - does not go into an infinite loop even if the diretcory structure has loops
|
||||||
|
|
||||||
1 - prints the total on disk space used. (NOT the sum of all the file sizes)
|
1 - prints the total on disk space used. (NOT the sum of all the file sizes)
|
||||||
|
|
||||||
-1 - Each day after Wed Oct 26th.
|
-1 - Each day after Wed Oct 26th.
|
||||||
|
|
||||||
|
|
152
dircount.cc
152
dircount.cc
|
@ -1,76 +1,76 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Hashtable {
|
class Hashtable {
|
||||||
unordered_map<ino_t, bool> htmap;
|
unordered_map<ino_t, bool> htmap;
|
||||||
public:
|
public:
|
||||||
void put(ino_t key, bool value) { htmap[key] = value; }
|
void put(ino_t key, bool value) { htmap[key] = value; }
|
||||||
bool get(ino_t key) { return htmap[key]; }
|
bool get(ino_t key) { return htmap[key]; }
|
||||||
};
|
};
|
||||||
|
|
||||||
int file_cnt = 0, link_cnt = 0, dir_cnt = 0;
|
int file_cnt = 0, link_cnt = 0, dir_cnt = 0;
|
||||||
unsigned long space_used = 0;
|
unsigned long space_used = 0;
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
Hashtable ht;
|
Hashtable ht;
|
||||||
|
|
||||||
void listdir (const char *name) {
|
void listdir (const char *name) {
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *entry;
|
struct dirent *entry;
|
||||||
|
|
||||||
if (!(dir = opendir(name))) return;
|
if (!(dir = opendir(name))) return;
|
||||||
if (!(entry = readdir(dir))) return;
|
if (!(entry = readdir(dir))) return;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
|
||||||
if (entry->d_type == DT_DIR) {
|
if (entry->d_type == DT_DIR) {
|
||||||
// if (ht.get(&entry->d_ino)) continue;
|
// if (ht.get(&entry->d_ino)) continue;
|
||||||
// ht.put(&entry->d_ino, true);
|
// ht.put(&entry->d_ino, true);
|
||||||
dir_cnt++;
|
dir_cnt++;
|
||||||
char path[4096];
|
char path[4096];
|
||||||
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
|
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
|
||||||
path[len] = 0;
|
path[len] = 0;
|
||||||
printf("[d] %s/%s\n", name, entry->d_name);
|
printf("[d] %s/%s\n", name, entry->d_name);
|
||||||
listdir(path);
|
listdir(path);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (ht.get(entry->d_ino)) continue;
|
if (ht.get(entry->d_ino)) continue;
|
||||||
|
|
||||||
if (entry->d_type == DT_LNK) {
|
if (entry->d_type == DT_LNK) {
|
||||||
link_cnt++;
|
link_cnt++;
|
||||||
printf("[l] %s/%s\n", name, entry->d_name);
|
printf("[l] %s/%s\n", name, entry->d_name);
|
||||||
|
|
||||||
char path[4096];
|
char path[4096];
|
||||||
int len = snprintf(path, sizeof(path) - 1, "%s/%s", name, entry->d_name);
|
int len = snprintf(path, sizeof(path) - 1, "%s/%s", name, entry->d_name);
|
||||||
path[len] = 0;
|
path[len] = 0;
|
||||||
stat(path, &buf);
|
stat(path, &buf);
|
||||||
space_used += buf.st_blocks * 512;
|
space_used += buf.st_blocks * 512;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
file_cnt++;
|
file_cnt++;
|
||||||
printf("[f] %d %s/%s\n", (int)entry->d_ino, name, entry->d_name);
|
printf("[f] %d %s/%s\n", (int)entry->d_ino, name, entry->d_name);
|
||||||
|
|
||||||
char path[4096];
|
char path[4096];
|
||||||
int len = snprintf(path, sizeof(path) - 1, "%s/%s", name, entry->d_name);
|
int len = snprintf(path, sizeof(path) - 1, "%s/%s", name, entry->d_name);
|
||||||
path[len] = 0;
|
path[len] = 0;
|
||||||
stat(path, &buf);
|
stat(path, &buf);
|
||||||
space_used += buf.st_blocks * 512;
|
space_used += buf.st_blocks * 512;
|
||||||
}
|
}
|
||||||
|
|
||||||
ht.put(entry->d_ino, true);
|
ht.put(entry->d_ino, true);
|
||||||
|
|
||||||
}
|
}
|
||||||
} while ((entry = readdir(dir)));
|
} while ((entry = readdir(dir)));
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main (int argc, char** argv) {
|
int main (int argc, char** argv) {
|
||||||
listdir(argv[1]);
|
listdir(argv[1]);
|
||||||
printf("\ntotals\nfile count: %d\tdir count: %d\tlink count: %d\nspace used: %lu\n", file_cnt, dir_cnt, link_cnt, space_used);
|
printf("\ntotals\nfile count: %d\tdir count: %d\tlink count: %d\nspace used: %lu\n", file_cnt, dir_cnt, link_cnt, space_used);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
164
dircount_v2.cc
164
dircount_v2.cc
|
@ -1,82 +1,82 @@
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Hashtable {
|
class Hashtable {
|
||||||
unordered_map<ino_t, bool> ht;
|
unordered_map<ino_t, bool> ht;
|
||||||
public:
|
public:
|
||||||
void put(ino_t key, bool value) { ht[key] = value; }
|
void put(ino_t key, bool value) { ht[key] = value; }
|
||||||
bool get(ino_t key) { return ht[key]; }
|
bool get(ino_t key) { return ht[key]; }
|
||||||
};
|
};
|
||||||
|
|
||||||
int dircnt = 0, filecnt = 0, lnkcnt = 0;
|
int dircnt = 0, filecnt = 0, lnkcnt = 0;
|
||||||
unsigned long space_used;
|
unsigned long space_used;
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
Hashtable ht;
|
Hashtable ht;
|
||||||
|
|
||||||
bool listFileAndType(const string &dir) {
|
bool listFileAndType(const string &dir) {
|
||||||
DIR *dirp = opendir(dir.c_str());
|
DIR *dirp = opendir(dir.c_str());
|
||||||
|
|
||||||
if (dirp) {
|
if (dirp) {
|
||||||
struct dirent *dp = 0;
|
struct dirent *dp = 0;
|
||||||
|
|
||||||
while ((dp = readdir(dirp)) != 0 ) {
|
while ((dp = readdir(dirp)) != 0 ) {
|
||||||
string file( dp->d_name );
|
string file( dp->d_name );
|
||||||
|
|
||||||
if (file == "." || file == "..") continue;
|
if (file == "." || file == "..") continue;
|
||||||
|
|
||||||
if (dp->d_type == DT_DIR) {
|
if (dp->d_type == DT_DIR) {
|
||||||
string dirPath = dir + "/" + file;
|
string dirPath = dir + "/" + file;
|
||||||
cout << "[d] " << dirPath << endl;
|
cout << "[d] " << dirPath << endl;
|
||||||
dircnt++;
|
dircnt++;
|
||||||
// recurse into directory
|
// recurse into directory
|
||||||
listFileAndType( dirPath );
|
listFileAndType( dirPath );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (ht.get(dp->d_ino)) continue;
|
if (ht.get(dp->d_ino)) continue;
|
||||||
switch(dp->d_type) {
|
switch(dp->d_type) {
|
||||||
case DT_REG:
|
case DT_REG:
|
||||||
filecnt++;
|
filecnt++;
|
||||||
cout << "[f] ";
|
cout << "[f] ";
|
||||||
break;
|
break;
|
||||||
case DT_LNK:
|
case DT_LNK:
|
||||||
lnkcnt++;
|
lnkcnt++;
|
||||||
cout << "[l] ";
|
cout << "[l] ";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
cout << "[none] ";
|
cout << "[none] ";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
string statpath = dir + "/" + file;
|
string statpath = dir + "/" + file;
|
||||||
ht.put(dp->d_ino, true);
|
ht.put(dp->d_ino, true);
|
||||||
stat(statpath.c_str(), &buf);
|
stat(statpath.c_str(), &buf);
|
||||||
space_used += buf.st_blocks * 512;
|
space_used += buf.st_blocks * 512;
|
||||||
|
|
||||||
cout << statpath << endl;
|
cout << statpath << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir( dirp );
|
closedir( dirp );
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main( int argc, char **argv ) {
|
int main( int argc, char **argv ) {
|
||||||
const string dir = (argc > 1 ? argv[1] : "foo");
|
const string dir = (argc > 1 ? argv[1] : "foo");
|
||||||
|
|
||||||
if (!listFileAndType(dir)) {
|
if (!listFileAndType(dir)) {
|
||||||
cout << "Error: Cannot open directory '" << dir << "'" << endl;
|
cout << "Error: Cannot open directory '" << dir << "'" << endl;
|
||||||
}
|
}
|
||||||
cout << endl << "totals" << endl;
|
cout << endl << "totals" << endl;
|
||||||
cout << "file count: " << filecnt << "\tdir cnt: " << dircnt << "\tlink cnt: " << lnkcnt << endl;
|
cout << "file count: " << filecnt << "\tdir cnt: " << dircnt << "\tlink cnt: " << lnkcnt << endl;
|
||||||
cout << "space used: " << space_used << endl;
|
cout << "space used: " << space_used << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue