Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <dirent.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <pthread.h>
  9.  
  10. pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  11.  
  12. struct Node {
  13. char *value;
  14. Node *next;
  15. };
  16.  
  17. struct Queue {
  18. Node *firstNode;
  19. Node *lastNode;
  20. };
  21.  
  22. Queue *newQueue() {
  23. Queue *q = (Queue *)malloc(sizeof(struct Queue));
  24. q->firstNode = q->lastNode = NULL;
  25. return q;
  26. }
  27.  
  28. Queue *q = newQueue();
  29.  
  30. Node *newNode(char *valueToInsert) {
  31. Node *nodeToInsert = (Node *)malloc(sizeof(struct Node));
  32. nodeToInsert->value = (char *)malloc(strlen(valueToInsert));
  33. strcpy(nodeToInsert->value, valueToInsert);
  34. nodeToInsert->next = NULL;
  35. return nodeToInsert;
  36. }
  37.  
  38. void push(Queue *q, char *valueToInsert) {
  39. Node *nodeToInsert = newNode(valueToInsert);
  40. if (q->lastNode == NULL) {
  41. q->firstNode = q->lastNode = nodeToInsert;
  42. return;
  43. }
  44. q->lastNode->next = nodeToInsert;
  45. q->lastNode = nodeToInsert;
  46. }
  47.  
  48. Node *pop(Queue *q) {
  49. if (q->firstNode == NULL) {
  50. return NULL;
  51. }
  52. Node *temporaryNode;
  53. temporaryNode = q->firstNode;
  54. q->firstNode = q->firstNode->next;
  55. if (q->firstNode == NULL) {
  56. q->lastNode = NULL;
  57. }
  58. return temporaryNode;
  59. }
  60.  
  61. void printQueue(Queue *q) {
  62. if (q->lastNode == NULL) {
  63. printf("%s\n", "Empty queue!");
  64. return;
  65. }
  66. printf("%s", "QUEUE: ");
  67. Node *temporaryNode = q->firstNode;
  68. do {
  69. printf("%s ", temporaryNode->value);
  70. temporaryNode = temporaryNode->next;
  71. } while (temporaryNode != NULL);
  72. printf("\n");
  73. }
  74.  
  75. void recShow(char* dirName) {
  76. DIR* fileDescriptor;
  77. struct dirent *d;
  78. fileDescriptor = opendir(dirName);
  79. if (fileDescriptor == NULL) {
  80. printf("%s\n", "Problem with directory opening...");
  81. return;
  82. }
  83.  
  84. while ((d = readdir(fileDescriptor)) != NULL) {
  85. printf("%s/%s\n", dirName, d->d_name);
  86. if (d->d_type == DT_DIR && strcmp(".", d->d_name) != 0 && strcmp("..", d->d_name) != 0) {
  87. char *newPath = (char*)malloc(strlen(dirName)+strlen(d->d_name)+1);
  88. strcpy(newPath, dirName);
  89. strcat(newPath, "/");
  90. strcat(newPath, d->d_name);
  91. recShow(newPath);
  92. }
  93. }
  94. closedir(fileDescriptor);
  95. }
  96.  
  97. int main(int argc, char *argv[]) {
  98. if (argc < 2) {
  99. printf("%s\n", "Please provide a directory...");
  100. return 1;
  101. }
  102. //recShow(argv[1]);
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement