Tnode *add_tnode(Tnode *current_tnode, char* value) { if (current_tnode == NULL) { //if the pointer passed is null, allocate a memory space for it return createNode(value); } else { //if the node passed in is available int compare = strcmp(value, (current_tnode)->data); //compare the data with the parent data if (compare < 0) { //if the new node is smaller than the passed in node, add to the left of the parent if (current_tnode->left == NULL) { //if left child is NULL, allocate a memory for it current_tnode->left = createNode(value); return current_tnode->left; } else { return add_tnode(current_tnode->left, value); //if not recursively compare to its children } } else if (compare > 0) { //if the new node is larger than the passed in node, add to the right of the parent if (current_tnode->right == NULL) { //if right child is NULL, allocate a memory for it current_tnode->right = createNode(value); return current_tnode->right; } else { return add_tnode(current_tnode->right, value); //if not recursively compare to its children } } else { //if the new node is equal to the passed in node, don't add it printf("data was not added because it exists in the Tree\n"); return NULL; } } }
/** adds a tnode to a tree * @param current_tnode pointer to the Tnode which we are adding below (initially root node) */ Tnode* add_tnode(Tnode* current_tnode, char* value){ if(current_tnode == NULL){ //if the pointer is null, allocate a new node create_tnode(current_tnode, value); } else if(strcmp(current_tnode->data, value)<0){ //the string at current node comes before value current_tnode->right = add_tnode(current_tnode->right, value); //add node to the right subtree } else { //string at current node either matches or comes after value current_tnode->left = add_tnode(current_tnode->left, value); //add node to the left subtree } }
static void charge(char *n) { struct stat statb; struct disk *entry; struct passwd *pw; if (lstat(n, &statb) == -1) return; /* * do not count the duplicate entries. */ if (statb.st_nlink > 1) { switch (add_tnode(&tree, statb.st_dev, statb.st_ino)) { case 0: /* already exist */ return; case 1: /* added */ break; default: perror("acctdusg"); exit(1); } } /* * st_blocks is not defined for character/block special files. */ if (S_ISCHR(statb.st_mode) || S_ISBLK(statb.st_mode)) statb.st_blocks = 0; /* * If -p is given, we've all loaded the passwd entries. * Files with unknown uid should go into nchrg. Otherwise * (without -p), we try creating new entry for the uid. */ if ((entry = hash_find(statb.st_uid)) == NULL) { if (pfile == NULL) { pw = getpwuid(statb.st_uid); entry = hash_insert(statb.st_uid); if (pw != NULL) { validate_entry(entry, pw); } } } if (entry != NULL && entry->validuser) { entry->dsk_du += statb.st_blocks; } else if (nchrg) { (void) fprintf(nchrg, "%9ld\t%7llu\t%s\n", statb.st_uid, statb.st_blocks, n); } }
int main() { srand(time(NULL)); //reset random number seed every time program runs Tnode* root_node; root_node = NULL; printf("randomly generated strings inside of tree:\n"); char* str1 = rand_str_generator(6); printf("%s\n", str1); char* str2 = rand_str_generator(6); printf("%s\n", str2); char* str3 = rand_str_generator(6); printf("%s\n", str3); char* str4 = rand_str_generator(6); printf("%s\n", str4); char* str5 = rand_str_generator(6); printf("%s\n", str5); char* str6 = rand_str_generator(6); printf("%s\n", str6); Tnode* tree; tree = add_tnode(root_node, str1); add_tnode(tree, str2); add_tnode(tree, str3); add_tnode(tree, str4); add_tnode(tree, str5); add_tnode(tree, str6); printf("\n"); printf("Printing in-order traversal of tree:\n"); print_tree(tree); //free all the nodes of the tree free_tree(tree); }
int main() { srand(time(NULL)); // seed random function int i; Tnode* tree = add_tnode(tree,randomchar(length_string)); // initialize a tree for(i=0; i<num_nodes; i++) //insert random elements insert(tree, randomchar(length_string) ); printTree(tree); // print the tree once printf("--------free from now on-----------\n"); freeTree(tree); // go in the right order and free the tree // printTree(tree); if this line added, it shows that the free function works return 0; }
int main() { Tnode* root_node; root_node = NULL; char *data[] = {"first" , "second", "third", "fourth" , "fifth", "sixth"}; Tnode* tree; tree = add_tnode(root_node, data[0]); add_tnode(tree, data[1]); add_tnode(tree, data[2]); add_tnode(tree, data[3]); add_tnode(tree, data[4]); add_tnode(tree, data[5]); print_tree(tree); }
int main() { const int numchrs = 6; const int numwords = 12; srand(time(0)); Tnode* root = NULL; printf("Tree sort of random string (biggest to smallest): \n"); for(int i=0; i<numwords; i++) { root = add_tnode(root, generate_str(numchrs)); } print_tnode(root); delete_tnode(root); printf("\nTest run successfully.\n"); return 0; }
int main() { Tnode* tree = add_tnode(tree,"Father"); // initialize a tree Tnode* a; Tnode* b; Tnode* c; Tnode* d; Tnode* e; // just put some values a = add_tnode( a, "Karl"); b = add_tnode( b, "Josh"); c = add_tnode( c, "Andrew"); d = add_tnode( d, "Ben"); e = add_tnode( e, "Charles"); insert(tree, a->value); // insert values and prints them for testing insert(tree, b->value); printTree(tree); printf("-----\n"); insert(tree, c->value); insert(tree, d->value); printTree(tree); printf("-----\n"); insert(tree, e->value); printTree(tree); return 0; // success }
/* * chgrpr() - recursive chown() * * Recursively chowns the input directory then its contents. rflag must * have been set if chgrpr() is called. The input directory should not * be a sym link (this is handled in the calling routine). In * addition, the calling routine should have already added the input * directory to the search tree so we do not get into endless loops. * Note: chgrpr() doesn't need a return value as errors are reported * through the global "status" variable. */ static void chgrpr(char *dir, gid_t gid) { struct dirent *dp; DIR *dirp; struct stat st, st2; char savedir[1024]; if (getcwd(savedir, 1024) == 0) { (void) fprintf(stderr, "chgrp: "); (void) fprintf(stderr, gettext("%s\n"), savedir); exit(255); } /* * Attempt to chown the directory, however don't return if we * can't as we still may be able to chown the contents of the * directory. Note: the calling routine resets the SUID bits * on this directory so we don't have to perform an extra 'stat'. */ CHOWN(dir, -1, gid); if (chdir(dir) < 0) { status += Perror(dir); return; } if ((dirp = opendir(".")) == NULL) { status += Perror(dir); return; } for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..") == 0)) { continue; /* skip "." and ".." */ } if (lstat(dp->d_name, &st) < 0) { status += Perror(dp->d_name); continue; } if ((st.st_mode & S_IFMT) == S_IFLNK) { if (hflag || Pflag) { /* * Change the group id of the symbolic link * encountered while traversing the * directory. Don't follow the symbolic * link to any other part of the file * hierarchy. */ LCHOWN(dp->d_name, -1, gid); } else { if (stat(dp->d_name, &st2) < 0) { status += Perror(dp->d_name); continue; } /* * We know that we are to change the * group of the file referenced by the * symlink encountered while traversing * the directory. Now check to see if we * are to follow the symlink to any other * part of the file hierarchy. */ if (FOLLOW_D_LINKS) { if ((st2.st_mode & S_IFMT) == S_IFDIR) { /* * We are following symlinks so * traverse into the directory. * Add this node to the search * tree so we don't get into an * endless loop. */ int rc; if ((rc = add_tnode(&tree, st2.st_dev, st2.st_ino)) == 1) { chgrpr(dp->d_name, gid); /* * Restore SET[UG]ID * bits. */ SETUGID_PRESERVE( dp->d_name, st2.st_mode & ~S_IFMT); } else if (rc == 0) { /* already visited */ continue; } else { /* * An error occurred * while trying to add * the node to the tree. */ status += Perror( dp->d_name); continue; } } else { /* * Change the group id of the * file referenced by the * symbolic link. */ CHOWN(dp->d_name, -1, gid); } } else { /* * Change the group id of the file * referenced by the symbolic link. */ CHOWN(dp->d_name, -1, gid); if ((st2.st_mode & S_IFMT) == S_IFDIR) { /* Restore SET[UG]ID bits. */ SETUGID_PRESERVE(dp->d_name, st2.st_mode & ~S_IFMT); } } } } else if ((st.st_mode & S_IFMT) == S_IFDIR) { /* * Add this node to the search tree so we don't * get into a endless loop. */ int rc; if ((rc = add_tnode(&tree, st.st_dev, st.st_ino)) == 1) { chgrpr(dp->d_name, gid); /* Restore the SET[UG]ID bits. */ SETUGID_PRESERVE(dp->d_name, st.st_mode & ~S_IFMT); } else if (rc == 0) { /* already visited */ continue; } else { /* * An error occurred while trying * to add the node to the search tree. */ status += Perror(dp->d_name); continue; } } else { CHOWN(dp->d_name, -1, gid); } } (void) closedir(dirp); if (chdir(savedir) < 0) { (void) fprintf(stderr, "chgrp: "); (void) fprintf(stderr, gettext("can't change back to %s\n"), savedir); exit(255); } }
int main(int argc, char *argv[]) { int c; /* set the locale for only the messages system (all else is clean) */ (void) setlocale(LC_ALL, ""); #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ #endif (void) textdomain(TEXT_DOMAIN); while ((c = getopt(argc, argv, "RhfHLPs")) != EOF) switch (c) { case 'R': rflag++; break; case 'h': hflag++; break; case 'f': fflag++; break; case 'H': /* * If more than one of -H, -L, and -P * are specified, only the last option * specified determines the behavior of * chgrp. In addition, make [-H|-L] * mutually exclusive of -h. */ Lflag = Pflag = 0; Hflag++; break; case 'L': Hflag = Pflag = 0; Lflag++; break; case 'P': Hflag = Lflag = 0; Pflag++; break; case 's': sflag++; break; default: usage(); } /* * Set Pflag by default for recursive operations * if no other options were specified. */ if (rflag && !(Lflag || Hflag || Pflag || hflag)) { Pflag = 1; } /* * Check for sufficient arguments * or a usage error. */ argc -= optind; argv = &argv[optind]; if ((argc < 2) || ((Hflag || Lflag || Pflag) && !rflag) || ((Hflag || Lflag || Pflag) && hflag)) { usage(); } if (sflag) { if (sid_to_id(argv[0], B_FALSE, &gid)) { (void) fprintf(stderr, gettext( "chgrp: invalid group sid %s\n"), argv[0]); exit(2); } } else if ((gr = getgrnam(argv[0])) != NULL) { gid = gr->gr_gid; } else { if (isnumber(argv[0])) { errno = 0; /* gid is an int */ gid = (gid_t)strtoul(argv[0], NULL, 10); if (errno != 0) { if (errno == ERANGE) { (void) fprintf(stderr, gettext( "chgrp: group id is too large\n")); exit(2); } else { (void) fprintf(stderr, gettext( "chgrp: invalid group id\n")); exit(2); } } } else { (void) fprintf(stderr, "chgrp: "); (void) fprintf(stderr, gettext("unknown group: %s\n"), argv[0]); exit(2); } } for (c = 1; c < argc; c++) { tree = NULL; if (lstat(argv[c], &stbuf) < 0) { status += Perror(argv[c]); continue; } if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFLNK)) { if (hflag || Pflag) { /* * Change the group id of the symbolic link * specified on the command line. * Don't follow the symbolic link to * any other part of the file hierarchy. */ LCHOWN(argv[c], -1, gid); } else { if (stat(argv[c], &stbuf2) < 0) { status += Perror(argv[c]); continue; } /* * We know that we are to change the * group of the file referenced by the * symlink specified on the command line. * Now check to see if we are to follow * the symlink to any other part of the * file hierarchy. */ if (FOLLOW_CL_LINKS) { if ((stbuf2.st_mode & S_IFMT) == S_IFDIR) { /* * We are following symlinks so * traverse into the directory. * Add this node to the search * tree so we don't get into an * endless loop. */ if (add_tnode(&tree, stbuf2.st_dev, stbuf2.st_ino) == 1) { chgrpr(argv[c], gid); /* * Try to restore the * SET[UG]ID bits. */ SETUGID_PRESERVE( argv[c], stbuf2.st_mode & ~S_IFMT); } else { /* * Error occurred. * rc can't be 0 * as this is the first * node to be added to * the search tree. */ status += Perror( argv[c]); } } else { /* * Change the group id of the * file referenced by the * symbolic link. */ CHOWN(argv[c], -1, gid); } } else { /* * Change the group id of the file * referenced by the symbolic link. */ CHOWN(argv[c], -1, gid); if ((stbuf2.st_mode & S_IFMT) == S_IFDIR) { /* Reset the SET[UG]ID bits. */ SETUGID_PRESERVE(argv[c], stbuf2.st_mode & ~S_IFMT); } } } } else if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFDIR)) { /* * Add this node to the search tree so we don't * get into a endless loop. */ if (add_tnode(&tree, stbuf.st_dev, stbuf.st_ino) == 1) { chgrpr(argv[c], gid); /* Restore the SET[UG]ID bits. */ SETUGID_PRESERVE(argv[c], stbuf.st_mode & ~S_IFMT); } else { /* * An error occurred while trying * to add the node to the tree. * Continue on with next file * specified. Note: rc shouldn't * be 0 as this was the first node * being added to the search tree. */ status += Perror(argv[c]); } } else { if (hflag || Pflag) { LCHOWN(argv[c], -1, gid); } else { CHOWN(argv[c], -1, gid); } /* If a directory, reset the SET[UG]ID bits. */ if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { SETUGID_PRESERVE(argv[c], stbuf.st_mode & ~S_IFMT); } } } return (status); }
int main() { Tnode* root = NULL; root = add_tnode(root, "this "); add_tnode(root, "is "); add_tnode(root, "a "); add_tnode(root, "test "); add_tnode(root, "to "); add_tnode(root, "SEe "); add_tnode(root, "if "); add_tnode(root, "sorts "); add_tnode(root, "in "); add_tnode(root, "the "); add_tnode(root, "Proper "); add_tnode(root, "Order "); add_tnode(root, "! "); add_tnode(root, "! "); print_tnode(root); delete_tnode(root); printf("\nTest run successfully.\n"); return 0; }