This file is indexed.

/usr/share/doc/libcdk5-doc/examples/demos/vinstall.c is in libcdk5-doc 5.0.20161210-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/* $Id: vinstall.c,v 1.21 2016/12/04 15:22:16 tom Exp $ */

#include <cdk_test.h>

#ifdef HAVE_XCURSES
char *XCursesProgramName = "vinstall";
#endif

/*
 * Written by:	Mike Glover
 * Purpose:
 *		This is a fairly basic install interface.
 */

/* Declare global types and prototypes. */
static const char *FPUsage = "-f filename [-s source directory] [-d destination directory] [-t title] [-o Output file] [-q]";

typedef enum
{
   vCanNotOpenSource,
   vCanNotOpenDest,
   vOK
}
ECopyFile;

static ECopyFile copyFile (CDKSCREEN *cdkScreen, char *src, char *dest);
static int verifyDirectory (CDKSCREEN *screen, char *directory);

int main (int argc, char **argv)
{
   /* *INDENT-EQLS* */
   CDKSCREEN *cdkScreen         = 0;
   CDKSWINDOW *installOutput    = 0;
   CDKENTRY *sourceEntry        = 0;
   CDKENTRY *destEntry          = 0;
   CDKLABEL *titleWin           = 0;
   CDKHISTOGRAM *progressBar    = 0;
   char *sourcePath             = 0;
   char *destPath               = 0;
   char *sourceDir              = 0;
   char *destDir                = 0;
   char *filename               = 0;
   char *title                  = 0;
   char *output                 = 0;
   int quiet                    = FALSE;
   int errors                   = 0;
   int sWindowHeight            = 0;
   char *titleMessage[10];
   char **fileList              = 0;
   const char *mesg[20];
   char oldPath[512], newPath[512], temp[2000];
   int count, ret, x;

   /* Parse up the command line. */
   while (1)
   {
      ret = getopt (argc, argv, "d:s:f:t:o:q");
      if (ret == -1)
      {
	 break;
      }
      switch (ret)
      {
      case 's':
	 sourcePath = strdup (optarg);
	 break;

      case 'd':
	 destPath = strdup (optarg);
	 break;

      case 'f':
	 filename = strdup (optarg);
	 break;

      case 't':
	 title = strdup (optarg);
	 break;

      case 'o':
	 output = strdup (optarg);
	 break;

      case 'q':
	 quiet = TRUE;
	 break;
      }
   }

   /* Make sure have everything we need. */
   if (filename == 0)
   {
      fprintf (stderr, "Usage: %s %s\n", argv[0], FPUsage);
      ExitProgram (EXIT_FAILURE);
   }

   /* Open the file list file and read it in. */
   count = CDKreadFile (filename, &fileList);
   if (count == 0)
   {
      fprintf (stderr, "%s: Input filename <%s> is empty.\n", argv[0], filename);
      ExitProgram (EXIT_FAILURE);
   }

   /*
    * Cycle through what was given to us and save it.
    */
   for (x = 0; x < count; x++)
   {
      /* Strip white space from the line. */
      stripWhiteSpace (vBOTH, fileList[x]);
   }

   cdkScreen = initCDKScreen (NULL);

   /* Start color. */
   initCDKColor ();

   /* Create the title label. */
   titleMessage[0] = copyChar ("<C></32/B><#HL(30)>");
   if (title == 0)
   {
      sprintf (temp, "<C></32/B>CDK Installer");
   }
   else
   {
      sprintf (temp, "<C></32/B>%.256s", title);
   }
   titleMessage[1] = copyChar (temp);
   titleMessage[2] = copyChar ("<C></32/B><#HL(30)>");
   titleWin = newCDKLabel (cdkScreen, CENTER, TOP,
			   (CDK_CSTRING2)titleMessage, 3,
			   FALSE, FALSE);
   freeCharList (titleMessage, 3);

   /* Allow them to change the install directory. */
   if (sourcePath == 0)
   {
      sourceEntry = newCDKEntry (cdkScreen, CENTER, 8,
				 0, "Source Directory        :",
				 A_NORMAL, '.', vMIXED,
				 40, 0, 256, TRUE, FALSE);
   }
   if (destPath == 0)
   {
      destEntry = newCDKEntry (cdkScreen, CENTER, 11,
			       0, "Destination Directory:", A_NORMAL,
			       '.', vMIXED, 40, 0, 256, TRUE, FALSE);
   }

   /* Get the source install path. */
   if (sourceEntry != 0)
   {
      drawCDKScreen (cdkScreen);
      sourceDir = copyChar (activateCDKEntry (sourceEntry, 0));
   }
   else
   {
      sourceDir = copyChar (sourcePath);
   }

   /* Get the destination install path. */
   if (destEntry != 0)
   {
      drawCDKScreen (cdkScreen);
      destDir = copyChar (activateCDKEntry (destEntry, 0));
   }
   else
   {
      destDir = copyChar (destPath);
   }

   /* Destroy the path entry fields. */
   if (sourceEntry != 0)
   {
      destroyCDKEntry (sourceEntry);
   }
   if (destEntry != 0)
   {
      destroyCDKEntry (destEntry);
   }

   /*
    * Verify that the source directory is valid.
    */
   if (verifyDirectory (cdkScreen, sourceDir) != 0)
   {
      /* Clean up and leave. */
      freeChar (destDir);
      freeChar (sourceDir);
      destroyCDKLabel (titleWin);
      destroyCDKScreen (cdkScreen);
      endCDK ();
      ExitProgram (EXIT_FAILURE);
   }

   /*
    * Verify that the source directory is valid.
    */
   if (verifyDirectory (cdkScreen, destDir) != 0)
   {
      /* Clean up and leave. */
      freeChar (destDir);
      freeChar (sourceDir);
      destroyCDKLabel (titleWin);
      destroyCDKScreen (cdkScreen);
      endCDK ();
      ExitProgram (EXIT_FAILURE);
   }

   /* Create the histogram. */
   progressBar = newCDKHistogram (cdkScreen, CENTER, 5,
				  3, 0, HORIZONTAL,
				  "<C></56/B>Install Progress",
				  TRUE, FALSE);

   /* Set the top left/right characters of the histogram. */
   setCDKHistogramLLChar (progressBar, ACS_LTEE);
   setCDKHistogramLRChar (progressBar, ACS_RTEE);

   /* Set the initial value of the histogram. */
   setCDKHistogram (progressBar, vPERCENT, TOP, A_BOLD,
		    1, count, 1,
		    COLOR_PAIR (24) | A_REVERSE | ' ',
		    TRUE);

   /* Determine the height of the scrolling window. */
   if (LINES >= 16)
   {
      sWindowHeight = LINES - 13;
   }
   else
   {
      sWindowHeight = 3;
   }

   /* Create the scrolling window. */
   installOutput = newCDKSwindow (cdkScreen, CENTER, BOTTOM,
				  sWindowHeight, 0,
				  "<C></56/B>Install Results",
				  2000, TRUE, FALSE);

   /* Set the top left/right characters of the scrolling window. */
   setCDKSwindowULChar (installOutput, ACS_LTEE);
   setCDKSwindowURChar (installOutput, ACS_RTEE);

   /* Draw the screen. */
   drawCDKScreen (cdkScreen);

   /* Start copying the files. */
   for (x = 0; x < count; x++)
   {
      char **files;
      int chunks;

      /*
       * If the 'file' list file has 2 columns, the first is
       * the source filename, the second being the destination
       * filename.
       */
      files = CDKsplitString (fileList[x], ' ');
      chunks = (int)CDKcountStrings ((CDK_CSTRING2)files);
      if (chunks == 2)
      {
	 /* Create the correct paths. */
	 sprintf (oldPath, "%s/%s", sourceDir, files[0]);
	 sprintf (newPath, "%s/%s", destDir, files[1]);
      }
      else
      {
	 /* Create the correct paths. */
	 sprintf (oldPath, "%s/%s", sourceDir, fileList[x]);
	 sprintf (newPath, "%s/%s", destDir, fileList[x]);
      }
      CDKfreeStrings (files);

      /* Copy the file from the source to the destination. */
      ret = copyFile (cdkScreen, oldPath, newPath);
      if (ret == vCanNotOpenSource)
      {
	 sprintf (temp,
		  "</16>Error: Can not open source file \"%.256s\"<!16>", oldPath);
	 errors++;
      }
      else if (ret == vCanNotOpenDest)
      {
	 sprintf (temp,
		  "</16>Error: Can not open destination file \"%.256s\"<!16>", newPath);
	 errors++;
      }
      else
      {
	 sprintf (temp, "</24>%.256s -> %.256s", oldPath, newPath);
      }

      /* Add the message to the scrolling window. */
      addCDKSwindow (installOutput, temp, BOTTOM);
      drawCDKSwindow (installOutput, ObjOf (installOutput)->box);

      /* Update the histogram. */
      setCDKHistogram (progressBar, vPERCENT, TOP, A_BOLD,
		       1, count, x + 1,
		       COLOR_PAIR (24) | A_REVERSE | ' ',
		       TRUE);

      /* Update the screen. */
      drawCDKHistogram (progressBar, TRUE);
   }

   /*
    * If there were errors, inform the user and allow them to look at the
    * errors in the scrolling window.
    */
   if (errors != 0)
   {
      /* Create the information for the dialog box. */
      const char *buttons[] =
      {
	 "Look At Errors Now",
	 "Save Output To A File",
	 "Ignore Errors"
      };
      mesg[0] = "<C>There were errors in the installation.";
      mesg[1] = "<C>If you want, you may scroll through the";
      mesg[2] = "<C>messages of the scrolling window to see";
      mesg[3] = "<C>what the errors were. If you want to save";
      mesg[4] = "<C>the output of the window you may press </R>s<!R>";
      mesg[5] = "<C>while in the window, or you may save the output";
      mesg[6] = "<C>of the install now and look at the install";
      mesg[7] = "<C>history at a later date.";

      /* Popup the dialog box. */
      ret = popupDialog (cdkScreen,
			 (CDK_CSTRING2)mesg, 8,
			 (CDK_CSTRING2)buttons, 3);
      if (ret == 0)
      {
	 activateCDKSwindow (installOutput, 0);
      }
      else if (ret == 1)
      {
	 (void)injectCDKSwindow (installOutput, 's');
      }
   }
   else
   {
      /*
       * If they specified the name of an output file, then save the
       * results of the installation to that file.
       */
      if (output != 0)
      {
	 dumpCDKSwindow (installOutput, output);
      }
      else
      {
	 /* Ask them if they want to save the output of the scrolling window. */
	 if (quiet == FALSE)
	 {
	    const char *buttons[] =
	    {
	       "No",
	       "Yes"
	    };
	    mesg[0] = "<C>Do you want to save the output of the";
	    mesg[1] = "<C>scrolling window to a file?";

	    if (popupDialog (cdkScreen,
			     (CDK_CSTRING2)mesg, 2,
			     (CDK_CSTRING2)buttons, 2) == 1)
	    {
	       (void)injectCDKSwindow (installOutput, 's');
	    }
	 }
      }
   }

   /* Clean up. */
   destroyCDKLabel (titleWin);
   destroyCDKHistogram (progressBar);
   destroyCDKSwindow (installOutput);
   destroyCDKScreen (cdkScreen);
   endCDK ();
   ExitProgram (EXIT_SUCCESS);
}

