1 /*************
2 * com_chdir.c
3 ************/
4 
5 #include "ngspice/ngspice.h"
6 
7 #include "ngspice/wordlist.h"
8 
9 #ifdef HAVE_PWD_H
10 #include <pwd.h>
11 #endif
12 
13 #include "com_chdir.h"
14 #include "ngspice/cpextern.h"
15 
16 
17 void
com_chdir(wordlist * wl)18 com_chdir(wordlist *wl)
19 {
20     char *s;
21 #ifdef HAVE_PWD_H
22     struct passwd *pw;
23 #endif
24 #ifdef HAVE_GETCWD
25     char localbuf[257];
26 #endif
27     int copied = 0;
28 
29     s = NULL;
30 
31     if (wl == NULL) {
32 
33         s = getenv("HOME");
34         if (!s)
35             s = getenv("USERPROFILE");
36 
37 #ifdef HAVE_PWD_H
38         if (s == NULL) {
39             pw = getpwuid(getuid());
40             if (pw == NULL) {
41                 fprintf(cp_err, "Can't get your password entry\n");
42                 return;
43             }
44             s = pw->pw_dir;
45         }
46 #endif
47     } else {
48         s = cp_unquote(wl->wl_word);
49         copied = 1;
50     }
51 
52 
53     if (s != NULL)
54         if (chdir(s) == -1)
55             perror(s);
56 
57     if (copied)
58         tfree(s);
59 
60 #ifdef HAVE_GETCWD
61     s = getcwd(localbuf, sizeof(localbuf));
62     if (s)
63         printf("Current directory: %s\n", s);
64     else
65         fprintf(cp_err, "Can't get current working directory.\n");
66 #endif
67 
68 }
69 
70 /* just print the current working directory */
71 void
com_getcwd(wordlist * wl)72 com_getcwd(wordlist *wl)
73 {
74     NG_IGNORE(wl);
75 #ifdef HAVE_GETCWD
76     char *s;
77     char localbuf[257];
78     s = getcwd(localbuf, sizeof(localbuf));
79     if (s)
80         printf("Current directory: %s\n", s);
81     else
82         fprintf(cp_err, "Can't get current working directory.\n");
83 #else
84     fprintf(cp_err, "Error, function getcwd not available\n");
85 #endif
86 }
87