/* ** libtar_hash_add() - add an element to the hash ** returns: ** 0 success ** -1 (and sets errno) failure */ int libtar_hash_add(libtar_hash_t *h, void *data) { int bucket, i; #ifdef DS_DEBUG printf("==> libtar_hash_add(h=0x%lx, data=0x%lx)\n", h, data); #endif bucket = (*(h->hashfunc))(data, h->numbuckets); #ifdef DS_DEBUG printf(" libtar_hash_add(): inserting in bucket %d\n", bucket); #endif if (h->table[bucket] == NULL) { #ifdef DS_DEBUG printf(" libtar_hash_add(): creating new list\n"); #endif h->table[bucket] = libtar_list_new(LIST_QUEUE, NULL); } #ifdef DS_DEBUG printf("<== libtar_hash_add(): " "returning libtar_list_add()\n"); #endif i = libtar_list_add(h->table[bucket], data); if (i == 0) h->nents++; return i; }
int main(int argc, char *argv[]) { DIR *dirp; struct dirent *direntp; if(argc != 2) { fprintf( stderr, "Usage: %s tarfilename\n", argv[0]); exit(1); } if ((dirp = opendir(".")) == NULL) { /* yes, we're assuming the CWD */ perror("open CWD"); exit(2); } libtar_list_t *l; l = libtar_list_new(LIST_QUEUE, NULL); /* First arg an int, a typey-index of some sort, second arg is "libtar_cmpfunc_t" a fnptr which we set to NULL here */ while ((direntp = readdir(dirp)) != NULL) if(direntp->d_type == DT_REG) libtar_list_add(l, direntp->d_name); tarupthelist(argv[1], ".", l); closedir(dirp); return 0; }
/* ** libtar_list_dup() - copy an existing list */ libtar_list_t * libtar_list_dup(libtar_list_t *l) { libtar_list_t *newlist; libtar_listptr_t n; newlist = libtar_list_new(l->flags, l->cmpfunc); for (n = l->first; n != NULL; n = n->next) libtar_list_add(newlist, n->data); #ifdef DS_DEBUG printf("returning from libtar_list_dup()\n"); #endif return newlist; }
/* ** libtar_list_merge() - merge two lists into a new list */ libtar_list_t * libtar_list_merge(libtar_cmpfunc_t cmpfunc, int flags, libtar_list_t *list1, libtar_list_t *list2) { libtar_list_t *newlist; libtar_listptr_t n; newlist = libtar_list_new(flags, cmpfunc); n = NULL; while (libtar_list_next(list1, &n) != 0) libtar_list_add(newlist, n->data); n = NULL; while (libtar_list_next(list2, &n) != 0) libtar_list_add(newlist, n->data); return newlist; }
int main(int argc, char *argv[]) { char* tarfile; char *rootdir = NULL; int c; int mode; libtar_list_t *l; #if defined(_WIN32) && !defined(__CYGWIN__) int optind; #endif progname = basename(argv[0]); #if !defined(_WIN32) || defined(__CYGWIN__) mode = 0; while ((c = getopt(argc, argv, "cC:gtvVxz")) != -1) switch (c) { case 'V': printf("libtar %s by Mark D. Roth <*****@*****.**>\n", libtar_version); break; case 'C': rootdir = strdup(optarg); break; case 'v': verbose = 1; break; case 'g': use_gnu = 1; break; case 'c': if (mode) usage(); mode = MODE_CREATE; break; case 'x': if (mode) usage(); mode = MODE_EXTRACT; break; case 't': if (mode) usage(); mode = MODE_LIST; break; #ifdef HAVE_LIBZ case 'z': use_zlib = 1; break; #endif /* HAVE_LIBZ */ default: usage(); } if (!mode || ((argc - optind) < (mode == MODE_CREATE ? 2 : 1))) { #ifdef DEBUG printf("argc - optind == %d\tmode == %d\n", argc - optind, mode); #endif usage(); } #else mode = MODE_EXTRACT; use_zlib=1; optind = 1; #endif #ifdef DEBUG signal(SIGSEGV, segv_handler); #endif switch (mode) { case MODE_EXTRACT: return extract(argv[optind], rootdir); case MODE_CREATE: tarfile = argv[optind]; l = libtar_list_new(LIST_QUEUE, NULL); for (c = optind + 1; c < argc; c++) libtar_list_add(l, argv[c]); return create(tarfile, rootdir, l); case MODE_LIST: return list(argv[optind]); default: break; } /* NOTREACHED */ return -2; }
int main(int argc, char *argv[]) { char *tarfile = NULL; char *rootdir = NULL; int c; int mode = 0; libtar_list_t *l; int return_code = -2; while ((c = getopt(argc, argv, "clvVxh")) != -1) { switch (c) { case 'v': puts("libmavpar " PACKAGE_VERSION "\nCopyright 2015-2016 Mavicona, Quenza Inc.\n"); break; case 'c': if (mode) { usage(argv[0]); return 1; } mode = MODE_CREATE; break; case 'x': if (mode){ usage(argv[0]); return 1; } mode = MODE_EXTRACT; break; case 'l': if (mode) { usage(argv[0]); return 1; } mode = MODE_LIST; break; case 'h': default: usage(argv[0]); return 1; } } if (!mode || ((argc - optind) < (mode == MODE_CREATE ? 2 : 1))) { usage(argv[0]); return 2; } switch (mode) { case MODE_EXTRACT: return_code = par_extract(argv[optind], rootdir); break; case MODE_CREATE: tarfile = argv[optind]; l = libtar_list_new(LIST_QUEUE, NULL); for (c = optind + 1; c < argc; c++) libtar_list_add(l, argv[c]); return_code = par_create(tarfile, rootdir, l); libtar_list_free(l, NULL); break; case MODE_LIST: return_code = par_list(argv[optind]); break; default: break; } free(rootdir); return return_code; }