]> cygwin.com Git - cygwin-apps/setup.git/blob - desktop.cc
* desktop.cc: make sure we use backslashes, not slashes.
[cygwin-apps/setup.git] / desktop.cc
1 /*
2 * Copyright (c) 2000, Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@cygnus.com>
13 *
14 */
15
16 /* The purpose of this file is to manage all the desktop setup, such
17 as start menu, batch files, desktop icons, and shortcuts. Note
18 that unlike other do_* functions, this one is called directly from
19 install.cc */
20
21 #include "win32.h"
22 #include <shlobj.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 #include "resource.h"
29 #include "msg.h"
30 #include "state.h"
31 #include "concat.h"
32 #include "mkdir.h"
33 #include "dialog.h"
34
35 #include "port.h"
36
37 extern "C" {
38 void make_link_2 (char *exepath, char *args, char *icon, char *lname);
39 };
40
41 char *etc_profile[] = {
42 "PATH=/bin:/usr/bin:/usr/local/bin:$PATH",
43 "unset DOSDRIVE",
44 "unset DOSDIR",
45 "unset TMPDIR",
46 "unset TMP",
47 "",
48 "if [ ! -f /etc/group ]; then",
49 " echo Creating /etc/group",
50 " mkgroup -l >/etc/group",
51 "fi",
52 "",
53 "USER=`id -un`",
54 "",
55 "# Set up USER's home directory",
56 "if [ -z \"$HOME\" ]; then",
57 " HOME=/home/$USER",
58 "fi",
59 "",
60 "if [ ! -d $HOME ]; then",
61 " mkdir -p $HOME",
62 "fi",
63 "",
64 "export HOME USER",
65 "",
66 "for i in /etc/profile.d/*.sh ; do",
67 " if [ -f $i ]; then",
68 " . $i",
69 " fi",
70 "done",
71 "",
72 "export PS1='\033]0;\\w\a",
73 "\033[32m\\u@\\h \033[33m\\w\033[0m",
74 "$ '",
75 "",
76 "cd $HOME",
77 0
78 };
79
80 #define COMMAND9XARGS "/E:4096 /c"
81 #define COMMAND9XEXE "\\command.com"
82
83 static char *batname;
84 static char *iconname;
85
86 static char *
87 backslash (char *s)
88 {
89 for (char *t = s; *t; t++)
90 if (*t == '/')
91 *t = '\\';
92 return s;
93 }
94
95 static void
96 make_link (char *linkpath, char *title, char *target)
97 {
98 char argbuf[_MAX_PATH];
99 char *fname = concat (linkpath, "/", title, ".lnk", 0);
100
101 if (_access (fname, 0) == 0)
102 return; /* already exists */
103
104 msg ("make_link %s, %s, %s\n", fname, title, target);
105
106 mkdir_p (0, fname);
107
108 char *exepath, *args;
109 OSVERSIONINFO verinfo;
110 verinfo.dwOSVersionInfoSize = sizeof (verinfo);
111
112 /* If we are running Win9x, build a command line. */
113 GetVersionEx (&verinfo);
114 if (verinfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
115 {
116 exepath = target;
117 args = "";
118 }
119 else
120 {
121 char *pccmd;
122 char windir[MAX_PATH];
123
124 GetWindowsDirectory (windir, sizeof (windir));
125 exepath = concat (windir, COMMAND9XEXE, 0);
126 sprintf (argbuf, "%s %s", COMMAND9XARGS, target);
127 args = argbuf;
128 }
129
130 msg ("make_link_2 (%s, %s, %s, %s)", exepath, args, iconname, fname);
131 make_link_2 (exepath, args, iconname, fname);
132 }
133
134 static void
135 start_menu (char *title, char *target)
136 {
137 char path[_MAX_PATH];
138 LPITEMIDLIST id;
139 int issystem = (root_scope == IDC_ROOT_SYSTEM) ? 1 : 0;
140 SHGetSpecialFolderLocation (NULL, issystem ? CSIDL_COMMON_PROGRAMS : CSIDL_PROGRAMS, &id);
141 SHGetPathFromIDList (id, path);
142 strcat (path, "/Cygnus Solutions");
143 make_link (path, title, target);
144 }
145
146 static void
147 desktop_icon (char *title, char *target)
148 {
149 char path[_MAX_PATH];
150 LPITEMIDLIST id;
151 int issystem = (root_scope == IDC_ROOT_SYSTEM) ? 1 : 0;
152 SHGetSpecialFolderLocation (NULL, issystem ? CSIDL_DESKTOP : CSIDL_COMMON_DESKTOPDIRECTORY, &id);
153 SHGetPathFromIDList (id, path);
154 make_link (path, title, target);
155 }
156
157 static void
158 make_cygwin_bat ()
159 {
160 batname = backslash (concat (root_dir, "/cygwin.bat", 0));
161
162 /* if the batch file exists, don't overwrite it */
163 if (_access (batname, 0) == 0)
164 return;
165
166 FILE *bat = fopen (batname, "wt");
167 if (!bat)
168 return;
169
170 fprintf (bat, "@echo off\n\n");
171
172 fprintf (bat, "SET MAKE_MODE=unix\n");
173
174 char *bin = backslash (concat (root_dir, "/bin", 0));
175 char *usrbin = backslash (concat (root_dir, "/usr/bin", 0));
176 char *usrlocalbin = backslash (concat (root_dir, "/usr/local/bin", 0));
177
178 fprintf (bat, "SET PATH=%s;%s;%s;\"%%PATH%%\"\n", usrlocalbin, usrbin, bin);
179
180 fprintf (bat, "%.2s\n", root_dir);
181 fprintf (bat, "chdir %s\n\n", backslash (_strdup (root_dir+2)));
182
183 fprintf (bat, "bash --login -i\n");
184
185 fclose (bat);
186
187 free (bin);
188 free (usrbin);
189 free (usrlocalbin);
190 }
191
192 static void
193 make_etc_profile ()
194 {
195 char *fname = concat (root_dir, "/etc/profile", 0);
196
197 /* if the file exists, don't overwrite it */
198 if (_access (fname, 0) == 0)
199 return;
200
201 FILE *p = fopen (fname, "wb");
202 if (!p)
203 return;
204
205 int i;
206 for (i=0; etc_profile[i]; i++)
207 fprintf (p, "%s\n", etc_profile[i]);
208
209 fclose (p);
210 }
211
212 static void
213 save_icon ()
214 {
215 iconname = backslash (concat (root_dir, "/cygwin.ico", 0));
216
217 HRSRC rsrc = FindResource (NULL, "CYGWIN.ICON", "FILE");
218 if (rsrc == NULL)
219 {
220 fatal ("FindResource failed");
221 }
222 HGLOBAL res = LoadResource (NULL, rsrc);
223 char *data = (char *) LockResource (res);
224 int len = SizeofResource (NULL, rsrc);
225
226 FILE *f = fopen (iconname, "wb");
227 if (f)
228 {
229 fwrite (data, 1, len, f);
230 fclose (f);
231 }
232 }
233
234 void
235 do_desktop (HINSTANCE h)
236 {
237 CoInitialize (NULL);
238
239 save_icon ();
240
241 make_cygwin_bat ();
242 make_etc_profile ();
243
244 start_menu ("Cygwin 1.1 Bash Shell", batname);
245
246 desktop_icon ("Cygwin", batname);
247 }
This page took 0.045158 seconds and 5 git commands to generate.