/*
 * This copies a file from one place to another. (tried rename
 * library call, but it is equivalent to mv)
 */
static ECopyFile copyFile (CDKSCREEN *cdkScreen GCC_UNUSED, char *src, char *dest)
{
   char command[2000];
   FILE *fd;

   /* Make sure we can open the source file. */
   if ((fd = fopen (src, "r")) == 0)
   {
      return vCanNotOpenSource;
   }
   fclose (fd);

   /*
    * Remove the destination file first, just in case it already exists.
    * This allows us to check if we can write to the desintation file.
    */
   sprintf (command, "rm -f %s", dest);
   system (command);

   /* Try to open the destination. */
   if ((fd = fopen (dest, "w")) == 0)
   {
      return vCanNotOpenDest;
   }
   fclose (fd);

   /*
    * Copy the file. There has to be a better way to do this. I
    * tried rename and link but they both have the same limitation
    * as the 'mv' command that you can not move across partitions.
    * Quite limiting in an install binary.
    */
   sprintf (command, "rm -f %s; cp %s %s; chmod 444 %s", dest, src, dest, dest);
   system (command);
   return vOK;
}

/*
 * This makes sure that the directory given exists. If it
 * doesn't then it will make it.
 * THINK
 */
static int verifyDirectory (CDKSCREEN *cdkScreen, char *directory)
{
   int status = 0;
#if !defined (__MINGW32__)
   mode_t dirMode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH;
#endif
   struct stat fileStat;
   char temp[512];

   /* Stat the directory. */
   if (lstat (directory, &fileStat) != 0)
   {
      /* The directory does not exist. */
      if (errno == ENOENT)
      {
	 const char *buttons[] =
	 {
	    "Yes",
	    "No"
	 };
	 const char *mesg[10];
	 char *error[10];

	 /* Create the question. */
	 mesg[0] = "<C>The directory ";
	 sprintf (temp, "<C>%.256s", directory);
	 mesg[1] = temp;
	 mesg[2] = "<C>Does not exist. Do you want to";
	 mesg[3] = "<C>create it?";

	 /* Ask them if they want to create the directory. */
	 if (popupDialog (cdkScreen,
			  (CDK_CSTRING2)mesg, 4,
			  (CDK_CSTRING2)buttons, 2) == 0)
	 {
	    /* Create the directory. */
#if defined (__MINGW32__)
	    if (mkdir (directory) != 0)
#else
	    if (mkdir (directory, dirMode) != 0)
#endif
	    {
	       /* Create the error message. */
	       error[0] = copyChar ("<C>Could not create the directory");
	       sprintf (temp, "<C>%.256s", directory);
	       error[1] = copyChar (temp);

#ifdef HAVE_STRERROR
	       sprintf (temp, "<C>%.256s", strerror (errno));
#else
	       sprintf (temp, "<C>Check the permissions and try again.");
#endif
	       error[2] = copyChar (temp);

	       /* Pop up the error message. */
	       popupLabel (cdkScreen, (CDK_CSTRING2)error, 3);

	       freeCharList (error, 3);
	       status = -1;
	    }
	 }
	 else
	 {
	    /* Create the message. */
	    error[0] = copyChar ("<C>Installation aborted.");

	    /* Pop up the error message. */
	    popupLabel (cdkScreen, (CDK_CSTRING2)error, 1);

	    freeCharList (error, 1);
	    status = -1;
	 }
      }
   }
   return status;
}