comparison src/fileio.c @ 400:a86b2b5e0111 r21-2-30

Import from CVS: tag r21-2-30
author cvs
date Mon, 13 Aug 2007 11:14:34 +0200
parents 74fd4e045ea6
children 2f8bb876ab1d
comparison
equal deleted inserted replaced
399:376370fb5946 400:a86b2b5e0111
444 /* Expansion of "c:" to drive and default directory. */ 444 /* Expansion of "c:" to drive and default directory. */
445 /* (NT does the right thing.) */ 445 /* (NT does the right thing.) */
446 if (p == beg + 2 && beg[1] == ':') 446 if (p == beg + 2 && beg[1] == ':')
447 { 447 {
448 /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir. */ 448 /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir. */
449 Bufbyte *res = alloca (MAXPATHLEN + 1); 449 Bufbyte *res = (Bufbyte*) alloca (MAXPATHLEN + 1);
450 if (getdefdir (toupper (*beg) - 'A' + 1, res)) 450 if (getdefdir (toupper (*beg) - 'A' + 1, (char *)res))
451 { 451 {
452 char *c=((char *) res) + strlen ((char *) res); 452 char *c=((char *) res) + strlen ((char *) res);
453 if (!IS_DIRECTORY_SEP (*c)) 453 if (!IS_DIRECTORY_SEP (*c))
454 { 454 {
455 *c++ = DIRECTORY_SEP; 455 *c++ = DIRECTORY_SEP;
843 nm = XSTRING_DATA (name); 843 nm = XSTRING_DATA (name);
844 844
845 #ifdef WINDOWSNT 845 #ifdef WINDOWSNT
846 /* We will force directory separators to be either all \ or /, so make 846 /* We will force directory separators to be either all \ or /, so make
847 a local copy to modify, even if there ends up being no change. */ 847 a local copy to modify, even if there ends up being no change. */
848 nm = strcpy (alloca (strlen (nm) + 1), nm); 848 nm = strcpy ((char *)alloca (strlen ((char *)nm) + 1), (char *)nm);
849 849
850 /* Find and remove drive specifier if present; this makes nm absolute 850 /* Find and remove drive specifier if present; this makes nm absolute
851 even if the rest of the name appears to be relative. */ 851 even if the rest of the name appears to be relative. */
852 { 852 {
853 Bufbyte *colon = strrchr (nm, ':'); 853 Bufbyte *colon = (Bufbyte *) strrchr ((char *)nm, ':');
854 854
855 if (colon) 855 if (colon)
856 /* Only recognize colon as part of drive specifier if there is a 856 /* Only recognize colon as part of drive specifier if there is a
857 single alphabetic character preceding the colon (and if the 857 single alphabetic character preceding the colon (and if the
858 character before the drive letter, if present, is a directory 858 character before the drive letter, if present, is a directory
1254 #endif /* WINDOWSNT */ 1254 #endif /* WINDOWSNT */
1255 1255
1256 return make_string (target, o - target); 1256 return make_string (target, o - target);
1257 } 1257 }
1258 1258
1259 #if 0 /* FSFmacs */
1260 /* another older version of expand-file-name; */
1261 #endif
1262
1263 DEFUN ("file-truename", Ffile_truename, 1, 2, 0, /* 1259 DEFUN ("file-truename", Ffile_truename, 1, 2, 0, /*
1264 Return the canonical name of the given FILE. 1260 Return the canonical name of the given FILE.
1265 Second arg DEFAULT is directory to start with if FILE is relative 1261 Second arg DEFAULT is directory to start with if FILE is relative
1266 (does not start with slash); if DEFAULT is nil or missing, 1262 (does not start with slash); if DEFAULT is nil or missing,
1267 the current buffer's value of default-directory is used. 1263 the current buffer's value of default-directory is used.
1268 No component of the resulting pathname will be a symbolic link, as 1264 No component of the resulting pathname will be a symbolic link, as
1269 in the realpath() function. 1265 in the realpath() function.
1270 */ 1266 */
1271 (filename, default_)) 1267 (filename, default_))
1272 { 1268 {
1273 /* This function can GC. GC checked 1997.04.06. */ 1269 /* This function can GC. */
1274 Lisp_Object expanded_name; 1270 Lisp_Object expanded_name;
1275 Lisp_Object handler;
1276 struct gcpro gcpro1; 1271 struct gcpro gcpro1;
1277 1272
1278 CHECK_STRING (filename); 1273 CHECK_STRING (filename);
1279 1274
1280 expanded_name = Fexpand_file_name (filename, default_); 1275 expanded_name = Fexpand_file_name (filename, default_);
1276
1277 GCPRO1 (expanded_name);
1281 1278
1282 if (!STRINGP (expanded_name)) 1279 if (!STRINGP (expanded_name))
1283 return Qnil; 1280 return Qnil;
1284 1281
1285 GCPRO1 (expanded_name); 1282 {
1286 handler = Ffind_file_name_handler (expanded_name, Qfile_truename); 1283 Lisp_Object handler =
1287 UNGCPRO; 1284 Ffind_file_name_handler (expanded_name, Qfile_truename);
1288 1285
1289 if (!NILP (handler)) 1286 if (!NILP (handler))
1290 return call2_check_string (handler, Qfile_truename, expanded_name); 1287 RETURN_UNGCPRO
1288 (call2_check_string (handler, Qfile_truename, expanded_name));
1289 }
1291 1290
1292 { 1291 {
1293 char resolved_path[MAXPATHLEN]; 1292 char resolved_path[MAXPATHLEN];
1294 Extbyte *path; 1293 Extbyte *path;
1295 Extbyte *p; 1294 Extbyte *p;
1299 ALLOCA, (path, elen), 1298 ALLOCA, (path, elen),
1300 Qfile_name); 1299 Qfile_name);
1301 p = path; 1300 p = path;
1302 if (elen > MAXPATHLEN) 1301 if (elen > MAXPATHLEN)
1303 goto toolong; 1302 goto toolong;
1304 1303
1305 /* Try doing it all at once. */ 1304 /* Try doing it all at once. */
1306 /* !! Does realpath() Mule-encapsulate? 1305 /* !! Does realpath() Mule-encapsulate?
1307 Answer: Nope! So we do it above */ 1306 Answer: Nope! So we do it above */
1308 if (!xrealpath ((char *) path, resolved_path)) 1307 if (!xrealpath ((char *) path, resolved_path))
1309 { 1308 {
1310 /* Didn't resolve it -- have to do it one component at a time. */ 1309 /* Didn't resolve it -- have to do it one component at a time. */
1311 /* "realpath" is a typically useless, stupid un*x piece of crap. 1310 /* "realpath" is a typically useless, stupid un*x piece of crap.
1312 It claims to return a useful value in the "error" case, but since 1311 It claims to return a useful value in the "error" case, but since
1313 there is no indication provided of how far along the pathname 1312 there is no indication provided of how far along the pathname
1314 the function went before erring, there is no way to use the 1313 the function went before erring, there is no way to use the
1315 partial result returned. What a piece of junk. */ 1314 partial result returned. What a piece of junk.
1315
1316 The above comment refers to historical versions of
1317 realpath(). The Unix98 specs state:
1318
1319 "On successful completion, realpath() returns a
1320 pointer to the resolved name. Otherwise, realpath()
1321 returns a null pointer and sets errno to indicate the
1322 error, and the contents of the buffer pointed to by
1323 resolved_name are undefined."
1324
1325 Since we depend on undocumented semantics of various system realpath()s,
1326 we just use our own version in realpath.c. */
1316 for (;;) 1327 for (;;)
1317 { 1328 {
1318 p = (Extbyte *) memchr (p + 1, '/', elen - (p + 1 - path)); 1329 p = (Extbyte *) memchr (p + 1, '/', elen - (p + 1 - path));
1319 if (p) 1330 if (p)
1320 *p = 0; 1331 *p = 0;
1321 1332
1322 /* memset (resolved_path, 0, sizeof (resolved_path)); */
1323 if (xrealpath ((char *) path, resolved_path)) 1333 if (xrealpath ((char *) path, resolved_path))
1324 { 1334 {
1325 if (p) 1335 if (p)
1326 *p = '/'; 1336 *p = '/';
1327 else 1337 else
1335 int rlen = strlen (resolved_path); 1345 int rlen = strlen (resolved_path);
1336 1346
1337 /* "On failure, it returns NULL, sets errno to indicate 1347 /* "On failure, it returns NULL, sets errno to indicate
1338 the error, and places in resolved_path the absolute pathname 1348 the error, and places in resolved_path the absolute pathname
1339 of the path component which could not be resolved." */ 1349 of the path component which could not be resolved." */
1340 if (p) 1350
1351 if (p)
1341 { 1352 {
1342 int plen = elen - (p - path); 1353 int plen = elen - (p - path);
1343 1354
1344 if (rlen > 1 && resolved_path[rlen - 1] == '/') 1355 if (rlen > 1 && resolved_path[rlen - 1] == '/')
1345 rlen = rlen - 1; 1356 rlen = rlen - 1;
1356 goto lose; 1367 goto lose;
1357 } 1368 }
1358 } 1369 }
1359 1370
1360 { 1371 {
1372 Lisp_Object resolved_name;
1361 int rlen = strlen (resolved_path); 1373 int rlen = strlen (resolved_path);
1362 if (elen > 0 && XSTRING_BYTE (expanded_name, elen - 1) == '/' 1374 if (elen > 0 && XSTRING_BYTE (expanded_name, elen - 1) == '/'
1363 && !(rlen > 0 && resolved_path[rlen - 1] == '/')) 1375 && !(rlen > 0 && resolved_path[rlen - 1] == '/'))
1364 { 1376 {
1365 if (rlen + 1 > countof (resolved_path)) 1377 if (rlen + 1 > countof (resolved_path))
1366 goto toolong; 1378 goto toolong;
1367 resolved_path[rlen] = '/'; 1379 resolved_path[rlen++] = '/';
1368 resolved_path[rlen + 1] = 0; 1380 resolved_path[rlen] = '\0';
1369 rlen = rlen + 1;
1370 } 1381 }
1371 return make_ext_string ((Bufbyte *) resolved_path, rlen, Qbinary); 1382 TO_INTERNAL_FORMAT (DATA, (resolved_path, rlen),
1383 LISP_STRING, resolved_name,
1384 Qfile_name);
1385 RETURN_UNGCPRO (resolved_name);
1372 } 1386 }
1373 1387
1374 toolong: 1388 toolong:
1375 errno = ENAMETOOLONG; 1389 errno = ENAMETOOLONG;
1376 goto lose; 1390 goto lose;
1377 lose: 1391 lose:
1378 report_file_error ("Finding truename", list1 (expanded_name)); 1392 report_file_error ("Finding truename", list1 (expanded_name));
1379 } 1393 }
1380 return Qnil; /* suppress compiler warning */ 1394 RETURN_UNGCPRO (Qnil);
1381 } 1395 }
1382 1396
1383 1397
1384 DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name, 1, 1, 0, /* 1398 DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name, 1, 1, 0, /*
1385 Substitute environment variables referred to in FILENAME. 1399 Substitute environment variables referred to in FILENAME.
1872 1886
1873 return Qnil; 1887 return Qnil;
1874 } 1888 }
1875 1889
1876 DEFUN ("delete-file", Fdelete_file, 1, 1, "fDelete file: ", /* 1890 DEFUN ("delete-file", Fdelete_file, 1, 1, "fDelete file: ", /*
1877 Delete specified file. One argument, a file name string. 1891 Delete the file named FILENAME (a string).
1878 If file has multiple names, it continues to exist with the other names. 1892 If FILENAME has multiple names, it continues to exist with the other names.
1879 */ 1893 */
1880 (filename)) 1894 (filename))
1881 { 1895 {
1882 /* This function can GC. GC checked 1997.04.06. */ 1896 /* This function can GC. GC checked 1997.04.06. */
1883 Lisp_Object handler; 1897 Lisp_Object handler;
3219 desc = open ((char *) XSTRING_DATA (fn), O_WRONLY | OPEN_BINARY, 0); 3233 desc = open ((char *) XSTRING_DATA (fn), O_WRONLY | OPEN_BINARY, 0);
3220 } 3234 }
3221 if (desc < 0) 3235 if (desc < 0)
3222 { 3236 {
3223 desc = open ((char *) XSTRING_DATA (fn), 3237 desc = open ((char *) XSTRING_DATA (fn),
3224 (O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY), 3238 O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY,
3225 ((auto_saving) ? auto_save_mode_bits : CREAT_MODE)); 3239 auto_saving ? auto_save_mode_bits : CREAT_MODE);
3226 } 3240 }
3227 3241
3228 if (desc < 0) 3242 if (desc < 0)
3229 { 3243 {
3230 #ifdef CLASH_DETECTION 3244 #ifdef CLASH_DETECTION