1 /*
2     wmget - A background download manager as a Window Maker dock app
3     Copyright (c) 2001-2003 Aaron Trickey <aaron@amtrickey.net>
4 
5     Permission is hereby granted, free of charge, to any person
6     obtaining a copy of this software and associated documentation files
7     (the "Software"), to deal in the Software without restriction,
8     including without limitation the rights to use, copy, modify, merge,
9     publish, distribute, sublicense, and/or sell copies of the Software,
10     and to permit persons to whom the Software is furnished to do so,
11     subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be
14     included in all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19     NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20     CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21     TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24     ********************************************************************
25     wmget.c - main() and a few common functions
26 
27     This file contains the main() for wmget; it simply checks for the
28     presence of ``dock'' as the first argument and invokes server(),
29     request(), cancel(), or list() as appropriate.  Also found here are
30     a few global utilities.
31 */
32 
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <pwd.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 
43 #include "wmget.h"
44 
debug_dump_job(Job * job)45 void debug_dump_job (Job *job)
46 {
47     if (output_level () < OL_DEBUG)
48         return;
49 
50     debug ("id = %lu\n"
51            "status = %d\n"
52            "progress=%d/%d\n"
53            "stop_request=%d\n"
54            "display=[%s]\n"
55            "source_url=[%s]\n"
56            "save_to=[%s]\n"
57            "continue_from=%d\n\n",
58            job->job_id,
59            job->status, job->progress, job->stop_request,
60            job->prog_max, job->options.display, job->source_url,
61            job->options.save_to, job->options.continue_from);
62 }
63 
64 
home_directory(void)65 const char *home_directory (void)
66 {
67     static char *home_directory = 0;
68     struct passwd *pwent;
69 
70     if (home_directory)
71         return home_directory;
72 
73     home_directory = getenv ("HOME");
74 
75     if (!home_directory) {
76         pwent = getpwuid (geteuid ());
77         home_directory = strdup (pwent->pw_dir);
78     }
79 
80     return home_directory;
81 }
82 
83 
main(int argc,char ** argv)84 int main (int argc, char **argv)
85 {
86     char **arg;
87     int (*function) (int, char **) = &request;
88 
89     if (argc < 2) {
90         usage ();
91         return 0;
92     }
93 
94     /* Cheat, because we don't invoke getopt until we know what mode
95      * we're in...
96      */
97     for (arg = argv; *arg; ++arg) {
98         if (strcmp (*arg, "dock") == 0) {
99             function = &server;
100 
101         } else if (strcmp (*arg, "cancel") == 0) {
102             function = &cancel;
103 
104         } else if (strcmp (*arg, "list") == 0) {
105             function = &list;
106 
107         } else if (strcmp (*arg, "-v") == 0
108                 || strcmp (*arg, "--version") == 0) {
109             puts (WMGET_VERSION_BANNER);
110             puts (WMGET_COPYRIGHT);
111             return 0;
112 
113         } else if (strcmp (*arg, "-h") == 0
114                 || strcmp (*arg, "--help") == 0) {
115             usage ();
116             return 0;
117         }
118     }
119 
120     return function (argc, argv);
121 }
122 
123 
124 
125 
126 
127 
128