/** * @brief Do the increments */ static void increment(huff_t *huff, node_t *node) { if (!node) { return; } if (node->next != NULL && node->next->weight == node->weight) { node_t *lnode = *node->head; if (lnode != node->parent) { swap(huff, lnode, node); } swaplist(lnode, node); } if (node->prev && node->prev->weight == node->weight) { *node->head = node->prev; } else { *node->head = NULL; free_ppnode(huff, node->head); } node->weight++; if (node->next && node->next->weight == node->weight) { node->head = node->next->head; } else { node->head = get_ppnode(huff); *node->head = node; } if (node->parent) { increment(huff, node->parent); if (node->prev == node->parent) { swaplist(node, node->parent); if (*node->head == node) { *node->head = node->parent; } } } }
/* Do the increments */ static void increment(Huff* huff, Node *n) { Node *ln; if(n == nil) return; if(n->next != nil && n->next->weight == n->weight){ ln = *n->head; if(ln != n->parent) swap(huff, ln, n); swaplist(ln, n); } if(n->prev && n->prev->weight == n->weight) *n->head = n->prev; else{ *n->head = nil; free_ppnode(huff, n->head); } n->weight++; if(n->next && n->next->weight == n->weight) n->head = n->next->head; else{ n->head = get_ppnode(huff); *n->head = n; } if(n->parent){ increment(huff, n->parent); if(n->prev == n->parent){ swaplist(n, n->parent); if(*n->head == n) *n->head = n->parent; } } }
int main(int argc, char **argv) { struct fstab *fsp; char *ptr; int ret; int ch, doall; int sflag = 0, lflag = 0, hflag = 0, qflag = 0; const char *etc_fstab; if ((ptr = strrchr(argv[0], '/')) == NULL) ptr = argv[0]; if (strstr(ptr, "swapon")) which_prog = SWAPON; else if (strstr(ptr, "swapoff")) which_prog = SWAPOFF; orig_prog = which_prog; doall = 0; etc_fstab = NULL; while ((ch = getopt(argc, argv, "AadghklmqsUF:")) != -1) { switch(ch) { case 'A': if (which_prog == SWAPCTL) { doall = 1; which_prog = SWAPON; } else { usage(); } break; case 'a': if (which_prog == SWAPON || which_prog == SWAPOFF) doall = 1; else which_prog = SWAPON; break; case 'd': if (which_prog == SWAPCTL) which_prog = SWAPOFF; else usage(); break; case 'g': hflag = 'G'; break; case 'h': hflag = 'H'; break; case 'k': hflag = 'K'; break; case 'l': lflag = 1; break; case 'm': hflag = 'M'; break; case 'q': if (which_prog == SWAPON || which_prog == SWAPOFF) qflag = 1; break; case 's': sflag = 1; break; case 'U': if (which_prog == SWAPCTL) { doall = 1; which_prog = SWAPOFF; } else { usage(); } break; case 'F': etc_fstab = optarg; break; case '?': default: usage(); } } argv += optind; ret = 0; if (etc_fstab != NULL) setfstab(etc_fstab); if (which_prog == SWAPON || which_prog == SWAPOFF) { if (doall) { while ((fsp = getfsent()) != NULL) { if (strcmp(fsp->fs_type, FSTAB_SW)) continue; if (strstr(fsp->fs_mntops, "noauto")) continue; if (swap_on_off(fsp->fs_spec, 1)) { ret = 1; } else { if (!qflag) { printf("%s: %sing %s as swap device\n", getprogname(), which_prog == SWAPOFF ? "remov" : "add", fsp->fs_spec); } } } } else if (!*argv) usage(); for (; *argv; ++argv) { if (swap_on_off(*argv, 0)) { ret = 1; } else if (orig_prog == SWAPCTL) { printf("%s: %sing %s as swap device\n", getprogname(), which_prog == SWAPOFF ? "remov" : "add", *argv); } } } else { if (lflag || sflag) swaplist(lflag, sflag, hflag); else usage(); } exit(ret); }