~ubuntu-branches/ubuntu/trusty/tla/trusty

« back to all changes in this revision

Viewing changes to src/hackerlab/fs/cwd.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Suffield
  • Date: 2004-05-30 20:13:29 UTC
  • Revision ID: james.westby@ubuntu.com-20040530201329-mgovd2u99mkxi0hf
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* cwd.c: 
 
2
 *
 
3
 ****************************************************************
 
4
 * Copyright (C) 2002 Tom Lord
 
5
 * 
 
6
 * See the file "COPYING" for further information about
 
7
 * the copyright and warranty status of this work.
 
8
 */
 
9
 
 
10
 
 
11
#include "hackerlab/bugs/panic.h"
 
12
#include "hackerlab/os/errno.h"
 
13
#include "hackerlab/os/unistd.h"
 
14
#include "hackerlab/char/str.h"
 
15
#include "hackerlab/fs/cwd.h"
 
16
 
 
17
 
 
18
 
 
19
t_uchar * 
 
20
safe_current_working_directory (void)
 
21
{
 
22
  int ign;
 
23
  char * it;
 
24
 
 
25
  it = current_working_directory (&ign, 0);
 
26
  if (!it)
 
27
    panic ("unable to compute current working directory");
 
28
 
 
29
  return it;
 
30
}
 
31
 
 
32
char *
 
33
current_working_directory (int * errn, struct alloc_limits * limits)
 
34
{
 
35
  char * path;
 
36
  size_t sizeof_path;
 
37
 
 
38
  sizeof_path = 4096;
 
39
  path = lim_malloc (limits, sizeof_path);
 
40
  if (!path)
 
41
    {
 
42
    enomem_error:
 
43
      *errn = ENOMEM;
 
44
      if (path)
 
45
        lim_free (limits, path);
 
46
      return 0;
 
47
    }
 
48
 
 
49
  while (1)
 
50
    {
 
51
      if (getcwd (path, sizeof_path))
 
52
        {
 
53
          char * answer;
 
54
          answer = lim_realloc (limits, path, str_length (path) + 1);
 
55
          if (!answer)
 
56
            goto enomem_error;
 
57
          return answer;
 
58
        }
 
59
      else if (errno == ERANGE)
 
60
        {
 
61
          char * new_path;
 
62
          sizeof_path *= 2;
 
63
          new_path = lim_realloc (limits, path, sizeof_path);
 
64
          if (!new_path)
 
65
            goto enomem_error;
 
66
        }
 
67
      else
 
68
        {
 
69
          *errn = errno;
 
70
          lim_free (limits, path);
 
71
          return 0;
 
72
        }
 
73
    }
 
74
}
 
75
 
 
76
 
 
77
 
 
78
/* tag: Tom Lord Fri Feb 22 04:10:53 2002 (cwd.c)
 
79
 */