sharedFuncs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: sharedFuncs.c /main/9 1998/04/06 13:15:57 mgreess $ */
  24. /************************************<+>*************************************
  25. ****************************************************************************
  26. *
  27. * FILE: sharedFuncs.c
  28. *
  29. *
  30. * DESCRIPTION: Common functions for both dtfile and dtfile_copy
  31. *
  32. * FUNCTIONS: CreateDefaultImage
  33. * ImageInitialize
  34. * auto_rename
  35. * build_path
  36. * generate_NewPath
  37. * split_path
  38. *
  39. * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
  40. * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
  41. * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
  42. * (c) Copyright 1993, 1994, 1995 Novell, Inc.
  43. *
  44. ****************************************************************************
  45. ************************************<+>*************************************/
  46. #if defined(SVR4)
  47. # include <sys/fs/ufs_fs.h>
  48. # define ROOTINO UFSROOTINO
  49. #endif /* SVR4 */
  50. #if defined(__linux__) || defined(CSRG_BASED)
  51. # include <sys/param.h>
  52. # define ROOTINO 2
  53. #endif
  54. #include <string.h>
  55. #include <sys/stat.h>
  56. #include <stdio.h>
  57. #include <errno.h>
  58. #include <sys/types.h>
  59. #include <sys/mount.h>
  60. #include <pwd.h>
  61. #include <fcntl.h>
  62. #if !defined(CSRG_BASED) && !defined(__linux__)
  63. #include <ustat.h>
  64. #endif
  65. #if defined(__linux__)
  66. #include <sys/vfs.h>
  67. #include <linux/magic.h>
  68. #endif
  69. #include <dirent.h>
  70. #include <Xm/Xm.h>
  71. #include <Xm/Form.h>
  72. #include <Xm/PushB.h>
  73. #include <Dt/Connect.h>
  74. #include <Dt/DtNlUtils.h>
  75. #include <Dt/SharedProcs.h>
  76. #include "sharedFuncs.h"
  77. #include "dtcopy.h"
  78. /************************************************************************
  79. * Bitmap Data for Default Symbol
  80. **********************************<->***********************************/
  81. static unsigned char errorBits[] = {
  82. 0x00, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0xf0, 0x3a, 0x00, 0x58, 0x55, 0x00,
  83. 0x2c, 0xa0, 0x00, 0x56, 0x40, 0x01, 0xaa, 0x80, 0x02, 0x46, 0x81, 0x01,
  84. 0x8a, 0x82, 0x02, 0x06, 0x85, 0x01, 0x0a, 0x8a, 0x02, 0x06, 0x94, 0x01,
  85. 0x0a, 0xe8, 0x02, 0x14, 0x50, 0x01, 0x28, 0xb0, 0x00, 0xd0, 0x5f, 0x00,
  86. 0xa0, 0x2a, 0x00, 0x40, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  87. static unsigned char infoBits[] = {
  88. 0x00, 0x00, 0x78, 0x00, 0x54, 0x00, 0x2c, 0x00, 0x54, 0x00, 0x28, 0x00,
  89. 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x2a, 0x00, 0x5c, 0x00, 0x28, 0x00,
  90. 0x58, 0x00, 0x28, 0x00, 0x58, 0x00, 0x28, 0x00, 0x58, 0x00, 0x28, 0x00,
  91. 0x58, 0x00, 0xae, 0x01, 0x56, 0x01, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00};
  92. static unsigned char questionBits[] = {
  93. 0xf0, 0x3f, 0x00, 0x58, 0x55, 0x00, 0xac, 0xaa, 0x00, 0xd6, 0x5f, 0x01,
  94. 0xea, 0xbf, 0x02, 0xf6, 0x7f, 0x01, 0xea, 0xba, 0x02, 0xf6, 0x7d, 0x05,
  95. 0xea, 0xba, 0x0a, 0x56, 0x7d, 0x15, 0xaa, 0xbe, 0x1e, 0x56, 0x5f, 0x01,
  96. 0xac, 0xaf, 0x02, 0x58, 0x57, 0x01, 0xb0, 0xaf, 0x00, 0x60, 0x55, 0x01,
  97. 0xa0, 0xaa, 0x00, 0x60, 0x17, 0x00, 0xa0, 0x2f, 0x00, 0x60, 0x17, 0x00,
  98. 0xb0, 0x2a, 0x00, 0x50, 0x55, 0x00};
  99. static unsigned char warningBits[] = {
  100. 0x00, 0x00, 0x18, 0x00, 0x2c, 0x00, 0x56, 0x00, 0x2a, 0x00, 0x56, 0x00,
  101. 0x2a, 0x00, 0x56, 0x00, 0x2c, 0x00, 0x14, 0x00, 0x2c, 0x00, 0x14, 0x00,
  102. 0x2c, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x14, 0x00,
  103. 0x2c, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00};
  104. static unsigned char workingBits[] = {
  105. 0x00, 0x00, 0x00, 0xfe, 0xff, 0x0f, 0xaa, 0xaa, 0x0a, 0x44, 0x55, 0x06,
  106. 0xcc, 0x2a, 0x02, 0x44, 0x55, 0x06, 0xcc, 0x2a, 0x02, 0x84, 0x15, 0x06,
  107. 0x8c, 0x2a, 0x02, 0x04, 0x15, 0x06, 0x0c, 0x0a, 0x02, 0x04, 0x06, 0x06,
  108. 0x0c, 0x0b, 0x02, 0x84, 0x15, 0x06, 0xcc, 0x2a, 0x02, 0x44, 0x55, 0x06,
  109. 0xcc, 0x2a, 0x02, 0x44, 0x55, 0x06, 0xcc, 0x2a, 0x02, 0x44, 0x55, 0x06,
  110. 0xfe, 0xff, 0x0f, 0x56, 0x55, 0x05, 0x00, 0x00, 0x00};
  111. Widget G_toplevel;
  112. int G_dialog_closed = FALSE;
  113. /*----------------------------------------------------------
  114. *
  115. * split_path
  116. *
  117. * Given a path, return a pointer to the directory (folder)
  118. * and file (object). On error, both will be set to empty
  119. * strings.
  120. *
  121. * The calling routine is responsible for allocating space
  122. * for object and folder.
  123. *
  124. * This function simply searches for the last slash (/) in
  125. * the path and returns the characters preceding the slash
  126. * as folder and the characters after the last slash as
  127. * object. Thus, object could be a directory/folder or a
  128. * file.
  129. *
  130. * There is a complimentary function, build_path, to put
  131. * object and folder back together to form a path.
  132. *
  133. *-----------------------------------------------------------*/
  134. void
  135. split_path(const String path, String folder, String object)
  136. {
  137. String lastSlash;
  138. if ( (lastSlash = strrchr(path,'/')) != NULL)
  139. {
  140. strcpy(object,lastSlash+1);
  141. if(lastSlash == path) /* Must be root Folder */
  142. strcpy(folder,"/");
  143. else
  144. {
  145. *lastSlash = '\0';
  146. strcpy(folder,path);
  147. *lastSlash = '/';
  148. }
  149. }
  150. else
  151. {
  152. folder[0] = object[0] = '\0';
  153. }
  154. } /* end split_path */
  155. char *
  156. get_path(char *path)
  157. {
  158. char *rpath, tmppath[MAX_PATH];
  159. char * _DtCopyPathFromInput();
  160. if (!getcwd(tmppath,MAX_PATH))
  161. return NULL;
  162. rpath = _DtCopyPathFromInput(path,tmppath);
  163. return rpath;
  164. }
  165. /*-----------------------------------------------------------------
  166. *
  167. * build_path
  168. *
  169. * Given a directory (folder) and file (object), build a string
  170. * with the complete path and return it.
  171. *
  172. * The calling routine is responsible for freeing storage used
  173. * for the returned string.
  174. *
  175. * There is a complimentary function, split_path, to take
  176. * path apart to form folder and object
  177. *
  178. *-----------------------------------------------------------------*/
  179. String
  180. build_path(const String folder, const String object)
  181. {
  182. char s[MAX_PATH];
  183. String path;
  184. strncpy(s, folder, MAX_PATH);
  185. strncat(s, "/", MAX_PATH-strlen(s));
  186. strncat(s, object, MAX_PATH-strlen(s));
  187. path = (String) malloc(strlen(s)+1);
  188. strcpy(path,s);
  189. return path;
  190. } /* end build_path */
  191. /*--------------------------------------------------------------
  192. *
  193. * auto_rename
  194. *
  195. * Given a path, generate a new file name and rename the file.
  196. *
  197. * The rc from the system call is returned and errno is set.
  198. *
  199. *--------------------------------------------------------------*/
  200. int
  201. auto_rename(const String path)
  202. {
  203. char newPath[MAX_PATH];
  204. generate_NewPath(newPath,path);
  205. errno = 0;
  206. return (rename(path,newPath));
  207. } /* end auto_rename */
  208. /*-----------------------------------------------------------------------
  209. *
  210. * generate_NewPath
  211. *
  212. * Given a path (and a complete path is required), append a
  213. * sequence number to generate a new file name. The new file
  214. * will not exist. The sequence number consists of a one-character
  215. * delimiter and an integer. The function will start with
  216. * 1, if the file with 1 exists it will continue to 2, etc.
  217. * newPath and oldPath can point to the same area. The sequence number
  218. * is appended to the end of the name unless it contains a
  219. * dot, in which case the sequence number precedes the dot. For example,
  220. * /cde/dtfile/dtcopy/overwrtdialog.c becomes
  221. * /cde/dtfile/dtcopy/overwrtdialog~1.c. However, if the dot
  222. * is the first character in the file name, the sequence number is
  223. * appended, (e.g. .profile --> .profile~1).
  224. *
  225. *-----------------------------------------------------------------------*/
  226. void
  227. generate_NewPath(String newPath, String oldPath)
  228. {
  229. const char delim = '_';
  230. struct stat buf;
  231. String lastDot, lastSlash;
  232. char firstPart[MAX_PATH], lastPart[MAX_PATH];
  233. int i = 0;
  234. int len;
  235. lastDot = strrchr(oldPath,'.');
  236. lastSlash = strrchr(oldPath,'/');
  237. if (lastSlash == NULL)
  238. lastSlash = oldPath - 1; /* allows for no path and first char is dot */
  239. len = lastDot - oldPath;
  240. if ( lastDot != NULL && /* no dot */
  241. lastDot > lastSlash+1 && /* dot is in filename, not directory name */
  242. len != strlen(oldPath)-1 ) /* dot is not last character of filename */
  243. {
  244. /* sequence number will be inserted before filename suffix */
  245. memcpy(firstPart,oldPath,len);
  246. firstPart[len] = '\0';
  247. strcpy(lastPart,lastDot);
  248. }
  249. else
  250. {
  251. /* sequence number will be appended to filename */
  252. strcpy(firstPart,oldPath);
  253. lastPart[0] = '\0';
  254. }
  255. do
  256. {
  257. i++;
  258. sprintf(newPath,"%s%c%d%s",firstPart,delim,i,lastPart);
  259. } while (lstat(newPath,&buf) == 0);
  260. return;
  261. } /* end generate_NewPath */
  262. /****************************************************************
  263. * Create a default images for symbol... used in ClassInitialize.
  264. ****************/
  265. XImage *
  266. CreateDefaultImage(
  267. Display *display,
  268. char *bits,
  269. unsigned int width,
  270. unsigned int height )
  271. {
  272. XImage *image ;
  273. image = XCreateImage(display,
  274. DefaultVisual(display, DefaultScreen(display)),
  275. 1, XYBitmap, 0, bits, width, height, 8,
  276. (width+7) >> 3);
  277. image->byte_order = LSBFirst;
  278. image->bitmap_unit = 8;
  279. image->bitmap_bit_order = LSBFirst;
  280. return( image) ;
  281. } /* end CreateDefaultImage */
  282. void
  283. ImageInitialize( Display *display )
  284. {
  285. XImage *image;
  286. /* create and install the default images for the symbol */
  287. image = CreateDefaultImage (display, (char *)errorBits, 20, 20);
  288. XmInstallImage (image, "default_xm_error");
  289. image = CreateDefaultImage (display, (char *)infoBits, 11, 24);
  290. XmInstallImage (image, "default_xm_information");
  291. image = CreateDefaultImage (display, (char *)questionBits, 22, 22);
  292. XmInstallImage (image, "default_xm_question");
  293. image = CreateDefaultImage (display, (char *)warningBits, 9, 22);
  294. XmInstallImage (image, "default_xm_warning");
  295. image = CreateDefaultImage (display, (char *)workingBits, 21, 23);
  296. XmInstallImage (image, "default_xm_working");
  297. return ;
  298. } /* end ImageInitialize */
  299. static int
  300. CopyCheckDeletePermissionRecur(
  301. char *destinationPath)
  302. {
  303. struct stat statbuf;
  304. DIR *dirp;
  305. struct dirent * dp;
  306. Boolean first_file;
  307. char *fnamep;
  308. DPRINTF(("CheckDeletePermissionRecur(\"%s\")\n", destinationPath));
  309. if (lstat(destinationPath, &statbuf) < 0)
  310. return -1; /* probably does not exist */
  311. if (! S_ISDIR(statbuf.st_mode))
  312. {
  313. if(access(destinationPath,04) < 0)
  314. return -1;
  315. return 0; /* no need to check anything more */
  316. }
  317. dirp = opendir (destinationPath);
  318. if (dirp == NULL)
  319. return -1; /* could not read directory */
  320. first_file = True;
  321. while (dp = readdir (dirp))
  322. {
  323. if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
  324. {
  325. if (first_file)
  326. {
  327. /* check for write permission in this directory */
  328. if (access(destinationPath, 04|02|01) < 0)
  329. {
  330. closedir(dirp);
  331. return -1;
  332. }
  333. /* append a '/' to the end of directory name */
  334. fnamep = destinationPath + strlen(destinationPath);
  335. *fnamep++ = '/';
  336. first_file = False;
  337. }
  338. /* append file name to end of directory name */
  339. strcpy(fnamep, dp->d_name);
  340. /* recursively check permission on this file */
  341. if (CopyCheckDeletePermissionRecur(destinationPath))
  342. {
  343. closedir(dirp);
  344. return -1;
  345. }
  346. }
  347. }
  348. closedir(dirp);
  349. return 0;
  350. }
  351. static int
  352. CopyCheckDeletePermission(
  353. char *parentdir,
  354. char *destinationPath)
  355. {
  356. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__)
  357. struct statfs statbuf;
  358. #elif defined(__NetBSD__)
  359. struct statvfs statbuf;
  360. #else
  361. struct stat statbuf;
  362. #endif
  363. char fname[PATH_MAX];
  364. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__)
  365. if (statfs(parentdir,&statbuf) < 0) /* does not exist */
  366. #elif defined(__NetBSD__)
  367. if (statvfs(parentdir,&statbuf) < 0) /* does not exist */
  368. #else
  369. if (lstat(parentdir,&statbuf) < 0) /* does not exist */
  370. #endif
  371. return -1;
  372. /* check if we are root */
  373. if (getuid() == 0)
  374. {
  375. /* if NFS, need to check if server trusts root */
  376. #if defined(CSRG_BASED)
  377. if (!strcmp(statbuf.f_fstypename, "nfs")) /* Root user and nfs */
  378. #elif defined(__linux__)
  379. if (statbuf.f_type == NFS_SUPER_MAGIC)
  380. #else
  381. /* nothing - always check if root */
  382. #endif
  383. {
  384. char *tmpfile;
  385. int rv;
  386. tmpfile = tempnam(parentdir,"dtfile");
  387. if (tmpfile)
  388. {
  389. /* Create a temporary file */
  390. if ( (rv = creat(tmpfile,O_RDONLY)) < 0)
  391. {
  392. free(tmpfile);
  393. return -1;
  394. }
  395. close(rv);
  396. /* Delete the created file */
  397. if (remove(tmpfile) < 0)
  398. {
  399. free(tmpfile);
  400. return -1;
  401. }
  402. free(tmpfile);
  403. }
  404. else
  405. return -1;
  406. }
  407. /* root user can delete anything */
  408. return 0;
  409. }
  410. /* check for read/write and execute permission on parent dir */
  411. if (access(parentdir, R_OK | W_OK | X_OK) < 0)
  412. return -1;
  413. /* copy destinationPath to tmp buffer */
  414. strcpy(fname, destinationPath);
  415. return CopyCheckDeletePermissionRecur(fname);
  416. }
  417. void
  418. CloseTopLevel(
  419. Widget w,
  420. void *client_data,
  421. void *call_data)
  422. {
  423. XtDestroyWidget((Widget)G_toplevel);
  424. exit(-1);
  425. }
  426. void
  427. CheckDeleteAccess(
  428. XtAppContext app_context,
  429. int delay,
  430. Boolean checkPerms,
  431. Boolean move,
  432. char *source_name)
  433. {
  434. if(checkPerms && move)
  435. {
  436. char title[200],*msg,*tmpmsg;
  437. char *tmpstring = strdup(source_name),*tmpptr;
  438. XEvent event;
  439. int perm_status = 0;
  440. delay = 10000;
  441. tmpptr = strrchr(tmpstring,'/');
  442. if(!tmpstring || !tmpptr) /* Error */
  443. perm_status = 1;
  444. else
  445. {
  446. if(tmpptr == tmpstring)
  447. tmpptr = "/";
  448. else
  449. {
  450. *tmpptr = '\0';
  451. tmpptr = tmpstring;
  452. }
  453. perm_status = CopyCheckDeletePermission(tmpptr,source_name);
  454. free(tmpstring);
  455. tmpstring = NULL;
  456. }
  457. if(!perm_status) /* Everything is fine just return */
  458. {
  459. return;
  460. }
  461. free(tmpstring);
  462. strcpy(title,GETMESSAGE(4,7,"Object Trash - Error"));
  463. tmpmsg = GETMESSAGE(4,8,"You do not have permission to put the object \n\n%s\n\ninto trash.\n\nUse the Change Permissions choice from the object's\npopup menu or from the Selected menu to turn on your\nRead permission on the object.\n\nNote: If this object is a folder, you must also have\nRead permission for each of the objects inside the\nfolder before you can put the folder in the trash.");
  464. msg = XtMalloc(strlen(tmpmsg)+strlen(source_name)+2);
  465. sprintf(msg,tmpmsg,source_name);
  466. _DtMessageDialog (G_toplevel, title, msg, 0, FALSE, NULL,
  467. CloseTopLevel, NULL, NULL, False, ERROR_DIALOG);
  468. /*
  469. XtAppAddTimeOut(app_context, delay, TimeoutHandler, NULL);
  470. */
  471. /* wait for user to close the dialog before exiting */
  472. XtFree(msg);
  473. while (!G_dialog_closed)
  474. {
  475. XtAppNextEvent(app_context, &event);
  476. XtDispatchEvent(&event);
  477. }
  478. }
  479. }
  480. void
  481. TimeoutHandler(XtPointer client_data, XtIntervalId *id)
  482. {
  483. exit(0);
  484. }
  485. /*
  486. * This is a generic function for resolving a cannonical path from user input.
  487. */
  488. char *
  489. _DtCopyPathFromInput(char *input_string, char *current_dir)
  490. {
  491. char *path = NULL;
  492. char *tmp_path = NULL;
  493. int dir_len;
  494. char *_DtCopyChangeTildeToHome();
  495. /* find relative path */
  496. tmp_path = path = XtNewString(input_string);
  497. /* Strip any spaces from name -- input is overwritten */
  498. path = (char *) _DtStripSpaces(path);
  499. /* Resolve, if there're any, environement variables */
  500. {
  501. FILE *pfp;
  502. char command[MAX_PATH];
  503. sprintf(command,"echo %s",path);
  504. if((pfp=popen(command,"r")) == NULL)
  505. {
  506. }
  507. else
  508. {
  509. if (fscanf(pfp,"%s",command) >= 1)
  510. {
  511. XtFree(path);
  512. path = XtNewString(command);
  513. }
  514. else
  515. path = NULL;
  516. pclose(pfp);
  517. }
  518. }
  519. if (!path)
  520. return NULL;
  521. /* Resolve '~' -- new memory is allocated, old memory is freed */
  522. if (*path == '~')
  523. path = _DtCopyChangeTildeToHome(path);
  524. /* If current dir provided, check for relative path */
  525. if (path && current_dir)
  526. {
  527. if (*path != '/')
  528. {
  529. /* file is relative path i.e. xyz/abc */
  530. if (strcmp(current_dir, "/") == 0)
  531. {
  532. tmp_path = (char *)XtMalloc(strlen(current_dir) + strlen(path) + 1);
  533. sprintf(tmp_path, "%s%s", current_dir, path);
  534. }
  535. else
  536. {
  537. tmp_path = (char *)XtMalloc(strlen(current_dir) + strlen(path) + 2);
  538. sprintf(tmp_path, "%s/%s", current_dir, path);
  539. }
  540. XtFree(path);
  541. path = tmp_path;
  542. tmp_path = NULL;
  543. }
  544. }
  545. else if (!path)
  546. {
  547. XtFree(tmp_path);
  548. return NULL;
  549. }
  550. /* Resolve '.' or '..' -- input is overwritten, output may be NULL! */
  551. /* Save pointer to path to free if output is NULL. */
  552. tmp_path = path;
  553. path = (char *) XeEliminateDots(path);
  554. /* Strip off trailing '/' */
  555. dir_len = strlen(path);
  556. if (dir_len > 1 && *(path + dir_len - 1) == '/')
  557. *(path + dir_len - 1) = '\0';
  558. return path;
  559. }
  560. char *
  561. #ifdef _NO_PROTO
  562. _DtCopyChangeTildeToHome (input_string)
  563. char *input_string;
  564. #else
  565. _DtCopyChangeTildeToHome (
  566. char *input_string)
  567. #endif
  568. {
  569. char *path;
  570. char *full_path;
  571. struct passwd * pwInfo;
  572. char * homedir = (char *) XtNewString(getenv("HOME"));
  573. if ((input_string[1] != '/'))
  574. {
  575. char *path;
  576. /* ~user or ~user/path format */
  577. /* is there a path? */
  578. path = (char *) DtStrchr(input_string, '/');
  579. /* find user */
  580. if (path)
  581. *path = '/';
  582. if ((pwInfo = getpwnam(input_string + 1)) == NULL)
  583. {
  584. /* user doesn't exist */
  585. if (path)
  586. *path = '/';
  587. return NULL;
  588. }
  589. if (path)
  590. {
  591. /* ~user/path format */
  592. *path = '/';
  593. if (strcmp(pwInfo->pw_dir, "/") == 0)
  594. {
  595. /* We don't want to end up with double '/' in the path */
  596. full_path = (char *) XtMalloc(strlen(path) + 1);
  597. strcpy(full_path, path);
  598. }
  599. else
  600. {
  601. full_path = (char *) XtMalloc(strlen(pwInfo->pw_dir) +
  602. strlen(path) + 1);
  603. sprintf(full_path, "%s%s", pwInfo->pw_dir, path);
  604. }
  605. }
  606. else
  607. {
  608. /* ~user format */
  609. full_path = XtMalloc(strlen(pwInfo->pw_dir) + 1);
  610. strcpy(full_path, pwInfo->pw_dir);
  611. }
  612. }
  613. else if (input_string[1])
  614. {
  615. /* ~/path format */
  616. /* NOTE: users_home_dir has trailing '/' */
  617. full_path = (char *) XtMalloc(strlen(homedir) + strlen(input_string+2) + 1);
  618. sprintf(full_path, "%s%s", homedir, (input_string + 2));
  619. }
  620. else
  621. {
  622. /* ~ format */
  623. full_path = XtMalloc(strlen(homedir) + 1);
  624. strcpy(full_path, homedir);
  625. }
  626. XtFree(homedir);
  627. XtFree(input_string);
  628. return(full_path);
  629. }
  630. int
  631. Check(char *spth, char *dpth, int mode)
  632. {
  633. struct stat sbuf, dbuf;
  634. char filename [MAX_PATH];
  635. char * msg;
  636. char * tmpStr;
  637. char title[300];
  638. sbuf.st_ino = 0;
  639. if (lstat (spth, &sbuf) < 0)
  640. {
  641. tmpStr = (GETMESSAGE(4,9, "Cannot open %s"));
  642. msg = XtMalloc(strlen(tmpStr)+strlen(filename)+1);
  643. sprintf(msg,tmpStr,filename);
  644. _DtMessageDialog (G_toplevel, title, msg, 0, FALSE, NULL,
  645. CloseTopLevel, NULL, NULL, False, ERROR_DIALOG);
  646. XtFree(msg);
  647. return 0;
  648. }
  649. (void) strcpy (filename, dpth);
  650. if(mode)
  651. strcpy(title,GETMESSAGE(4,5,"Object Move - Error"));
  652. else
  653. strcpy(title,GETMESSAGE(4,6,"Object Copy - Error"));
  654. dbuf.st_ino = 0;
  655. while (dbuf.st_ino != ROOTINO)
  656. {
  657. /* Destination may not be available, in which case we need to
  658. create it, so just return as successful and the remaining
  659. code takes care of everything */
  660. if (lstat (filename, &dbuf) < 0)
  661. return 0;
  662. if (dbuf.st_ino == sbuf.st_ino)
  663. {
  664. if(mode)
  665. tmpStr = GETMESSAGE(3,19,"Cannot move folder into itself. %s");
  666. else
  667. tmpStr = GETMESSAGE(3,20,"Cannot copy folder into itself. %s");
  668. msg = XtMalloc(strlen(tmpStr)+strlen(dpth)+1);
  669. sprintf(msg,tmpStr,dpth);
  670. _DtMessageDialog (G_toplevel, title, msg, 0, FALSE, NULL,
  671. CloseTopLevel, NULL, NULL, False, ERROR_DIALOG);
  672. XtFree(msg);
  673. return(1);
  674. }
  675. (void) strcat (filename, "/..");
  676. }
  677. return(0);
  678. }